1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2023 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 /* Various options and other info passed from the linker. */
122 struct riscv_elf_params
*params
;
124 /* Short-cuts to get to dynamic linker sections. */
127 /* The max alignment of output sections. */
128 bfd_vma max_alignment
;
130 /* Used by local STT_GNU_IFUNC symbols. */
131 htab_t loc_hash_table
;
132 void * loc_hash_memory
;
134 /* The index of the last unused .rel.iplt slot. */
135 bfd_vma last_iplt_index
;
137 /* The data segment phase, don't relax the section
138 when it is exp_seg_relro_adjust. */
139 int *data_segment_phase
;
141 /* Relocations for variant CC symbols may be present. */
145 /* Instruction access functions. */
146 #define riscv_get_insn(bits, ptr) \
147 ((bits) == 16 ? bfd_getl16 (ptr) \
148 : (bits) == 32 ? bfd_getl32 (ptr) \
149 : (bits) == 64 ? bfd_getl64 (ptr) \
150 : (abort (), (bfd_vma) - 1))
151 #define riscv_put_insn(bits, val, ptr) \
152 ((bits) == 16 ? bfd_putl16 (val, ptr) \
153 : (bits) == 32 ? bfd_putl32 (val, ptr) \
154 : (bits) == 64 ? bfd_putl64 (val, ptr) \
155 : (abort (), (void) 0))
157 /* Get the RISC-V ELF linker hash table from a link_info structure. */
158 #define riscv_elf_hash_table(p) \
159 ((is_elf_hash_table ((p)->hash) \
160 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
161 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
164 riscv_elfNN_set_options (struct bfd_link_info
*link_info
,
165 struct riscv_elf_params
*params
)
167 riscv_elf_hash_table (link_info
)->params
= params
;
171 riscv_info_to_howto_rela (bfd
*abfd
,
173 Elf_Internal_Rela
*dst
)
175 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
176 return cache_ptr
->howto
!= NULL
;
180 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
182 const struct elf_backend_data
*bed
;
185 bed
= get_elf_backend_data (abfd
);
186 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
187 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
190 /* Return true if a relocation is modifying an instruction. */
193 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
195 /* Heuristic: A multibyte destination with a nontrivial mask
197 return (howto
->bitsize
> 8
198 && howto
->dst_mask
!= 0
199 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
200 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
204 #define PLT_HEADER_INSNS 8
205 #define PLT_ENTRY_INSNS 4
206 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
207 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
208 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
209 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
210 the other is used for link map. Other targets also reserve one more
211 entry used for runtime profile? */
212 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
214 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
217 # define MATCH_LREG MATCH_LW
219 # define MATCH_LREG MATCH_LD
222 /* Generate a PLT header. */
225 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
228 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
229 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
231 /* RVE has no t3 register, so this won't work, and is not supported. */
232 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
234 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
239 /* auipc t2, %hi(.got.plt)
240 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
241 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
242 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
243 addi t0, t2, %lo(.got.plt) # &.got.plt
244 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
245 l[w|d] t0, PTRSIZE(t0) # link map
248 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
249 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
250 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
251 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
252 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
253 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
254 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
255 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
260 /* Generate a PLT entry. */
263 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
266 /* RVE has no t3 register, so this won't work, and is not supported. */
267 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
269 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
274 /* auipc t3, %hi(.got.plt entry)
275 l[w|d] t3, %lo(.got.plt entry)(t3)
279 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
280 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
281 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
282 entry
[3] = RISCV_NOP
;
287 /* Create an entry in an RISC-V ELF linker hash table. */
289 static struct bfd_hash_entry
*
290 link_hash_newfunc (struct bfd_hash_entry
*entry
,
291 struct bfd_hash_table
*table
, const char *string
)
293 /* Allocate the structure if it has not already been allocated by a
298 bfd_hash_allocate (table
,
299 sizeof (struct riscv_elf_link_hash_entry
));
304 /* Call the allocation method of the superclass. */
305 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
308 struct riscv_elf_link_hash_entry
*eh
;
310 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
311 eh
->tls_type
= GOT_UNKNOWN
;
317 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
318 for local symbol so that we can handle local STT_GNU_IFUNC symbols
319 as global symbol. We reuse indx and dynstr_index for local symbol
320 hash since they aren't used by global symbols in this backend. */
323 riscv_elf_local_htab_hash (const void *ptr
)
325 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
326 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
329 /* Compare local hash entries. */
332 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
334 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
335 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
337 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
340 /* Find and/or create a hash entry for local symbol. */
342 static struct elf_link_hash_entry
*
343 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
344 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
347 struct riscv_elf_link_hash_entry eh
, *ret
;
348 asection
*sec
= abfd
->sections
;
349 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
350 ELFNN_R_SYM (rel
->r_info
));
353 eh
.elf
.indx
= sec
->id
;
354 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
355 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
356 create
? INSERT
: NO_INSERT
);
363 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
367 ret
= (struct riscv_elf_link_hash_entry
*)
368 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
369 sizeof (struct riscv_elf_link_hash_entry
));
372 memset (ret
, 0, sizeof (*ret
));
373 ret
->elf
.indx
= sec
->id
;
374 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
375 ret
->elf
.dynindx
= -1;
381 /* Destroy a RISC-V elf linker hash table. */
384 riscv_elf_link_hash_table_free (bfd
*obfd
)
386 struct riscv_elf_link_hash_table
*ret
387 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
389 if (ret
->loc_hash_table
)
390 htab_delete (ret
->loc_hash_table
);
391 if (ret
->loc_hash_memory
)
392 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
394 _bfd_elf_link_hash_table_free (obfd
);
397 /* Create a RISC-V ELF linker hash table. */
399 static struct bfd_link_hash_table
*
400 riscv_elf_link_hash_table_create (bfd
*abfd
)
402 struct riscv_elf_link_hash_table
*ret
;
403 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
405 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
409 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
410 sizeof (struct riscv_elf_link_hash_entry
),
417 ret
->max_alignment
= (bfd_vma
) -1;
419 /* Create hash table for local ifunc. */
420 ret
->loc_hash_table
= htab_try_create (1024,
421 riscv_elf_local_htab_hash
,
422 riscv_elf_local_htab_eq
,
424 ret
->loc_hash_memory
= objalloc_create ();
425 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
427 riscv_elf_link_hash_table_free (abfd
);
430 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
432 return &ret
->elf
.root
;
435 /* Create the .got section. */
438 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
442 struct elf_link_hash_entry
*h
;
443 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
444 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
446 /* This function may be called more than once. */
447 if (htab
->sgot
!= NULL
)
450 flags
= bed
->dynamic_sec_flags
;
452 s
= bfd_make_section_anyway_with_flags (abfd
,
453 (bed
->rela_plts_and_copies_p
454 ? ".rela.got" : ".rel.got"),
455 (bed
->dynamic_sec_flags
458 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
462 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
464 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
468 /* The first bit of the global offset table is the header. */
469 s
->size
+= bed
->got_header_size
;
471 if (bed
->want_got_plt
)
473 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
475 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
479 /* Reserve room for the header. */
480 s
->size
+= GOTPLT_HEADER_SIZE
;
483 if (bed
->want_got_sym
)
485 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
486 section. We don't do this in the linker script because we don't want
487 to define the symbol if we are not creating a global offset
489 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
490 "_GLOBAL_OFFSET_TABLE_");
491 elf_hash_table (info
)->hgot
= h
;
499 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
500 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
504 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
505 struct bfd_link_info
*info
)
507 struct riscv_elf_link_hash_table
*htab
;
509 htab
= riscv_elf_hash_table (info
);
510 BFD_ASSERT (htab
!= NULL
);
512 if (!riscv_elf_create_got_section (dynobj
, info
))
515 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
518 if (!bfd_link_pic (info
))
520 /* Technically, this section doesn't have contents. It is used as the
521 target of TLS copy relocs, to copy TLS data from shared libraries into
522 the executable. However, if we don't mark it as loadable, then it
523 matches the IS_TBSS test in ldlang.c, and there is no run-time address
524 space allocated for it even though it has SEC_ALLOC. That test is
525 correct for .tbss, but not correct for this section. There is also
526 a second problem that having a section with no contents can only work
527 if it comes after all sections with contents in the same segment,
528 but the linker script does not guarantee that. This is just mixed in
529 with other .tdata.* sections. We can fix both problems by lying and
530 saying that there are contents. This section is expected to be small
531 so this should not cause a significant extra program startup cost. */
533 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
534 (SEC_ALLOC
| SEC_THREAD_LOCAL
535 | SEC_LOAD
| SEC_DATA
537 | SEC_LINKER_CREATED
));
540 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
541 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
547 /* Copy the extra info we tack onto an elf_link_hash_entry. */
550 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
551 struct elf_link_hash_entry
*dir
,
552 struct elf_link_hash_entry
*ind
)
554 struct riscv_elf_link_hash_entry
*edir
, *eind
;
556 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
557 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
559 if (ind
->root
.type
== bfd_link_hash_indirect
560 && dir
->got
.refcount
<= 0)
562 edir
->tls_type
= eind
->tls_type
;
563 eind
->tls_type
= GOT_UNKNOWN
;
565 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
569 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
570 unsigned long symndx
, char tls_type
)
572 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
574 *new_tls_type
|= tls_type
;
575 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
577 (*_bfd_error_handler
)
578 (_("%pB: `%s' accessed both as normal and thread local symbol"),
579 abfd
, h
? h
->root
.root
.string
: "<local>");
586 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
587 struct elf_link_hash_entry
*h
, long symndx
)
589 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
590 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
592 if (htab
->elf
.sgot
== NULL
)
594 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
600 h
->got
.refcount
+= 1;
604 /* This is a global offset table entry for a local symbol. */
605 if (elf_local_got_refcounts (abfd
) == NULL
)
607 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
608 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
610 _bfd_riscv_elf_local_got_tls_type (abfd
)
611 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
613 elf_local_got_refcounts (abfd
) [symndx
] += 1;
619 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
621 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
623 /* We propably can improve the information to tell users that they
624 should be recompile the code with -fPIC or -fPIE, just like what
626 (*_bfd_error_handler
)
627 (_("%pB: relocation %s against `%s' can not be used when making a shared "
628 "object; recompile with -fPIC"),
629 abfd
, r
? r
->name
: _("<unknown>"),
630 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
631 bfd_set_error (bfd_error_bad_value
);
635 /* Look through the relocs for a section during the first phase, and
636 allocate space in the global offset table or procedure linkage
640 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
641 asection
*sec
, const Elf_Internal_Rela
*relocs
)
643 struct riscv_elf_link_hash_table
*htab
;
644 Elf_Internal_Shdr
*symtab_hdr
;
645 struct elf_link_hash_entry
**sym_hashes
;
646 const Elf_Internal_Rela
*rel
;
647 asection
*sreloc
= NULL
;
649 if (bfd_link_relocatable (info
))
652 htab
= riscv_elf_hash_table (info
);
653 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
654 sym_hashes
= elf_sym_hashes (abfd
);
656 if (htab
->elf
.dynobj
== NULL
)
657 htab
->elf
.dynobj
= abfd
;
659 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
662 unsigned int r_symndx
;
663 struct elf_link_hash_entry
*h
;
665 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
666 r_type
= ELFNN_R_TYPE (rel
->r_info
);
668 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
670 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
675 if (r_symndx
< symtab_hdr
->sh_info
)
677 /* A local symbol. */
678 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
683 /* Check relocation against local STT_GNU_IFUNC symbol. */
684 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
686 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
690 /* Fake STT_GNU_IFUNC global symbol. */
691 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
693 h
->type
= STT_GNU_IFUNC
;
697 h
->root
.type
= bfd_link_hash_defined
;
704 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
705 while (h
->root
.type
== bfd_link_hash_indirect
706 || h
->root
.type
== bfd_link_hash_warning
)
707 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
717 case R_RISCV_CALL_PLT
:
719 case R_RISCV_GOT_HI20
:
720 case R_RISCV_PCREL_HI20
:
721 /* Create the ifunc sections, iplt and ipltgot, for static
723 if (h
->type
== STT_GNU_IFUNC
724 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
732 /* It is referenced by a non-shared object. */
738 case R_RISCV_TLS_GD_HI20
:
739 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
740 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
744 case R_RISCV_TLS_GOT_HI20
:
745 if (bfd_link_pic (info
))
746 info
->flags
|= DF_STATIC_TLS
;
747 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
748 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
752 case R_RISCV_GOT_HI20
:
753 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
754 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
759 case R_RISCV_CALL_PLT
:
760 /* These symbol requires a procedure linkage table entry.
761 We actually build the entry in adjust_dynamic_symbol,
762 because these might be a case of linking PIC code without
763 linking in any dynamic objects, in which case we don't
764 need to generate a procedure linkage table after all. */
766 /* If it is a local symbol, then we resolve it directly
767 without creating a PLT entry. */
772 h
->plt
.refcount
+= 1;
775 case R_RISCV_PCREL_HI20
:
777 && h
->type
== STT_GNU_IFUNC
)
780 h
->pointer_equality_needed
= 1;
782 /* We don't use the PCREL_HI20 in the data section,
783 so we always need the plt when it refers to
785 h
->plt
.refcount
+= 1;
791 case R_RISCV_RVC_BRANCH
:
792 case R_RISCV_RVC_JUMP
:
793 /* In shared libraries and pie, these relocs are known
795 if (bfd_link_pic (info
))
799 case R_RISCV_TPREL_HI20
:
800 if (!bfd_link_executable (info
))
801 return bad_static_reloc (abfd
, r_type
, h
);
803 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
807 if (bfd_link_pic (info
))
808 return bad_static_reloc (abfd
, r_type
, h
);
812 case R_RISCV_JUMP_SLOT
:
813 case R_RISCV_RELATIVE
:
821 && (!bfd_link_pic (info
)
822 || h
->type
== STT_GNU_IFUNC
))
824 /* This reloc might not bind locally. */
826 h
->pointer_equality_needed
= 1;
829 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
831 /* We may need a .plt entry if the symbol is a function
832 defined in a shared lib or is a function referenced
833 from the code or read-only section. */
834 h
->plt
.refcount
+= 1;
838 /* If we are creating a shared library, and this is a reloc
839 against a global symbol, or a non PC relative reloc
840 against a local symbol, then we need to copy the reloc
841 into the shared library. However, if we are linking with
842 -Bsymbolic, we do not need to copy a reloc against a
843 global symbol which is defined in an object we are
844 including in the link (i.e., DEF_REGULAR is set). At
845 this point we have not seen all the input files, so it is
846 possible that DEF_REGULAR is not set now but will be set
847 later (it is never cleared). In case of a weak definition,
848 DEF_REGULAR may be cleared later by a strong definition in
849 a shared library. We account for that possibility below by
850 storing information in the relocs_copied field of the hash
851 table entry. A similar situation occurs when creating
852 shared libraries and symbol visibility changes render the
855 If on the other hand, we are creating an executable, we
856 may need to keep relocations for symbols satisfied by a
857 dynamic library if we manage to avoid copy relocs for the
860 Generate dynamic pointer relocation against STT_GNU_IFUNC
861 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
862 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
864 if ((bfd_link_pic (info
)
865 && (sec
->flags
& SEC_ALLOC
) != 0
866 && ((r
!= NULL
&& !r
->pc_relative
)
869 || h
->root
.type
== bfd_link_hash_defweak
870 || !h
->def_regular
))))
871 || (!bfd_link_pic (info
)
872 && (sec
->flags
& SEC_ALLOC
) != 0
874 && (h
->root
.type
== bfd_link_hash_defweak
876 || (!bfd_link_pic (info
)
878 && h
->type
== STT_GNU_IFUNC
879 && (sec
->flags
& SEC_CODE
) == 0))
881 struct elf_dyn_relocs
*p
;
882 struct elf_dyn_relocs
**head
;
884 /* When creating a shared object, we must copy these
885 relocs into the output file. We create a reloc
886 section in dynobj and make room for the reloc. */
889 sreloc
= _bfd_elf_make_dynamic_reloc_section
890 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
891 abfd
, /*rela?*/ true);
897 /* If this is a global symbol, we count the number of
898 relocations we need for this symbol. */
900 head
= &h
->dyn_relocs
;
903 /* Track dynamic relocs needed for local syms too.
904 We really need local syms available to do this
909 Elf_Internal_Sym
*isym
;
911 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
916 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
920 vpp
= &elf_section_data (s
)->local_dynrel
;
921 head
= (struct elf_dyn_relocs
**) vpp
;
925 if (p
== NULL
|| p
->sec
!= sec
)
927 size_t amt
= sizeof *p
;
928 p
= ((struct elf_dyn_relocs
*)
929 bfd_alloc (htab
->elf
.dynobj
, amt
));
940 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
953 /* Adjust a symbol defined by a dynamic object and referenced by a
954 regular object. The current definition is in some section of the
955 dynamic object, but we're not including those sections. We have to
956 change the definition to something the rest of the link can
960 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
961 struct elf_link_hash_entry
*h
)
963 struct riscv_elf_link_hash_table
*htab
;
964 struct riscv_elf_link_hash_entry
* eh
;
968 htab
= riscv_elf_hash_table (info
);
969 BFD_ASSERT (htab
!= NULL
);
971 dynobj
= htab
->elf
.dynobj
;
973 /* Make sure we know what is going on here. */
974 BFD_ASSERT (dynobj
!= NULL
976 || h
->type
== STT_GNU_IFUNC
980 && !h
->def_regular
)));
982 /* If this is a function, put it in the procedure linkage table. We
983 will fill in the contents of the procedure linkage table later
984 (although we could actually do it here). */
985 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
987 if (h
->plt
.refcount
<= 0
988 || (h
->type
!= STT_GNU_IFUNC
989 && (SYMBOL_CALLS_LOCAL (info
, h
)
990 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
991 && h
->root
.type
== bfd_link_hash_undefweak
))))
993 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
994 input file, but the symbol was never referred to by a dynamic
995 object, or if all references were garbage collected. In such
996 a case, we don't actually need to build a PLT entry. */
997 h
->plt
.offset
= (bfd_vma
) -1;
1004 h
->plt
.offset
= (bfd_vma
) -1;
1006 /* If this is a weak symbol, and there is a real definition, the
1007 processor independent code will have arranged for us to see the
1008 real definition first, and we can just use the same value. */
1009 if (h
->is_weakalias
)
1011 struct elf_link_hash_entry
*def
= weakdef (h
);
1012 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1013 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1014 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1018 /* This is a reference to a symbol defined by a dynamic object which
1019 is not a function. */
1021 /* If we are creating a shared library, we must presume that the
1022 only references to the symbol are via the global offset table.
1023 For such cases we need not do anything here; the relocations will
1024 be handled correctly by relocate_section. */
1025 if (bfd_link_pic (info
))
1028 /* If there are no references to this symbol that do not use the
1029 GOT, we don't need to generate a copy reloc. */
1030 if (!h
->non_got_ref
)
1033 /* If -z nocopyreloc was given, we won't generate them either. */
1034 if (info
->nocopyreloc
)
1040 /* If we don't find any dynamic relocs in read-only sections, then
1041 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1042 if (!_bfd_elf_readonly_dynrelocs (h
))
1048 /* We must allocate the symbol in our .dynbss section, which will
1049 become part of the .bss section of the executable. There will be
1050 an entry for this symbol in the .dynsym section. The dynamic
1051 object will contain position independent code, so all references
1052 from the dynamic object to this symbol will go through the global
1053 offset table. The dynamic linker will use the .dynsym entry to
1054 determine the address it must put in the global offset table, so
1055 both the dynamic object and the regular object will refer to the
1056 same memory location for the variable. */
1058 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1059 to copy the initial value out of the dynamic object and into the
1060 runtime process image. We need to remember the offset into the
1061 .rel.bss section we are going to use. */
1062 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1063 if (eh
->tls_type
& ~GOT_NORMAL
)
1065 s
= htab
->sdyntdata
;
1066 srel
= htab
->elf
.srelbss
;
1068 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1070 s
= htab
->elf
.sdynrelro
;
1071 srel
= htab
->elf
.sreldynrelro
;
1075 s
= htab
->elf
.sdynbss
;
1076 srel
= htab
->elf
.srelbss
;
1078 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1080 srel
->size
+= sizeof (ElfNN_External_Rela
);
1084 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1087 /* Allocate space in .plt, .got and associated reloc sections for
1091 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1093 struct bfd_link_info
*info
;
1094 struct riscv_elf_link_hash_table
*htab
;
1095 struct elf_dyn_relocs
*p
;
1097 if (h
->root
.type
== bfd_link_hash_indirect
)
1100 info
= (struct bfd_link_info
*) inf
;
1101 htab
= riscv_elf_hash_table (info
);
1102 BFD_ASSERT (htab
!= NULL
);
1104 /* When we are generating pde, make sure gp symbol is output as a
1105 dynamic symbol. Then ld.so can set the gp register earlier, before
1106 resolving the ifunc. */
1107 if (!bfd_link_pic (info
)
1108 && htab
->elf
.dynamic_sections_created
1109 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1110 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1113 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1114 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1115 if they are defined and referenced in a non-shared object. */
1116 if (h
->type
== STT_GNU_IFUNC
1119 else if (htab
->elf
.dynamic_sections_created
1120 && h
->plt
.refcount
> 0)
1122 /* Make sure this symbol is output as a dynamic symbol.
1123 Undefined weak syms won't yet be marked as dynamic. */
1124 if (h
->dynindx
== -1
1125 && !h
->forced_local
)
1127 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1131 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1133 asection
*s
= htab
->elf
.splt
;
1136 s
->size
= PLT_HEADER_SIZE
;
1138 h
->plt
.offset
= s
->size
;
1140 /* Make room for this entry. */
1141 s
->size
+= PLT_ENTRY_SIZE
;
1143 /* We also need to make an entry in the .got.plt section. */
1144 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1146 /* We also need to make an entry in the .rela.plt section. */
1147 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1149 /* If this symbol is not defined in a regular file, and we are
1150 not generating a shared library, then set the symbol to this
1151 location in the .plt. This is required to make function
1152 pointers compare as equal between the normal executable and
1153 the shared library. */
1154 if (! bfd_link_pic (info
)
1157 h
->root
.u
.def
.section
= s
;
1158 h
->root
.u
.def
.value
= h
->plt
.offset
;
1161 /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1162 variant_cc flag of riscv_elf_link_hash_table. */
1163 if (h
->other
& STO_RISCV_VARIANT_CC
)
1164 htab
->variant_cc
= 1;
1168 h
->plt
.offset
= (bfd_vma
) -1;
1174 h
->plt
.offset
= (bfd_vma
) -1;
1178 if (h
->got
.refcount
> 0)
1182 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1184 /* Make sure this symbol is output as a dynamic symbol.
1185 Undefined weak syms won't yet be marked as dynamic. */
1186 if (h
->dynindx
== -1
1187 && !h
->forced_local
)
1189 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1194 h
->got
.offset
= s
->size
;
1195 dyn
= htab
->elf
.dynamic_sections_created
;
1196 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1198 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1199 if (tls_type
& GOT_TLS_GD
)
1201 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1202 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1205 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1206 if (tls_type
& GOT_TLS_IE
)
1208 s
->size
+= RISCV_ELF_WORD_BYTES
;
1209 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1214 s
->size
+= RISCV_ELF_WORD_BYTES
;
1215 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1216 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1217 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1221 h
->got
.offset
= (bfd_vma
) -1;
1223 if (h
->dyn_relocs
== NULL
)
1226 /* In the shared -Bsymbolic case, discard space allocated for
1227 dynamic pc-relative relocs against symbols which turn out to be
1228 defined in regular objects. For the normal shared case, discard
1229 space for pc-relative relocs that have become local due to symbol
1230 visibility changes. */
1232 if (bfd_link_pic (info
))
1234 if (SYMBOL_CALLS_LOCAL (info
, h
))
1236 struct elf_dyn_relocs
**pp
;
1238 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1240 p
->count
-= p
->pc_count
;
1249 /* Also discard relocs on undefined weak syms with non-default
1251 if (h
->dyn_relocs
!= NULL
1252 && h
->root
.type
== bfd_link_hash_undefweak
)
1254 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1255 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1256 h
->dyn_relocs
= NULL
;
1258 /* Make sure undefined weak symbols are output as a dynamic
1260 else if (h
->dynindx
== -1
1261 && !h
->forced_local
)
1263 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1270 /* For the non-shared case, discard space for relocs against
1271 symbols which turn out to need copy relocs or are not
1277 || (htab
->elf
.dynamic_sections_created
1278 && (h
->root
.type
== bfd_link_hash_undefweak
1279 || h
->root
.type
== bfd_link_hash_undefined
))))
1281 /* Make sure this symbol is output as a dynamic symbol.
1282 Undefined weak syms won't yet be marked as dynamic. */
1283 if (h
->dynindx
== -1
1284 && !h
->forced_local
)
1286 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1290 /* If that succeeded, we know we'll be keeping all the
1292 if (h
->dynindx
!= -1)
1296 h
->dyn_relocs
= NULL
;
1301 /* Finally, allocate space. */
1302 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1304 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1305 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1311 /* Allocate space in .plt, .got and associated reloc sections for
1312 ifunc dynamic relocs. */
1315 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1318 struct bfd_link_info
*info
;
1320 if (h
->root
.type
== bfd_link_hash_indirect
)
1323 if (h
->root
.type
== bfd_link_hash_warning
)
1324 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1326 info
= (struct bfd_link_info
*) inf
;
1328 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1329 here if it is defined and referenced in a non-shared object. */
1330 if (h
->type
== STT_GNU_IFUNC
1332 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1341 /* Allocate space in .plt, .got and associated reloc sections for
1342 local ifunc dynamic relocs. */
1345 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1347 struct elf_link_hash_entry
*h
1348 = (struct elf_link_hash_entry
*) *slot
;
1350 if (h
->type
!= STT_GNU_IFUNC
1354 || h
->root
.type
!= bfd_link_hash_defined
)
1357 return allocate_ifunc_dynrelocs (h
, inf
);
1361 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1363 struct riscv_elf_link_hash_table
*htab
;
1368 htab
= riscv_elf_hash_table (info
);
1369 BFD_ASSERT (htab
!= NULL
);
1370 dynobj
= htab
->elf
.dynobj
;
1371 BFD_ASSERT (dynobj
!= NULL
);
1373 if (elf_hash_table (info
)->dynamic_sections_created
)
1375 /* Set the contents of the .interp section to the interpreter. */
1376 if (bfd_link_executable (info
) && !info
->nointerp
)
1378 s
= bfd_get_linker_section (dynobj
, ".interp");
1379 BFD_ASSERT (s
!= NULL
);
1380 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1381 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1385 /* Set up .got offsets for local syms, and space for local dynamic
1387 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1389 bfd_signed_vma
*local_got
;
1390 bfd_signed_vma
*end_local_got
;
1391 char *local_tls_type
;
1392 bfd_size_type locsymcount
;
1393 Elf_Internal_Shdr
*symtab_hdr
;
1396 if (! is_riscv_elf (ibfd
))
1399 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1401 struct elf_dyn_relocs
*p
;
1403 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1405 if (!bfd_is_abs_section (p
->sec
)
1406 && bfd_is_abs_section (p
->sec
->output_section
))
1408 /* Input section has been discarded, either because
1409 it is a copy of a linkonce section or due to
1410 linker script /DISCARD/, so we'll be discarding
1413 else if (p
->count
!= 0)
1415 srel
= elf_section_data (p
->sec
)->sreloc
;
1416 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1417 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1418 info
->flags
|= DF_TEXTREL
;
1423 local_got
= elf_local_got_refcounts (ibfd
);
1427 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1428 locsymcount
= symtab_hdr
->sh_info
;
1429 end_local_got
= local_got
+ locsymcount
;
1430 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1432 srel
= htab
->elf
.srelgot
;
1433 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1437 *local_got
= s
->size
;
1438 s
->size
+= RISCV_ELF_WORD_BYTES
;
1439 if (*local_tls_type
& GOT_TLS_GD
)
1440 s
->size
+= RISCV_ELF_WORD_BYTES
;
1441 if (bfd_link_pic (info
)
1442 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1443 srel
->size
+= sizeof (ElfNN_External_Rela
);
1446 *local_got
= (bfd_vma
) -1;
1450 /* Allocate .plt and .got entries and space dynamic relocs for
1452 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1454 /* Allocate .plt and .got entries and space dynamic relocs for
1455 global ifunc symbols. */
1456 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1458 /* Allocate .plt and .got entries and space dynamic relocs for
1459 local ifunc symbols. */
1460 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1462 /* Used to resolve the dynamic relocs overwite problems when
1463 generating static executable. */
1464 if (htab
->elf
.irelplt
)
1465 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1467 if (htab
->elf
.sgotplt
)
1469 struct elf_link_hash_entry
*got
;
1470 got
= elf_link_hash_lookup (elf_hash_table (info
),
1471 "_GLOBAL_OFFSET_TABLE_",
1472 false, false, false);
1474 /* Don't allocate .got.plt section if there are no GOT nor PLT
1475 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1477 || !got
->ref_regular_nonweak
)
1478 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1479 && (htab
->elf
.splt
== NULL
1480 || htab
->elf
.splt
->size
== 0)
1481 && (htab
->elf
.sgot
== NULL
1482 || (htab
->elf
.sgot
->size
1483 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1484 htab
->elf
.sgotplt
->size
= 0;
1487 /* The check_relocs and adjust_dynamic_symbol entry points have
1488 determined the sizes of the various dynamic sections. Allocate
1490 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1492 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1495 if (s
== htab
->elf
.splt
1496 || s
== htab
->elf
.sgot
1497 || s
== htab
->elf
.sgotplt
1498 || s
== htab
->elf
.iplt
1499 || s
== htab
->elf
.igotplt
1500 || s
== htab
->elf
.sdynbss
1501 || s
== htab
->elf
.sdynrelro
1502 || s
== htab
->sdyntdata
)
1504 /* Strip this section if we don't need it; see the
1507 else if (startswith (s
->name
, ".rela"))
1511 /* We use the reloc_count field as a counter if we need
1512 to copy relocs into the output file. */
1518 /* It's not one of our sections. */
1524 /* If we don't need this section, strip it from the
1525 output file. This is mostly to handle .rela.bss and
1526 .rela.plt. We must create both sections in
1527 create_dynamic_sections, because they must be created
1528 before the linker maps input sections to output
1529 sections. The linker does that before
1530 adjust_dynamic_symbol is called, and it is that
1531 function which decides whether anything needs to go
1532 into these sections. */
1533 s
->flags
|= SEC_EXCLUDE
;
1537 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1540 /* Allocate memory for the section contents. Zero the memory
1541 for the benefit of .rela.plt, which has 4 unused entries
1542 at the beginning, and we don't want garbage. */
1543 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1544 if (s
->contents
== NULL
)
1548 /* Add dynamic entries. */
1549 if (elf_hash_table (info
)->dynamic_sections_created
)
1551 if (!_bfd_elf_add_dynamic_tags (output_bfd
, info
, true))
1554 if (htab
->variant_cc
1555 && !_bfd_elf_add_dynamic_entry (info
, DT_RISCV_VARIANT_CC
, 0))
1563 #define DTP_OFFSET 0x800
1565 /* Return the relocation value for a TLS dtp-relative reloc. */
1568 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1570 /* If tls_sec is NULL, we should have signalled an error already. */
1571 if (elf_hash_table (info
)->tls_sec
== NULL
)
1573 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1576 /* Return the relocation value for a static TLS tp-relative relocation. */
1579 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1581 /* If tls_sec is NULL, we should have signalled an error already. */
1582 if (elf_hash_table (info
)->tls_sec
== NULL
)
1584 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1587 /* Return the global pointer's value, or 0 if it is not in use. */
1590 riscv_global_pointer_value (struct bfd_link_info
*info
)
1592 struct bfd_link_hash_entry
*h
;
1594 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1595 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1598 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1601 /* Emplace a static relocation. */
1603 static bfd_reloc_status_type
1604 perform_relocation (const reloc_howto_type
*howto
,
1605 const Elf_Internal_Rela
*rel
,
1607 asection
*input_section
,
1611 if (howto
->pc_relative
)
1612 value
-= sec_addr (input_section
) + rel
->r_offset
;
1613 value
+= rel
->r_addend
;
1615 switch (ELFNN_R_TYPE (rel
->r_info
))
1618 case R_RISCV_TPREL_HI20
:
1619 case R_RISCV_PCREL_HI20
:
1620 case R_RISCV_GOT_HI20
:
1621 case R_RISCV_TLS_GOT_HI20
:
1622 case R_RISCV_TLS_GD_HI20
:
1623 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1624 return bfd_reloc_overflow
;
1625 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1628 case R_RISCV_LO12_I
:
1629 case R_RISCV_GPREL_I
:
1630 case R_RISCV_TPREL_LO12_I
:
1631 case R_RISCV_TPREL_I
:
1632 case R_RISCV_PCREL_LO12_I
:
1633 value
= ENCODE_ITYPE_IMM (value
);
1636 case R_RISCV_LO12_S
:
1637 case R_RISCV_GPREL_S
:
1638 case R_RISCV_TPREL_LO12_S
:
1639 case R_RISCV_TPREL_S
:
1640 case R_RISCV_PCREL_LO12_S
:
1641 value
= ENCODE_STYPE_IMM (value
);
1645 case R_RISCV_CALL_PLT
:
1646 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1647 return bfd_reloc_overflow
;
1648 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1649 | (ENCODE_ITYPE_IMM (value
) << 32);
1653 if (!VALID_JTYPE_IMM (value
))
1654 return bfd_reloc_overflow
;
1655 value
= ENCODE_JTYPE_IMM (value
);
1658 case R_RISCV_BRANCH
:
1659 if (!VALID_BTYPE_IMM (value
))
1660 return bfd_reloc_overflow
;
1661 value
= ENCODE_BTYPE_IMM (value
);
1664 case R_RISCV_RVC_BRANCH
:
1665 if (!VALID_CBTYPE_IMM (value
))
1666 return bfd_reloc_overflow
;
1667 value
= ENCODE_CBTYPE_IMM (value
);
1670 case R_RISCV_RVC_JUMP
:
1671 if (!VALID_CJTYPE_IMM (value
))
1672 return bfd_reloc_overflow
;
1673 value
= ENCODE_CJTYPE_IMM (value
);
1676 case R_RISCV_RVC_LUI
:
1677 if (RISCV_CONST_HIGH_PART (value
) == 0)
1679 /* Linker relaxation can convert an address equal to or greater than
1680 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1681 valid immediate. We can fix this by converting it to a C.LI. */
1682 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1683 contents
+ rel
->r_offset
);
1684 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1685 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1686 value
= ENCODE_CITYPE_IMM (0);
1688 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1689 return bfd_reloc_overflow
;
1691 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1709 case R_RISCV_32_PCREL
:
1710 case R_RISCV_TLS_DTPREL32
:
1711 case R_RISCV_TLS_DTPREL64
:
1714 case R_RISCV_DELETE
:
1715 return bfd_reloc_ok
;
1718 return bfd_reloc_notsupported
;
1722 if (riscv_is_insn_reloc (howto
))
1723 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1725 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1726 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1727 if (riscv_is_insn_reloc (howto
))
1728 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1730 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1732 return bfd_reloc_ok
;
1735 /* Remember all PC-relative high-part relocs we've encountered to help us
1736 later resolve the corresponding low-part relocs. */
1742 /* Relocation value with addend. */
1744 /* Original reloc type. */
1746 } riscv_pcrel_hi_reloc
;
1748 typedef struct riscv_pcrel_lo_reloc
1750 /* PC value of auipc. */
1752 /* Internal relocation. */
1753 const Elf_Internal_Rela
*reloc
;
1754 /* Record the following information helps to resolve the %pcrel
1755 which cross different input section. For now we build a hash
1756 for pcrel at the start of riscv_elf_relocate_section, and then
1757 free the hash at the end. But riscv_elf_relocate_section only
1758 handles an input section at a time, so that means we can only
1759 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1760 section. Otherwise, we will report dangerous relocation errors
1761 for those %pcrel which are not in the same input section. */
1762 asection
*input_section
;
1763 struct bfd_link_info
*info
;
1764 reloc_howto_type
*howto
;
1766 /* The next riscv_pcrel_lo_reloc. */
1767 struct riscv_pcrel_lo_reloc
*next
;
1768 } riscv_pcrel_lo_reloc
;
1772 /* Hash table for riscv_pcrel_hi_reloc. */
1774 /* Linked list for riscv_pcrel_lo_reloc. */
1775 riscv_pcrel_lo_reloc
*lo_relocs
;
1776 } riscv_pcrel_relocs
;
1779 riscv_pcrel_reloc_hash (const void *entry
)
1781 const riscv_pcrel_hi_reloc
*e
= entry
;
1782 return (hashval_t
)(e
->address
>> 2);
1786 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1788 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1789 return e1
->address
== e2
->address
;
1793 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1795 p
->lo_relocs
= NULL
;
1796 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1797 riscv_pcrel_reloc_eq
, free
);
1798 return p
->hi_relocs
!= NULL
;
1802 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1804 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1808 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1813 htab_delete (p
->hi_relocs
);
1817 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1818 struct bfd_link_info
*info
,
1822 const reloc_howto_type
*howto
)
1824 /* We may need to reference low addreses in PC-relative modes even when the
1825 PC is far away from these addresses. For example, undefweak references
1826 need to produce the address 0 when linked. As 0 is far from the arbitrary
1827 addresses that we can link PC-relative programs at, the linker can't
1828 actually relocate references to those symbols. In order to allow these
1829 programs to work we simply convert the PC-relative auipc sequences to
1830 0-relative lui sequences. */
1831 if (bfd_link_pic (info
))
1834 /* If it's possible to reference the symbol using auipc we do so, as that's
1835 more in the spirit of the PC-relative relocations we're processing. */
1836 bfd_vma offset
= addr
- pc
;
1837 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1840 /* If it's impossible to reference this with a LUI-based offset then don't
1841 bother to convert it at all so users still see the PC-relative relocation
1842 in the truncation message. */
1843 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1846 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
1848 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1849 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1850 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1855 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
1861 bfd_vma offset
= absolute
? value
: value
- addr
;
1862 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
};
1863 riscv_pcrel_hi_reloc
**slot
=
1864 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1866 BFD_ASSERT (*slot
== NULL
);
1867 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1875 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1877 const Elf_Internal_Rela
*reloc
,
1878 asection
*input_section
,
1879 struct bfd_link_info
*info
,
1880 reloc_howto_type
*howto
,
1883 riscv_pcrel_lo_reloc
*entry
;
1884 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1887 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
1888 howto
, contents
, p
->lo_relocs
};
1889 p
->lo_relocs
= entry
;
1894 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1896 riscv_pcrel_lo_reloc
*r
;
1898 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1900 bfd
*input_bfd
= r
->input_section
->owner
;
1902 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0};
1903 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1904 /* There may be a risk if the %pcrel_lo with addend refers to
1905 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
1906 so the corresponding %pcrel_lo with addend looks wrong. */
1907 char *string
= NULL
;
1909 string
= _("%pcrel_lo missing matching %pcrel_hi");
1910 else if (entry
->type
== R_RISCV_GOT_HI20
1911 && r
->reloc
->r_addend
!= 0)
1912 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
1913 else if (RISCV_CONST_HIGH_PART (entry
->value
)
1914 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
1916 /* Check the overflow when adding reloc addend. */
1917 if (asprintf (&string
,
1918 _("%%pcrel_lo overflow with an addend, the "
1919 "value of %%pcrel_hi is 0x%" PRIx64
" without "
1920 "any addend, but may be 0x%" PRIx64
" after "
1921 "adding the %%pcrel_lo addend"),
1922 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
1923 (int64_t) RISCV_CONST_HIGH_PART
1924 (entry
->value
+ r
->reloc
->r_addend
)) == -1)
1925 string
= _("%pcrel_lo overflow with an addend");
1930 (*r
->info
->callbacks
->reloc_dangerous
)
1931 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1935 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1936 input_bfd
, r
->contents
);
1942 /* Relocate a RISC-V ELF section.
1944 The RELOCATE_SECTION function is called by the new ELF backend linker
1945 to handle the relocations for a section.
1947 The relocs are always passed as Rela structures.
1949 This function is responsible for adjusting the section contents as
1950 necessary, and (if generating a relocatable output file) adjusting
1951 the reloc addend as necessary.
1953 This function does not have to worry about setting the reloc
1954 address or the reloc symbol index.
1956 LOCAL_SYMS is a pointer to the swapped in local symbols.
1958 LOCAL_SECTIONS is an array giving the section in the input file
1959 corresponding to the st_shndx field of each local symbol.
1961 The global hash table entry for the global symbols can be found
1962 via elf_sym_hashes (input_bfd).
1964 When generating relocatable output, this function must handle
1965 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1966 going to be the section symbol corresponding to the output
1967 section, which means that the addend must be adjusted
1971 riscv_elf_relocate_section (bfd
*output_bfd
,
1972 struct bfd_link_info
*info
,
1974 asection
*input_section
,
1976 Elf_Internal_Rela
*relocs
,
1977 Elf_Internal_Sym
*local_syms
,
1978 asection
**local_sections
)
1980 Elf_Internal_Rela
*rel
;
1981 Elf_Internal_Rela
*relend
;
1982 riscv_pcrel_relocs pcrel_relocs
;
1984 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1985 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1986 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1987 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1990 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1993 relend
= relocs
+ input_section
->reloc_count
;
1994 for (rel
= relocs
; rel
< relend
; rel
++)
1996 unsigned long r_symndx
;
1997 struct elf_link_hash_entry
*h
;
1998 Elf_Internal_Sym
*sym
;
2001 bfd_reloc_status_type r
= bfd_reloc_ok
;
2002 const char *name
= NULL
;
2003 bfd_vma off
, ie_off
;
2004 bool unresolved_reloc
, is_ie
= false;
2005 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
2006 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
2007 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2008 const char *msg
= NULL
;
2009 char *msg_buf
= NULL
;
2010 bool resolved_to_zero
;
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
;
2441 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2442 contents
+ rel
->r_offset
);
2443 relocation
= (old_value
& ~howto
->dst_mask
)
2444 | (((old_value
& howto
->dst_mask
) - relocation
)
2454 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2455 contents
+ rel
->r_offset
);
2456 relocation
= old_value
- relocation
;
2461 case R_RISCV_CALL_PLT
:
2462 /* Handle a call to an undefined weak function. This won't be
2463 relaxed, so we have to handle it here. */
2464 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2465 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2467 /* We can use x0 as the base register. */
2468 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2469 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2470 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2471 /* Set the relocation value so that we get 0 after the pc
2472 relative adjustment. */
2473 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2478 case R_RISCV_RVC_JUMP
:
2479 if (bfd_link_pic (info
) && h
!= NULL
)
2481 if (h
->plt
.offset
!= MINUS_ONE
)
2483 /* Refer to the PLT entry. This check has to match the
2484 check in _bfd_riscv_relax_section. */
2485 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2486 unresolved_reloc
= false;
2488 else if (!SYMBOL_REFERENCES_LOCAL (info
, h
)
2489 && (input_section
->flags
& SEC_ALLOC
) != 0
2490 && (input_section
->flags
& SEC_READONLY
) != 0
2491 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2493 /* PR 28509, when generating the shared object, these
2494 referenced symbols may bind externally, which means
2495 they will be exported to the dynamic symbol table,
2496 and are preemptible by default. These symbols cannot
2497 be referenced by the non-pic relocations, like
2498 R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2500 However, consider that linker may relax the R_RISCV_CALL
2501 relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2502 these relocations are relocated to the plt entries,
2503 then we won't report error for them.
2505 Perhaps we also need the similar checks for the
2506 R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
2507 if (asprintf (&msg_buf
,
2508 _("%%X%%P: relocation %s against `%s' which "
2509 "may bind externally can not be used when "
2510 "making a shared object; recompile "
2512 howto
->name
, h
->root
.root
.string
) == -1)
2515 r
= bfd_reloc_notsupported
;
2520 case R_RISCV_TPREL_HI20
:
2521 relocation
= tpoff (info
, relocation
);
2524 case R_RISCV_TPREL_LO12_I
:
2525 case R_RISCV_TPREL_LO12_S
:
2526 relocation
= tpoff (info
, relocation
);
2529 case R_RISCV_TPREL_I
:
2530 case R_RISCV_TPREL_S
:
2531 relocation
= tpoff (info
, relocation
);
2532 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2534 /* We can use tp as the base register. */
2535 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2536 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2537 insn
|= X_TP
<< OP_SH_RS1
;
2538 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2541 r
= bfd_reloc_overflow
;
2544 case R_RISCV_GPREL_I
:
2545 case R_RISCV_GPREL_S
:
2547 bfd_vma gp
= riscv_global_pointer_value (info
);
2548 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2549 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2551 /* We can use x0 or gp as the base register. */
2552 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2553 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2556 rel
->r_addend
-= gp
;
2557 insn
|= X_GP
<< OP_SH_RS1
;
2559 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2562 r
= bfd_reloc_overflow
;
2566 case R_RISCV_PCREL_HI20
:
2567 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, relocation
,
2569 /* Update howto if relocation is changed. */
2570 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2571 ELFNN_R_TYPE (rel
->r_info
));
2573 r
= bfd_reloc_notsupported
;
2574 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2575 relocation
+ rel
->r_addend
,
2577 r
= bfd_reloc_overflow
;
2580 case R_RISCV_PCREL_LO12_I
:
2581 case R_RISCV_PCREL_LO12_S
:
2582 /* We don't allow section symbols plus addends as the auipc address,
2583 because then riscv_relax_delete_bytes would have to search through
2584 all relocs to update these addends. This is also ambiguous, as
2585 we do allow offsets to be added to the target address, which are
2586 not to be used to find the auipc address. */
2587 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2588 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2591 msg
= _("%pcrel_lo section symbol with an addend");
2592 r
= bfd_reloc_dangerous
;
2596 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2597 input_section
, info
, howto
,
2600 r
= bfd_reloc_overflow
;
2603 case R_RISCV_TLS_DTPREL32
:
2604 case R_RISCV_TLS_DTPREL64
:
2605 relocation
= dtpoff (info
, relocation
);
2610 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2613 if ((bfd_link_pic (info
)
2615 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2616 && !resolved_to_zero
)
2617 || h
->root
.type
!= bfd_link_hash_undefweak
)
2618 && (!howto
->pc_relative
2619 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2620 || (!bfd_link_pic (info
)
2626 || h
->root
.type
== bfd_link_hash_undefweak
2627 || h
->root
.type
== bfd_link_hash_undefined
)))
2629 Elf_Internal_Rela outrel
;
2631 bool skip_static_relocation
, skip_dynamic_relocation
;
2633 /* When generating a shared object, these relocations
2634 are copied into the output file to be resolved at run
2638 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2640 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2641 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2642 outrel
.r_offset
+= sec_addr (input_section
);
2644 if (skip_dynamic_relocation
)
2645 memset (&outrel
, 0, sizeof outrel
);
2646 else if (h
!= NULL
&& h
->dynindx
!= -1
2647 && !(bfd_link_pic (info
)
2648 && SYMBOLIC_BIND (info
, h
)
2651 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2652 outrel
.r_addend
= rel
->r_addend
;
2656 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2657 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2660 sreloc
= elf_section_data (input_section
)->sreloc
;
2661 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2662 if (skip_static_relocation
)
2667 case R_RISCV_TLS_GOT_HI20
:
2671 case R_RISCV_TLS_GD_HI20
:
2674 off
= h
->got
.offset
;
2679 off
= local_got_offsets
[r_symndx
];
2680 local_got_offsets
[r_symndx
] |= 1;
2683 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2684 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2685 /* If this symbol is referenced by both GD and IE TLS, the IE
2686 reference's GOT slot follows the GD reference's slots. */
2688 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2689 ie_off
= 2 * GOT_ENTRY_SIZE
;
2695 Elf_Internal_Rela outrel
;
2697 bool need_relocs
= false;
2699 if (htab
->elf
.srelgot
== NULL
)
2705 dyn
= htab
->elf
.dynamic_sections_created
;
2706 pic
= bfd_link_pic (info
);
2708 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2709 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2713 /* The GOT entries have not been initialized yet. Do it
2714 now, and emit any relocations. */
2715 if ((bfd_link_pic (info
) || indx
!= 0)
2717 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2718 || h
->root
.type
!= bfd_link_hash_undefweak
))
2721 if (tls_type
& GOT_TLS_GD
)
2725 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2726 outrel
.r_addend
= 0;
2727 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2728 bfd_put_NN (output_bfd
, 0,
2729 htab
->elf
.sgot
->contents
+ off
);
2730 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2733 BFD_ASSERT (! unresolved_reloc
);
2734 bfd_put_NN (output_bfd
,
2735 dtpoff (info
, relocation
),
2736 (htab
->elf
.sgot
->contents
2737 + off
+ RISCV_ELF_WORD_BYTES
));
2741 bfd_put_NN (output_bfd
, 0,
2742 (htab
->elf
.sgot
->contents
2743 + off
+ RISCV_ELF_WORD_BYTES
));
2744 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2745 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2746 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2751 /* If we are not emitting relocations for a
2752 general dynamic reference, then we must be in a
2753 static link or an executable link with the
2754 symbol binding locally. Mark it as belonging
2755 to module 1, the executable. */
2756 bfd_put_NN (output_bfd
, 1,
2757 htab
->elf
.sgot
->contents
+ off
);
2758 bfd_put_NN (output_bfd
,
2759 dtpoff (info
, relocation
),
2760 (htab
->elf
.sgot
->contents
2761 + off
+ RISCV_ELF_WORD_BYTES
));
2765 if (tls_type
& GOT_TLS_IE
)
2769 bfd_put_NN (output_bfd
, 0,
2770 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2771 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2773 outrel
.r_addend
= 0;
2775 outrel
.r_addend
= tpoff (info
, relocation
);
2776 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2777 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2781 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2782 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2787 BFD_ASSERT (off
< (bfd_vma
) -2);
2788 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2789 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2792 r
= bfd_reloc_overflow
;
2793 unresolved_reloc
= false;
2797 r
= bfd_reloc_notsupported
;
2800 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2801 because such sections are not SEC_ALLOC and thus ld.so will
2802 not process them. */
2803 if (unresolved_reloc
2804 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2806 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2807 rel
->r_offset
) != (bfd_vma
) -1)
2809 if (asprintf (&msg_buf
,
2810 _("%%X%%P: unresolvable %s relocation against "
2813 h
->root
.root
.string
) == -1)
2816 r
= bfd_reloc_notsupported
;
2820 if (r
== bfd_reloc_ok
)
2821 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2822 input_bfd
, contents
);
2824 /* We should have already detected the error and set message before.
2825 If the error message isn't set since the linker runs out of memory
2826 or we don't set it before, then we should set the default message
2827 with the "internal error" string here. */
2833 case bfd_reloc_overflow
:
2834 info
->callbacks
->reloc_overflow
2835 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2836 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2839 case bfd_reloc_undefined
:
2840 info
->callbacks
->undefined_symbol
2841 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2845 case bfd_reloc_outofrange
:
2847 msg
= _("%X%P: internal error: out of range error\n");
2850 case bfd_reloc_notsupported
:
2852 msg
= _("%X%P: internal error: unsupported relocation error\n");
2855 case bfd_reloc_dangerous
:
2856 /* The error message should already be set. */
2858 msg
= _("dangerous relocation error");
2859 info
->callbacks
->reloc_dangerous
2860 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2864 msg
= _("%X%P: internal error: unknown error\n");
2868 /* Do not report error message for the dangerous relocation again. */
2869 if (msg
&& r
!= bfd_reloc_dangerous
)
2870 info
->callbacks
->einfo (msg
);
2872 /* Free the unused `msg_buf`. */
2875 /* We already reported the error via a callback, so don't try to report
2876 it again by returning false. That leads to spurious errors. */
2881 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2883 riscv_free_pcrel_relocs (&pcrel_relocs
);
2887 /* Finish up dynamic symbol handling. We set the contents of various
2888 dynamic sections here. */
2891 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2892 struct bfd_link_info
*info
,
2893 struct elf_link_hash_entry
*h
,
2894 Elf_Internal_Sym
*sym
)
2896 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2897 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2899 if (h
->plt
.offset
!= (bfd_vma
) -1)
2901 /* We've decided to create a PLT entry for this symbol. */
2903 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2904 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2905 Elf_Internal_Rela rela
;
2906 asection
*plt
, *gotplt
, *relplt
;
2908 /* When building a static executable, use .iplt, .igot.plt and
2909 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2910 if (htab
->elf
.splt
!= NULL
)
2912 plt
= htab
->elf
.splt
;
2913 gotplt
= htab
->elf
.sgotplt
;
2914 relplt
= htab
->elf
.srelplt
;
2918 plt
= htab
->elf
.iplt
;
2919 gotplt
= htab
->elf
.igotplt
;
2920 relplt
= htab
->elf
.irelplt
;
2923 /* This symbol has an entry in the procedure linkage table. Set
2925 if ((h
->dynindx
== -1
2926 && !((h
->forced_local
|| bfd_link_executable (info
))
2928 && h
->type
== STT_GNU_IFUNC
))
2934 /* Calculate the address of the PLT header. */
2935 header_address
= sec_addr (plt
);
2937 /* Calculate the index of the entry and the offset of .got.plt entry.
2938 For static executables, we don't reserve anything. */
2939 if (plt
== htab
->elf
.splt
)
2941 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2942 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2946 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2947 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2950 /* Calculate the address of the .got.plt entry. */
2951 got_address
= sec_addr (gotplt
) + got_offset
;
2953 /* Find out where the .plt entry should go. */
2954 loc
= plt
->contents
+ h
->plt
.offset
;
2956 /* Fill in the PLT entry itself. */
2957 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2958 header_address
+ h
->plt
.offset
,
2962 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2963 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
2965 /* Fill in the initial value of the .got.plt entry. */
2966 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2967 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2969 rela
.r_offset
= got_address
;
2971 if (h
->dynindx
== -1
2972 || ((bfd_link_executable (info
)
2973 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2975 && h
->type
== STT_GNU_IFUNC
))
2977 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2978 h
->root
.root
.string
,
2979 h
->root
.u
.def
.section
->owner
);
2981 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2982 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2983 asection
*sec
= h
->root
.u
.def
.section
;
2984 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2985 rela
.r_addend
= h
->root
.u
.def
.value
2986 + sec
->output_section
->vma
2987 + sec
->output_offset
;
2991 /* Fill in the entry in the .rela.plt section. */
2992 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2996 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2997 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2999 if (!h
->def_regular
)
3001 /* Mark the symbol as undefined, rather than as defined in
3002 the .plt section. Leave the value alone. */
3003 sym
->st_shndx
= SHN_UNDEF
;
3004 /* If the symbol is weak, we do need to clear the value.
3005 Otherwise, the PLT entry would provide a definition for
3006 the symbol even if the symbol wasn't defined anywhere,
3007 and so the symbol would never be NULL. */
3008 if (!h
->ref_regular_nonweak
)
3013 if (h
->got
.offset
!= (bfd_vma
) -1
3014 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
3015 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3019 Elf_Internal_Rela rela
;
3020 bool use_elf_append_rela
= true;
3022 /* This symbol has an entry in the GOT. Set it up. */
3024 sgot
= htab
->elf
.sgot
;
3025 srela
= htab
->elf
.srelgot
;
3026 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3028 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3030 /* Handle the ifunc symbol in GOT entry. */
3032 && h
->type
== STT_GNU_IFUNC
)
3034 if (h
->plt
.offset
== (bfd_vma
) -1)
3036 /* STT_GNU_IFUNC is referenced without PLT. */
3038 if (htab
->elf
.splt
== NULL
)
3040 /* Use .rela.iplt section to store .got relocations
3041 in static executable. */
3042 srela
= htab
->elf
.irelplt
;
3044 /* Do not use riscv_elf_append_rela to add dynamic
3046 use_elf_append_rela
= false;
3049 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3051 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3052 h
->root
.root
.string
,
3053 h
->root
.u
.def
.section
->owner
);
3055 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3056 rela
.r_addend
= (h
->root
.u
.def
.value
3057 + h
->root
.u
.def
.section
->output_section
->vma
3058 + h
->root
.u
.def
.section
->output_offset
);
3062 /* Generate R_RISCV_NN. */
3063 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3064 BFD_ASSERT (h
->dynindx
!= -1);
3065 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3069 else if (bfd_link_pic (info
))
3071 /* Generate R_RISCV_NN. */
3072 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3073 BFD_ASSERT (h
->dynindx
!= -1);
3074 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3081 if (!h
->pointer_equality_needed
)
3084 /* For non-shared object, we can't use .got.plt, which
3085 contains the real function address if we need pointer
3086 equality. We load the GOT entry with the PLT entry. */
3087 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3088 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3089 + plt
->output_offset
3091 htab
->elf
.sgot
->contents
3092 + (h
->got
.offset
& ~(bfd_vma
) 1));
3096 else if (bfd_link_pic (info
)
3097 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3099 /* If this is a local symbol reference, we just want to emit
3100 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3101 or a pie link, or the symbol was forced to be local because
3102 of a version file. The entry in the global offset table will
3103 already have been initialized in the relocate_section function. */
3104 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3105 asection
*sec
= h
->root
.u
.def
.section
;
3106 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3107 rela
.r_addend
= (h
->root
.u
.def
.value
3108 + sec
->output_section
->vma
3109 + sec
->output_offset
);
3113 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3114 BFD_ASSERT (h
->dynindx
!= -1);
3115 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3119 bfd_put_NN (output_bfd
, 0,
3120 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3122 if (use_elf_append_rela
)
3123 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3126 /* Use riscv_elf_append_rela to add the dynamic relocs into
3127 .rela.iplt may cause the overwrite problems. Since we insert
3128 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3129 but the riscv_elf_append_rela adds the relocs to the place
3130 that are calculated from the reloc_index (in seqential).
3132 One solution is that add these dynamic relocs (GOT IFUNC)
3133 from the last of .rela.iplt section. */
3134 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3135 bfd_byte
*loc
= srela
->contents
3136 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3137 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3143 Elf_Internal_Rela rela
;
3146 /* This symbols needs a copy reloc. Set it up. */
3147 BFD_ASSERT (h
->dynindx
!= -1);
3149 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3150 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3152 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3153 s
= htab
->elf
.sreldynrelro
;
3155 s
= htab
->elf
.srelbss
;
3156 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3159 /* Mark some specially defined symbols as absolute. */
3160 if (h
== htab
->elf
.hdynamic
3161 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3162 sym
->st_shndx
= SHN_ABS
;
3167 /* Finish up local dynamic symbol handling. We set the contents of
3168 various dynamic sections here. */
3171 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3173 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3174 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3176 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3179 /* Finish up the dynamic sections. */
3182 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3183 bfd
*dynobj
, asection
*sdyn
)
3185 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3186 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3187 size_t dynsize
= bed
->s
->sizeof_dyn
;
3188 bfd_byte
*dyncon
, *dynconend
;
3190 dynconend
= sdyn
->contents
+ sdyn
->size
;
3191 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3193 Elf_Internal_Dyn dyn
;
3196 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3201 s
= htab
->elf
.sgotplt
;
3202 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3205 s
= htab
->elf
.srelplt
;
3206 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3209 s
= htab
->elf
.srelplt
;
3210 dyn
.d_un
.d_val
= s
->size
;
3216 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3222 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3223 struct bfd_link_info
*info
)
3227 struct riscv_elf_link_hash_table
*htab
;
3229 htab
= riscv_elf_hash_table (info
);
3230 BFD_ASSERT (htab
!= NULL
);
3231 dynobj
= htab
->elf
.dynobj
;
3233 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3235 if (elf_hash_table (info
)->dynamic_sections_created
)
3240 splt
= htab
->elf
.splt
;
3241 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3243 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3248 /* Fill in the head and tail entries in the procedure linkage table. */
3252 uint32_t plt_header
[PLT_HEADER_INSNS
];
3253 ret
= riscv_make_plt_header (output_bfd
,
3254 sec_addr (htab
->elf
.sgotplt
),
3255 sec_addr (splt
), plt_header
);
3259 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3260 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3262 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3267 if (htab
->elf
.sgotplt
)
3269 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3271 if (bfd_is_abs_section (output_section
))
3273 (*_bfd_error_handler
)
3274 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3278 if (htab
->elf
.sgotplt
->size
> 0)
3280 /* Write the first two entries in .got.plt, needed for the dynamic
3282 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3283 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3284 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3287 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3292 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3294 if (htab
->elf
.sgot
->size
> 0)
3296 /* Set the first entry in the global offset table to the address of
3297 the dynamic section. */
3298 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3299 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3302 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3305 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3306 htab_traverse (htab
->loc_hash_table
,
3307 riscv_elf_finish_local_dynamic_symbol
,
3313 /* Return address for Ith PLT stub in section PLT, for relocation REL
3314 or (bfd_vma) -1 if it should not be included. */
3317 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3318 const arelent
*rel ATTRIBUTE_UNUSED
)
3320 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3323 static enum elf_reloc_type_class
3324 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3325 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3326 const Elf_Internal_Rela
*rela
)
3328 switch (ELFNN_R_TYPE (rela
->r_info
))
3330 case R_RISCV_RELATIVE
:
3331 return reloc_class_relative
;
3332 case R_RISCV_JUMP_SLOT
:
3333 return reloc_class_plt
;
3335 return reloc_class_copy
;
3337 return reloc_class_normal
;
3341 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3345 riscv_float_abi_string (flagword flags
)
3347 switch (flags
& EF_RISCV_FLOAT_ABI
)
3349 case EF_RISCV_FLOAT_ABI_SOFT
:
3350 return "soft-float";
3352 case EF_RISCV_FLOAT_ABI_SINGLE
:
3353 return "single-float";
3355 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3356 return "double-float";
3358 case EF_RISCV_FLOAT_ABI_QUAD
:
3359 return "quad-float";
3366 /* The information of architecture elf attributes. */
3367 static riscv_subset_list_t in_subsets
;
3368 static riscv_subset_list_t out_subsets
;
3369 static riscv_subset_list_t merged_subsets
;
3371 /* Predicator for standard extension. */
3374 riscv_std_ext_p (const char *name
)
3376 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3379 /* Update the output subset's version to match the input when the input
3380 subset's version is newer. */
3383 riscv_update_subset_version (struct riscv_subset_t
*in
,
3384 struct riscv_subset_t
*out
)
3386 if (in
== NULL
|| out
== NULL
)
3389 /* Update the output ISA versions to the newest ones, but otherwise don't
3390 provide any errors or warnings about mis-matched ISA versions as it's
3391 generally too tricky to check for these at link time. */
3392 if ((in
->major_version
> out
->major_version
)
3393 || (in
->major_version
== out
->major_version
3394 && in
->minor_version
> out
->minor_version
)
3395 || (out
->major_version
== RISCV_UNKNOWN_VERSION
))
3397 out
->major_version
= in
->major_version
;
3398 out
->minor_version
= in
->minor_version
;
3402 /* Return true if subset is 'i' or 'e'. */
3405 riscv_i_or_e_p (bfd
*ibfd
,
3407 struct riscv_subset_t
*subset
)
3409 if ((strcasecmp (subset
->name
, "e") != 0)
3410 && (strcasecmp (subset
->name
, "i") != 0))
3413 (_("error: %pB: corrupted ISA string '%s'. "
3414 "First letter should be 'i' or 'e' but got '%s'"),
3415 ibfd
, arch
, subset
->name
);
3421 /* Merge standard extensions.
3424 Return FALSE if failed to merge.
3428 `in_arch`: Raw ISA string for input object.
3429 `out_arch`: Raw ISA string for output object.
3430 `pin`: Subset list for input object.
3431 `pout`: Subset list for output object. */
3434 riscv_merge_std_ext (bfd
*ibfd
,
3435 const char *in_arch
,
3436 const char *out_arch
,
3437 struct riscv_subset_t
**pin
,
3438 struct riscv_subset_t
**pout
)
3440 const char *standard_exts
= "mafdqlcbjtpvnh";
3442 struct riscv_subset_t
*in
= *pin
;
3443 struct riscv_subset_t
*out
= *pout
;
3445 /* First letter should be 'i' or 'e'. */
3446 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3449 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3452 if (strcasecmp (in
->name
, out
->name
) != 0)
3454 /* TODO: We might allow merge 'i' with 'e'. */
3456 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3457 ibfd
, in
->name
, out
->name
);
3461 riscv_update_subset_version(in
, out
);
3462 riscv_add_subset (&merged_subsets
,
3463 out
->name
, out
->major_version
, out
->minor_version
);
3468 /* Handle standard extension first. */
3469 for (p
= standard_exts
; *p
; ++p
)
3471 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3472 char find_ext
[2] = {*p
, '\0'};
3473 bool find_in
, find_out
;
3475 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3476 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3478 if (!find_in
&& !find_out
)
3481 if (find_in
&& find_out
)
3482 riscv_update_subset_version(ext_in
, ext_out
);
3484 ext_merged
= find_out
? ext_out
: ext_in
;
3485 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3486 ext_merged
->major_version
, ext_merged
->minor_version
);
3489 /* Skip all standard extensions. */
3490 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3491 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3499 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3500 object subset list. Likewise for POUT and the output object. Return TRUE
3501 on success and FALSE when a conflict is found. */
3504 riscv_merge_multi_letter_ext (riscv_subset_t
**pin
,
3505 riscv_subset_t
**pout
)
3507 riscv_subset_t
*in
= *pin
;
3508 riscv_subset_t
*out
= *pout
;
3509 riscv_subset_t
*tail
;
3515 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3519 /* `in' comes before `out', append `in' and increment. */
3520 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3526 /* `out' comes before `in', append `out' and increment. */
3527 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3528 out
->minor_version
);
3533 /* Both present, check version and increment both. */
3534 riscv_update_subset_version (in
, out
);
3536 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3537 out
->minor_version
);
3545 /* If we're here, either `in' or `out' is running longer than
3546 the other. So, we need to append the corresponding tail. */
3547 tail
= in
? in
: out
;
3550 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3551 tail
->minor_version
);
3559 /* Merge Tag_RISCV_arch attribute. */
3562 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3564 riscv_subset_t
*in
, *out
;
3565 char *merged_arch_str
;
3567 unsigned xlen_in
, xlen_out
;
3568 merged_subsets
.head
= NULL
;
3569 merged_subsets
.tail
= NULL
;
3571 riscv_parse_subset_t riscv_rps_ld_in
=
3572 {&in_subsets
, _bfd_error_handler
, &xlen_in
, NULL
, false};
3573 riscv_parse_subset_t riscv_rps_ld_out
=
3574 {&out_subsets
, _bfd_error_handler
, &xlen_out
, NULL
, false};
3576 if (in_arch
== NULL
&& out_arch
== NULL
)
3578 if (in_arch
== NULL
&& out_arch
!= NULL
)
3580 if (in_arch
!= NULL
&& out_arch
== NULL
)
3583 /* Parse subset from ISA string. */
3584 if (!riscv_parse_subset (&riscv_rps_ld_in
, in_arch
))
3586 if (!riscv_parse_subset (&riscv_rps_ld_out
, out_arch
))
3589 /* Checking XLEN. */
3590 if (xlen_out
!= xlen_in
)
3593 (_("error: %pB: ISA string of input (%s) doesn't match "
3594 "output (%s)"), ibfd
, in_arch
, out_arch
);
3598 /* Merge subset list. */
3599 in
= in_subsets
.head
;
3600 out
= out_subsets
.head
;
3602 /* Merge standard extension. */
3603 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3606 /* Merge all non-single letter extensions with single call. */
3607 if (!riscv_merge_multi_letter_ext (&in
, &out
))
3610 if (xlen_in
!= xlen_out
)
3613 (_("error: %pB: XLEN of input (%u) doesn't match "
3614 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3618 if (xlen_in
!= ARCH_SIZE
)
3621 (_("error: %pB: unsupported XLEN (%u), you might be "
3622 "using wrong emulation"), ibfd
, xlen_in
);
3626 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3628 /* Release the subset lists. */
3629 riscv_release_subset_list (&in_subsets
);
3630 riscv_release_subset_list (&out_subsets
);
3631 riscv_release_subset_list (&merged_subsets
);
3633 return merged_arch_str
;
3636 /* Merge object attributes from IBFD into output_bfd of INFO.
3637 Raise an error if there are conflicting attributes. */
3640 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3642 bfd
*obfd
= info
->output_bfd
;
3643 obj_attribute
*in_attr
;
3644 obj_attribute
*out_attr
;
3646 bool priv_attrs_merged
= false;
3647 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3650 /* Skip linker created files. */
3651 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3654 /* Skip any input that doesn't have an attribute section.
3655 This enables to link object files without attribute section with
3657 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3660 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3662 /* This is the first object. Copy the attributes. */
3663 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3665 out_attr
= elf_known_obj_attributes_proc (obfd
);
3667 /* Use the Tag_null value to indicate the attributes have been
3674 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3675 out_attr
= elf_known_obj_attributes_proc (obfd
);
3677 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3681 case Tag_RISCV_arch
:
3682 if (!out_attr
[Tag_RISCV_arch
].s
)
3683 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3684 else if (in_attr
[Tag_RISCV_arch
].s
3685 && out_attr
[Tag_RISCV_arch
].s
)
3687 /* Check compatible. */
3689 riscv_merge_arch_attr_info (ibfd
,
3690 in_attr
[Tag_RISCV_arch
].s
,
3691 out_attr
[Tag_RISCV_arch
].s
);
3692 if (merged_arch
== NULL
)
3695 out_attr
[Tag_RISCV_arch
].s
= "";
3698 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3702 case Tag_RISCV_priv_spec
:
3703 case Tag_RISCV_priv_spec_minor
:
3704 case Tag_RISCV_priv_spec_revision
:
3705 /* If we have handled the privileged elf attributes, then skip it. */
3706 if (!priv_attrs_merged
)
3708 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3709 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3710 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3711 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3712 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3714 /* Get the privileged spec class from elf attributes. */
3715 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3719 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3724 /* Allow to link the object without the privileged specs. */
3725 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3727 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3728 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3729 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3731 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3732 && in_priv_spec
!= out_priv_spec
)
3735 (_("warning: %pB use privileged spec version %u.%u.%u but "
3736 "the output use version %u.%u.%u"),
3745 /* The privileged spec v1.9.1 can not be linked with others
3746 since the conflicts, so we plan to drop it in a year or
3748 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3749 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3752 (_("warning: privileged spec version 1.9.1 can not be "
3753 "linked with other spec versions"));
3756 /* Update the output privileged spec to the newest one. */
3757 if (in_priv_spec
> out_priv_spec
)
3759 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3760 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3761 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3764 priv_attrs_merged
= true;
3768 case Tag_RISCV_unaligned_access
:
3769 out_attr
[i
].i
|= in_attr
[i
].i
;
3772 case Tag_RISCV_stack_align
:
3773 if (out_attr
[i
].i
== 0)
3774 out_attr
[i
].i
= in_attr
[i
].i
;
3775 else if (in_attr
[i
].i
!= 0
3776 && out_attr
[i
].i
!= 0
3777 && out_attr
[i
].i
!= in_attr
[i
].i
)
3780 (_("error: %pB use %u-byte stack aligned but the output "
3781 "use %u-byte stack aligned"),
3782 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3788 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3791 /* If out_attr was copied from in_attr then it won't have a type yet. */
3792 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3793 out_attr
[i
].type
= in_attr
[i
].type
;
3796 /* Merge Tag_compatibility attributes and any common GNU ones. */
3797 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3800 /* Check for any attributes not known on RISC-V. */
3801 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3806 /* Merge backend specific data from an object file to the output
3807 object file when linking. */
3810 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3812 bfd
*obfd
= info
->output_bfd
;
3813 flagword new_flags
, old_flags
;
3815 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3818 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3820 (*_bfd_error_handler
)
3821 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3822 " target emulation `%s' does not match `%s'"),
3823 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3827 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3830 if (!riscv_merge_attributes (ibfd
, info
))
3833 /* Check to see if the input BFD actually contains any sections. If not,
3834 its flags may not have been initialized either, but it cannot actually
3835 cause any incompatibility. Do not short-circuit dynamic objects; their
3836 section list may be emptied by elf_link_add_object_symbols.
3838 Also check to see if there are no code sections in the input. In this
3839 case, there is no need to check for code specific flags. */
3840 if (!(ibfd
->flags
& DYNAMIC
))
3842 bool null_input_bfd
= true;
3843 bool only_data_sections
= true;
3846 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3848 null_input_bfd
= false;
3850 if ((bfd_section_flags (sec
)
3851 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3852 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3854 only_data_sections
= false;
3859 if (null_input_bfd
|| only_data_sections
)
3863 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3864 old_flags
= elf_elfheader (obfd
)->e_flags
;
3866 if (!elf_flags_init (obfd
))
3868 elf_flags_init (obfd
) = true;
3869 elf_elfheader (obfd
)->e_flags
= new_flags
;
3873 /* Disallow linking different float ABIs. */
3874 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3876 (*_bfd_error_handler
)
3877 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3878 riscv_float_abi_string (new_flags
),
3879 riscv_float_abi_string (old_flags
));
3883 /* Disallow linking RVE and non-RVE. */
3884 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3886 (*_bfd_error_handler
)
3887 (_("%pB: can't link RVE with other target"), ibfd
);
3891 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3892 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3894 /* Allow linking TSO and non-TSO, and keep the TSO flag. */
3895 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_TSO
;
3900 bfd_set_error (bfd_error_bad_value
);
3904 /* A second format for recording PC-relative hi relocations. This stores the
3905 information required to relax them to GP-relative addresses. */
3907 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3908 struct riscv_pcgp_hi_reloc
3915 bool undefined_weak
;
3916 riscv_pcgp_hi_reloc
*next
;
3919 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3920 struct riscv_pcgp_lo_reloc
3923 riscv_pcgp_lo_reloc
*next
;
3928 riscv_pcgp_hi_reloc
*hi
;
3929 riscv_pcgp_lo_reloc
*lo
;
3930 } riscv_pcgp_relocs
;
3932 /* Initialize the pcgp reloc info in P. */
3935 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3942 /* Free the pcgp reloc info in P. */
3945 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3946 bfd
*abfd ATTRIBUTE_UNUSED
,
3947 asection
*sec ATTRIBUTE_UNUSED
)
3949 riscv_pcgp_hi_reloc
*c
;
3950 riscv_pcgp_lo_reloc
*l
;
3952 for (c
= p
->hi
; c
!= NULL
; )
3954 riscv_pcgp_hi_reloc
*next
= c
->next
;
3959 for (l
= p
->lo
; l
!= NULL
; )
3961 riscv_pcgp_lo_reloc
*next
= l
->next
;
3967 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3968 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3969 relax the corresponding lo part reloc. */
3972 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3973 bfd_vma hi_addend
, bfd_vma hi_addr
,
3974 unsigned hi_sym
, asection
*sym_sec
,
3975 bool undefined_weak
)
3977 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
3980 new->hi_sec_off
= hi_sec_off
;
3981 new->hi_addend
= hi_addend
;
3982 new->hi_addr
= hi_addr
;
3983 new->hi_sym
= hi_sym
;
3984 new->sym_sec
= sym_sec
;
3985 new->undefined_weak
= undefined_weak
;
3991 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3992 This is used by a lo part reloc to find the corresponding hi part reloc. */
3994 static riscv_pcgp_hi_reloc
*
3995 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3997 riscv_pcgp_hi_reloc
*c
;
3999 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4000 if (c
->hi_sec_off
== hi_sec_off
)
4005 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4006 This is used to record relocs that can't be relaxed. */
4009 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4011 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4014 new->hi_sec_off
= hi_sec_off
;
4020 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4021 This is used by a hi part reloc to find the corresponding lo part reloc. */
4024 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4026 riscv_pcgp_lo_reloc
*c
;
4028 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4029 if (c
->hi_sec_off
== hi_sec_off
)
4035 riscv_update_pcgp_relocs (riscv_pcgp_relocs
*p
, asection
*deleted_sec
,
4036 bfd_vma deleted_addr
, size_t deleted_count
)
4038 /* Bytes have already been deleted and toaddr should match the old section
4039 size for our checks, so adjust it here. */
4040 bfd_vma toaddr
= deleted_sec
->size
+ deleted_count
;
4041 riscv_pcgp_lo_reloc
*l
;
4042 riscv_pcgp_hi_reloc
*h
;
4044 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4045 entries where they occur after the deleted bytes. */
4046 for (l
= p
->lo
; l
!= NULL
; l
= l
->next
)
4047 if (l
->hi_sec_off
> deleted_addr
4048 && l
->hi_sec_off
< toaddr
)
4049 l
->hi_sec_off
-= deleted_count
;
4051 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4052 these values occur after the deleted bytes. */
4053 for (h
= p
->hi
; h
!= NULL
; h
= h
->next
)
4055 if (h
->hi_sec_off
> deleted_addr
4056 && h
->hi_sec_off
< toaddr
)
4057 h
->hi_sec_off
-= deleted_count
;
4058 if (h
->sym_sec
== deleted_sec
4059 && h
->hi_addr
> deleted_addr
4060 && h
->hi_addr
< toaddr
)
4061 h
->hi_addr
-= deleted_count
;
4065 /* Delete some bytes, adjust relcocations and symbol table from a section. */
4068 _riscv_relax_delete_bytes (bfd
*abfd
,
4072 struct bfd_link_info
*link_info
,
4073 riscv_pcgp_relocs
*p
,
4074 bfd_vma delete_total
,
4077 unsigned int i
, symcount
;
4078 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
4079 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4080 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4081 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4082 bfd_byte
*contents
= data
->this_hdr
.contents
;
4083 size_t bytes_to_move
= toaddr
- addr
- count
;
4085 /* Actually delete the bytes. */
4087 memmove (contents
+ addr
, contents
+ addr
+ count
+ delete_total
, bytes_to_move
);
4089 /* Still adjust relocations and symbols in non-linear times. */
4090 toaddr
= sec
->size
+ count
;
4092 /* Adjust the location of all of the relocs. Note that we need not
4093 adjust the addends, since all PC-relative references must be against
4094 symbols, which we will adjust below. */
4095 for (i
= 0; i
< sec
->reloc_count
; i
++)
4096 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
4097 data
->relocs
[i
].r_offset
-= count
;
4099 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4100 table for which these values occur after the deleted bytes. */
4102 riscv_update_pcgp_relocs (p
, sec
, addr
, count
);
4104 /* Adjust the local symbols defined in this section. */
4105 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4107 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
4108 if (sym
->st_shndx
== sec_shndx
)
4110 /* If the symbol is in the range of memory we just moved, we
4111 have to adjust its value. */
4112 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
4113 sym
->st_value
-= count
;
4115 /* If the symbol *spans* the bytes we just deleted (i.e. its
4116 *end* is in the moved bytes but its *start* isn't), then we
4117 must adjust its size.
4119 This test needs to use the original value of st_value, otherwise
4120 we might accidentally decrease size when deleting bytes right
4121 before the symbol. But since deleted relocs can't span across
4122 symbols, we can't have both a st_value and a st_size decrease,
4123 so it is simpler to just use an else. */
4124 else if (sym
->st_value
<= addr
4125 && sym
->st_value
+ sym
->st_size
> addr
4126 && sym
->st_value
+ sym
->st_size
<= toaddr
)
4127 sym
->st_size
-= count
;
4131 /* Now adjust the global symbols defined in this section. */
4132 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
4133 - symtab_hdr
->sh_info
);
4135 for (i
= 0; i
< symcount
; i
++)
4137 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
4139 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4140 containing the definition of __wrap_SYMBOL, includes a direct
4141 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4142 the same symbol (which is __wrap_SYMBOL), but still exist as two
4143 different symbols in 'sym_hashes', we don't want to adjust
4144 the global symbol __wrap_SYMBOL twice.
4146 The same problem occurs with symbols that are versioned_hidden, as
4147 foo becomes an alias for foo@BAR, and hence they need the same
4149 if (link_info
->wrap_hash
!= NULL
4150 || sym_hash
->versioned
!= unversioned
)
4152 struct elf_link_hash_entry
**cur_sym_hashes
;
4154 /* Loop only over the symbols which have already been checked. */
4155 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4158 /* If the current symbol is identical to 'sym_hash', that means
4159 the symbol was already adjusted (or at least checked). */
4160 if (*cur_sym_hashes
== sym_hash
)
4163 /* Don't adjust the symbol again. */
4164 if (cur_sym_hashes
< &sym_hashes
[i
])
4168 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4169 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4170 && sym_hash
->root
.u
.def
.section
== sec
)
4172 /* As above, adjust the value if needed. */
4173 if (sym_hash
->root
.u
.def
.value
> addr
4174 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4175 sym_hash
->root
.u
.def
.value
-= count
;
4177 /* As above, adjust the size if needed. */
4178 else if (sym_hash
->root
.u
.def
.value
<= addr
4179 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4180 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4181 sym_hash
->size
-= count
;
4188 typedef bool (*relax_delete_t
) (bfd
*, asection
*,
4190 struct bfd_link_info
*,
4191 riscv_pcgp_relocs
*,
4192 Elf_Internal_Rela
*);
4194 static relax_delete_t riscv_relax_delete_bytes
;
4196 /* Do not delete some bytes from a section while relaxing.
4197 Just mark the deleted bytes as R_RISCV_DELETE. */
4200 _riscv_relax_delete_piecewise (bfd
*abfd ATTRIBUTE_UNUSED
,
4201 asection
*sec ATTRIBUTE_UNUSED
,
4204 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
,
4205 riscv_pcgp_relocs
*p ATTRIBUTE_UNUSED
,
4206 Elf_Internal_Rela
*rel
)
4210 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4211 rel
->r_offset
= addr
;
4212 rel
->r_addend
= count
;
4216 /* Delete some bytes from a section while relaxing. */
4219 _riscv_relax_delete_immediate (bfd
*abfd
,
4223 struct bfd_link_info
*link_info
,
4224 riscv_pcgp_relocs
*p
,
4225 Elf_Internal_Rela
*rel
)
4228 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4229 return _riscv_relax_delete_bytes (abfd
, sec
, addr
, count
,
4230 link_info
, p
, 0, sec
->size
);
4233 /* Delete the bytes for R_RISCV_DELETE relocs. */
4236 riscv_relax_resolve_delete_relocs (bfd
*abfd
,
4238 struct bfd_link_info
*link_info
,
4239 Elf_Internal_Rela
*relocs
)
4241 bfd_vma delete_total
= 0;
4244 for (i
= 0; i
< sec
->reloc_count
; i
++)
4246 Elf_Internal_Rela
*rel
= relocs
+ i
;
4247 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_DELETE
)
4250 /* Find the next R_RISCV_DELETE reloc if possible. */
4251 Elf_Internal_Rela
*rel_next
= NULL
;
4252 unsigned int start
= rel
- relocs
;
4253 for (i
= start
; i
< sec
->reloc_count
; i
++)
4255 /* Since we only replace existing relocs and don't add new relocs, the
4256 relocs are in sequential order. We can skip the relocs prior to this
4257 one, making this search linear time. */
4258 rel_next
= relocs
+ i
;
4259 if (ELFNN_R_TYPE ((rel_next
)->r_info
) == R_RISCV_DELETE
4260 && (rel_next
)->r_offset
> rel
->r_offset
)
4262 BFD_ASSERT (rel_next
- rel
> 0);
4269 bfd_vma toaddr
= rel_next
== NULL
? sec
->size
: rel_next
->r_offset
;
4270 if (!_riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4271 link_info
, NULL
, delete_total
, toaddr
))
4274 delete_total
+= rel
->r_addend
;
4275 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4277 /* Skip ahead to the next delete reloc. */
4278 i
= rel_next
!= NULL
? (unsigned int) (rel_next
- relocs
- 1)
4285 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4286 struct bfd_link_info
*,
4287 Elf_Internal_Rela
*,
4288 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4289 riscv_pcgp_relocs
*,
4290 bool undefined_weak
);
4292 /* Relax AUIPC + JALR into JAL. */
4295 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4296 struct bfd_link_info
*link_info
,
4297 Elf_Internal_Rela
*rel
,
4299 bfd_vma max_alignment
,
4300 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4302 riscv_pcgp_relocs
*pcgp_relocs
,
4303 bool undefined_weak ATTRIBUTE_UNUSED
)
4305 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4306 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4307 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4308 bfd_vma auipc
, jalr
;
4309 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4311 /* If the call crosses section boundaries, an alignment directive could
4312 cause the PC-relative offset to later increase, so we need to add in the
4313 max alignment of any section inclusive from the call to the target.
4314 Otherwise, we only need to use the alignment of the current section. */
4315 if (VALID_JTYPE_IMM (foff
))
4317 if (sym_sec
->output_section
== sec
->output_section
4318 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4319 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4320 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4323 /* See if this function call can be shortened. */
4324 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4327 /* Shorten the function call. */
4328 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4330 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4331 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4332 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4333 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4335 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4336 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4340 /* Relax to C.J[AL] rd, addr. */
4341 r_type
= R_RISCV_RVC_JUMP
;
4342 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4345 else if (VALID_JTYPE_IMM (foff
))
4347 /* Relax to JAL rd, addr. */
4348 r_type
= R_RISCV_JAL
;
4349 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4353 /* Near zero, relax to JALR rd, x0, addr. */
4354 r_type
= R_RISCV_LO12_I
;
4355 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4358 /* Replace the R_RISCV_CALL reloc. */
4359 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4360 /* Replace the AUIPC. */
4361 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4363 /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */
4365 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4366 link_info
, pcgp_relocs
, rel
+ 1);
4369 /* Traverse all output sections and return the max alignment. */
4372 _bfd_riscv_get_max_alignment (asection
*sec
)
4374 unsigned int max_alignment_power
= 0;
4377 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4379 if (o
->alignment_power
> max_alignment_power
)
4380 max_alignment_power
= o
->alignment_power
;
4383 return (bfd_vma
) 1 << max_alignment_power
;
4386 /* Relax non-PIC global variable references to GP-relative references. */
4389 _bfd_riscv_relax_lui (bfd
*abfd
,
4392 struct bfd_link_info
*link_info
,
4393 Elf_Internal_Rela
*rel
,
4395 bfd_vma max_alignment
,
4396 bfd_vma reserve_size
,
4398 riscv_pcgp_relocs
*pcgp_relocs
,
4399 bool undefined_weak
)
4401 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4402 bfd_vma gp
= riscv_elf_hash_table (link_info
)->params
->relax_gp
4403 ? riscv_global_pointer_value (link_info
)
4405 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4407 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4411 /* If gp and the symbol are in the same output section, which is not the
4412 abs section, then consider only that output section's alignment. */
4413 struct bfd_link_hash_entry
*h
=
4414 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4416 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4417 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4418 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4421 /* Is the reference in range of x0 or gp?
4422 Valid gp range conservatively because of alignment issue. */
4424 || (VALID_ITYPE_IMM (symval
)
4426 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4428 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4430 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4431 switch (ELFNN_R_TYPE (rel
->r_info
))
4433 case R_RISCV_LO12_I
:
4436 /* Change the RS1 to zero. */
4437 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4438 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4439 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4442 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4445 case R_RISCV_LO12_S
:
4448 /* Change the RS1 to zero. */
4449 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4450 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4451 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4454 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4458 /* Delete unnecessary LUI and reuse the reloc. */
4460 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4461 link_info
, pcgp_relocs
, rel
);
4468 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4469 account for this assuming page alignment at worst. In the presence of
4470 RELRO segment the linker aligns it by one page size, therefore sections
4471 after the segment can be moved more than one page. */
4474 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4475 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4476 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4477 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4478 : ELF_MAXPAGESIZE
)))
4480 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4481 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4482 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4483 if (rd
== 0 || rd
== X_SP
)
4486 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4487 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4489 /* Replace the R_RISCV_HI20 reloc. */
4490 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4492 /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */
4494 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4495 link_info
, pcgp_relocs
, rel
+ 1);
4501 /* Relax non-PIC TLS references to TP-relative references. */
4504 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4506 asection
*sym_sec ATTRIBUTE_UNUSED
,
4507 struct bfd_link_info
*link_info
,
4508 Elf_Internal_Rela
*rel
,
4510 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4511 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4513 riscv_pcgp_relocs
*pcgp_relocs
,
4514 bool undefined_weak ATTRIBUTE_UNUSED
)
4516 /* See if this symbol is in range of tp. */
4517 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4520 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4521 switch (ELFNN_R_TYPE (rel
->r_info
))
4523 case R_RISCV_TPREL_LO12_I
:
4524 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4527 case R_RISCV_TPREL_LO12_S
:
4528 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4531 case R_RISCV_TPREL_HI20
:
4532 case R_RISCV_TPREL_ADD
:
4533 /* Delete unnecessary instruction and reuse the reloc. */
4535 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4543 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4544 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4547 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4549 struct bfd_link_info
*link_info
,
4550 Elf_Internal_Rela
*rel
,
4552 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4553 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4554 bool *again ATTRIBUTE_UNUSED
,
4555 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4556 bool undefined_weak ATTRIBUTE_UNUSED
)
4558 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4559 bfd_vma alignment
= 1, pos
;
4560 while (alignment
<= rel
->r_addend
)
4563 symval
-= rel
->r_addend
;
4564 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4565 bfd_vma nop_bytes
= aligned_addr
- symval
;
4567 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4568 sec
->sec_flg0
= true;
4570 /* Make sure there are enough NOPs to actually achieve the alignment. */
4571 if (rel
->r_addend
< nop_bytes
)
4574 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4575 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4576 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4577 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4578 bfd_set_error (bfd_error_bad_value
);
4582 /* Delete the reloc. */
4583 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4585 /* If the number of NOPs is already correct, there's nothing to do. */
4586 if (nop_bytes
== rel
->r_addend
)
4589 /* Write as many RISC-V NOPs as we need. */
4590 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4591 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4593 /* Write a final RVC NOP if need be. */
4594 if (nop_bytes
% 4 != 0)
4595 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4597 /* Delete excess bytes. */
4598 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4599 rel
->r_addend
- nop_bytes
, link_info
,
4603 /* Relax PC-relative references to GP-relative references. */
4606 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4609 struct bfd_link_info
*link_info
,
4610 Elf_Internal_Rela
*rel
,
4612 bfd_vma max_alignment
,
4613 bfd_vma reserve_size
,
4615 riscv_pcgp_relocs
*pcgp_relocs
,
4616 bool undefined_weak
)
4618 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4619 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4621 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4623 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4624 actual target address. */
4625 riscv_pcgp_hi_reloc hi_reloc
;
4626 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4627 switch (ELFNN_R_TYPE (rel
->r_info
))
4629 case R_RISCV_PCREL_LO12_I
:
4630 case R_RISCV_PCREL_LO12_S
:
4632 /* If the %lo has an addend, it isn't for the label pointing at the
4633 hi part instruction, but rather for the symbol pointed at by the
4634 hi part instruction. So we must subtract it here for the lookup.
4635 It is still used below in the final symbol address. */
4636 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4637 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4641 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4646 symval
= hi_reloc
.hi_addr
;
4647 sym_sec
= hi_reloc
.sym_sec
;
4649 /* We can not know whether the undefined weak symbol is referenced
4650 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4651 we have to record the 'undefined_weak' flag when handling the
4652 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4653 undefined_weak
= hi_reloc
.undefined_weak
;
4657 case R_RISCV_PCREL_HI20
:
4658 /* Mergeable symbols and code might later move out of range. */
4659 if (! undefined_weak
4660 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4663 /* If the cooresponding lo relocation has already been seen then it's not
4664 safe to relax this relocation. */
4665 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4676 /* If gp and the symbol are in the same output section, which is not the
4677 abs section, then consider only that output section's alignment. */
4678 struct bfd_link_hash_entry
*h
=
4679 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4681 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4682 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4683 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4686 /* Is the reference in range of x0 or gp?
4687 Valid gp range conservatively because of alignment issue. */
4689 || (VALID_ITYPE_IMM (symval
)
4691 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4693 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4695 unsigned sym
= hi_reloc
.hi_sym
;
4696 switch (ELFNN_R_TYPE (rel
->r_info
))
4698 case R_RISCV_PCREL_LO12_I
:
4701 /* Change the RS1 to zero, and then modify the relocation
4702 type to R_RISCV_LO12_I. */
4703 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4704 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4705 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4706 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4707 rel
->r_addend
= hi_reloc
.hi_addend
;
4711 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4712 rel
->r_addend
+= hi_reloc
.hi_addend
;
4716 case R_RISCV_PCREL_LO12_S
:
4719 /* Change the RS1 to zero, and then modify the relocation
4720 type to R_RISCV_LO12_S. */
4721 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4722 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4723 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4724 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4725 rel
->r_addend
= hi_reloc
.hi_addend
;
4729 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4730 rel
->r_addend
+= hi_reloc
.hi_addend
;
4734 case R_RISCV_PCREL_HI20
:
4735 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4739 ELFNN_R_SYM(rel
->r_info
),
4742 /* Delete unnecessary AUIPC and reuse the reloc. */
4744 riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4756 /* Called by after_allocation to set the information of data segment
4760 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
4761 int *data_segment_phase
)
4763 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4764 htab
->data_segment_phase
= data_segment_phase
;
4769 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs and
4770 deletes the obsolete bytes.
4771 Pass 1: Which cannot be disabled, handles code alignment directives. */
4774 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4775 struct bfd_link_info
*info
,
4778 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4779 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4780 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4781 Elf_Internal_Rela
*relocs
;
4784 bfd_vma max_alignment
, reserve_size
= 0;
4785 riscv_pcgp_relocs pcgp_relocs
;
4789 if (bfd_link_relocatable (info
)
4791 || sec
->reloc_count
== 0
4792 || (sec
->flags
& SEC_RELOC
) == 0
4793 || (sec
->flags
& SEC_HAS_CONTENTS
) == 0
4794 || (info
->disable_target_specific_optimizations
4795 && info
->relax_pass
== 0)
4796 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4797 and defined in ld/ldexp.h. */
4798 || *(htab
->data_segment_phase
) == 4)
4801 riscv_init_pcgp_relocs (&pcgp_relocs
);
4803 /* Read this BFD's relocs if we haven't done so already. */
4805 relocs
= data
->relocs
;
4806 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4807 info
->keep_memory
)))
4812 max_alignment
= htab
->max_alignment
;
4813 if (max_alignment
== (bfd_vma
) -1)
4815 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4816 htab
->max_alignment
= max_alignment
;
4820 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4822 /* Examine and consider relaxing each reloc. */
4823 for (i
= 0; i
< sec
->reloc_count
; i
++)
4826 Elf_Internal_Rela
*rel
= relocs
+ i
;
4827 relax_func_t relax_func
;
4828 int type
= ELFNN_R_TYPE (rel
->r_info
);
4831 bool undefined_weak
= false;
4834 riscv_relax_delete_bytes
= NULL
;
4835 if (info
->relax_pass
== 0)
4837 if (type
== R_RISCV_CALL
4838 || type
== R_RISCV_CALL_PLT
)
4839 relax_func
= _bfd_riscv_relax_call
;
4840 else if (type
== R_RISCV_HI20
4841 || type
== R_RISCV_LO12_I
4842 || type
== R_RISCV_LO12_S
)
4843 relax_func
= _bfd_riscv_relax_lui
;
4844 else if (type
== R_RISCV_TPREL_HI20
4845 || type
== R_RISCV_TPREL_ADD
4846 || type
== R_RISCV_TPREL_LO12_I
4847 || type
== R_RISCV_TPREL_LO12_S
)
4848 relax_func
= _bfd_riscv_relax_tls_le
;
4849 else if (!bfd_link_pic (info
) && htab
->params
->relax_gp
4850 && (type
== R_RISCV_PCREL_HI20
4851 || type
== R_RISCV_PCREL_LO12_I
4852 || type
== R_RISCV_PCREL_LO12_S
))
4853 relax_func
= _bfd_riscv_relax_pc
;
4856 riscv_relax_delete_bytes
= _riscv_relax_delete_piecewise
;
4858 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4859 if (i
== sec
->reloc_count
- 1
4860 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4861 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4864 /* Skip over the R_RISCV_RELAX. */
4867 else if (info
->relax_pass
== 1 && type
== R_RISCV_ALIGN
)
4869 relax_func
= _bfd_riscv_relax_align
;
4870 riscv_relax_delete_bytes
= _riscv_relax_delete_immediate
;
4875 data
->relocs
= relocs
;
4877 /* Read this BFD's contents if we haven't done so already. */
4878 if (!data
->this_hdr
.contents
4879 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4882 /* Read this BFD's symbols if we haven't done so already. */
4883 if (symtab_hdr
->sh_info
!= 0
4884 && !symtab_hdr
->contents
4885 && !(symtab_hdr
->contents
=
4886 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4887 symtab_hdr
->sh_info
,
4888 0, NULL
, NULL
, NULL
)))
4891 /* Get the value of the symbol referred to by the reloc. */
4892 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4894 /* A local symbol. */
4895 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4896 + ELFNN_R_SYM (rel
->r_info
));
4897 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4898 ? 0 : isym
->st_size
- rel
->r_addend
;
4900 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4901 a fake global symbol entry for this, so deal with the local ifunc
4903 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4906 if (isym
->st_shndx
== SHN_UNDEF
)
4907 sym_sec
= sec
, symval
= rel
->r_offset
;
4910 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4911 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4913 /* The purpose of this code is unknown. It breaks linker scripts
4914 for embedded development that place sections at address zero.
4915 This code is believed to be unnecessary. Disabling it but not
4916 yet removing it, in case something breaks. */
4917 if (sec_addr (sym_sec
) == 0)
4920 symval
= isym
->st_value
;
4922 symtype
= ELF_ST_TYPE (isym
->st_info
);
4927 struct elf_link_hash_entry
*h
;
4929 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4930 h
= elf_sym_hashes (abfd
)[indx
];
4932 while (h
->root
.type
== bfd_link_hash_indirect
4933 || h
->root
.type
== bfd_link_hash_warning
)
4934 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4936 /* Disable the relaxation for ifunc. */
4937 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4940 if (h
->root
.type
== bfd_link_hash_undefweak
4941 && (relax_func
== _bfd_riscv_relax_lui
4942 || relax_func
== _bfd_riscv_relax_pc
))
4944 /* For the lui and auipc relaxations, since the symbol
4945 value of an undefined weak symbol is always be zero,
4946 we can optimize the patterns into a single LI/MV/ADDI
4949 Note that, creating shared libraries and pie output may
4950 break the rule above. Fortunately, since we do not relax
4951 pc relocs when creating shared libraries and pie output,
4952 and the absolute address access for R_RISCV_HI20 isn't
4953 allowed when "-fPIC" is set, the problem of creating shared
4954 libraries can not happen currently. Once we support the
4955 auipc relaxations when creating shared libraries, then we will
4956 need the more rigorous checking for this optimization. */
4957 undefined_weak
= true;
4960 /* This line has to match the check in riscv_elf_relocate_section
4961 in the R_RISCV_CALL[_PLT] case. */
4962 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4964 sym_sec
= htab
->elf
.splt
;
4965 symval
= h
->plt
.offset
;
4967 else if (undefined_weak
)
4970 sym_sec
= bfd_und_section_ptr
;
4972 else if ((h
->root
.type
== bfd_link_hash_defined
4973 || h
->root
.type
== bfd_link_hash_defweak
)
4974 && h
->root
.u
.def
.section
!= NULL
4975 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4977 symval
= h
->root
.u
.def
.value
;
4978 sym_sec
= h
->root
.u
.def
.section
;
4983 if (h
->type
!= STT_FUNC
)
4985 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4989 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4990 && (sym_sec
->flags
& SEC_MERGE
))
4992 /* At this stage in linking, no SEC_MERGE symbol has been
4993 adjusted, so all references to such symbols need to be
4994 passed through _bfd_merged_section_offset. (Later, in
4995 relocate_section, all SEC_MERGE symbols *except* for
4996 section symbols have been adjusted.)
4998 gas may reduce relocations against symbols in SEC_MERGE
4999 sections to a relocation against the section symbol when
5000 the original addend was zero. When the reloc is against
5001 a section symbol we should include the addend in the
5002 offset passed to _bfd_merged_section_offset, since the
5003 location of interest is the original symbol. On the
5004 other hand, an access to "sym+addend" where "sym" is not
5005 a section symbol should not include the addend; Such an
5006 access is presumed to be an offset from "sym"; The
5007 location of interest is just "sym". */
5008 if (symtype
== STT_SECTION
)
5009 symval
+= rel
->r_addend
;
5011 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
5012 elf_section_data (sym_sec
)->sec_info
,
5015 if (symtype
!= STT_SECTION
)
5016 symval
+= rel
->r_addend
;
5019 symval
+= rel
->r_addend
;
5021 symval
+= sec_addr (sym_sec
);
5023 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
5024 max_alignment
, reserve_size
, again
,
5025 &pcgp_relocs
, undefined_weak
))
5029 /* Resolve R_RISCV_DELETE relocations. */
5030 if (!riscv_relax_resolve_delete_relocs (abfd
, sec
, info
, relocs
))
5036 if (relocs
!= data
->relocs
)
5038 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
5044 # define PRSTATUS_SIZE 204
5045 # define PRSTATUS_OFFSET_PR_CURSIG 12
5046 # define PRSTATUS_OFFSET_PR_PID 24
5047 # define PRSTATUS_OFFSET_PR_REG 72
5048 # define ELF_GREGSET_T_SIZE 128
5049 # define PRPSINFO_SIZE 128
5050 # define PRPSINFO_OFFSET_PR_PID 16
5051 # define PRPSINFO_OFFSET_PR_FNAME 32
5052 # define PRPSINFO_OFFSET_PR_PSARGS 48
5053 # define PRPSINFO_PR_FNAME_LENGTH 16
5054 # define PRPSINFO_PR_PSARGS_LENGTH 80
5056 # define PRSTATUS_SIZE 376
5057 # define PRSTATUS_OFFSET_PR_CURSIG 12
5058 # define PRSTATUS_OFFSET_PR_PID 32
5059 # define PRSTATUS_OFFSET_PR_REG 112
5060 # define ELF_GREGSET_T_SIZE 256
5061 # define PRPSINFO_SIZE 136
5062 # define PRPSINFO_OFFSET_PR_PID 24
5063 # define PRPSINFO_OFFSET_PR_FNAME 40
5064 # define PRPSINFO_OFFSET_PR_PSARGS 56
5065 # define PRPSINFO_PR_FNAME_LENGTH 16
5066 # define PRPSINFO_PR_PSARGS_LENGTH 80
5069 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
5070 before the generic code in elf.c. By checking the compiler defines we
5071 only perform any action here if the generic code would otherwise not be
5072 able to help us. The intention is that bare metal core dumps (where the
5073 prstatus_t and/or prpsinfo_t might not be available) will use this code,
5074 while non bare metal tools will use the generic elf code. */
5077 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
5078 char *buf ATTRIBUTE_UNUSED
,
5079 int *bufsiz ATTRIBUTE_UNUSED
,
5080 int note_type ATTRIBUTE_UNUSED
, ...)
5087 #if !defined (HAVE_PRPSINFO_T)
5090 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5093 va_start (ap
, note_type
);
5094 memset (data
, 0, sizeof (data
));
5095 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5096 PRPSINFO_PR_FNAME_LENGTH
);
5097 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5099 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5100 -Wstringop-truncation:
5101 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5103 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5105 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5106 PRPSINFO_PR_PSARGS_LENGTH
);
5107 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5111 return elfcore_write_note (abfd
, buf
, bufsiz
,
5112 "CORE", note_type
, data
, sizeof (data
));
5114 #endif /* !HAVE_PRPSINFO_T */
5116 #if !defined (HAVE_PRSTATUS_T)
5119 char data
[PRSTATUS_SIZE
];
5125 va_start (ap
, note_type
);
5126 memset (data
, 0, sizeof(data
));
5127 pid
= va_arg (ap
, long);
5128 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5129 cursig
= va_arg (ap
, int);
5130 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5131 greg
= va_arg (ap
, const void *);
5132 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5133 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5135 return elfcore_write_note (abfd
, buf
, bufsiz
,
5136 "CORE", note_type
, data
, sizeof (data
));
5138 #endif /* !HAVE_PRSTATUS_T */
5142 /* Support for core dump NOTE sections. */
5145 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5147 switch (note
->descsz
)
5152 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5154 elf_tdata (abfd
)->core
->signal
5155 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5158 elf_tdata (abfd
)->core
->lwpid
5159 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5163 /* Make a ".reg/999" section. */
5164 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5165 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5169 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5171 switch (note
->descsz
)
5176 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5178 elf_tdata (abfd
)->core
->pid
5179 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5182 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5183 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5184 PRPSINFO_PR_FNAME_LENGTH
);
5187 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5188 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5189 PRPSINFO_PR_PSARGS_LENGTH
);
5193 /* Note that for some reason, a spurious space is tacked
5194 onto the end of the args in some (at least one anyway)
5195 implementations, so strip it off if it exists. */
5198 char *command
= elf_tdata (abfd
)->core
->command
;
5199 int n
= strlen (command
);
5201 if (0 < n
&& command
[n
- 1] == ' ')
5202 command
[n
- 1] = '\0';
5208 /* Set the right mach type. */
5211 riscv_elf_object_p (bfd
*abfd
)
5213 /* There are only two mach types in RISCV currently. */
5214 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5215 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5216 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5218 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5223 /* Determine whether an object attribute tag takes an integer, a
5227 riscv_elf_obj_attrs_arg_type (int tag
)
5229 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5232 /* Do not choose mapping symbols as a function name. */
5234 static bfd_size_type
5235 riscv_maybe_function_sym (const asymbol
*sym
,
5239 if (sym
->flags
& BSF_LOCAL
5240 && riscv_elf_is_mapping_symbols (sym
->name
))
5243 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5246 /* Treat the following cases as target special symbols, they are
5250 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5252 /* PR27584, local and empty symbols. Since they are usually
5253 generated for pcrel relocations. */
5254 return (!strcmp (sym
->name
, "")
5255 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5256 /* PR27916, mapping symbols. */
5257 || riscv_elf_is_mapping_symbols (sym
->name
));
5261 riscv_elf_additional_program_headers (bfd
*abfd
,
5262 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5266 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5267 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5274 riscv_elf_modify_segment_map (bfd
*abfd
,
5275 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5278 struct elf_segment_map
*m
, **pm
;
5281 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5283 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5286 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5287 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5289 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5294 m
= bfd_zalloc (abfd
, amt
);
5298 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5302 /* We want to put it after the PHDR and INTERP segments. */
5303 pm
= &elf_seg_map (abfd
);
5305 && ((*pm
)->p_type
== PT_PHDR
5306 || (*pm
)->p_type
== PT_INTERP
))
5317 /* Merge non-visibility st_other attributes. */
5320 riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
5321 unsigned int st_other
,
5322 bool definition ATTRIBUTE_UNUSED
,
5323 bool dynamic ATTRIBUTE_UNUSED
)
5325 unsigned int isym_sto
= st_other
& ~ELF_ST_VISIBILITY (-1);
5326 unsigned int h_sto
= h
->other
& ~ELF_ST_VISIBILITY (-1);
5328 if (isym_sto
== h_sto
)
5331 if (isym_sto
& ~STO_RISCV_VARIANT_CC
)
5332 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5333 h
->root
.root
.string
, isym_sto
);
5335 if (isym_sto
& STO_RISCV_VARIANT_CC
)
5336 h
->other
|= STO_RISCV_VARIANT_CC
;
5339 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5340 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5341 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5342 #define TARGET_BIG_NAME "elfNN-bigriscv"
5344 #define elf_backend_reloc_type_class riscv_reloc_type_class
5346 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5347 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5348 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5349 #define bfd_elfNN_bfd_merge_private_bfd_data \
5350 _bfd_riscv_elf_merge_private_bfd_data
5351 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5353 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5354 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5355 #define elf_backend_check_relocs riscv_elf_check_relocs
5356 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5357 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5358 #define elf_backend_relocate_section riscv_elf_relocate_section
5359 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5360 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5361 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5362 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5363 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5364 #define elf_backend_object_p riscv_elf_object_p
5365 #define elf_backend_write_core_note riscv_write_core_note
5366 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5367 #define elf_info_to_howto_rel NULL
5368 #define elf_info_to_howto riscv_info_to_howto_rela
5369 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5370 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5371 #define elf_backend_additional_program_headers \
5372 riscv_elf_additional_program_headers
5373 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5374 #define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
5376 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5378 #define elf_backend_can_gc_sections 1
5379 #define elf_backend_can_refcount 1
5380 #define elf_backend_want_got_plt 1
5381 #define elf_backend_plt_readonly 1
5382 #define elf_backend_plt_alignment 4
5383 #define elf_backend_want_plt_sym 1
5384 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5385 #define elf_backend_want_dynrelro 1
5386 #define elf_backend_rela_normal 1
5387 #define elf_backend_default_execstack 0
5389 #undef elf_backend_obj_attrs_vendor
5390 #define elf_backend_obj_attrs_vendor "riscv"
5391 #undef elf_backend_obj_attrs_arg_type
5392 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5393 #undef elf_backend_obj_attrs_section_type
5394 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5395 #undef elf_backend_obj_attrs_section
5396 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5398 #include "elfNN-target.h"