1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2024 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 /* True if dynamic relocation is needed. If we are creating a shared library,
42 and this is a reloc against a global symbol, or a non PC relative reloc
43 against a local symbol, then we need to copy the reloc into the shared
44 library. However, if we are linking with -Bsymbolic, we do not need to
45 copy a reloc against a global symbol which is defined in an object we are
46 including in the link (i.e., DEF_REGULAR is set).
48 At this point we have not seen all the input files, so it is possible that
49 DEF_REGULAR is not set now but will be set later (it is never cleared).
50 In case of a weak definition, DEF_REGULAR may be cleared later by a strong
51 definition in a shared library. We account for that possibility below by
52 storing information in the relocs_copied field of the hash table entry.
53 A similar situation occurs when creating shared libraries and symbol
54 visibility changes render the symbol local.
56 If on the other hand, we are creating an executable, we may need to keep
57 relocations for symbols satisfied by a dynamic library if we manage to
58 avoid copy relocs for the symbol.
60 Generate dynamic pointer relocation against STT_GNU_IFUNC symbol in the
61 non-code section (R_RISCV_32/R_RISCV_64). */
62 #define RISCV_NEED_DYNAMIC_RELOC(PCREL, INFO, H, SEC) \
63 ((bfd_link_pic (INFO) \
64 && ((SEC)->flags & SEC_ALLOC) != 0 \
67 && (!(INFO)->symbolic \
68 || (H)->root.type == bfd_link_hash_defweak \
69 || !(H)->def_regular)))) \
70 || (!bfd_link_pic (INFO) \
71 && ((SEC)->flags & SEC_ALLOC) != 0 \
73 && ((H)->root.type == bfd_link_hash_defweak \
74 || !(H)->def_regular)) \
75 || (!bfd_link_pic (INFO) \
77 && (H)->type == STT_GNU_IFUNC \
78 && ((SEC)->flags & SEC_CODE) == 0))
80 /* True if dynamic relocation should be generated. */
81 #define RISCV_GENERATE_DYNAMIC_RELOC(PCREL, INFO, H, RESOLVED_TO_ZERO) \
82 ((bfd_link_pic (INFO) \
84 || (ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT && !(RESOLVED_TO_ZERO)) \
85 || (H)->root.type != bfd_link_hash_undefweak) \
87 || !SYMBOL_CALLS_LOCAL ((INFO), (H)))) \
88 || (!bfd_link_pic (INFO) \
90 && (H)->dynindx != -1 \
91 && !(H)->non_got_ref \
92 && (((H)->def_dynamic && !(H)->def_regular) \
93 || (H)->root.type == bfd_link_hash_undefweak \
94 || (H)->root.type == bfd_link_hash_undefined)))
96 /* True if this input relocation should be copied to output. H->dynindx
97 may be -1 if this symbol was marked to become local. */
98 #define RISCV_COPY_INPUT_RELOC(INFO, H) \
100 && (H)->dynindx != -1 \
101 && (!bfd_link_pic (INFO) \
102 || !SYMBOLIC_BIND ((INFO), (H)) \
103 || !(H)->def_regular))
105 /* True if this is actually a static link, or it is a -Bsymbolic link
106 and the symbol is defined locally, or the symbol was forced to be
107 local because of a version file. */
108 #define RISCV_RESOLVED_LOCALLY(INFO, H) \
109 (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (elf_hash_table (INFO)->dynamic_sections_created, \
110 bfd_link_pic (INFO), (H)) \
111 || (bfd_link_pic (INFO) \
112 && SYMBOL_REFERENCES_LOCAL ((INFO), (H))))
114 /* Set NEED_RELOC to true if TLS GD/IE needs dynamic relocations, and INDX will
115 be the dynamic index. PR22263, use the same check in allocate_dynrelocs and
116 riscv_elf_relocate_section for TLS GD/IE. */
117 #define RISCV_TLS_GD_IE_NEED_DYN_RELOC(INFO, DYN, H, INDX, NEED_RELOC) \
121 && (H)->dynindx != -1 \
122 && WILL_CALL_FINISH_DYNAMIC_SYMBOL ((DYN), bfd_link_pic (INFO), (H)) \
123 && (bfd_link_dll (INFO) || !SYMBOL_REFERENCES_LOCAL ((INFO), (H)))) \
124 (INDX) = (H)->dynindx; \
125 if ((bfd_link_dll (INFO) || (INDX) != 0) \
127 || ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT \
128 || (H)->root.type != bfd_link_hash_undefweak)) \
129 (NEED_RELOC) = true; \
135 #define MINUS_ONE ((bfd_vma)0 - 1)
137 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
139 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
141 /* The name of the dynamic interpreter. This is put in the .interp
144 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
145 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
147 #define ELF_ARCH bfd_arch_riscv
148 #define ELF_TARGET_ID RISCV_ELF_DATA
149 #define ELF_MACHINE_CODE EM_RISCV
150 #define ELF_MAXPAGESIZE 0x1000
151 #define ELF_COMMONPAGESIZE 0x1000
153 #define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
155 /* RISC-V ELF linker hash entry. */
157 struct riscv_elf_link_hash_entry
159 struct elf_link_hash_entry elf
;
161 #define GOT_UNKNOWN 0
166 #define GOT_TLSDESC 16
170 #define riscv_elf_hash_entry(ent) \
171 ((struct riscv_elf_link_hash_entry *) (ent))
173 struct _bfd_riscv_elf_obj_tdata
175 struct elf_obj_tdata root
;
177 /* tls_type for each local got entry. */
178 char *local_got_tls_type
;
181 #define _bfd_riscv_elf_tdata(abfd) \
182 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
184 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
185 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
187 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
188 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
189 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
191 #define is_riscv_elf(bfd) \
192 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
193 && elf_tdata (bfd) != NULL \
194 && elf_object_id (bfd) == RISCV_ELF_DATA)
197 elfNN_riscv_mkobject (bfd
*abfd
)
199 return bfd_elf_allocate_object (abfd
,
200 sizeof (struct _bfd_riscv_elf_obj_tdata
),
204 #include "elf/common.h"
205 #include "elf/internal.h"
207 struct riscv_elf_link_hash_table
209 struct elf_link_hash_table elf
;
211 /* Various options and other info passed from the linker. */
212 struct riscv_elf_params
*params
;
214 /* Short-cuts to get to dynamic linker sections. */
217 /* The max alignment of output sections. */
218 bfd_vma max_alignment
;
220 /* The max alignment of output sections in [gp-2K, gp+2K) range. */
221 bfd_vma max_alignment_for_gp
;
223 /* Used by local STT_GNU_IFUNC symbols. */
224 htab_t loc_hash_table
;
225 void * loc_hash_memory
;
227 /* The index of the last unused .rel.iplt slot. */
228 bfd_vma last_iplt_index
;
230 /* The data segment phase, don't relax the section
231 when it is exp_seg_relro_adjust. */
232 int *data_segment_phase
;
234 /* Relocations for variant CC symbols may be present. */
238 /* Instruction access functions. */
239 #define riscv_get_insn(bits, ptr) \
240 ((bits) == 16 ? bfd_getl16 (ptr) \
241 : (bits) == 32 ? bfd_getl32 (ptr) \
242 : (bits) == 64 ? bfd_getl64 (ptr) \
243 : (abort (), (bfd_vma) - 1))
244 #define riscv_put_insn(bits, val, ptr) \
245 ((bits) == 16 ? bfd_putl16 (val, ptr) \
246 : (bits) == 32 ? bfd_putl32 (val, ptr) \
247 : (bits) == 64 ? bfd_putl64 (val, ptr) \
248 : (abort (), (void) 0))
250 /* Get the RISC-V ELF linker hash table from a link_info structure. */
251 #define riscv_elf_hash_table(p) \
252 ((is_elf_hash_table ((p)->hash) \
253 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
254 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
257 riscv_elfNN_set_options (struct bfd_link_info
*link_info
,
258 struct riscv_elf_params
*params
)
260 riscv_elf_hash_table (link_info
)->params
= params
;
264 riscv_info_to_howto_rela (bfd
*abfd
,
266 Elf_Internal_Rela
*dst
)
268 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
269 return cache_ptr
->howto
!= NULL
;
273 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
275 const struct elf_backend_data
*bed
;
278 bed
= get_elf_backend_data (abfd
);
279 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
280 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
283 /* Return true if a relocation is modifying an instruction. */
286 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
288 /* Heuristic: A multibyte destination with a nontrivial mask
290 return (howto
->bitsize
> 8
291 && howto
->dst_mask
!= 0
292 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
293 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
297 #define PLT_HEADER_INSNS 8
298 #define PLT_ENTRY_INSNS 4
299 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
300 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
301 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
302 #define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
303 #define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
304 #define TLSDESC_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
305 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
306 the other is used for link map. Other targets also reserve one more
307 entry used for runtime profile? */
308 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
310 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
313 # define MATCH_LREG MATCH_LW
315 # define MATCH_LREG MATCH_LD
318 /* Generate a PLT header. */
321 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
324 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
325 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
327 /* RVE has no t3 register, so this won't work, and is not supported. */
328 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
330 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
335 /* auipc t2, %hi(.got.plt)
336 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
337 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
338 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
339 addi t0, t2, %lo(.got.plt) # &.got.plt
340 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
341 l[w|d] t0, PTRSIZE(t0) # link map
344 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
345 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
346 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
347 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
348 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
349 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
350 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
351 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
356 /* Generate a PLT entry. */
359 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
362 /* RVE has no t3 register, so this won't work, and is not supported. */
363 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
365 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
370 /* auipc t3, %hi(.got.plt entry)
371 l[w|d] t3, %lo(.got.plt entry)(t3)
375 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
376 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
377 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
378 entry
[3] = RISCV_NOP
;
383 /* Create an entry in an RISC-V ELF linker hash table. */
385 static struct bfd_hash_entry
*
386 link_hash_newfunc (struct bfd_hash_entry
*entry
,
387 struct bfd_hash_table
*table
, const char *string
)
389 /* Allocate the structure if it has not already been allocated by a
394 bfd_hash_allocate (table
,
395 sizeof (struct riscv_elf_link_hash_entry
));
400 /* Call the allocation method of the superclass. */
401 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
404 struct riscv_elf_link_hash_entry
*eh
;
406 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
407 eh
->tls_type
= GOT_UNKNOWN
;
413 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
414 for local symbol so that we can handle local STT_GNU_IFUNC symbols
415 as global symbol. We reuse indx and dynstr_index for local symbol
416 hash since they aren't used by global symbols in this backend. */
419 riscv_elf_local_htab_hash (const void *ptr
)
421 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
422 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
425 /* Compare local hash entries. */
428 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
430 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
431 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
433 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
436 /* Find and/or create a hash entry for local symbol. */
438 static struct elf_link_hash_entry
*
439 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
440 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
443 struct riscv_elf_link_hash_entry eh
, *ret
;
444 asection
*sec
= abfd
->sections
;
445 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
446 ELFNN_R_SYM (rel
->r_info
));
449 eh
.elf
.indx
= sec
->id
;
450 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
451 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
452 create
? INSERT
: NO_INSERT
);
459 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
463 ret
= (struct riscv_elf_link_hash_entry
*)
464 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
465 sizeof (struct riscv_elf_link_hash_entry
));
468 memset (ret
, 0, sizeof (*ret
));
469 ret
->elf
.indx
= sec
->id
;
470 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
471 ret
->elf
.dynindx
= -1;
477 /* Destroy a RISC-V elf linker hash table. */
480 riscv_elf_link_hash_table_free (bfd
*obfd
)
482 struct riscv_elf_link_hash_table
*ret
483 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
485 if (ret
->loc_hash_table
)
486 htab_delete (ret
->loc_hash_table
);
487 if (ret
->loc_hash_memory
)
488 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
490 _bfd_elf_link_hash_table_free (obfd
);
493 /* Create a RISC-V ELF linker hash table. */
495 static struct bfd_link_hash_table
*
496 riscv_elf_link_hash_table_create (bfd
*abfd
)
498 struct riscv_elf_link_hash_table
*ret
;
499 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
501 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
505 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
506 sizeof (struct riscv_elf_link_hash_entry
),
513 ret
->max_alignment
= (bfd_vma
) -1;
514 ret
->max_alignment_for_gp
= (bfd_vma
) -1;
516 /* Create hash table for local ifunc. */
517 ret
->loc_hash_table
= htab_try_create (1024,
518 riscv_elf_local_htab_hash
,
519 riscv_elf_local_htab_eq
,
521 ret
->loc_hash_memory
= objalloc_create ();
522 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
524 riscv_elf_link_hash_table_free (abfd
);
527 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
529 return &ret
->elf
.root
;
532 /* Create the .got section. */
535 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
539 struct elf_link_hash_entry
*h
;
540 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
541 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
543 /* This function may be called more than once. */
544 if (htab
->sgot
!= NULL
)
547 flags
= bed
->dynamic_sec_flags
;
549 s
= bfd_make_section_anyway_with_flags (abfd
,
550 (bed
->rela_plts_and_copies_p
551 ? ".rela.got" : ".rel.got"),
552 (bed
->dynamic_sec_flags
555 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
559 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
561 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
565 /* The first bit of the global offset table is the header. */
566 s
->size
+= bed
->got_header_size
;
568 if (bed
->want_got_plt
)
570 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
572 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
576 /* Reserve room for the header. */
577 s
->size
+= GOTPLT_HEADER_SIZE
;
580 if (bed
->want_got_sym
)
582 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
583 section. We don't do this in the linker script because we don't want
584 to define the symbol if we are not creating a global offset
586 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
587 "_GLOBAL_OFFSET_TABLE_");
588 elf_hash_table (info
)->hgot
= h
;
596 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
597 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
601 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
602 struct bfd_link_info
*info
)
604 struct riscv_elf_link_hash_table
*htab
;
606 htab
= riscv_elf_hash_table (info
);
607 BFD_ASSERT (htab
!= NULL
);
609 if (!riscv_elf_create_got_section (dynobj
, info
))
612 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
615 if (!bfd_link_pic (info
))
617 /* Technically, this section doesn't have contents. It is used as the
618 target of TLS copy relocs, to copy TLS data from shared libraries into
619 the executable. However, if we don't mark it as loadable, then it
620 matches the IS_TBSS test in ldlang.c, and there is no run-time address
621 space allocated for it even though it has SEC_ALLOC. That test is
622 correct for .tbss, but not correct for this section. There is also
623 a second problem that having a section with no contents can only work
624 if it comes after all sections with contents in the same segment,
625 but the linker script does not guarantee that. This is just mixed in
626 with other .tdata.* sections. We can fix both problems by lying and
627 saying that there are contents. This section is expected to be small
628 so this should not cause a significant extra program startup cost. */
630 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
631 (SEC_ALLOC
| SEC_THREAD_LOCAL
632 | SEC_LOAD
| SEC_DATA
634 | SEC_LINKER_CREATED
));
637 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
638 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
644 /* Copy the extra info we tack onto an elf_link_hash_entry. */
647 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
648 struct elf_link_hash_entry
*dir
,
649 struct elf_link_hash_entry
*ind
)
651 struct riscv_elf_link_hash_entry
*edir
, *eind
;
653 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
654 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
656 if (ind
->root
.type
== bfd_link_hash_indirect
657 && dir
->got
.refcount
<= 0)
659 edir
->tls_type
= eind
->tls_type
;
660 eind
->tls_type
= GOT_UNKNOWN
;
662 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
666 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
667 unsigned long symndx
, char tls_type
)
669 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
671 *new_tls_type
|= tls_type
;
672 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
674 (*_bfd_error_handler
)
675 (_("%pB: `%s' accessed both as normal and thread local symbol"),
676 abfd
, h
? h
->root
.root
.string
: "<local>");
683 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
684 struct elf_link_hash_entry
*h
, long symndx
)
686 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
687 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
689 if (htab
->elf
.sgot
== NULL
)
691 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
697 h
->got
.refcount
+= 1;
701 /* This is a global offset table entry for a local symbol. */
702 if (elf_local_got_refcounts (abfd
) == NULL
)
704 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
705 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
707 _bfd_riscv_elf_local_got_tls_type (abfd
)
708 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
710 elf_local_got_refcounts (abfd
) [symndx
] += 1;
716 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
718 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
720 /* We propably can improve the information to tell users that they
721 should be recompile the code with -fPIC or -fPIE, just like what
723 (*_bfd_error_handler
)
724 (_("%pB: relocation %s against `%s' can not be used when making a shared "
725 "object; recompile with -fPIC"),
726 abfd
, r
? r
->name
: _("<unknown>"),
727 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
728 bfd_set_error (bfd_error_bad_value
);
732 /* Look through the relocs for a section during the first phase, and
733 allocate space in the global offset table or procedure linkage
737 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
738 asection
*sec
, const Elf_Internal_Rela
*relocs
)
740 struct riscv_elf_link_hash_table
*htab
;
741 Elf_Internal_Shdr
*symtab_hdr
;
742 struct elf_link_hash_entry
**sym_hashes
;
743 const Elf_Internal_Rela
*rel
;
744 asection
*sreloc
= NULL
;
746 if (bfd_link_relocatable (info
))
749 htab
= riscv_elf_hash_table (info
);
750 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
751 sym_hashes
= elf_sym_hashes (abfd
);
753 if (htab
->elf
.dynobj
== NULL
)
754 htab
->elf
.dynobj
= abfd
;
756 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
759 unsigned int r_symndx
;
760 struct elf_link_hash_entry
*h
;
761 bool is_abs_symbol
= false;
763 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
764 r_type
= ELFNN_R_TYPE (rel
->r_info
);
766 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
768 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
773 if (r_symndx
< symtab_hdr
->sh_info
)
775 /* A local symbol. */
776 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
781 is_abs_symbol
= isym
->st_shndx
== SHN_ABS
? true : false;
783 /* Check relocation against local STT_GNU_IFUNC symbol. */
784 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
786 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
790 /* Fake STT_GNU_IFUNC global symbol. */
791 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
793 h
->type
= STT_GNU_IFUNC
;
797 h
->root
.type
= bfd_link_hash_defined
;
804 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
805 while (h
->root
.type
== bfd_link_hash_indirect
806 || h
->root
.type
== bfd_link_hash_warning
)
807 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
809 is_abs_symbol
= bfd_is_abs_symbol (&h
->root
) ? true : false;
819 case R_RISCV_CALL_PLT
:
821 case R_RISCV_GOT_HI20
:
822 case R_RISCV_PCREL_HI20
:
823 /* Create the ifunc sections, iplt and ipltgot, for static
825 if (h
->type
== STT_GNU_IFUNC
826 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
834 /* It is referenced by a non-shared object. */
840 case R_RISCV_TLS_GD_HI20
:
841 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
842 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
846 case R_RISCV_TLS_GOT_HI20
:
847 if (bfd_link_dll (info
))
848 info
->flags
|= DF_STATIC_TLS
;
849 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
850 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
854 case R_RISCV_GOT_HI20
:
855 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
856 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
860 case R_RISCV_TLSDESC_HI20
:
861 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
862 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLSDESC
))
867 case R_RISCV_CALL_PLT
:
868 /* These symbol requires a procedure linkage table entry.
869 We actually build the entry in adjust_dynamic_symbol,
870 because these might be a case of linking PIC code without
871 linking in any dynamic objects, in which case we don't
872 need to generate a procedure linkage table after all. */
874 /* If it is a local symbol, then we resolve it directly
875 without creating a PLT entry. */
880 h
->plt
.refcount
+= 1;
883 case R_RISCV_PCREL_HI20
:
885 && h
->type
== STT_GNU_IFUNC
)
888 h
->pointer_equality_needed
= 1;
890 /* We don't use the PCREL_HI20 in the data section,
891 so we always need the plt when it refers to
893 h
->plt
.refcount
+= 1;
896 /* The non-preemptible absolute symbol shouldn't be referneced with
897 pc-relative relocation when generating shared object. However,
898 PCREL_HI20/LO12 relocs are always bind locally when generating
899 shared object, so all absolute symbol referenced need to be
900 disallowed, except they are defined in linker script.
902 Maybe we should add this check for all pc-relative relocations,
903 please see pr28789 and pr25749 for details. */
904 if (bfd_link_pic (info
)
905 /* (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)) */
908 if (h
!= NULL
&& (h
)->root
.ldscript_def
)
909 /* Disallow the absolute symbol defined in linker script here
910 will cause the glibc-linux toolchain build failed, so regard
911 them as pc-relative symbols, just like what x86 did. */
916 if (h
->root
.root
.string
)
917 name
= h
->root
.root
.string
;
920 Elf_Internal_Sym
*sym
;
921 sym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
, abfd
,
923 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, sym
, NULL
);
926 reloc_howto_type
*r_t
=
927 riscv_elf_rtype_to_howto (abfd
, r_type
);
929 (_("%pB: relocation %s against absolute symbol `%s' can "
930 "not be used when making a shared object"),
931 abfd
, r_t
? r_t
->name
: _("<unknown>"), name
);
932 bfd_set_error (bfd_error_bad_value
);
940 case R_RISCV_RVC_BRANCH
:
941 case R_RISCV_RVC_JUMP
:
942 /* In shared libraries and pie, these relocs are known
944 if (bfd_link_pic (info
))
948 case R_RISCV_TPREL_HI20
:
949 /* This is not allowed in the pic, but okay in pie. */
950 if (!bfd_link_executable (info
))
951 return bad_static_reloc (abfd
, r_type
, h
);
953 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
957 if (bfd_link_pic (info
))
958 return bad_static_reloc (abfd
, r_type
, h
);
963 && bfd_link_pic (info
)
964 && (sec
->flags
& SEC_ALLOC
) != 0)
969 reloc_howto_type
*r_t
= riscv_elf_rtype_to_howto (abfd
, r_type
);
971 (_("%pB: relocation %s against non-absolute symbol `%s' can "
972 "not be used in RVNN when making a shared object"),
973 abfd
, r_t
? r_t
->name
: _("<unknown>"),
974 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
975 bfd_set_error (bfd_error_bad_value
);
981 case R_RISCV_JUMP_SLOT
:
982 case R_RISCV_RELATIVE
:
989 && (!bfd_link_pic (info
)
990 || h
->type
== STT_GNU_IFUNC
))
992 /* This reloc might not bind locally. */
994 h
->pointer_equality_needed
= 1;
997 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
999 /* We may need a .plt entry if the symbol is a function
1000 defined in a shared lib or is a function referenced
1001 from the code or read-only section. */
1002 h
->plt
.refcount
+= 1;
1006 reloc_howto_type
*r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
1007 if (RISCV_NEED_DYNAMIC_RELOC (r
->pc_relative
, info
, h
, sec
))
1009 struct elf_dyn_relocs
*p
;
1010 struct elf_dyn_relocs
**head
;
1012 /* When creating a shared object, we must copy these
1013 relocs into the output file. We create a reloc
1014 section in dynobj and make room for the reloc. */
1017 sreloc
= _bfd_elf_make_dynamic_reloc_section
1018 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
1019 abfd
, /*rela?*/ true);
1025 /* If this is a global symbol, we count the number of
1026 relocations we need for this symbol. */
1028 head
= &h
->dyn_relocs
;
1031 /* Track dynamic relocs needed for local syms too.
1032 We really need local syms available to do this
1037 Elf_Internal_Sym
*isym
;
1039 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
1044 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1048 vpp
= &elf_section_data (s
)->local_dynrel
;
1049 head
= (struct elf_dyn_relocs
**) vpp
;
1053 if (p
== NULL
|| p
->sec
!= sec
)
1055 size_t amt
= sizeof *p
;
1056 p
= ((struct elf_dyn_relocs
*)
1057 bfd_alloc (htab
->elf
.dynobj
, amt
));
1068 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
1081 /* Adjust a symbol defined by a dynamic object and referenced by a
1082 regular object. The current definition is in some section of the
1083 dynamic object, but we're not including those sections. We have to
1084 change the definition to something the rest of the link can
1088 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1089 struct elf_link_hash_entry
*h
)
1091 struct riscv_elf_link_hash_table
*htab
;
1092 struct riscv_elf_link_hash_entry
* eh
;
1096 htab
= riscv_elf_hash_table (info
);
1097 BFD_ASSERT (htab
!= NULL
);
1099 dynobj
= htab
->elf
.dynobj
;
1101 /* Make sure we know what is going on here. */
1102 BFD_ASSERT (dynobj
!= NULL
1104 || h
->type
== STT_GNU_IFUNC
1108 && !h
->def_regular
)));
1110 /* If this is a function, put it in the procedure linkage table. We
1111 will fill in the contents of the procedure linkage table later
1112 (although we could actually do it here). */
1113 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1115 if (h
->plt
.refcount
<= 0
1116 || (h
->type
!= STT_GNU_IFUNC
1117 && (SYMBOL_CALLS_LOCAL (info
, h
)
1118 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1119 && h
->root
.type
== bfd_link_hash_undefweak
))))
1121 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1122 input file, but the symbol was never referred to by a dynamic
1123 object, or if all references were garbage collected. In such
1124 a case, we don't actually need to build a PLT entry. */
1125 h
->plt
.offset
= (bfd_vma
) -1;
1132 h
->plt
.offset
= (bfd_vma
) -1;
1134 /* If this is a weak symbol, and there is a real definition, the
1135 processor independent code will have arranged for us to see the
1136 real definition first, and we can just use the same value. */
1137 if (h
->is_weakalias
)
1139 struct elf_link_hash_entry
*def
= weakdef (h
);
1140 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1141 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1142 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1146 /* This is a reference to a symbol defined by a dynamic object which
1147 is not a function. */
1149 /* If we are creating a shared library, we must presume that the
1150 only references to the symbol are via the global offset table.
1151 For such cases we need not do anything here; the relocations will
1152 be handled correctly by relocate_section. */
1153 if (bfd_link_pic (info
))
1156 /* If there are no references to this symbol that do not use the
1157 GOT, we don't need to generate a copy reloc. */
1158 if (!h
->non_got_ref
)
1161 /* If -z nocopyreloc was given, we won't generate them either. */
1162 if (info
->nocopyreloc
)
1168 /* If we don't find any dynamic relocs in read-only sections, then
1169 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1170 if (!_bfd_elf_readonly_dynrelocs (h
))
1176 /* We must allocate the symbol in our .dynbss section, which will
1177 become part of the .bss section of the executable. There will be
1178 an entry for this symbol in the .dynsym section. The dynamic
1179 object will contain position independent code, so all references
1180 from the dynamic object to this symbol will go through the global
1181 offset table. The dynamic linker will use the .dynsym entry to
1182 determine the address it must put in the global offset table, so
1183 both the dynamic object and the regular object will refer to the
1184 same memory location for the variable. */
1186 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1187 to copy the initial value out of the dynamic object and into the
1188 runtime process image. We need to remember the offset into the
1189 .rel.bss section we are going to use. */
1190 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1191 if (eh
->tls_type
& ~GOT_NORMAL
)
1193 s
= htab
->sdyntdata
;
1194 srel
= htab
->elf
.srelbss
;
1196 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1198 s
= htab
->elf
.sdynrelro
;
1199 srel
= htab
->elf
.sreldynrelro
;
1203 s
= htab
->elf
.sdynbss
;
1204 srel
= htab
->elf
.srelbss
;
1206 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1208 srel
->size
+= sizeof (ElfNN_External_Rela
);
1212 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1215 /* Allocate space in .plt, .got and associated reloc sections for
1219 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1221 struct bfd_link_info
*info
;
1222 struct riscv_elf_link_hash_table
*htab
;
1223 struct elf_dyn_relocs
*p
;
1225 if (h
->root
.type
== bfd_link_hash_indirect
)
1228 info
= (struct bfd_link_info
*) inf
;
1229 htab
= riscv_elf_hash_table (info
);
1230 BFD_ASSERT (htab
!= NULL
);
1232 /* When we are generating pde, make sure gp symbol is output as a
1233 dynamic symbol. Then ld.so can set the gp register earlier, before
1234 resolving the ifunc. */
1235 if (!bfd_link_pic (info
)
1236 && htab
->elf
.dynamic_sections_created
1237 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1238 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1241 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1242 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1243 if they are defined and referenced in a non-shared object. */
1244 if (h
->type
== STT_GNU_IFUNC
1247 else if (htab
->elf
.dynamic_sections_created
1248 && h
->plt
.refcount
> 0)
1250 /* Make sure this symbol is output as a dynamic symbol.
1251 Undefined weak syms won't yet be marked as dynamic. */
1252 if (h
->dynindx
== -1
1253 && !h
->forced_local
)
1255 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1259 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1261 asection
*s
= htab
->elf
.splt
;
1264 s
->size
= PLT_HEADER_SIZE
;
1266 h
->plt
.offset
= s
->size
;
1268 /* Make room for this entry. */
1269 s
->size
+= PLT_ENTRY_SIZE
;
1271 /* We also need to make an entry in the .got.plt section. */
1272 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1274 /* We also need to make an entry in the .rela.plt section. */
1275 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1277 /* If this symbol is not defined in a regular file, and we are
1278 not generating a shared library, then set the symbol to this
1279 location in the .plt. This is required to make function
1280 pointers compare as equal between the normal executable and
1281 the shared library. */
1282 if (! bfd_link_pic (info
)
1285 h
->root
.u
.def
.section
= s
;
1286 h
->root
.u
.def
.value
= h
->plt
.offset
;
1289 /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1290 variant_cc flag of riscv_elf_link_hash_table. */
1291 if (h
->other
& STO_RISCV_VARIANT_CC
)
1292 htab
->variant_cc
= 1;
1296 h
->plt
.offset
= (bfd_vma
) -1;
1302 h
->plt
.offset
= (bfd_vma
) -1;
1306 if (h
->got
.refcount
> 0)
1310 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1312 /* Make sure this symbol is output as a dynamic symbol.
1313 Undefined weak syms won't yet be marked as dynamic. */
1314 if (h
->dynindx
== -1
1315 && !h
->forced_local
)
1317 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1322 h
->got
.offset
= s
->size
;
1323 dyn
= htab
->elf
.dynamic_sections_created
;
1324 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
| GOT_TLSDESC
))
1327 bool need_reloc
= false;
1328 RISCV_TLS_GD_IE_NEED_DYN_RELOC(info
, dyn
, h
, indx
, need_reloc
);
1330 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1331 if (tls_type
& GOT_TLS_GD
)
1333 s
->size
+= TLS_GD_GOT_ENTRY_SIZE
;
1335 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1338 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1339 if (tls_type
& GOT_TLS_IE
)
1341 s
->size
+= TLS_IE_GOT_ENTRY_SIZE
;
1343 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1346 /* TLSDESC needs one dynamic reloc and two GOT slots. */
1347 if (tls_type
& GOT_TLSDESC
)
1349 s
->size
+= TLSDESC_GOT_ENTRY_SIZE
;
1350 /* TLSDESC always use dynamic relocs. */
1351 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1356 s
->size
+= GOT_ENTRY_SIZE
;
1357 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1358 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1359 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1363 h
->got
.offset
= (bfd_vma
) -1;
1365 if (h
->dyn_relocs
== NULL
)
1368 /* In the shared -Bsymbolic case, discard space allocated for
1369 dynamic pc-relative relocs against symbols which turn out to be
1370 defined in regular objects. For the normal shared case, discard
1371 space for pc-relative relocs that have become local due to symbol
1372 visibility changes. */
1374 if (bfd_link_pic (info
))
1376 if (SYMBOL_CALLS_LOCAL (info
, h
))
1378 struct elf_dyn_relocs
**pp
;
1380 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1382 p
->count
-= p
->pc_count
;
1391 /* Also discard relocs on undefined weak syms with non-default
1393 if (h
->dyn_relocs
!= NULL
1394 && h
->root
.type
== bfd_link_hash_undefweak
)
1396 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1397 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1398 h
->dyn_relocs
= NULL
;
1400 /* Make sure undefined weak symbols are output as a dynamic
1402 else if (h
->dynindx
== -1
1403 && !h
->forced_local
)
1405 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1412 /* For the non-shared case, discard space for relocs against
1413 symbols which turn out to need copy relocs or are not
1419 || (htab
->elf
.dynamic_sections_created
1420 && (h
->root
.type
== bfd_link_hash_undefweak
1421 || h
->root
.type
== bfd_link_hash_undefined
))))
1423 /* Make sure this symbol is output as a dynamic symbol.
1424 Undefined weak syms won't yet be marked as dynamic. */
1425 if (h
->dynindx
== -1
1426 && !h
->forced_local
)
1428 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1432 /* If that succeeded, we know we'll be keeping all the
1434 if (h
->dynindx
!= -1)
1438 h
->dyn_relocs
= NULL
;
1443 /* Finally, allocate space. */
1444 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1446 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1447 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1453 /* Allocate space in .plt, .got and associated reloc sections for
1454 ifunc dynamic relocs. */
1457 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1460 struct bfd_link_info
*info
;
1462 if (h
->root
.type
== bfd_link_hash_indirect
)
1465 if (h
->root
.type
== bfd_link_hash_warning
)
1466 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1468 info
= (struct bfd_link_info
*) inf
;
1470 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1471 here if it is defined and referenced in a non-shared object. */
1472 if (h
->type
== STT_GNU_IFUNC
1474 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1483 /* Allocate space in .plt, .got and associated reloc sections for
1484 local ifunc dynamic relocs. */
1487 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1489 struct elf_link_hash_entry
*h
1490 = (struct elf_link_hash_entry
*) *slot
;
1492 if (h
->type
!= STT_GNU_IFUNC
1496 || h
->root
.type
!= bfd_link_hash_defined
)
1499 return allocate_ifunc_dynrelocs (h
, inf
);
1503 riscv_elf_late_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1505 struct riscv_elf_link_hash_table
*htab
;
1510 htab
= riscv_elf_hash_table (info
);
1511 BFD_ASSERT (htab
!= NULL
);
1512 dynobj
= htab
->elf
.dynobj
;
1516 if (elf_hash_table (info
)->dynamic_sections_created
)
1518 /* Set the contents of the .interp section to the interpreter. */
1519 if (bfd_link_executable (info
) && !info
->nointerp
)
1521 s
= bfd_get_linker_section (dynobj
, ".interp");
1522 BFD_ASSERT (s
!= NULL
);
1523 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1524 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1528 /* Set up .got offsets for local syms, and space for local dynamic
1530 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1532 bfd_signed_vma
*local_got
;
1533 bfd_signed_vma
*end_local_got
;
1534 char *local_tls_type
;
1535 bfd_size_type locsymcount
;
1536 Elf_Internal_Shdr
*symtab_hdr
;
1539 if (! is_riscv_elf (ibfd
))
1542 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1544 struct elf_dyn_relocs
*p
;
1546 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1548 if (!bfd_is_abs_section (p
->sec
)
1549 && bfd_is_abs_section (p
->sec
->output_section
))
1551 /* Input section has been discarded, either because
1552 it is a copy of a linkonce section or due to
1553 linker script /DISCARD/, so we'll be discarding
1556 else if (p
->count
!= 0)
1558 srel
= elf_section_data (p
->sec
)->sreloc
;
1559 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1560 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1561 info
->flags
|= DF_TEXTREL
;
1566 local_got
= elf_local_got_refcounts (ibfd
);
1570 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1571 locsymcount
= symtab_hdr
->sh_info
;
1572 end_local_got
= local_got
+ locsymcount
;
1573 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1575 srel
= htab
->elf
.srelgot
;
1576 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1580 *local_got
= s
->size
;
1581 if (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
| GOT_TLSDESC
))
1583 if (*local_tls_type
& GOT_TLS_GD
)
1585 s
->size
+= TLS_GD_GOT_ENTRY_SIZE
;
1586 if (bfd_link_dll (info
))
1587 srel
->size
+= sizeof (ElfNN_External_Rela
);
1589 if (*local_tls_type
& GOT_TLS_IE
)
1591 s
->size
+= TLS_IE_GOT_ENTRY_SIZE
;
1592 if (bfd_link_dll (info
))
1593 srel
->size
+= sizeof (ElfNN_External_Rela
);
1595 if (*local_tls_type
& GOT_TLSDESC
)
1597 s
->size
+= TLSDESC_GOT_ENTRY_SIZE
;
1598 srel
->size
+= sizeof (ElfNN_External_Rela
);
1603 s
->size
+= GOT_ENTRY_SIZE
;
1604 if (bfd_link_pic (info
))
1605 srel
->size
+= sizeof (ElfNN_External_Rela
);
1609 *local_got
= (bfd_vma
) -1;
1613 /* Allocate .plt and .got entries and space dynamic relocs for
1615 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1617 /* Allocate .plt and .got entries and space dynamic relocs for
1618 global ifunc symbols. */
1619 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1621 /* Allocate .plt and .got entries and space dynamic relocs for
1622 local ifunc symbols. */
1623 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1625 /* Used to resolve the dynamic relocs overwite problems when
1626 generating static executable. */
1627 if (htab
->elf
.irelplt
)
1628 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1630 if (htab
->elf
.sgotplt
)
1632 struct elf_link_hash_entry
*got
;
1633 got
= elf_link_hash_lookup (elf_hash_table (info
),
1634 "_GLOBAL_OFFSET_TABLE_",
1635 false, false, false);
1637 /* Don't allocate .got.plt section if there are no GOT nor PLT
1638 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1640 || !got
->ref_regular_nonweak
)
1641 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1642 && (htab
->elf
.splt
== NULL
1643 || htab
->elf
.splt
->size
== 0)
1644 && (htab
->elf
.sgot
== NULL
1645 || (htab
->elf
.sgot
->size
1646 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1647 htab
->elf
.sgotplt
->size
= 0;
1650 /* The check_relocs and adjust_dynamic_symbol entry points have
1651 determined the sizes of the various dynamic sections. Allocate
1653 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1655 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1658 if (s
== htab
->elf
.splt
1659 || s
== htab
->elf
.sgot
1660 || s
== htab
->elf
.sgotplt
1661 || s
== htab
->elf
.iplt
1662 || s
== htab
->elf
.igotplt
1663 || s
== htab
->elf
.sdynbss
1664 || s
== htab
->elf
.sdynrelro
1665 || s
== htab
->sdyntdata
)
1667 /* Strip this section if we don't need it; see the
1670 else if (startswith (s
->name
, ".rela"))
1674 /* We use the reloc_count field as a counter if we need
1675 to copy relocs into the output file. */
1681 /* It's not one of our sections. */
1687 /* If we don't need this section, strip it from the
1688 output file. This is mostly to handle .rela.bss and
1689 .rela.plt. We must create both sections in
1690 create_dynamic_sections, because they must be created
1691 before the linker maps input sections to output
1692 sections. The linker does that before
1693 adjust_dynamic_symbol is called, and it is that
1694 function which decides whether anything needs to go
1695 into these sections. */
1696 s
->flags
|= SEC_EXCLUDE
;
1700 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1703 /* Allocate memory for the section contents. Zero the memory
1704 for the benefit of .rela.plt, which has 4 unused entries
1705 at the beginning, and we don't want garbage. */
1706 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1707 if (s
->contents
== NULL
)
1711 /* Add dynamic entries. */
1712 if (elf_hash_table (info
)->dynamic_sections_created
)
1714 if (!_bfd_elf_add_dynamic_tags (output_bfd
, info
, true))
1717 if (htab
->variant_cc
1718 && !_bfd_elf_add_dynamic_entry (info
, DT_RISCV_VARIANT_CC
, 0))
1726 #define DTP_OFFSET 0x800
1728 /* Return the relocation value for a TLS dtp-relative reloc. */
1731 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1733 /* If tls_sec is NULL, we should have signalled an error already. */
1734 if (elf_hash_table (info
)->tls_sec
== NULL
)
1736 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1739 /* Return the relocation value for a static TLS tp-relative relocation. */
1742 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1744 /* If tls_sec is NULL, we should have signalled an error already. */
1745 if (elf_hash_table (info
)->tls_sec
== NULL
)
1747 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1750 /* Return the relocation value for a static TLSDESC relocation. */
1753 tlsdescoff (struct bfd_link_info
*info
, bfd_vma address
)
1755 /* If tls_sec is NULL, we should have signalled an error already. */
1756 if (elf_hash_table (info
)->tls_sec
== NULL
)
1758 return address
- elf_hash_table (info
)->tls_sec
->vma
;
1761 /* Return the global pointer's value, or 0 if it is not in use. */
1764 riscv_global_pointer_value (struct bfd_link_info
*info
)
1766 struct bfd_link_hash_entry
*h
;
1768 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1769 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1772 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1775 /* Emplace a static relocation. */
1777 static bfd_reloc_status_type
1778 perform_relocation (const reloc_howto_type
*howto
,
1779 const Elf_Internal_Rela
*rel
,
1781 asection
*input_section
,
1785 if (howto
->pc_relative
)
1786 value
-= sec_addr (input_section
) + rel
->r_offset
;
1788 /* PR31179, ignore the non-zero addend of R_RISCV_SUB_ULEB128. */
1789 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_SUB_ULEB128
)
1790 value
+= rel
->r_addend
;
1792 switch (ELFNN_R_TYPE (rel
->r_info
))
1795 case R_RISCV_TPREL_HI20
:
1796 case R_RISCV_PCREL_HI20
:
1797 case R_RISCV_GOT_HI20
:
1798 case R_RISCV_TLS_GOT_HI20
:
1799 case R_RISCV_TLS_GD_HI20
:
1800 case R_RISCV_TLSDESC_HI20
:
1801 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1802 return bfd_reloc_overflow
;
1803 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1806 case R_RISCV_LO12_I
:
1807 case R_RISCV_GPREL_I
:
1808 case R_RISCV_TPREL_LO12_I
:
1809 case R_RISCV_TPREL_I
:
1810 case R_RISCV_PCREL_LO12_I
:
1811 case R_RISCV_TLSDESC_LOAD_LO12
:
1812 case R_RISCV_TLSDESC_ADD_LO12
:
1813 value
= ENCODE_ITYPE_IMM (value
);
1816 case R_RISCV_LO12_S
:
1817 case R_RISCV_GPREL_S
:
1818 case R_RISCV_TPREL_LO12_S
:
1819 case R_RISCV_TPREL_S
:
1820 case R_RISCV_PCREL_LO12_S
:
1821 value
= ENCODE_STYPE_IMM (value
);
1825 case R_RISCV_CALL_PLT
:
1826 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1827 return bfd_reloc_overflow
;
1828 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1829 | (ENCODE_ITYPE_IMM (value
) << 32);
1833 if (!VALID_JTYPE_IMM (value
))
1834 return bfd_reloc_overflow
;
1835 value
= ENCODE_JTYPE_IMM (value
);
1838 case R_RISCV_BRANCH
:
1839 if (!VALID_BTYPE_IMM (value
))
1840 return bfd_reloc_overflow
;
1841 value
= ENCODE_BTYPE_IMM (value
);
1844 case R_RISCV_RVC_BRANCH
:
1845 if (!VALID_CBTYPE_IMM (value
))
1846 return bfd_reloc_overflow
;
1847 value
= ENCODE_CBTYPE_IMM (value
);
1850 case R_RISCV_RVC_JUMP
:
1851 if (!VALID_CJTYPE_IMM (value
))
1852 return bfd_reloc_overflow
;
1853 value
= ENCODE_CJTYPE_IMM (value
);
1856 case R_RISCV_RVC_LUI
:
1857 if (RISCV_CONST_HIGH_PART (value
) == 0)
1859 /* Linker relaxation can convert an address equal to or greater than
1860 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1861 valid immediate. We can fix this by converting it to a C.LI. */
1862 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1863 contents
+ rel
->r_offset
);
1864 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1865 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1866 value
= ENCODE_CITYPE_IMM (0);
1868 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1869 return bfd_reloc_overflow
;
1871 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1874 /* R_RISCV_SET_ULEB128 won't go into here. */
1875 case R_RISCV_SUB_ULEB128
:
1877 unsigned int len
= 0;
1878 _bfd_read_unsigned_leb128 (input_bfd
, contents
+ rel
->r_offset
, &len
);
1880 /* Clean the contents value to zero (0x80), but keep the original
1882 bfd_byte
*p
= contents
+ rel
->r_offset
;
1883 bfd_byte
*endp
= p
+ len
- 1;
1884 memset (p
, 0x80, len
- 1);
1887 /* Make sure the length of the new uleb128 value within the
1888 original (available) length. */
1889 unsigned int new_len
= 0;
1890 unsigned int val_t
= value
;
1900 (_("final size of uleb128 value at offset 0x%lx in %pA from "
1901 "%pB exceeds available space"),
1902 (long) rel
->r_offset
, input_section
, input_bfd
);
1903 return bfd_reloc_dangerous
;
1907 p
= _bfd_write_unsigned_leb128 (p
, endp
, value
);
1910 /* If the length of the value is reduced and shorter than the
1911 original uleb128 length, then _bfd_write_unsigned_leb128 may
1912 clear the 0x80 to 0x0 for the last byte that was written.
1913 So reset it to keep the the original uleb128 length. */
1917 return bfd_reloc_ok
;
1935 case R_RISCV_32_PCREL
:
1936 case R_RISCV_TLS_DTPREL32
:
1937 case R_RISCV_TLS_DTPREL64
:
1940 case R_RISCV_DELETE
:
1941 return bfd_reloc_ok
;
1944 return bfd_reloc_notsupported
;
1948 if (riscv_is_insn_reloc (howto
))
1949 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1951 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1952 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1953 if (riscv_is_insn_reloc (howto
))
1954 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1956 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1958 return bfd_reloc_ok
;
1961 /* Remember all PC-relative high-part relocs we've encountered to help us
1962 later resolve the corresponding low-part relocs. */
1968 /* Relocation value with addend. */
1970 /* Original reloc type. */
1972 } riscv_pcrel_hi_reloc
;
1974 typedef struct riscv_pcrel_lo_reloc
1976 /* PC value of auipc. */
1978 /* Internal relocation. */
1979 const Elf_Internal_Rela
*reloc
;
1980 /* Record the following information helps to resolve the %pcrel
1981 which cross different input section. For now we build a hash
1982 for pcrel at the start of riscv_elf_relocate_section, and then
1983 free the hash at the end. But riscv_elf_relocate_section only
1984 handles an input section at a time, so that means we can only
1985 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1986 section. Otherwise, we will report dangerous relocation errors
1987 for those %pcrel which are not in the same input section. */
1988 asection
*input_section
;
1989 struct bfd_link_info
*info
;
1990 reloc_howto_type
*howto
;
1992 /* The next riscv_pcrel_lo_reloc. */
1993 struct riscv_pcrel_lo_reloc
*next
;
1994 } riscv_pcrel_lo_reloc
;
1998 /* Hash table for riscv_pcrel_hi_reloc. */
2000 /* Linked list for riscv_pcrel_lo_reloc. */
2001 riscv_pcrel_lo_reloc
*lo_relocs
;
2002 } riscv_pcrel_relocs
;
2005 riscv_pcrel_reloc_hash (const void *entry
)
2007 const riscv_pcrel_hi_reloc
*e
= entry
;
2008 return (hashval_t
)(e
->address
>> 2);
2012 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
2014 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
2015 return e1
->address
== e2
->address
;
2019 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
2021 p
->lo_relocs
= NULL
;
2022 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
2023 riscv_pcrel_reloc_eq
, free
);
2024 return p
->hi_relocs
!= NULL
;
2028 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
2030 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
2034 riscv_pcrel_lo_reloc
*next
= cur
->next
;
2039 htab_delete (p
->hi_relocs
);
2043 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
2044 struct bfd_link_info
*info
,
2048 const reloc_howto_type
*howto
)
2050 /* We may need to reference low addreses in PC-relative modes even when the
2051 PC is far away from these addresses. For example, undefweak references
2052 need to produce the address 0 when linked. As 0 is far from the arbitrary
2053 addresses that we can link PC-relative programs at, the linker can't
2054 actually relocate references to those symbols. In order to allow these
2055 programs to work we simply convert the PC-relative auipc sequences to
2056 0-relative lui sequences. */
2057 if (bfd_link_pic (info
))
2060 /* If it's possible to reference the symbol using auipc we do so, as that's
2061 more in the spirit of the PC-relative relocations we're processing. */
2062 bfd_vma offset
= addr
- pc
;
2063 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
2066 /* If it's impossible to reference this with a LUI-based offset then don't
2067 bother to convert it at all so users still see the PC-relative relocation
2068 in the truncation message. */
2069 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
2072 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
2074 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
2075 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
2076 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
2081 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
2087 bfd_vma offset
= absolute
? value
: value
- addr
;
2088 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
};
2089 riscv_pcrel_hi_reloc
**slot
=
2090 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
2092 BFD_ASSERT (*slot
== NULL
);
2093 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
2101 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
2103 const Elf_Internal_Rela
*reloc
,
2104 asection
*input_section
,
2105 struct bfd_link_info
*info
,
2106 reloc_howto_type
*howto
,
2109 riscv_pcrel_lo_reloc
*entry
;
2110 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
2113 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
2114 howto
, contents
, p
->lo_relocs
};
2115 p
->lo_relocs
= entry
;
2120 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
2122 riscv_pcrel_lo_reloc
*r
;
2124 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
2126 bfd
*input_bfd
= r
->input_section
->owner
;
2128 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0};
2129 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
2130 /* There may be a risk if the %pcrel_lo with addend refers to
2131 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
2132 so the corresponding %pcrel_lo with addend looks wrong. */
2133 char *string
= NULL
;
2135 string
= _("%pcrel_lo missing matching %pcrel_hi");
2136 else if (entry
->type
== R_RISCV_GOT_HI20
2137 && r
->reloc
->r_addend
!= 0)
2138 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
2139 else if (RISCV_CONST_HIGH_PART (entry
->value
)
2140 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
2142 /* Check the overflow when adding reloc addend. */
2143 string
= bfd_asprintf (_("%%pcrel_lo overflow with an addend,"
2144 " the value of %%pcrel_hi is 0x%" PRIx64
2145 " without any addend, but may be 0x%" PRIx64
2146 " after adding the %%pcrel_lo addend"),
2147 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
2148 (int64_t) RISCV_CONST_HIGH_PART
2149 (entry
->value
+ r
->reloc
->r_addend
));
2151 string
= _("%pcrel_lo overflow with an addend");
2156 (*r
->info
->callbacks
->reloc_dangerous
)
2157 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
2161 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
2162 input_bfd
, r
->contents
);
2168 /* Relocate a RISC-V ELF section.
2170 The RELOCATE_SECTION function is called by the new ELF backend linker
2171 to handle the relocations for a section.
2173 The relocs are always passed as Rela structures.
2175 This function is responsible for adjusting the section contents as
2176 necessary, and (if generating a relocatable output file) adjusting
2177 the reloc addend as necessary.
2179 This function does not have to worry about setting the reloc
2180 address or the reloc symbol index.
2182 LOCAL_SYMS is a pointer to the swapped in local symbols.
2184 LOCAL_SECTIONS is an array giving the section in the input file
2185 corresponding to the st_shndx field of each local symbol.
2187 The global hash table entry for the global symbols can be found
2188 via elf_sym_hashes (input_bfd).
2190 When generating relocatable output, this function must handle
2191 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2192 going to be the section symbol corresponding to the output
2193 section, which means that the addend must be adjusted
2197 riscv_elf_relocate_section (bfd
*output_bfd
,
2198 struct bfd_link_info
*info
,
2200 asection
*input_section
,
2202 Elf_Internal_Rela
*relocs
,
2203 Elf_Internal_Sym
*local_syms
,
2204 asection
**local_sections
)
2206 Elf_Internal_Rela
*rel
;
2207 Elf_Internal_Rela
*relend
;
2208 riscv_pcrel_relocs pcrel_relocs
;
2210 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2211 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2212 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
2213 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
2214 bfd_vma uleb128_set_vma
= 0;
2215 Elf_Internal_Rela
*uleb128_set_rel
= NULL
;
2218 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
2221 relend
= relocs
+ input_section
->reloc_count
;
2222 for (rel
= relocs
; rel
< relend
; rel
++)
2224 unsigned long r_symndx
;
2225 struct elf_link_hash_entry
*h
;
2226 Elf_Internal_Sym
*sym
;
2229 bfd_reloc_status_type r
= bfd_reloc_ok
;
2230 const char *name
= NULL
;
2231 bfd_vma off
, ie_off
, desc_off
;
2232 bool unresolved_reloc
, is_ie
= false, is_desc
= false;
2233 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
2234 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
2235 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2236 const char *msg
= NULL
;
2237 bool resolved_to_zero
;
2242 /* This is a final link. */
2243 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2247 unresolved_reloc
= false;
2248 if (r_symndx
< symtab_hdr
->sh_info
)
2250 sym
= local_syms
+ r_symndx
;
2251 sec
= local_sections
[r_symndx
];
2252 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2254 /* Relocate against local STT_GNU_IFUNC symbol. */
2255 if (!bfd_link_relocatable (info
)
2256 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2258 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2262 /* Set STT_GNU_IFUNC symbol value. */
2263 h
->root
.u
.def
.value
= sym
->st_value
;
2264 h
->root
.u
.def
.section
= sec
;
2269 bool warned
, ignored
;
2271 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2272 r_symndx
, symtab_hdr
, sym_hashes
,
2274 unresolved_reloc
, warned
, ignored
);
2277 /* To avoid generating warning messages about truncated
2278 relocations, set the relocation's address to be the same as
2279 the start of this section. */
2280 if (input_section
->output_section
!= NULL
)
2281 relocation
= input_section
->output_section
->vma
;
2287 if (sec
!= NULL
&& discarded_section (sec
))
2288 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2289 rel
, 1, relend
, howto
, 0, contents
);
2291 if (bfd_link_relocatable (info
))
2294 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2295 it here if it is defined in a non-shared object. */
2297 && h
->type
== STT_GNU_IFUNC
2300 asection
*plt
, *base_got
;
2302 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2304 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2305 STT_GNU_IFUNC symbol as STT_FUNC. */
2306 if (elf_section_type (input_section
) == SHT_NOTE
)
2309 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2310 sections because such sections are not SEC_ALLOC and
2311 thus ld.so will not process them. */
2312 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2317 else if (h
->plt
.offset
== (bfd_vma
) -1
2318 /* The following relocation may not need the .plt entries
2319 when all references to a STT_GNU_IFUNC symbols are done
2320 via GOT or static function pointers. */
2321 && r_type
!= R_RISCV_32
2322 && r_type
!= R_RISCV_64
2323 && r_type
!= R_RISCV_HI20
2324 && r_type
!= R_RISCV_GOT_HI20
2325 && r_type
!= R_RISCV_LO12_I
2326 && r_type
!= R_RISCV_LO12_S
)
2327 goto bad_ifunc_reloc
;
2329 /* STT_GNU_IFUNC symbol must go through PLT. */
2330 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2331 relocation
= plt
->output_section
->vma
2332 + plt
->output_offset
2339 if (rel
->r_addend
!= 0)
2341 if (h
->root
.root
.string
)
2342 name
= h
->root
.root
.string
;
2344 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2347 /* xgettext:c-format */
2348 (_("%pB: relocation %s against STT_GNU_IFUNC "
2349 "symbol `%s' has non-zero addend: %" PRId64
),
2350 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2351 bfd_set_error (bfd_error_bad_value
);
2355 /* Generate dynamic relocation only when there is a non-GOT
2356 reference in a shared object or there is no PLT. */
2357 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2358 || h
->plt
.offset
== (bfd_vma
) -1)
2360 Elf_Internal_Rela outrel
;
2363 /* Need a dynamic relocation to get the real function
2365 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2369 if (outrel
.r_offset
== (bfd_vma
) -1
2370 || outrel
.r_offset
== (bfd_vma
) -2)
2373 outrel
.r_offset
+= input_section
->output_section
->vma
2374 + input_section
->output_offset
;
2376 if (h
->dynindx
== -1
2378 || bfd_link_executable (info
))
2380 info
->callbacks
->minfo
2381 (_("Local IFUNC function `%s' in %pB\n"),
2382 h
->root
.root
.string
,
2383 h
->root
.u
.def
.section
->owner
);
2385 /* This symbol is resolved locally. */
2386 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2387 outrel
.r_addend
= h
->root
.u
.def
.value
2388 + h
->root
.u
.def
.section
->output_section
->vma
2389 + h
->root
.u
.def
.section
->output_offset
;
2393 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2394 outrel
.r_addend
= 0;
2397 /* Dynamic relocations are stored in
2398 1. .rela.ifunc section in PIC object.
2399 2. .rela.got section in dynamic executable.
2400 3. .rela.iplt section in static executable. */
2401 if (bfd_link_pic (info
))
2402 sreloc
= htab
->elf
.irelifunc
;
2403 else if (htab
->elf
.splt
!= NULL
)
2404 sreloc
= htab
->elf
.srelgot
;
2406 sreloc
= htab
->elf
.irelplt
;
2408 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2410 /* If this reloc is against an external symbol, we
2411 do not want to fiddle with the addend. Otherwise,
2412 we need to include the symbol value so that it
2413 becomes an addend for the dynamic reloc. For an
2414 internal symbol, we have updated addend. */
2419 case R_RISCV_GOT_HI20
:
2420 base_got
= htab
->elf
.sgot
;
2421 off
= h
->got
.offset
;
2423 if (base_got
== NULL
)
2426 if (off
== (bfd_vma
) -1)
2430 /* We can't use h->got.offset here to save state, or
2431 even just remember the offset, as finish_dynamic_symbol
2432 would use that as offset into .got. */
2434 if (htab
->elf
.splt
!= NULL
)
2436 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2438 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2439 base_got
= htab
->elf
.sgotplt
;
2443 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2444 off
= plt_idx
* GOT_ENTRY_SIZE
;
2445 base_got
= htab
->elf
.igotplt
;
2448 if (h
->dynindx
== -1
2452 /* This references the local definition. We must
2453 initialize this entry in the global offset table.
2454 Since the offset must always be a multiple of 8,
2455 we use the least significant bit to record
2456 whether we have initialized it already.
2458 When doing a dynamic link, we create a .rela.got
2459 relocation entry to initialize the value. This
2460 is done in the finish_dynamic_symbol routine. */
2465 bfd_put_NN (output_bfd
, relocation
,
2466 base_got
->contents
+ off
);
2467 /* Note that this is harmless for the case,
2468 as -1 | 1 still is -1. */
2474 relocation
= base_got
->output_section
->vma
2475 + base_got
->output_offset
+ off
;
2477 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2480 r
= bfd_reloc_overflow
;
2484 case R_RISCV_CALL_PLT
:
2486 case R_RISCV_LO12_I
:
2487 case R_RISCV_LO12_S
:
2490 case R_RISCV_PCREL_HI20
:
2491 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2494 r
= bfd_reloc_overflow
;
2499 if (h
->root
.root
.string
)
2500 name
= h
->root
.root
.string
;
2502 /* The entry of local ifunc is fake in global hash table,
2503 we should find the name by the original local symbol. */
2504 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2507 /* xgettext:c-format */
2508 (_("%pB: relocation %s against STT_GNU_IFUNC "
2509 "symbol `%s' isn't supported"), input_bfd
,
2511 bfd_set_error (bfd_error_bad_value
);
2518 name
= h
->root
.root
.string
;
2521 name
= (bfd_elf_string_from_elf_section
2522 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2523 if (name
== NULL
|| *name
== '\0')
2524 name
= bfd_section_name (sec
);
2527 resolved_to_zero
= (h
!= NULL
2528 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2534 case R_RISCV_TPREL_ADD
:
2535 case R_RISCV_TLSDESC_CALL
:
2537 case R_RISCV_JUMP_SLOT
:
2538 case R_RISCV_RELATIVE
:
2539 /* These require nothing of us at all. */
2543 case R_RISCV_BRANCH
:
2544 case R_RISCV_RVC_BRANCH
:
2545 case R_RISCV_RVC_LUI
:
2546 case R_RISCV_LO12_I
:
2547 case R_RISCV_LO12_S
:
2552 case R_RISCV_32_PCREL
:
2553 case R_RISCV_DELETE
:
2554 /* These require no special handling beyond perform_relocation. */
2557 case R_RISCV_SET_ULEB128
:
2558 if (uleb128_set_rel
== NULL
)
2560 /* Saved for later usage. */
2561 uleb128_set_vma
= relocation
;
2562 uleb128_set_rel
= rel
;
2567 msg
= ("Mismatched R_RISCV_SET_ULEB128, it must be paired with"
2568 " and applied before R_RISCV_SUB_ULEB128");
2569 r
= bfd_reloc_dangerous
;
2573 case R_RISCV_SUB_ULEB128
:
2574 if (uleb128_set_rel
!= NULL
2575 && uleb128_set_rel
->r_offset
== rel
->r_offset
)
2577 relocation
= uleb128_set_vma
- relocation
2578 + uleb128_set_rel
->r_addend
;
2579 uleb128_set_vma
= 0;
2580 uleb128_set_rel
= NULL
;
2582 /* PR31179, the addend of SUB_ULEB128 should be zero if using
2583 .uleb128, but we make it non-zero by accident in assembler,
2584 so just ignore it in perform_relocation, and make assembler
2585 continue doing the right thing. Don't reset the addend of
2586 SUB_ULEB128 to zero here since it will break the --emit-reloc,
2587 even though the non-zero addend is unexpected.
2589 We encourage people to rebuild their stuff to get the
2590 non-zero addend of SUB_ULEB128, but that might need some
2591 times, so report warnings to inform people need to rebuild
2592 if --check-uleb128 is enabled. However, since the failed
2593 .reloc cases for ADD/SET/SUB/ULEB128 are rarely to use, it
2594 may acceptable that stop supproting them until people rebuld
2595 their stuff, maybe half-year or one year later. I believe
2596 this might be the least harmful option that we should go.
2598 Or maybe we should teach people that don't write the
2599 .reloc R_RISCV_SUB* with non-zero constant, and report
2600 warnings/errors in assembler. */
2601 if (htab
->params
->check_uleb128
2602 && rel
->r_addend
!= 0)
2603 _bfd_error_handler (_("%pB: warning: R_RISCV_SUB_ULEB128 with"
2604 " non-zero addend, please rebuild by"
2605 " binutils 2.42 or up"), input_bfd
);
2609 msg
= ("Mismatched R_RISCV_SUB_ULEB128, it must be paired with"
2610 " and applied after R_RISCV_SET_ULEB128");
2611 r
= bfd_reloc_dangerous
;
2615 case R_RISCV_GOT_HI20
:
2618 off
= h
->got
.offset
;
2619 BFD_ASSERT (off
!= (bfd_vma
) -1);
2621 if (RISCV_RESOLVED_LOCALLY (info
, h
))
2623 /* We must initialize this entry in the global offset table.
2624 Since the offset must always be a multiple of the word
2625 size, we use the least significant bit to record whether
2626 we have initialized it already.
2628 When doing a dynamic link, we create a .rela.got
2629 relocation entry to initialize the value. This
2630 is done in the finish_dynamic_symbol routine. */
2635 bfd_put_NN (output_bfd
, relocation
,
2636 htab
->elf
.sgot
->contents
+ off
);
2641 unresolved_reloc
= false;
2645 BFD_ASSERT (local_got_offsets
!= NULL
2646 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2648 off
= local_got_offsets
[r_symndx
];
2650 /* The offset must always be a multiple of the word size.
2651 So, we can use the least significant bit to record
2652 whether we have already processed this entry. */
2657 if (bfd_link_pic (info
))
2660 Elf_Internal_Rela outrel
;
2662 /* We need to generate a R_RISCV_RELATIVE reloc
2663 for the dynamic linker. */
2664 s
= htab
->elf
.srelgot
;
2665 BFD_ASSERT (s
!= NULL
);
2667 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2669 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2670 outrel
.r_addend
= relocation
;
2672 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2675 bfd_put_NN (output_bfd
, relocation
,
2676 htab
->elf
.sgot
->contents
+ off
);
2677 local_got_offsets
[r_symndx
] |= 1;
2681 if (rel
->r_addend
!= 0)
2683 msg
= _("The addend isn't allowed for R_RISCV_GOT_HI20");
2684 r
= bfd_reloc_dangerous
;
2688 /* Address of got entry. */
2689 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2690 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
,
2691 relocation
, contents
,
2693 /* Update howto if relocation is changed. */
2694 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2695 ELFNN_R_TYPE (rel
->r_info
));
2697 r
= bfd_reloc_notsupported
;
2698 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2701 r
= bfd_reloc_overflow
;
2710 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2711 contents
+ rel
->r_offset
);
2712 relocation
= old_value
+ relocation
;
2718 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2719 contents
+ rel
->r_offset
);
2720 relocation
= (old_value
& ~howto
->dst_mask
)
2721 | (((old_value
& howto
->dst_mask
) - relocation
)
2731 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2732 contents
+ rel
->r_offset
);
2733 relocation
= old_value
- relocation
;
2738 case R_RISCV_CALL_PLT
:
2739 /* Handle a call to an undefined weak function. This won't be
2740 relaxed, so we have to handle it here. */
2741 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2742 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2744 /* We can use x0 as the base register. */
2745 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2746 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2747 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2748 /* Set the relocation value so that we get 0 after the pc
2749 relative adjustment. */
2750 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2755 case R_RISCV_RVC_JUMP
:
2756 if (bfd_link_pic (info
) && h
!= NULL
)
2758 if (h
->plt
.offset
!= MINUS_ONE
)
2760 /* Refer to the PLT entry. This check has to match the
2761 check in _bfd_riscv_relax_section. */
2762 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2763 unresolved_reloc
= false;
2765 else if (!SYMBOL_REFERENCES_LOCAL (info
, h
)
2766 && (input_section
->flags
& SEC_ALLOC
) != 0
2767 && (input_section
->flags
& SEC_READONLY
) != 0
2768 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2770 /* PR 28509, when generating the shared object, these
2771 referenced symbols may bind externally, which means
2772 they will be exported to the dynamic symbol table,
2773 and are preemptible by default. These symbols cannot
2774 be referenced by the non-pic relocations, like
2775 R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2777 However, consider that linker may relax the R_RISCV_CALL
2778 relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2779 these relocations are relocated to the plt entries,
2780 then we won't report error for them.
2782 Perhaps we also need the similar checks for the
2783 R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
2784 msg
= bfd_asprintf (_("%%X%%P: relocation %s against `%s'"
2785 " which may bind externally"
2787 " when making a shared object;"
2788 " recompile with -fPIC\n"),
2789 howto
->name
, h
->root
.root
.string
);
2790 r
= bfd_reloc_notsupported
;
2795 case R_RISCV_TPREL_HI20
:
2796 relocation
= tpoff (info
, relocation
);
2799 case R_RISCV_TPREL_LO12_I
:
2800 case R_RISCV_TPREL_LO12_S
:
2801 relocation
= tpoff (info
, relocation
);
2804 case R_RISCV_TPREL_I
:
2805 case R_RISCV_TPREL_S
:
2806 relocation
= tpoff (info
, relocation
);
2807 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2809 /* We can use tp as the base register. */
2810 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2811 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2812 insn
|= X_TP
<< OP_SH_RS1
;
2813 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2816 r
= bfd_reloc_overflow
;
2819 case R_RISCV_GPREL_I
:
2820 case R_RISCV_GPREL_S
:
2822 bfd_vma gp
= riscv_global_pointer_value (info
);
2823 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2824 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2826 /* We can use x0 or gp as the base register. */
2827 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2828 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2831 rel
->r_addend
-= gp
;
2832 insn
|= X_GP
<< OP_SH_RS1
;
2834 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2837 r
= bfd_reloc_overflow
;
2841 case R_RISCV_PCREL_HI20
:
2842 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, relocation
,
2844 /* Update howto if relocation is changed. */
2845 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2846 ELFNN_R_TYPE (rel
->r_info
));
2848 r
= bfd_reloc_notsupported
;
2849 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2850 relocation
+ rel
->r_addend
,
2852 r
= bfd_reloc_overflow
;
2855 case R_RISCV_PCREL_LO12_I
:
2856 case R_RISCV_PCREL_LO12_S
:
2857 /* We don't allow section symbols plus addends as the auipc address,
2858 because then riscv_relax_delete_bytes would have to search through
2859 all relocs to update these addends. This is also ambiguous, as
2860 we do allow offsets to be added to the target address, which are
2861 not to be used to find the auipc address. */
2862 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2863 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2866 msg
= _("%pcrel_lo section symbol with an addend");
2867 r
= bfd_reloc_dangerous
;
2871 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2872 input_section
, info
, howto
,
2875 r
= bfd_reloc_overflow
;
2878 case R_RISCV_TLS_DTPREL32
:
2879 case R_RISCV_TLS_DTPREL64
:
2880 relocation
= dtpoff (info
, relocation
);
2883 case R_RISCV_TLSDESC_LOAD_LO12
:
2884 case R_RISCV_TLSDESC_ADD_LO12
:
2887 msg
= _("%tlsdesc_lo with addend");
2888 r
= bfd_reloc_dangerous
;
2892 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2893 input_section
, info
, howto
,
2896 r
= bfd_reloc_overflow
;
2900 /* Non ABS symbol should be blocked in check_relocs. */
2906 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2909 if (RISCV_GENERATE_DYNAMIC_RELOC (howto
->pc_relative
, info
, h
,
2912 Elf_Internal_Rela outrel
;
2915 /* When generating a shared object, these relocations
2916 are copied into the output file to be resolved at run
2920 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2923 bool relocate
= false;
2924 if (outrel
.r_offset
== (bfd_vma
) -1)
2926 else if (outrel
.r_offset
== (bfd_vma
) -2)
2931 else if (h
!= NULL
&& bfd_is_abs_symbol (&h
->root
))
2933 /* Don't need dynamic reloc when the ABS symbol is
2934 non-dynamic or forced to local. Maybe just use
2935 SYMBOL_REFERENCES_LOCAL to check? */
2936 skip
= (h
->forced_local
|| (h
->dynindx
== -1));
2940 outrel
.r_offset
+= sec_addr (input_section
);
2943 memset (&outrel
, 0, sizeof outrel
); /* R_RISCV_NONE. */
2944 else if (RISCV_COPY_INPUT_RELOC (info
, h
))
2946 /* Maybe just use !SYMBOL_REFERENCES_LOCAL to check? */
2947 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2948 outrel
.r_addend
= rel
->r_addend
;
2952 /* This symbol is local, or marked to become local. */
2953 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2954 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2957 sreloc
= elf_section_data (input_section
)->sreloc
;
2958 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2964 case R_RISCV_TLSDESC_HI20
:
2968 case R_RISCV_TLS_GOT_HI20
:
2972 case R_RISCV_TLS_GD_HI20
:
2976 off
= h
->got
.offset
;
2981 off
= local_got_offsets
[r_symndx
];
2982 local_got_offsets
[r_symndx
] |= 1;
2985 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2986 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
| GOT_TLSDESC
));
2987 /* When more than one TLS type is used, the GD slot comes first,
2988 then IE, then finally TLSDESC. */
2990 if (tls_type
& GOT_TLS_GD
)
2991 ie_off
+= TLS_GD_GOT_ENTRY_SIZE
;
2994 if (tls_type
& GOT_TLS_IE
)
2995 desc_off
+= TLS_IE_GOT_ENTRY_SIZE
;
3001 Elf_Internal_Rela outrel
;
3003 bool need_relocs
= false;
3005 if (htab
->elf
.srelgot
== NULL
)
3008 bool dyn
= elf_hash_table (info
)->dynamic_sections_created
;
3009 RISCV_TLS_GD_IE_NEED_DYN_RELOC (info
, dyn
, h
, indx
, need_relocs
);
3011 /* The GOT entries have not been initialized yet. Do it
3012 now, and emit any relocations. */
3013 if (tls_type
& GOT_TLS_GD
)
3017 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
3018 outrel
.r_addend
= 0;
3019 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
3020 bfd_put_NN (output_bfd
, 0,
3021 htab
->elf
.sgot
->contents
+ off
);
3022 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3025 BFD_ASSERT (! unresolved_reloc
);
3026 bfd_put_NN (output_bfd
,
3027 dtpoff (info
, relocation
),
3028 (htab
->elf
.sgot
->contents
3029 + off
+ RISCV_ELF_WORD_BYTES
));
3033 bfd_put_NN (output_bfd
, 0,
3034 (htab
->elf
.sgot
->contents
3035 + off
+ RISCV_ELF_WORD_BYTES
));
3036 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
3037 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
3038 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3043 /* If we are not emitting relocations for a
3044 general dynamic reference, then we must be in a
3045 static link or an executable link with the
3046 symbol binding locally. Mark it as belonging
3047 to module 1, the executable. */
3048 bfd_put_NN (output_bfd
, 1,
3049 htab
->elf
.sgot
->contents
+ off
);
3050 bfd_put_NN (output_bfd
,
3051 dtpoff (info
, relocation
),
3052 (htab
->elf
.sgot
->contents
3053 + off
+ RISCV_ELF_WORD_BYTES
));
3057 if (tls_type
& GOT_TLS_IE
)
3061 bfd_put_NN (output_bfd
, 0,
3062 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
3063 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
3065 outrel
.r_addend
= 0;
3067 outrel
.r_addend
= tpoff (info
, relocation
);
3068 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
3069 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3073 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
3074 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
3078 if (tls_type
& GOT_TLSDESC
)
3080 /* TLSDESC is always handled by the dynamic linker and always need
3082 bfd_put_NN (output_bfd
, 0,
3083 htab
->elf
.sgot
->contents
+ off
+ desc_off
);
3084 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
3086 outrel
.r_addend
= 0;
3088 outrel
.r_addend
= tlsdescoff (info
, relocation
);
3089 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLSDESC
);
3090 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3094 BFD_ASSERT (off
< (bfd_vma
) -2);
3095 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
3097 relocation
+= ie_off
;
3099 relocation
+= desc_off
;
3100 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
3103 r
= bfd_reloc_overflow
;
3104 unresolved_reloc
= false;
3108 r
= bfd_reloc_notsupported
;
3111 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3112 because such sections are not SEC_ALLOC and thus ld.so will
3113 not process them. */
3114 if (unresolved_reloc
3115 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3117 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3118 rel
->r_offset
) != (bfd_vma
) -1)
3120 msg
= bfd_asprintf (_("%%X%%P: unresolvable %s relocation against "
3123 h
->root
.root
.string
);
3124 r
= bfd_reloc_notsupported
;
3128 if (r
== bfd_reloc_ok
)
3129 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
3130 input_bfd
, contents
);
3132 /* We should have already detected the error and set message before.
3133 If the error message isn't set since the linker runs out of memory
3134 or we don't set it before, then we should set the default message
3135 with the "internal error" string here. */
3141 case bfd_reloc_overflow
:
3142 info
->callbacks
->reloc_overflow
3143 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3144 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
3147 case bfd_reloc_undefined
:
3148 info
->callbacks
->undefined_symbol
3149 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
3153 case bfd_reloc_outofrange
:
3155 msg
= _("%X%P: internal error: out of range error\n");
3158 case bfd_reloc_notsupported
:
3160 msg
= _("%X%P: internal error: unsupported relocation error\n");
3163 case bfd_reloc_dangerous
:
3164 /* The error message should already be set. */
3166 msg
= _("dangerous relocation error");
3167 info
->callbacks
->reloc_dangerous
3168 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
3172 msg
= _("%X%P: internal error: unknown error\n");
3176 /* Do not report error message for the dangerous relocation again. */
3177 if (msg
&& r
!= bfd_reloc_dangerous
)
3178 info
->callbacks
->einfo (msg
);
3180 /* We already reported the error via a callback, so don't try to report
3181 it again by returning false. That leads to spurious errors. */
3186 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
3188 riscv_free_pcrel_relocs (&pcrel_relocs
);
3192 /* Finish up dynamic symbol handling. We set the contents of various
3193 dynamic sections here. */
3196 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
3197 struct bfd_link_info
*info
,
3198 struct elf_link_hash_entry
*h
,
3199 Elf_Internal_Sym
*sym
)
3201 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3202 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3204 if (h
->plt
.offset
!= (bfd_vma
) -1)
3206 /* We've decided to create a PLT entry for this symbol. */
3208 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
3209 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
3210 Elf_Internal_Rela rela
;
3211 asection
*plt
, *gotplt
, *relplt
;
3213 /* When building a static executable, use .iplt, .igot.plt and
3214 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3215 if (htab
->elf
.splt
!= NULL
)
3217 plt
= htab
->elf
.splt
;
3218 gotplt
= htab
->elf
.sgotplt
;
3219 relplt
= htab
->elf
.srelplt
;
3223 plt
= htab
->elf
.iplt
;
3224 gotplt
= htab
->elf
.igotplt
;
3225 relplt
= htab
->elf
.irelplt
;
3228 /* This symbol has an entry in the procedure linkage table. Set
3230 if ((h
->dynindx
== -1
3231 && !((h
->forced_local
|| bfd_link_executable (info
))
3233 && h
->type
== STT_GNU_IFUNC
))
3239 /* Calculate the address of the PLT header. */
3240 header_address
= sec_addr (plt
);
3242 /* Calculate the index of the entry and the offset of .got.plt entry.
3243 For static executables, we don't reserve anything. */
3244 if (plt
== htab
->elf
.splt
)
3246 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
3247 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
3251 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
3252 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
3255 /* Calculate the address of the .got.plt entry. */
3256 got_address
= sec_addr (gotplt
) + got_offset
;
3258 /* Find out where the .plt entry should go. */
3259 loc
= plt
->contents
+ h
->plt
.offset
;
3261 /* Fill in the PLT entry itself. */
3262 if (! riscv_make_plt_entry (output_bfd
, got_address
,
3263 header_address
+ h
->plt
.offset
,
3267 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
3268 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
3270 /* Fill in the initial value of the .got.plt entry. */
3271 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
3272 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
3274 rela
.r_offset
= got_address
;
3276 if (h
->dynindx
== -1
3277 || ((bfd_link_executable (info
)
3278 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3280 && h
->type
== STT_GNU_IFUNC
))
3282 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3283 h
->root
.root
.string
,
3284 h
->root
.u
.def
.section
->owner
);
3286 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3287 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
3288 asection
*sec
= h
->root
.u
.def
.section
;
3289 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3290 rela
.r_addend
= h
->root
.u
.def
.value
3291 + sec
->output_section
->vma
3292 + sec
->output_offset
;
3296 /* Fill in the entry in the .rela.plt section. */
3297 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
3301 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
3302 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3304 if (!h
->def_regular
)
3306 /* Mark the symbol as undefined, rather than as defined in
3307 the .plt section. Leave the value alone. */
3308 sym
->st_shndx
= SHN_UNDEF
;
3309 /* If the symbol is weak, we do need to clear the value.
3310 Otherwise, the PLT entry would provide a definition for
3311 the symbol even if the symbol wasn't defined anywhere,
3312 and so the symbol would never be NULL. */
3313 if (!h
->ref_regular_nonweak
)
3318 if (h
->got
.offset
!= (bfd_vma
) -1
3319 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
| GOT_TLSDESC
))
3320 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3324 Elf_Internal_Rela rela
;
3325 bool use_elf_append_rela
= true;
3327 /* This symbol has an entry in the GOT. Set it up. */
3329 sgot
= htab
->elf
.sgot
;
3330 srela
= htab
->elf
.srelgot
;
3331 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3333 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3335 /* Handle the ifunc symbol in GOT entry. */
3337 && h
->type
== STT_GNU_IFUNC
)
3339 if (h
->plt
.offset
== (bfd_vma
) -1)
3341 /* STT_GNU_IFUNC is referenced without PLT. */
3343 if (htab
->elf
.splt
== NULL
)
3345 /* Use .rela.iplt section to store .got relocations
3346 in static executable. */
3347 srela
= htab
->elf
.irelplt
;
3349 /* Do not use riscv_elf_append_rela to add dynamic
3351 use_elf_append_rela
= false;
3354 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3356 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3357 h
->root
.root
.string
,
3358 h
->root
.u
.def
.section
->owner
);
3360 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3361 rela
.r_addend
= (h
->root
.u
.def
.value
3362 + h
->root
.u
.def
.section
->output_section
->vma
3363 + h
->root
.u
.def
.section
->output_offset
);
3367 /* Generate R_RISCV_NN. */
3368 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3369 BFD_ASSERT (h
->dynindx
!= -1);
3370 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3374 else if (bfd_link_pic (info
))
3376 /* Generate R_RISCV_NN. */
3377 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3378 BFD_ASSERT (h
->dynindx
!= -1);
3379 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3386 if (!h
->pointer_equality_needed
)
3389 /* For non-shared object, we can't use .got.plt, which
3390 contains the real function address if we need pointer
3391 equality. We load the GOT entry with the PLT entry. */
3392 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3393 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3394 + plt
->output_offset
3396 htab
->elf
.sgot
->contents
3397 + (h
->got
.offset
& ~(bfd_vma
) 1));
3401 else if (bfd_link_pic (info
)
3402 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3404 /* If this is a local symbol reference, we just want to emit
3405 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3406 or a pie link, or the symbol was forced to be local because
3407 of a version file. The entry in the global offset table will
3408 already have been initialized in the relocate_section function. */
3409 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3410 asection
*sec
= h
->root
.u
.def
.section
;
3411 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3412 rela
.r_addend
= (h
->root
.u
.def
.value
3413 + sec
->output_section
->vma
3414 + sec
->output_offset
);
3418 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3419 BFD_ASSERT (h
->dynindx
!= -1);
3420 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3424 bfd_put_NN (output_bfd
, 0,
3425 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3427 if (use_elf_append_rela
)
3428 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3431 /* Use riscv_elf_append_rela to add the dynamic relocs into
3432 .rela.iplt may cause the overwrite problems. Since we insert
3433 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3434 but the riscv_elf_append_rela adds the relocs to the place
3435 that are calculated from the reloc_index (in seqential).
3437 One solution is that add these dynamic relocs (GOT IFUNC)
3438 from the last of .rela.iplt section. */
3439 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3440 bfd_byte
*loc
= srela
->contents
3441 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3442 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3448 Elf_Internal_Rela rela
;
3451 /* This symbols needs a copy reloc. Set it up. */
3452 BFD_ASSERT (h
->dynindx
!= -1);
3454 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3455 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3457 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3458 s
= htab
->elf
.sreldynrelro
;
3460 s
= htab
->elf
.srelbss
;
3461 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3464 /* Mark some specially defined symbols as absolute. */
3465 if (h
== htab
->elf
.hdynamic
3466 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3467 sym
->st_shndx
= SHN_ABS
;
3472 /* Finish up local dynamic symbol handling. We set the contents of
3473 various dynamic sections here. */
3476 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3478 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3479 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3481 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3484 /* Finish up the dynamic sections. */
3487 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3488 bfd
*dynobj
, asection
*sdyn
)
3490 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3491 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3492 size_t dynsize
= bed
->s
->sizeof_dyn
;
3493 bfd_byte
*dyncon
, *dynconend
;
3495 dynconend
= sdyn
->contents
+ sdyn
->size
;
3496 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3498 Elf_Internal_Dyn dyn
;
3501 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3506 s
= htab
->elf
.sgotplt
;
3507 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3510 s
= htab
->elf
.srelplt
;
3511 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3514 s
= htab
->elf
.srelplt
;
3515 dyn
.d_un
.d_val
= s
->size
;
3521 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3527 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3528 struct bfd_link_info
*info
)
3532 struct riscv_elf_link_hash_table
*htab
;
3534 htab
= riscv_elf_hash_table (info
);
3535 BFD_ASSERT (htab
!= NULL
);
3536 dynobj
= htab
->elf
.dynobj
;
3538 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3540 if (elf_hash_table (info
)->dynamic_sections_created
)
3545 splt
= htab
->elf
.splt
;
3546 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3548 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3553 /* Fill in the head and tail entries in the procedure linkage table. */
3557 uint32_t plt_header
[PLT_HEADER_INSNS
];
3558 ret
= riscv_make_plt_header (output_bfd
,
3559 sec_addr (htab
->elf
.sgotplt
),
3560 sec_addr (splt
), plt_header
);
3564 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3565 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3567 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3572 if (htab
->elf
.sgotplt
)
3574 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3576 if (bfd_is_abs_section (output_section
))
3578 (*_bfd_error_handler
)
3579 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3583 if (htab
->elf
.sgotplt
->size
> 0)
3585 /* Write the first two entries in .got.plt, needed for the dynamic
3587 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3588 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3589 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3592 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3597 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3599 if (htab
->elf
.sgot
->size
> 0)
3601 /* Set the first entry in the global offset table to the address of
3602 the dynamic section. */
3603 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3604 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3607 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3610 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3611 htab_traverse (htab
->loc_hash_table
,
3612 riscv_elf_finish_local_dynamic_symbol
,
3618 /* Return address for Ith PLT stub in section PLT, for relocation REL
3619 or (bfd_vma) -1 if it should not be included. */
3622 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3623 const arelent
*rel ATTRIBUTE_UNUSED
)
3625 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3628 static enum elf_reloc_type_class
3629 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3630 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3631 const Elf_Internal_Rela
*rela
)
3633 switch (ELFNN_R_TYPE (rela
->r_info
))
3635 case R_RISCV_RELATIVE
:
3636 return reloc_class_relative
;
3637 case R_RISCV_JUMP_SLOT
:
3638 return reloc_class_plt
;
3640 return reloc_class_copy
;
3642 return reloc_class_normal
;
3646 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3650 riscv_float_abi_string (flagword flags
)
3652 switch (flags
& EF_RISCV_FLOAT_ABI
)
3654 case EF_RISCV_FLOAT_ABI_SOFT
:
3655 return "soft-float";
3657 case EF_RISCV_FLOAT_ABI_SINGLE
:
3658 return "single-float";
3660 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3661 return "double-float";
3663 case EF_RISCV_FLOAT_ABI_QUAD
:
3664 return "quad-float";
3671 /* The information of architecture elf attributes. */
3672 static riscv_subset_list_t in_subsets
;
3673 static riscv_subset_list_t out_subsets
;
3674 static riscv_subset_list_t merged_subsets
;
3676 /* Predicator for standard extension. */
3679 riscv_std_ext_p (const char *name
)
3681 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3684 /* Update the output subset's version to match the input when the input
3685 subset's version is newer. */
3688 riscv_update_subset_version (struct riscv_subset_t
*in
,
3689 struct riscv_subset_t
*out
)
3691 if (in
== NULL
|| out
== NULL
)
3694 /* Update the output ISA versions to the newest ones, but otherwise don't
3695 provide any errors or warnings about mis-matched ISA versions as it's
3696 generally too tricky to check for these at link time. */
3697 if ((in
->major_version
> out
->major_version
)
3698 || (in
->major_version
== out
->major_version
3699 && in
->minor_version
> out
->minor_version
)
3700 || (out
->major_version
== RISCV_UNKNOWN_VERSION
))
3702 out
->major_version
= in
->major_version
;
3703 out
->minor_version
= in
->minor_version
;
3707 /* Return true if subset is 'i' or 'e'. */
3710 riscv_i_or_e_p (bfd
*ibfd
,
3712 struct riscv_subset_t
*subset
)
3714 if ((strcasecmp (subset
->name
, "e") != 0)
3715 && (strcasecmp (subset
->name
, "i") != 0))
3718 (_("error: %pB: corrupted ISA string '%s'. "
3719 "First letter should be 'i' or 'e' but got '%s'"),
3720 ibfd
, arch
, subset
->name
);
3726 /* Merge standard extensions.
3729 Return FALSE if failed to merge.
3733 `in_arch`: Raw ISA string for input object.
3734 `out_arch`: Raw ISA string for output object.
3735 `pin`: Subset list for input object.
3736 `pout`: Subset list for output object. */
3739 riscv_merge_std_ext (bfd
*ibfd
,
3740 const char *in_arch
,
3741 const char *out_arch
,
3742 struct riscv_subset_t
**pin
,
3743 struct riscv_subset_t
**pout
)
3745 const char *standard_exts
= "mafdqlcbjtpvnh";
3747 struct riscv_subset_t
*in
= *pin
;
3748 struct riscv_subset_t
*out
= *pout
;
3750 /* First letter should be 'i' or 'e'. */
3751 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3754 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3757 if (strcasecmp (in
->name
, out
->name
) != 0)
3759 /* TODO: We might allow merge 'i' with 'e'. */
3761 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3762 ibfd
, in
->name
, out
->name
);
3766 riscv_update_subset_version(in
, out
);
3767 riscv_add_subset (&merged_subsets
,
3768 out
->name
, out
->major_version
, out
->minor_version
);
3773 /* Handle standard extension first. */
3774 for (p
= standard_exts
; *p
; ++p
)
3776 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3777 char find_ext
[2] = {*p
, '\0'};
3778 bool find_in
, find_out
;
3780 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3781 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3783 if (!find_in
&& !find_out
)
3786 if (find_in
&& find_out
)
3787 riscv_update_subset_version(ext_in
, ext_out
);
3789 ext_merged
= find_out
? ext_out
: ext_in
;
3790 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3791 ext_merged
->major_version
, ext_merged
->minor_version
);
3794 /* Skip all standard extensions. */
3795 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3796 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3804 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3805 object subset list. Likewise for POUT and the output object. Return TRUE
3806 on success and FALSE when a conflict is found. */
3809 riscv_merge_multi_letter_ext (riscv_subset_t
**pin
,
3810 riscv_subset_t
**pout
)
3812 riscv_subset_t
*in
= *pin
;
3813 riscv_subset_t
*out
= *pout
;
3814 riscv_subset_t
*tail
;
3820 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3824 /* `in' comes before `out', append `in' and increment. */
3825 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3831 /* `out' comes before `in', append `out' and increment. */
3832 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3833 out
->minor_version
);
3838 /* Both present, check version and increment both. */
3839 riscv_update_subset_version (in
, out
);
3841 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3842 out
->minor_version
);
3850 /* If we're here, either `in' or `out' is running longer than
3851 the other. So, we need to append the corresponding tail. */
3852 tail
= in
? in
: out
;
3855 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3856 tail
->minor_version
);
3864 /* Merge Tag_RISCV_arch attribute. */
3867 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3869 riscv_subset_t
*in
, *out
;
3870 char *merged_arch_str
;
3872 unsigned xlen_in
, xlen_out
;
3873 merged_subsets
.head
= NULL
;
3874 merged_subsets
.tail
= NULL
;
3876 riscv_parse_subset_t riscv_rps_ld_in
=
3877 {&in_subsets
, _bfd_error_handler
, &xlen_in
, NULL
, false};
3878 riscv_parse_subset_t riscv_rps_ld_out
=
3879 {&out_subsets
, _bfd_error_handler
, &xlen_out
, NULL
, false};
3881 if (in_arch
== NULL
&& out_arch
== NULL
)
3883 if (in_arch
== NULL
&& out_arch
!= NULL
)
3885 if (in_arch
!= NULL
&& out_arch
== NULL
)
3888 /* Parse subset from ISA string. */
3889 if (!riscv_parse_subset (&riscv_rps_ld_in
, in_arch
))
3891 if (!riscv_parse_subset (&riscv_rps_ld_out
, out_arch
))
3894 /* Checking XLEN. */
3895 if (xlen_out
!= xlen_in
)
3898 (_("error: %pB: ISA string of input (%s) doesn't match "
3899 "output (%s)"), ibfd
, in_arch
, out_arch
);
3903 /* Merge subset list. */
3904 in
= in_subsets
.head
;
3905 out
= out_subsets
.head
;
3907 /* Merge standard extension. */
3908 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3911 /* Merge all non-single letter extensions with single call. */
3912 if (!riscv_merge_multi_letter_ext (&in
, &out
))
3915 if (xlen_in
!= xlen_out
)
3918 (_("error: %pB: XLEN of input (%u) doesn't match "
3919 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3923 if (xlen_in
!= ARCH_SIZE
)
3926 (_("error: %pB: unsupported XLEN (%u), you might be "
3927 "using wrong emulation"), ibfd
, xlen_in
);
3931 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3933 /* Release the subset lists. */
3934 riscv_release_subset_list (&in_subsets
);
3935 riscv_release_subset_list (&out_subsets
);
3936 riscv_release_subset_list (&merged_subsets
);
3938 return merged_arch_str
;
3941 /* Merge object attributes from IBFD into output_bfd of INFO.
3942 Raise an error if there are conflicting attributes. */
3945 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3947 bfd
*obfd
= info
->output_bfd
;
3948 obj_attribute
*in_attr
;
3949 obj_attribute
*out_attr
;
3951 bool priv_attrs_merged
= false;
3952 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3955 /* Skip linker created files. */
3956 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3959 /* Skip any input that doesn't have an attribute section.
3960 This enables to link object files without attribute section with
3962 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3965 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3967 /* This is the first object. Copy the attributes. */
3968 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3970 out_attr
= elf_known_obj_attributes_proc (obfd
);
3972 /* Use the Tag_null value to indicate the attributes have been
3979 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3980 out_attr
= elf_known_obj_attributes_proc (obfd
);
3982 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3986 case Tag_RISCV_arch
:
3987 if (!out_attr
[Tag_RISCV_arch
].s
)
3988 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3989 else if (in_attr
[Tag_RISCV_arch
].s
3990 && out_attr
[Tag_RISCV_arch
].s
)
3992 /* Check compatible. */
3994 riscv_merge_arch_attr_info (ibfd
,
3995 in_attr
[Tag_RISCV_arch
].s
,
3996 out_attr
[Tag_RISCV_arch
].s
);
3997 if (merged_arch
== NULL
)
4000 out_attr
[Tag_RISCV_arch
].s
= "";
4003 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
4007 case Tag_RISCV_priv_spec
:
4008 case Tag_RISCV_priv_spec_minor
:
4009 case Tag_RISCV_priv_spec_revision
:
4010 /* If we have handled the privileged elf attributes, then skip it. */
4011 if (!priv_attrs_merged
)
4013 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
4014 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
4015 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
4016 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
4017 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
4019 /* Get the privileged spec class from elf attributes. */
4020 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
4024 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
4029 /* Allow to link the object without the privileged specs. */
4030 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
4032 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
4033 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
4034 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
4036 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
4037 && in_priv_spec
!= out_priv_spec
)
4040 (_("warning: %pB use privileged spec version %u.%u.%u but "
4041 "the output use version %u.%u.%u"),
4050 /* The privileged spec v1.9.1 can not be linked with others
4051 since the conflicts, so we plan to drop it in a year or
4053 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
4054 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
4057 (_("warning: privileged spec version 1.9.1 can not be "
4058 "linked with other spec versions"));
4061 /* Update the output privileged spec to the newest one. */
4062 if (in_priv_spec
> out_priv_spec
)
4064 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
4065 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
4066 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
4069 priv_attrs_merged
= true;
4073 case Tag_RISCV_unaligned_access
:
4074 out_attr
[i
].i
|= in_attr
[i
].i
;
4077 case Tag_RISCV_stack_align
:
4078 if (out_attr
[i
].i
== 0)
4079 out_attr
[i
].i
= in_attr
[i
].i
;
4080 else if (in_attr
[i
].i
!= 0
4081 && out_attr
[i
].i
!= 0
4082 && out_attr
[i
].i
!= in_attr
[i
].i
)
4085 (_("error: %pB use %u-byte stack aligned but the output "
4086 "use %u-byte stack aligned"),
4087 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
4093 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
4096 /* If out_attr was copied from in_attr then it won't have a type yet. */
4097 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
4098 out_attr
[i
].type
= in_attr
[i
].type
;
4101 /* Merge Tag_compatibility attributes and any common GNU ones. */
4102 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
4105 /* Check for any attributes not known on RISC-V. */
4106 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
4111 /* Merge backend specific data from an object file to the output
4112 object file when linking. */
4115 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
4117 bfd
*obfd
= info
->output_bfd
;
4118 flagword new_flags
, old_flags
;
4120 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
4123 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
4125 (*_bfd_error_handler
)
4126 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
4127 " target emulation `%s' does not match `%s'"),
4128 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
4132 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
4135 if (!riscv_merge_attributes (ibfd
, info
))
4138 /* Check to see if the input BFD actually contains any sections. If not,
4139 its flags may not have been initialized either, but it cannot actually
4140 cause any incompatibility. Do not short-circuit dynamic objects; their
4141 section list may be emptied by elf_link_add_object_symbols.
4143 Also check to see if there are no code sections in the input. In this
4144 case, there is no need to check for code specific flags. */
4145 if (!(ibfd
->flags
& DYNAMIC
))
4147 bool null_input_bfd
= true;
4148 bool only_data_sections
= true;
4151 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4153 null_input_bfd
= false;
4155 if ((bfd_section_flags (sec
)
4156 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
4157 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
4159 only_data_sections
= false;
4164 if (null_input_bfd
|| only_data_sections
)
4168 new_flags
= elf_elfheader (ibfd
)->e_flags
;
4169 old_flags
= elf_elfheader (obfd
)->e_flags
;
4171 if (!elf_flags_init (obfd
))
4173 elf_flags_init (obfd
) = true;
4174 elf_elfheader (obfd
)->e_flags
= new_flags
;
4178 /* Disallow linking different float ABIs. */
4179 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
4181 (*_bfd_error_handler
)
4182 (_("%pB: can't link %s modules with %s modules"), ibfd
,
4183 riscv_float_abi_string (new_flags
),
4184 riscv_float_abi_string (old_flags
));
4188 /* Disallow linking RVE and non-RVE. */
4189 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
4191 (*_bfd_error_handler
)
4192 (_("%pB: can't link RVE with other target"), ibfd
);
4196 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
4197 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
4199 /* Allow linking TSO and non-TSO, and keep the TSO flag. */
4200 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_TSO
;
4205 bfd_set_error (bfd_error_bad_value
);
4209 /* Ignore and report warning for the unknwon elf attribute. */
4212 riscv_elf_obj_attrs_handle_unknown (bfd
*abfd
, int tag
)
4215 /* xgettext:c-format */
4216 (_("warning: %pB: unknown RISCV ABI object attribute %d"),
4221 /* A second format for recording PC-relative hi relocations. This stores the
4222 information required to relax them to GP-relative addresses. */
4224 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
4225 struct riscv_pcgp_hi_reloc
4232 bool undefined_weak
;
4233 riscv_pcgp_hi_reloc
*next
;
4236 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
4237 struct riscv_pcgp_lo_reloc
4240 riscv_pcgp_lo_reloc
*next
;
4245 riscv_pcgp_hi_reloc
*hi
;
4246 riscv_pcgp_lo_reloc
*lo
;
4247 } riscv_pcgp_relocs
;
4249 /* Initialize the pcgp reloc info in P. */
4252 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
4259 /* Free the pcgp reloc info in P. */
4262 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
4263 bfd
*abfd ATTRIBUTE_UNUSED
,
4264 asection
*sec ATTRIBUTE_UNUSED
)
4266 riscv_pcgp_hi_reloc
*c
;
4267 riscv_pcgp_lo_reloc
*l
;
4269 for (c
= p
->hi
; c
!= NULL
; )
4271 riscv_pcgp_hi_reloc
*next
= c
->next
;
4276 for (l
= p
->lo
; l
!= NULL
; )
4278 riscv_pcgp_lo_reloc
*next
= l
->next
;
4284 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4285 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4286 relax the corresponding lo part reloc. */
4289 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4290 bfd_vma hi_addend
, bfd_vma hi_addr
,
4291 unsigned hi_sym
, asection
*sym_sec
,
4292 bool undefined_weak
)
4294 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
4297 new->hi_sec_off
= hi_sec_off
;
4298 new->hi_addend
= hi_addend
;
4299 new->hi_addr
= hi_addr
;
4300 new->hi_sym
= hi_sym
;
4301 new->sym_sec
= sym_sec
;
4302 new->undefined_weak
= undefined_weak
;
4308 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4309 This is used by a lo part reloc to find the corresponding hi part reloc. */
4311 static riscv_pcgp_hi_reloc
*
4312 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4314 riscv_pcgp_hi_reloc
*c
;
4316 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4317 if (c
->hi_sec_off
== hi_sec_off
)
4322 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4323 This is used to record relocs that can't be relaxed. */
4326 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4328 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4331 new->hi_sec_off
= hi_sec_off
;
4337 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4338 This is used by a hi part reloc to find the corresponding lo part reloc. */
4341 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4343 riscv_pcgp_lo_reloc
*c
;
4345 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4346 if (c
->hi_sec_off
== hi_sec_off
)
4352 riscv_update_pcgp_relocs (riscv_pcgp_relocs
*p
, asection
*deleted_sec
,
4353 bfd_vma deleted_addr
, size_t deleted_count
)
4355 /* Bytes have already been deleted and toaddr should match the old section
4356 size for our checks, so adjust it here. */
4357 bfd_vma toaddr
= deleted_sec
->size
+ deleted_count
;
4358 riscv_pcgp_lo_reloc
*l
;
4359 riscv_pcgp_hi_reloc
*h
;
4361 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4362 entries where they occur after the deleted bytes. */
4363 for (l
= p
->lo
; l
!= NULL
; l
= l
->next
)
4364 if (l
->hi_sec_off
> deleted_addr
4365 && l
->hi_sec_off
< toaddr
)
4366 l
->hi_sec_off
-= deleted_count
;
4368 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4369 these values occur after the deleted bytes. */
4370 for (h
= p
->hi
; h
!= NULL
; h
= h
->next
)
4372 if (h
->hi_sec_off
> deleted_addr
4373 && h
->hi_sec_off
< toaddr
)
4374 h
->hi_sec_off
-= deleted_count
;
4375 if (h
->sym_sec
== deleted_sec
4376 && h
->hi_addr
> deleted_addr
4377 && h
->hi_addr
< toaddr
)
4378 h
->hi_addr
-= deleted_count
;
4382 /* Delete some bytes, adjust relcocations and symbol table from a section. */
4385 _riscv_relax_delete_bytes (bfd
*abfd
,
4389 struct bfd_link_info
*link_info
,
4390 riscv_pcgp_relocs
*p
,
4391 bfd_vma delete_total
,
4394 unsigned int i
, symcount
;
4395 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
4396 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4397 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4398 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4399 bfd_byte
*contents
= data
->this_hdr
.contents
;
4400 size_t bytes_to_move
= toaddr
- addr
- count
;
4402 /* Actually delete the bytes. */
4404 memmove (contents
+ addr
, contents
+ addr
+ count
+ delete_total
, bytes_to_move
);
4406 /* Still adjust relocations and symbols in non-linear times. */
4407 toaddr
= sec
->size
+ count
;
4409 /* Adjust the location of all of the relocs. Note that we need not
4410 adjust the addends, since all PC-relative references must be against
4411 symbols, which we will adjust below. */
4412 for (i
= 0; i
< sec
->reloc_count
; i
++)
4413 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
4414 data
->relocs
[i
].r_offset
-= count
;
4416 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4417 table for which these values occur after the deleted bytes. */
4419 riscv_update_pcgp_relocs (p
, sec
, addr
, count
);
4421 /* Adjust the local symbols defined in this section. */
4422 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4424 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
4425 if (sym
->st_shndx
== sec_shndx
)
4427 /* If the symbol is in the range of memory we just moved, we
4428 have to adjust its value. */
4429 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
4430 sym
->st_value
-= count
;
4432 /* If the symbol *spans* the bytes we just deleted (i.e. its
4433 *end* is in the moved bytes but its *start* isn't), then we
4434 must adjust its size.
4436 This test needs to use the original value of st_value, otherwise
4437 we might accidentally decrease size when deleting bytes right
4438 before the symbol. But since deleted relocs can't span across
4439 symbols, we can't have both a st_value and a st_size decrease,
4440 so it is simpler to just use an else. */
4441 else if (sym
->st_value
<= addr
4442 && sym
->st_value
+ sym
->st_size
> addr
4443 && sym
->st_value
+ sym
->st_size
<= toaddr
)
4444 sym
->st_size
-= count
;
4448 /* Now adjust the global symbols defined in this section. */
4449 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
4450 - symtab_hdr
->sh_info
);
4452 for (i
= 0; i
< symcount
; i
++)
4454 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
4456 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4457 containing the definition of __wrap_SYMBOL, includes a direct
4458 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4459 the same symbol (which is __wrap_SYMBOL), but still exist as two
4460 different symbols in 'sym_hashes', we don't want to adjust
4461 the global symbol __wrap_SYMBOL twice.
4463 The same problem occurs with symbols that are versioned_hidden, as
4464 foo becomes an alias for foo@BAR, and hence they need the same
4466 if (link_info
->wrap_hash
!= NULL
4467 || sym_hash
->versioned
!= unversioned
)
4469 struct elf_link_hash_entry
**cur_sym_hashes
;
4471 /* Loop only over the symbols which have already been checked. */
4472 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4475 /* If the current symbol is identical to 'sym_hash', that means
4476 the symbol was already adjusted (or at least checked). */
4477 if (*cur_sym_hashes
== sym_hash
)
4480 /* Don't adjust the symbol again. */
4481 if (cur_sym_hashes
< &sym_hashes
[i
])
4485 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4486 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4487 && sym_hash
->root
.u
.def
.section
== sec
)
4489 /* As above, adjust the value if needed. */
4490 if (sym_hash
->root
.u
.def
.value
> addr
4491 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4492 sym_hash
->root
.u
.def
.value
-= count
;
4494 /* As above, adjust the size if needed. */
4495 else if (sym_hash
->root
.u
.def
.value
<= addr
4496 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4497 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4498 sym_hash
->size
-= count
;
4505 typedef bool (*relax_delete_t
) (bfd
*, asection
*,
4507 struct bfd_link_info
*,
4508 riscv_pcgp_relocs
*,
4509 Elf_Internal_Rela
*);
4511 static relax_delete_t riscv_relax_delete_bytes
;
4513 /* Do not delete some bytes from a section while relaxing.
4514 Just mark the deleted bytes as R_RISCV_DELETE. */
4517 _riscv_relax_delete_piecewise (bfd
*abfd ATTRIBUTE_UNUSED
,
4518 asection
*sec ATTRIBUTE_UNUSED
,
4521 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
,
4522 riscv_pcgp_relocs
*p ATTRIBUTE_UNUSED
,
4523 Elf_Internal_Rela
*rel
)
4527 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4528 rel
->r_offset
= addr
;
4529 rel
->r_addend
= count
;
4533 /* Delete some bytes from a section while relaxing. */
4536 _riscv_relax_delete_immediate (bfd
*abfd
,
4540 struct bfd_link_info
*link_info
,
4541 riscv_pcgp_relocs
*p
,
4542 Elf_Internal_Rela
*rel
)
4545 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4546 return _riscv_relax_delete_bytes (abfd
, sec
, addr
, count
,
4547 link_info
, p
, 0, sec
->size
);
4550 /* Delete the bytes for R_RISCV_DELETE relocs. */
4553 riscv_relax_resolve_delete_relocs (bfd
*abfd
,
4555 struct bfd_link_info
*link_info
,
4556 Elf_Internal_Rela
*relocs
)
4558 bfd_vma delete_total
= 0;
4561 for (i
= 0; i
< sec
->reloc_count
; i
++)
4563 Elf_Internal_Rela
*rel
= relocs
+ i
;
4564 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_DELETE
)
4567 /* Find the next R_RISCV_DELETE reloc if possible. */
4568 Elf_Internal_Rela
*rel_next
= NULL
;
4569 unsigned int start
= rel
- relocs
;
4570 for (i
= start
; i
< sec
->reloc_count
; i
++)
4572 /* Since we only replace existing relocs and don't add new relocs, the
4573 relocs are in sequential order. We can skip the relocs prior to this
4574 one, making this search linear time. */
4575 rel_next
= relocs
+ i
;
4576 if (ELFNN_R_TYPE ((rel_next
)->r_info
) == R_RISCV_DELETE
4577 && (rel_next
)->r_offset
> rel
->r_offset
)
4579 BFD_ASSERT (rel_next
- rel
> 0);
4586 bfd_vma toaddr
= rel_next
== NULL
? sec
->size
: rel_next
->r_offset
;
4587 if (!_riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4588 link_info
, NULL
, delete_total
, toaddr
))
4591 delete_total
+= rel
->r_addend
;
4592 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4594 /* Skip ahead to the next delete reloc. */
4595 i
= rel_next
!= NULL
? (unsigned int) (rel_next
- relocs
- 1)
4602 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4603 struct bfd_link_info
*,
4604 Elf_Internal_Rela
*,
4605 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4606 riscv_pcgp_relocs
*,
4607 bool undefined_weak
);
4609 /* Relax AUIPC + JALR into JAL. */
4612 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4613 struct bfd_link_info
*link_info
,
4614 Elf_Internal_Rela
*rel
,
4616 bfd_vma max_alignment
,
4617 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4619 riscv_pcgp_relocs
*pcgp_relocs
,
4620 bool undefined_weak ATTRIBUTE_UNUSED
)
4622 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4623 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4624 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4625 bfd_vma auipc
, jalr
;
4626 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4628 /* If the call crosses section boundaries, an alignment directive could
4629 cause the PC-relative offset to later increase, so we need to add in the
4630 max alignment of any section inclusive from the call to the target.
4631 Otherwise, we only need to use the alignment of the current section. */
4632 if (VALID_JTYPE_IMM (foff
))
4634 if (sym_sec
->output_section
== sec
->output_section
4635 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4636 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4637 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4640 /* See if this function call can be shortened. */
4641 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4644 /* Shorten the function call. */
4645 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4647 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4648 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4649 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4650 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4652 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4653 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4657 /* Relax to C.J[AL] rd, addr. */
4658 r_type
= R_RISCV_RVC_JUMP
;
4659 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4662 else if (VALID_JTYPE_IMM (foff
))
4664 /* Relax to JAL rd, addr. */
4665 r_type
= R_RISCV_JAL
;
4666 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4670 /* Near zero, relax to JALR rd, x0, addr. */
4671 r_type
= R_RISCV_LO12_I
;
4672 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4675 /* Replace the R_RISCV_CALL reloc. */
4676 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4677 /* Replace the AUIPC. */
4678 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4680 /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */
4682 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4683 link_info
, pcgp_relocs
, rel
+ 1);
4686 /* Traverse all output sections and return the max alignment.
4688 If gp is zero, then all the output section alignments are
4689 possible candidates; Otherwise, only the output sections
4690 which are in the [gp-2K, gp+2K) range need to be considered. */
4693 _bfd_riscv_get_max_alignment (asection
*sec
, bfd_vma gp
)
4695 unsigned int max_alignment_power
= 0;
4698 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4702 && !(VALID_ITYPE_IMM (sec_addr (o
) - gp
)
4703 || VALID_ITYPE_IMM (sec_addr (o
) + o
->size
- gp
)))
4706 if (valid
&& o
->alignment_power
> max_alignment_power
)
4707 max_alignment_power
= o
->alignment_power
;
4710 return (bfd_vma
) 1 << max_alignment_power
;
4713 /* Relax non-PIC global variable references to GP-relative references. */
4716 _bfd_riscv_relax_lui (bfd
*abfd
,
4719 struct bfd_link_info
*link_info
,
4720 Elf_Internal_Rela
*rel
,
4722 bfd_vma max_alignment
,
4723 bfd_vma reserve_size
,
4725 riscv_pcgp_relocs
*pcgp_relocs
,
4726 bool undefined_weak
)
4728 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (link_info
);
4729 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4730 /* Can relax to x0 even when gp relaxation is disabled. */
4731 bfd_vma gp
= htab
->params
->relax_gp
4732 ? riscv_global_pointer_value (link_info
)
4734 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4736 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4738 if (!undefined_weak
&& gp
)
4740 /* If gp and the symbol are in the same output section, which is not the
4741 abs section, then consider only that output section's alignment. */
4742 struct bfd_link_hash_entry
*h
=
4743 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4745 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4746 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4747 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4750 /* Consider output section alignments which are in [gp-2K, gp+2K). */
4751 max_alignment
= htab
->max_alignment_for_gp
;
4752 if (max_alignment
== (bfd_vma
) -1)
4754 max_alignment
= _bfd_riscv_get_max_alignment (sec
, gp
);
4755 htab
->max_alignment_for_gp
= max_alignment
;
4760 /* Is the reference in range of x0 or gp?
4761 Valid gp range conservatively because of alignment issue.
4763 Should we also consider the alignment issue for x0 base? */
4765 || VALID_ITYPE_IMM (symval
)
4767 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4769 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
)))
4771 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4772 switch (ELFNN_R_TYPE (rel
->r_info
))
4774 case R_RISCV_LO12_I
:
4775 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4778 case R_RISCV_LO12_S
:
4779 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4783 /* Delete unnecessary LUI and reuse the reloc. */
4785 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4786 link_info
, pcgp_relocs
, rel
);
4793 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4794 account for this assuming page alignment at worst. In the presence of
4795 RELRO segment the linker aligns it by one page size, therefore sections
4796 after the segment can be moved more than one page. */
4799 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4800 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4801 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4802 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4803 : ELF_MAXPAGESIZE
)))
4805 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4806 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4807 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4808 if (rd
== 0 || rd
== X_SP
)
4811 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4812 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4814 /* Replace the R_RISCV_HI20 reloc. */
4815 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4817 /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */
4819 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4820 link_info
, pcgp_relocs
, rel
+ 1);
4826 /* Relax non-PIC TLS references to TP-relative references. */
4829 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4831 asection
*sym_sec ATTRIBUTE_UNUSED
,
4832 struct bfd_link_info
*link_info
,
4833 Elf_Internal_Rela
*rel
,
4835 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4836 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4838 riscv_pcgp_relocs
*pcgp_relocs
,
4839 bool undefined_weak ATTRIBUTE_UNUSED
)
4841 /* See if this symbol is in range of tp. */
4842 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4845 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4846 switch (ELFNN_R_TYPE (rel
->r_info
))
4848 case R_RISCV_TPREL_LO12_I
:
4849 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4852 case R_RISCV_TPREL_LO12_S
:
4853 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4856 case R_RISCV_TPREL_HI20
:
4857 case R_RISCV_TPREL_ADD
:
4858 /* Delete unnecessary instruction and reuse the reloc. */
4860 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4868 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4869 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4872 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4874 struct bfd_link_info
*link_info
,
4875 Elf_Internal_Rela
*rel
,
4877 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4878 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4879 bool *again ATTRIBUTE_UNUSED
,
4880 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4881 bool undefined_weak ATTRIBUTE_UNUSED
)
4883 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4884 bfd_vma alignment
= 1, pos
;
4885 while (alignment
<= rel
->r_addend
)
4888 symval
-= rel
->r_addend
;
4889 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4890 bfd_vma nop_bytes
= aligned_addr
- symval
;
4892 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4893 sec
->sec_flg0
= true;
4895 /* Make sure there are enough NOPs to actually achieve the alignment. */
4896 if (rel
->r_addend
< nop_bytes
)
4899 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4900 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4901 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4902 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4903 bfd_set_error (bfd_error_bad_value
);
4907 /* Delete the reloc. */
4908 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4910 /* If the number of NOPs is already correct, there's nothing to do. */
4911 if (nop_bytes
== rel
->r_addend
)
4914 /* Write as many RISC-V NOPs as we need. */
4915 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4916 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4918 /* Write a final RVC NOP if need be. */
4919 if (nop_bytes
% 4 != 0)
4920 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4922 /* Delete excess bytes. */
4923 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4924 rel
->r_addend
- nop_bytes
, link_info
,
4928 /* Relax PC-relative references to GP-relative references. */
4931 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4934 struct bfd_link_info
*link_info
,
4935 Elf_Internal_Rela
*rel
,
4937 bfd_vma max_alignment
,
4938 bfd_vma reserve_size
,
4940 riscv_pcgp_relocs
*pcgp_relocs
,
4941 bool undefined_weak
)
4943 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (link_info
);
4944 /* Can relax to x0 even when gp relaxation is disabled. */
4945 bfd_vma gp
= htab
->params
->relax_gp
4946 ? riscv_global_pointer_value (link_info
)
4949 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4951 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4952 actual target address. */
4953 riscv_pcgp_hi_reloc hi_reloc
;
4954 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4955 switch (ELFNN_R_TYPE (rel
->r_info
))
4957 case R_RISCV_PCREL_LO12_I
:
4958 case R_RISCV_PCREL_LO12_S
:
4960 /* If the %lo has an addend, it isn't for the label pointing at the
4961 hi part instruction, but rather for the symbol pointed at by the
4962 hi part instruction. So we must subtract it here for the lookup.
4963 It is still used below in the final symbol address. */
4964 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4965 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4969 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4974 symval
= hi_reloc
.hi_addr
;
4975 sym_sec
= hi_reloc
.sym_sec
;
4977 /* We can not know whether the undefined weak symbol is referenced
4978 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4979 we have to record the 'undefined_weak' flag when handling the
4980 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4981 undefined_weak
= hi_reloc
.undefined_weak
;
4985 case R_RISCV_PCREL_HI20
:
4986 /* Mergeable symbols and code might later move out of range. */
4987 if (! undefined_weak
4988 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4991 /* If the cooresponding lo relocation has already been seen then it's not
4992 safe to relax this relocation. */
4993 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
5002 if (!undefined_weak
&& gp
)
5004 /* If gp and the symbol are in the same output section, which is not the
5005 abs section, then consider only that output section's alignment. */
5006 struct bfd_link_hash_entry
*h
=
5007 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
5009 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
5010 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
5011 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
5014 /* Consider output section alignments which are in [gp-2K, gp+2K). */
5015 max_alignment
= htab
->max_alignment_for_gp
;
5016 if (max_alignment
== (bfd_vma
) -1)
5018 max_alignment
= _bfd_riscv_get_max_alignment (sec
, gp
);
5019 htab
->max_alignment_for_gp
= max_alignment
;
5024 /* Is the reference in range of x0 or gp?
5025 Valid gp range conservatively because of alignment issue.
5027 Should we also consider the alignment issue for x0 base? */
5029 || VALID_ITYPE_IMM (symval
)
5031 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
5033 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
)))
5035 unsigned sym
= hi_reloc
.hi_sym
;
5036 switch (ELFNN_R_TYPE (rel
->r_info
))
5038 case R_RISCV_PCREL_LO12_I
:
5039 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
5040 rel
->r_addend
+= hi_reloc
.hi_addend
;
5043 case R_RISCV_PCREL_LO12_S
:
5044 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
5045 rel
->r_addend
+= hi_reloc
.hi_addend
;
5048 case R_RISCV_PCREL_HI20
:
5049 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
5053 ELFNN_R_SYM(rel
->r_info
),
5056 /* Delete unnecessary AUIPC and reuse the reloc. */
5058 riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
5070 /* Called by after_allocation to set the information of data segment
5074 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
5075 int *data_segment_phase
)
5077 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
5078 htab
->data_segment_phase
= data_segment_phase
;
5083 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs and
5084 deletes the obsolete bytes.
5085 Pass 1: Which cannot be disabled, handles code alignment directives. */
5088 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
5089 struct bfd_link_info
*info
,
5092 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
5093 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
5094 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
5095 Elf_Internal_Rela
*relocs
;
5098 bfd_vma max_alignment
, reserve_size
= 0;
5099 riscv_pcgp_relocs pcgp_relocs
;
5100 static asection
*first_section
= NULL
;
5104 if (bfd_link_relocatable (info
)
5106 || sec
->reloc_count
== 0
5107 || (sec
->flags
& SEC_RELOC
) == 0
5108 || (sec
->flags
& SEC_HAS_CONTENTS
) == 0
5109 || (info
->disable_target_specific_optimizations
5110 && info
->relax_pass
== 0)
5111 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
5112 and defined in ld/ldexp.h. */
5113 || *(htab
->data_segment_phase
) == 4)
5116 /* Record the first relax section, so that we can reset the
5117 max_alignment_for_gp for the repeated relax passes. */
5118 if (first_section
== NULL
)
5119 first_section
= sec
;
5120 else if (first_section
== sec
)
5121 htab
->max_alignment_for_gp
= -1;
5123 riscv_init_pcgp_relocs (&pcgp_relocs
);
5125 /* Read this BFD's relocs if we haven't done so already. */
5127 relocs
= data
->relocs
;
5128 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
5129 info
->keep_memory
)))
5132 /* Estimate the maximum alignment for all output sections once time
5133 should be enough. */
5134 max_alignment
= htab
->max_alignment
;
5135 if (max_alignment
== (bfd_vma
) -1)
5137 max_alignment
= _bfd_riscv_get_max_alignment (sec
, 0/* gp */);
5138 htab
->max_alignment
= max_alignment
;
5141 /* Examine and consider relaxing each reloc. */
5142 for (i
= 0; i
< sec
->reloc_count
; i
++)
5145 Elf_Internal_Rela
*rel
= relocs
+ i
;
5146 relax_func_t relax_func
;
5147 int type
= ELFNN_R_TYPE (rel
->r_info
);
5150 bool undefined_weak
= false;
5153 riscv_relax_delete_bytes
= NULL
;
5154 if (info
->relax_pass
== 0)
5156 if (type
== R_RISCV_CALL
5157 || type
== R_RISCV_CALL_PLT
)
5158 relax_func
= _bfd_riscv_relax_call
;
5159 else if (type
== R_RISCV_HI20
5160 || type
== R_RISCV_LO12_I
5161 || type
== R_RISCV_LO12_S
)
5162 relax_func
= _bfd_riscv_relax_lui
;
5163 else if (type
== R_RISCV_TPREL_HI20
5164 || type
== R_RISCV_TPREL_ADD
5165 || type
== R_RISCV_TPREL_LO12_I
5166 || type
== R_RISCV_TPREL_LO12_S
)
5167 relax_func
= _bfd_riscv_relax_tls_le
;
5168 else if (!bfd_link_pic (info
)
5169 && (type
== R_RISCV_PCREL_HI20
5170 || type
== R_RISCV_PCREL_LO12_I
5171 || type
== R_RISCV_PCREL_LO12_S
))
5172 relax_func
= _bfd_riscv_relax_pc
;
5175 riscv_relax_delete_bytes
= _riscv_relax_delete_piecewise
;
5177 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
5178 if (i
== sec
->reloc_count
- 1
5179 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
5180 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
5183 /* Skip over the R_RISCV_RELAX. */
5186 else if (info
->relax_pass
== 1 && type
== R_RISCV_ALIGN
)
5188 relax_func
= _bfd_riscv_relax_align
;
5189 riscv_relax_delete_bytes
= _riscv_relax_delete_immediate
;
5194 data
->relocs
= relocs
;
5196 /* Read this BFD's contents if we haven't done so already. */
5197 if (!data
->this_hdr
.contents
5198 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
5201 /* Read this BFD's symbols if we haven't done so already. */
5202 if (symtab_hdr
->sh_info
!= 0
5203 && !symtab_hdr
->contents
5204 && !(symtab_hdr
->contents
=
5205 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
5206 symtab_hdr
->sh_info
,
5207 0, NULL
, NULL
, NULL
)))
5210 /* Get the value of the symbol referred to by the reloc. */
5211 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
5213 /* A local symbol. */
5214 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
5215 + ELFNN_R_SYM (rel
->r_info
));
5216 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
5217 ? 0 : isym
->st_size
- rel
->r_addend
;
5219 /* Relocate against local STT_GNU_IFUNC symbol. we have created
5220 a fake global symbol entry for this, so deal with the local ifunc
5222 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
5225 if (isym
->st_shndx
== SHN_UNDEF
)
5226 sym_sec
= sec
, symval
= rel
->r_offset
;
5229 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
5230 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
5232 /* The purpose of this code is unknown. It breaks linker scripts
5233 for embedded development that place sections at address zero.
5234 This code is believed to be unnecessary. Disabling it but not
5235 yet removing it, in case something breaks. */
5236 if (sec_addr (sym_sec
) == 0)
5239 symval
= isym
->st_value
;
5241 symtype
= ELF_ST_TYPE (isym
->st_info
);
5246 struct elf_link_hash_entry
*h
;
5248 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
5249 h
= elf_sym_hashes (abfd
)[indx
];
5251 while (h
->root
.type
== bfd_link_hash_indirect
5252 || h
->root
.type
== bfd_link_hash_warning
)
5253 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5255 /* Disable the relaxation for ifunc. */
5256 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
5259 /* Maybe we should check UNDEFWEAK_NO_DYNAMIC_RELOC here? But that
5260 will break the undefweak relaxation testcases, so just make sure
5261 we won't do relaxations for linker_def symbols in short-term. */
5262 if (h
->root
.type
== bfd_link_hash_undefweak
5263 /* The linker_def symbol like __ehdr_start that may be undefweak
5264 for now, but will be guaranteed to be defined later. */
5265 && !h
->root
.linker_def
5266 && (relax_func
== _bfd_riscv_relax_lui
5267 || relax_func
== _bfd_riscv_relax_pc
))
5269 /* For the lui and auipc relaxations, since the symbol
5270 value of an undefined weak symbol is always be zero,
5271 we can optimize the patterns into a single LI/MV/ADDI
5274 Note that, creating shared libraries and pie output may
5275 break the rule above. Fortunately, since we do not relax
5276 pc relocs when creating shared libraries and pie output,
5277 and the absolute address access for R_RISCV_HI20 isn't
5278 allowed when "-fPIC" is set, the problem of creating shared
5279 libraries can not happen currently. Once we support the
5280 auipc relaxations when creating shared libraries, then we will
5281 need the more rigorous checking for this optimization. */
5282 undefined_weak
= true;
5285 /* This line has to match the check in riscv_elf_relocate_section
5286 in the R_RISCV_CALL[_PLT] case. */
5287 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
5289 sym_sec
= htab
->elf
.splt
;
5290 symval
= h
->plt
.offset
;
5292 else if (undefined_weak
)
5295 sym_sec
= bfd_und_section_ptr
;
5297 else if ((h
->root
.type
== bfd_link_hash_defined
5298 || h
->root
.type
== bfd_link_hash_defweak
)
5299 && h
->root
.u
.def
.section
!= NULL
5300 && h
->root
.u
.def
.section
->output_section
!= NULL
)
5302 symval
= h
->root
.u
.def
.value
;
5303 sym_sec
= h
->root
.u
.def
.section
;
5308 if (h
->type
!= STT_FUNC
)
5310 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
5314 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
5315 && (sym_sec
->flags
& SEC_MERGE
))
5317 /* At this stage in linking, no SEC_MERGE symbol has been
5318 adjusted, so all references to such symbols need to be
5319 passed through _bfd_merged_section_offset. (Later, in
5320 relocate_section, all SEC_MERGE symbols *except* for
5321 section symbols have been adjusted.)
5323 gas may reduce relocations against symbols in SEC_MERGE
5324 sections to a relocation against the section symbol when
5325 the original addend was zero. When the reloc is against
5326 a section symbol we should include the addend in the
5327 offset passed to _bfd_merged_section_offset, since the
5328 location of interest is the original symbol. On the
5329 other hand, an access to "sym+addend" where "sym" is not
5330 a section symbol should not include the addend; Such an
5331 access is presumed to be an offset from "sym"; The
5332 location of interest is just "sym". */
5333 if (symtype
== STT_SECTION
)
5334 symval
+= rel
->r_addend
;
5336 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
5337 elf_section_data (sym_sec
)->sec_info
,
5340 if (symtype
!= STT_SECTION
)
5341 symval
+= rel
->r_addend
;
5344 symval
+= rel
->r_addend
;
5346 symval
+= sec_addr (sym_sec
);
5348 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
5349 max_alignment
, reserve_size
, again
,
5350 &pcgp_relocs
, undefined_weak
))
5354 /* Resolve R_RISCV_DELETE relocations. */
5355 if (!riscv_relax_resolve_delete_relocs (abfd
, sec
, info
, relocs
))
5361 if (relocs
!= data
->relocs
)
5363 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
5369 # define PRSTATUS_SIZE 204
5370 # define PRSTATUS_OFFSET_PR_CURSIG 12
5371 # define PRSTATUS_OFFSET_PR_PID 24
5372 # define PRSTATUS_OFFSET_PR_REG 72
5373 # define ELF_GREGSET_T_SIZE 128
5374 # define PRPSINFO_SIZE 128
5375 # define PRPSINFO_OFFSET_PR_PID 16
5376 # define PRPSINFO_OFFSET_PR_FNAME 32
5377 # define PRPSINFO_OFFSET_PR_PSARGS 48
5378 # define PRPSINFO_PR_FNAME_LENGTH 16
5379 # define PRPSINFO_PR_PSARGS_LENGTH 80
5381 # define PRSTATUS_SIZE 376
5382 # define PRSTATUS_OFFSET_PR_CURSIG 12
5383 # define PRSTATUS_OFFSET_PR_PID 32
5384 # define PRSTATUS_OFFSET_PR_REG 112
5385 # define ELF_GREGSET_T_SIZE 256
5386 # define PRPSINFO_SIZE 136
5387 # define PRPSINFO_OFFSET_PR_PID 24
5388 # define PRPSINFO_OFFSET_PR_FNAME 40
5389 # define PRPSINFO_OFFSET_PR_PSARGS 56
5390 # define PRPSINFO_PR_FNAME_LENGTH 16
5391 # define PRPSINFO_PR_PSARGS_LENGTH 80
5394 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
5395 before the generic code in elf.c. By checking the compiler defines we
5396 only perform any action here if the generic code would otherwise not be
5397 able to help us. The intention is that bare metal core dumps (where the
5398 prstatus_t and/or prpsinfo_t might not be available) will use this code,
5399 while non bare metal tools will use the generic elf code. */
5402 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
5403 char *buf ATTRIBUTE_UNUSED
,
5404 int *bufsiz ATTRIBUTE_UNUSED
,
5405 int note_type ATTRIBUTE_UNUSED
, ...)
5412 #if !defined (HAVE_PRPSINFO_T)
5415 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5418 va_start (ap
, note_type
);
5419 memset (data
, 0, sizeof (data
));
5420 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5421 PRPSINFO_PR_FNAME_LENGTH
);
5422 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5424 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5425 -Wstringop-truncation:
5426 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5428 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5430 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5431 PRPSINFO_PR_PSARGS_LENGTH
);
5432 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5436 return elfcore_write_note (abfd
, buf
, bufsiz
,
5437 "CORE", note_type
, data
, sizeof (data
));
5439 #endif /* !HAVE_PRPSINFO_T */
5441 #if !defined (HAVE_PRSTATUS_T)
5444 char data
[PRSTATUS_SIZE
];
5450 va_start (ap
, note_type
);
5451 memset (data
, 0, sizeof(data
));
5452 pid
= va_arg (ap
, long);
5453 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5454 cursig
= va_arg (ap
, int);
5455 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5456 greg
= va_arg (ap
, const void *);
5457 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5458 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5460 return elfcore_write_note (abfd
, buf
, bufsiz
,
5461 "CORE", note_type
, data
, sizeof (data
));
5463 #endif /* !HAVE_PRSTATUS_T */
5467 /* Support for core dump NOTE sections. */
5470 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5472 switch (note
->descsz
)
5477 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5479 elf_tdata (abfd
)->core
->signal
5480 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5483 elf_tdata (abfd
)->core
->lwpid
5484 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5488 /* Make a ".reg/999" section. */
5489 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5490 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5494 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5496 switch (note
->descsz
)
5501 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5503 elf_tdata (abfd
)->core
->pid
5504 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5507 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5508 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5509 PRPSINFO_PR_FNAME_LENGTH
);
5512 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5513 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5514 PRPSINFO_PR_PSARGS_LENGTH
);
5518 /* Note that for some reason, a spurious space is tacked
5519 onto the end of the args in some (at least one anyway)
5520 implementations, so strip it off if it exists. */
5523 char *command
= elf_tdata (abfd
)->core
->command
;
5524 int n
= strlen (command
);
5526 if (0 < n
&& command
[n
- 1] == ' ')
5527 command
[n
- 1] = '\0';
5533 /* Set the right mach type. */
5536 riscv_elf_object_p (bfd
*abfd
)
5538 /* There are only two mach types in RISCV currently. */
5539 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5540 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5541 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5543 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5548 /* Determine whether an object attribute tag takes an integer, a
5552 riscv_elf_obj_attrs_arg_type (int tag
)
5554 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5557 /* Do not choose mapping symbols as a function name. */
5559 static bfd_size_type
5560 riscv_maybe_function_sym (const asymbol
*sym
,
5564 if (sym
->flags
& BSF_LOCAL
5565 && (riscv_elf_is_mapping_symbols (sym
->name
)
5566 || _bfd_elf_is_local_label_name (sec
->owner
, sym
->name
)))
5569 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5572 /* Treat the following cases as target special symbols, they are
5576 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5578 /* PR27584, local and empty symbols. Since they are usually
5579 generated for pcrel relocations. */
5580 return (!strcmp (sym
->name
, "")
5581 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5582 /* PR27916, mapping symbols. */
5583 || riscv_elf_is_mapping_symbols (sym
->name
));
5587 riscv_elf_additional_program_headers (bfd
*abfd
,
5588 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5592 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5593 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5600 riscv_elf_modify_segment_map (bfd
*abfd
,
5601 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5604 struct elf_segment_map
*m
, **pm
;
5607 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5609 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5612 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5613 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5615 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5620 m
= bfd_zalloc (abfd
, amt
);
5624 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5628 /* We want to put it after the PHDR and INTERP segments. */
5629 pm
= &elf_seg_map (abfd
);
5631 && ((*pm
)->p_type
== PT_PHDR
5632 || (*pm
)->p_type
== PT_INTERP
))
5643 /* Merge non-visibility st_other attributes. */
5646 riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
5647 unsigned int st_other
,
5648 bool definition ATTRIBUTE_UNUSED
,
5649 bool dynamic ATTRIBUTE_UNUSED
)
5651 unsigned int isym_sto
= st_other
& ~ELF_ST_VISIBILITY (-1);
5652 unsigned int h_sto
= h
->other
& ~ELF_ST_VISIBILITY (-1);
5654 if (isym_sto
== h_sto
)
5657 if (isym_sto
& ~STO_RISCV_VARIANT_CC
)
5658 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5659 h
->root
.root
.string
, isym_sto
);
5661 if (isym_sto
& STO_RISCV_VARIANT_CC
)
5662 h
->other
|= STO_RISCV_VARIANT_CC
;
5665 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5666 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5667 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5668 #define TARGET_BIG_NAME "elfNN-bigriscv"
5670 #define elf_backend_reloc_type_class riscv_reloc_type_class
5672 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5673 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5674 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5675 #define bfd_elfNN_bfd_merge_private_bfd_data \
5676 _bfd_riscv_elf_merge_private_bfd_data
5677 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5679 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5680 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5681 #define elf_backend_check_relocs riscv_elf_check_relocs
5682 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5683 #define elf_backend_late_size_sections riscv_elf_late_size_sections
5684 #define elf_backend_relocate_section riscv_elf_relocate_section
5685 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5686 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5687 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5688 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5689 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5690 #define elf_backend_object_p riscv_elf_object_p
5691 #define elf_backend_write_core_note riscv_write_core_note
5692 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5693 #define elf_info_to_howto_rel NULL
5694 #define elf_info_to_howto riscv_info_to_howto_rela
5695 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5696 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5697 #define elf_backend_additional_program_headers \
5698 riscv_elf_additional_program_headers
5699 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5700 #define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
5702 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5704 #define elf_backend_can_gc_sections 1
5705 #define elf_backend_can_refcount 1
5706 #define elf_backend_want_got_plt 1
5707 #define elf_backend_plt_readonly 1
5708 #define elf_backend_plt_alignment 4
5709 #define elf_backend_want_plt_sym 1
5710 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5711 #define elf_backend_want_dynrelro 1
5712 #define elf_backend_rela_normal 1
5713 #define elf_backend_default_execstack 0
5715 #undef elf_backend_obj_attrs_vendor
5716 #define elf_backend_obj_attrs_vendor "riscv"
5717 #undef elf_backend_obj_attrs_arg_type
5718 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5719 #undef elf_backend_obj_attrs_section_type
5720 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5721 #undef elf_backend_obj_attrs_section
5722 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5723 #define elf_backend_obj_attrs_handle_unknown riscv_elf_obj_attrs_handle_unknown
5725 #include "elfNN-target.h"