1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
41 /* 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; \
133 /* Internal relocations used exclusively by the relaxation pass. */
134 #define R_RISCV_DELETE (R_RISCV_max + 1)
138 #define MINUS_ONE ((bfd_vma)0 - 1)
140 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
142 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
144 /* The name of the dynamic interpreter. This is put in the .interp
147 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
148 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
150 #define ELF_ARCH bfd_arch_riscv
151 #define ELF_TARGET_ID RISCV_ELF_DATA
152 #define ELF_MACHINE_CODE EM_RISCV
153 #define ELF_MAXPAGESIZE 0x1000
154 #define ELF_COMMONPAGESIZE 0x1000
156 #define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
158 /* RISC-V ELF linker hash entry. */
160 struct riscv_elf_link_hash_entry
162 struct elf_link_hash_entry elf
;
164 #define GOT_UNKNOWN 0
172 #define riscv_elf_hash_entry(ent) \
173 ((struct riscv_elf_link_hash_entry *) (ent))
175 struct _bfd_riscv_elf_obj_tdata
177 struct elf_obj_tdata root
;
179 /* tls_type for each local got entry. */
180 char *local_got_tls_type
;
183 #define _bfd_riscv_elf_tdata(abfd) \
184 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
186 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
187 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
189 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
190 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
191 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
193 #define is_riscv_elf(bfd) \
194 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
195 && elf_tdata (bfd) != NULL \
196 && elf_object_id (bfd) == RISCV_ELF_DATA)
199 elfNN_riscv_mkobject (bfd
*abfd
)
201 return bfd_elf_allocate_object (abfd
,
202 sizeof (struct _bfd_riscv_elf_obj_tdata
),
206 #include "elf/common.h"
207 #include "elf/internal.h"
209 struct riscv_elf_link_hash_table
211 struct elf_link_hash_table elf
;
213 /* Various options and other info passed from the linker. */
214 struct riscv_elf_params
*params
;
216 /* Short-cuts to get to dynamic linker sections. */
219 /* The max alignment of output sections. */
220 bfd_vma max_alignment
;
222 /* The max alignment of output sections in [gp-2K, gp+2K) range. */
223 bfd_vma max_alignment_for_gp
;
225 /* Used by local STT_GNU_IFUNC symbols. */
226 htab_t loc_hash_table
;
227 void * loc_hash_memory
;
229 /* The index of the last unused .rel.iplt slot. */
230 bfd_vma last_iplt_index
;
232 /* The data segment phase, don't relax the section
233 when it is exp_seg_relro_adjust. */
234 int *data_segment_phase
;
236 /* Relocations for variant CC symbols may be present. */
240 /* Instruction access functions. */
241 #define riscv_get_insn(bits, ptr) \
242 ((bits) == 16 ? bfd_getl16 (ptr) \
243 : (bits) == 32 ? bfd_getl32 (ptr) \
244 : (bits) == 64 ? bfd_getl64 (ptr) \
245 : (abort (), (bfd_vma) - 1))
246 #define riscv_put_insn(bits, val, ptr) \
247 ((bits) == 16 ? bfd_putl16 (val, ptr) \
248 : (bits) == 32 ? bfd_putl32 (val, ptr) \
249 : (bits) == 64 ? bfd_putl64 (val, ptr) \
250 : (abort (), (void) 0))
252 /* Get the RISC-V ELF linker hash table from a link_info structure. */
253 #define riscv_elf_hash_table(p) \
254 ((is_elf_hash_table ((p)->hash) \
255 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
256 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
259 riscv_elfNN_set_options (struct bfd_link_info
*link_info
,
260 struct riscv_elf_params
*params
)
262 riscv_elf_hash_table (link_info
)->params
= params
;
266 riscv_info_to_howto_rela (bfd
*abfd
,
268 Elf_Internal_Rela
*dst
)
270 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
271 return cache_ptr
->howto
!= NULL
;
275 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
277 const struct elf_backend_data
*bed
;
280 bed
= get_elf_backend_data (abfd
);
281 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
282 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
285 /* Return true if a relocation is modifying an instruction. */
288 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
290 /* Heuristic: A multibyte destination with a nontrivial mask
292 return (howto
->bitsize
> 8
293 && howto
->dst_mask
!= 0
294 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
295 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
299 #define PLT_HEADER_INSNS 8
300 #define PLT_ENTRY_INSNS 4
301 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
302 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
303 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
304 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
305 the other is used for link map. Other targets also reserve one more
306 entry used for runtime profile? */
307 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
309 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
312 # define MATCH_LREG MATCH_LW
314 # define MATCH_LREG MATCH_LD
317 /* Generate a PLT header. */
320 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
323 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
324 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
326 /* RVE has no t3 register, so this won't work, and is not supported. */
327 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
329 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
334 /* auipc t2, %hi(.got.plt)
335 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
336 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
337 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
338 addi t0, t2, %lo(.got.plt) # &.got.plt
339 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
340 l[w|d] t0, PTRSIZE(t0) # link map
343 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
344 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
345 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
346 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
347 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
348 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
349 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
350 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
355 /* Generate a PLT entry. */
358 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
361 /* RVE has no t3 register, so this won't work, and is not supported. */
362 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
364 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
369 /* auipc t3, %hi(.got.plt entry)
370 l[w|d] t3, %lo(.got.plt entry)(t3)
374 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
375 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
376 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
377 entry
[3] = RISCV_NOP
;
382 /* Create an entry in an RISC-V ELF linker hash table. */
384 static struct bfd_hash_entry
*
385 link_hash_newfunc (struct bfd_hash_entry
*entry
,
386 struct bfd_hash_table
*table
, const char *string
)
388 /* Allocate the structure if it has not already been allocated by a
393 bfd_hash_allocate (table
,
394 sizeof (struct riscv_elf_link_hash_entry
));
399 /* Call the allocation method of the superclass. */
400 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
403 struct riscv_elf_link_hash_entry
*eh
;
405 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
406 eh
->tls_type
= GOT_UNKNOWN
;
412 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
413 for local symbol so that we can handle local STT_GNU_IFUNC symbols
414 as global symbol. We reuse indx and dynstr_index for local symbol
415 hash since they aren't used by global symbols in this backend. */
418 riscv_elf_local_htab_hash (const void *ptr
)
420 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
421 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
424 /* Compare local hash entries. */
427 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
429 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
430 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
432 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
435 /* Find and/or create a hash entry for local symbol. */
437 static struct elf_link_hash_entry
*
438 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
439 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
442 struct riscv_elf_link_hash_entry eh
, *ret
;
443 asection
*sec
= abfd
->sections
;
444 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
445 ELFNN_R_SYM (rel
->r_info
));
448 eh
.elf
.indx
= sec
->id
;
449 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
450 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
451 create
? INSERT
: NO_INSERT
);
458 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
462 ret
= (struct riscv_elf_link_hash_entry
*)
463 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
464 sizeof (struct riscv_elf_link_hash_entry
));
467 memset (ret
, 0, sizeof (*ret
));
468 ret
->elf
.indx
= sec
->id
;
469 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
470 ret
->elf
.dynindx
= -1;
476 /* Destroy a RISC-V elf linker hash table. */
479 riscv_elf_link_hash_table_free (bfd
*obfd
)
481 struct riscv_elf_link_hash_table
*ret
482 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
484 if (ret
->loc_hash_table
)
485 htab_delete (ret
->loc_hash_table
);
486 if (ret
->loc_hash_memory
)
487 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
489 _bfd_elf_link_hash_table_free (obfd
);
492 /* Create a RISC-V ELF linker hash table. */
494 static struct bfd_link_hash_table
*
495 riscv_elf_link_hash_table_create (bfd
*abfd
)
497 struct riscv_elf_link_hash_table
*ret
;
498 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
500 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
504 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
505 sizeof (struct riscv_elf_link_hash_entry
),
512 ret
->max_alignment
= (bfd_vma
) -1;
513 ret
->max_alignment_for_gp
= (bfd_vma
) -1;
515 /* Create hash table for local ifunc. */
516 ret
->loc_hash_table
= htab_try_create (1024,
517 riscv_elf_local_htab_hash
,
518 riscv_elf_local_htab_eq
,
520 ret
->loc_hash_memory
= objalloc_create ();
521 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
523 riscv_elf_link_hash_table_free (abfd
);
526 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
528 return &ret
->elf
.root
;
531 /* Create the .got section. */
534 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
538 struct elf_link_hash_entry
*h
;
539 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
540 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
542 /* This function may be called more than once. */
543 if (htab
->sgot
!= NULL
)
546 flags
= bed
->dynamic_sec_flags
;
548 s
= bfd_make_section_anyway_with_flags (abfd
,
549 (bed
->rela_plts_and_copies_p
550 ? ".rela.got" : ".rel.got"),
551 (bed
->dynamic_sec_flags
554 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
558 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
560 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
564 /* The first bit of the global offset table is the header. */
565 s
->size
+= bed
->got_header_size
;
567 if (bed
->want_got_plt
)
569 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
571 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
575 /* Reserve room for the header. */
576 s
->size
+= GOTPLT_HEADER_SIZE
;
579 if (bed
->want_got_sym
)
581 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
582 section. We don't do this in the linker script because we don't want
583 to define the symbol if we are not creating a global offset
585 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
586 "_GLOBAL_OFFSET_TABLE_");
587 elf_hash_table (info
)->hgot
= h
;
595 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
596 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
600 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
601 struct bfd_link_info
*info
)
603 struct riscv_elf_link_hash_table
*htab
;
605 htab
= riscv_elf_hash_table (info
);
606 BFD_ASSERT (htab
!= NULL
);
608 if (!riscv_elf_create_got_section (dynobj
, info
))
611 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
614 if (!bfd_link_pic (info
))
616 /* Technically, this section doesn't have contents. It is used as the
617 target of TLS copy relocs, to copy TLS data from shared libraries into
618 the executable. However, if we don't mark it as loadable, then it
619 matches the IS_TBSS test in ldlang.c, and there is no run-time address
620 space allocated for it even though it has SEC_ALLOC. That test is
621 correct for .tbss, but not correct for this section. There is also
622 a second problem that having a section with no contents can only work
623 if it comes after all sections with contents in the same segment,
624 but the linker script does not guarantee that. This is just mixed in
625 with other .tdata.* sections. We can fix both problems by lying and
626 saying that there are contents. This section is expected to be small
627 so this should not cause a significant extra program startup cost. */
629 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
630 (SEC_ALLOC
| SEC_THREAD_LOCAL
631 | SEC_LOAD
| SEC_DATA
633 | SEC_LINKER_CREATED
));
636 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
637 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
643 /* Copy the extra info we tack onto an elf_link_hash_entry. */
646 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
647 struct elf_link_hash_entry
*dir
,
648 struct elf_link_hash_entry
*ind
)
650 struct riscv_elf_link_hash_entry
*edir
, *eind
;
652 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
653 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
655 if (ind
->root
.type
== bfd_link_hash_indirect
656 && dir
->got
.refcount
<= 0)
658 edir
->tls_type
= eind
->tls_type
;
659 eind
->tls_type
= GOT_UNKNOWN
;
661 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
665 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
666 unsigned long symndx
, char tls_type
)
668 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
670 *new_tls_type
|= tls_type
;
671 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
673 (*_bfd_error_handler
)
674 (_("%pB: `%s' accessed both as normal and thread local symbol"),
675 abfd
, h
? h
->root
.root
.string
: "<local>");
682 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
683 struct elf_link_hash_entry
*h
, long symndx
)
685 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
686 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
688 if (htab
->elf
.sgot
== NULL
)
690 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
696 h
->got
.refcount
+= 1;
700 /* This is a global offset table entry for a local symbol. */
701 if (elf_local_got_refcounts (abfd
) == NULL
)
703 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
704 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
706 _bfd_riscv_elf_local_got_tls_type (abfd
)
707 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
709 elf_local_got_refcounts (abfd
) [symndx
] += 1;
715 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
717 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
719 /* We propably can improve the information to tell users that they
720 should be recompile the code with -fPIC or -fPIE, just like what
722 (*_bfd_error_handler
)
723 (_("%pB: relocation %s against `%s' can not be used when making a shared "
724 "object; recompile with -fPIC"),
725 abfd
, r
? r
->name
: _("<unknown>"),
726 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
727 bfd_set_error (bfd_error_bad_value
);
731 /* Look through the relocs for a section during the first phase, and
732 allocate space in the global offset table or procedure linkage
736 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
737 asection
*sec
, const Elf_Internal_Rela
*relocs
)
739 struct riscv_elf_link_hash_table
*htab
;
740 Elf_Internal_Shdr
*symtab_hdr
;
741 struct elf_link_hash_entry
**sym_hashes
;
742 const Elf_Internal_Rela
*rel
;
743 asection
*sreloc
= NULL
;
745 if (bfd_link_relocatable (info
))
748 htab
= riscv_elf_hash_table (info
);
749 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
750 sym_hashes
= elf_sym_hashes (abfd
);
752 if (htab
->elf
.dynobj
== NULL
)
753 htab
->elf
.dynobj
= abfd
;
755 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
758 unsigned int r_symndx
;
759 struct elf_link_hash_entry
*h
;
760 bool is_abs_symbol
= false;
762 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
763 r_type
= ELFNN_R_TYPE (rel
->r_info
);
765 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
767 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
772 if (r_symndx
< symtab_hdr
->sh_info
)
774 /* A local symbol. */
775 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
780 is_abs_symbol
= isym
->st_shndx
== SHN_ABS
? true : false;
782 /* Check relocation against local STT_GNU_IFUNC symbol. */
783 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
785 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
789 /* Fake STT_GNU_IFUNC global symbol. */
790 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
792 h
->type
= STT_GNU_IFUNC
;
796 h
->root
.type
= bfd_link_hash_defined
;
803 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
804 while (h
->root
.type
== bfd_link_hash_indirect
805 || h
->root
.type
== bfd_link_hash_warning
)
806 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
808 is_abs_symbol
= bfd_is_abs_symbol (&h
->root
) ? true : false;
818 case R_RISCV_CALL_PLT
:
820 case R_RISCV_GOT_HI20
:
821 case R_RISCV_PCREL_HI20
:
822 /* Create the ifunc sections, iplt and ipltgot, for static
824 if (h
->type
== STT_GNU_IFUNC
825 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
833 /* It is referenced by a non-shared object. */
839 case R_RISCV_TLS_GD_HI20
:
840 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
841 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
845 case R_RISCV_TLS_GOT_HI20
:
846 if (bfd_link_dll (info
))
847 info
->flags
|= DF_STATIC_TLS
;
848 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
849 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
853 case R_RISCV_GOT_HI20
:
854 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
855 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
860 case R_RISCV_CALL_PLT
:
861 /* These symbol requires a procedure linkage table entry.
862 We actually build the entry in adjust_dynamic_symbol,
863 because these might be a case of linking PIC code without
864 linking in any dynamic objects, in which case we don't
865 need to generate a procedure linkage table after all. */
867 /* If it is a local symbol, then we resolve it directly
868 without creating a PLT entry. */
873 h
->plt
.refcount
+= 1;
876 case R_RISCV_PCREL_HI20
:
878 && h
->type
== STT_GNU_IFUNC
)
881 h
->pointer_equality_needed
= 1;
883 /* We don't use the PCREL_HI20 in the data section,
884 so we always need the plt when it refers to
886 h
->plt
.refcount
+= 1;
889 /* The non-preemptible absolute symbol shouldn't be referneced with
890 pc-relative relocation when generating shared object. However,
891 PCREL_HI20/LO12 relocs are always bind locally when generating
892 shared object, so all absolute symbol referenced need to be
893 disallowed, except they are defined in linker script.
895 Maybe we should add this check for all pc-relative relocations,
896 please see pr28789 and pr25749 for details. */
897 if (bfd_link_pic (info
)
898 /* (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)) */
901 if (h
!= NULL
&& (h
)->root
.ldscript_def
)
902 /* Disallow the absolute symbol defined in linker script here
903 will cause the glibc-linux toolchain build failed, so regard
904 them as pc-relative symbols, just like what x86 did. */
909 if (h
->root
.root
.string
)
910 name
= h
->root
.root
.string
;
913 Elf_Internal_Sym
*sym
;
914 sym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
, abfd
,
916 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, sym
, NULL
);
919 reloc_howto_type
*r_t
=
920 riscv_elf_rtype_to_howto (abfd
, r_type
);
922 (_("%pB: relocation %s against absolute symbol `%s' can "
923 "not be used when making a shared object"),
924 abfd
, r_t
? r_t
->name
: _("<unknown>"), name
);
925 bfd_set_error (bfd_error_bad_value
);
933 case R_RISCV_RVC_BRANCH
:
934 case R_RISCV_RVC_JUMP
:
935 /* In shared libraries and pie, these relocs are known
937 if (bfd_link_pic (info
))
941 case R_RISCV_TPREL_HI20
:
942 /* This is not allowed in the pic, but okay in pie. */
943 if (!bfd_link_executable (info
))
944 return bad_static_reloc (abfd
, r_type
, h
);
946 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
950 if (bfd_link_pic (info
))
951 return bad_static_reloc (abfd
, r_type
, h
);
956 && bfd_link_pic (info
)
957 && (sec
->flags
& SEC_ALLOC
) != 0)
962 reloc_howto_type
*r_t
= riscv_elf_rtype_to_howto (abfd
, r_type
);
964 (_("%pB: relocation %s against non-absolute symbol `%s' can "
965 "not be used in RVNN when making a shared object"),
966 abfd
, r_t
? r_t
->name
: _("<unknown>"),
967 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
968 bfd_set_error (bfd_error_bad_value
);
974 case R_RISCV_JUMP_SLOT
:
975 case R_RISCV_RELATIVE
:
982 && (!bfd_link_pic (info
)
983 || h
->type
== STT_GNU_IFUNC
))
985 /* This reloc might not bind locally. */
987 h
->pointer_equality_needed
= 1;
990 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
992 /* We may need a .plt entry if the symbol is a function
993 defined in a shared lib or is a function referenced
994 from the code or read-only section. */
995 h
->plt
.refcount
+= 1;
999 reloc_howto_type
*r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
1000 if (RISCV_NEED_DYNAMIC_RELOC (r
->pc_relative
, info
, h
, sec
))
1002 struct elf_dyn_relocs
*p
;
1003 struct elf_dyn_relocs
**head
;
1005 /* When creating a shared object, we must copy these
1006 relocs into the output file. We create a reloc
1007 section in dynobj and make room for the reloc. */
1010 sreloc
= _bfd_elf_make_dynamic_reloc_section
1011 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
1012 abfd
, /*rela?*/ true);
1018 /* If this is a global symbol, we count the number of
1019 relocations we need for this symbol. */
1021 head
= &h
->dyn_relocs
;
1024 /* Track dynamic relocs needed for local syms too.
1025 We really need local syms available to do this
1030 Elf_Internal_Sym
*isym
;
1032 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
1037 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1041 vpp
= &elf_section_data (s
)->local_dynrel
;
1042 head
= (struct elf_dyn_relocs
**) vpp
;
1046 if (p
== NULL
|| p
->sec
!= sec
)
1048 size_t amt
= sizeof *p
;
1049 p
= ((struct elf_dyn_relocs
*)
1050 bfd_alloc (htab
->elf
.dynobj
, amt
));
1061 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
1074 /* Adjust a symbol defined by a dynamic object and referenced by a
1075 regular object. The current definition is in some section of the
1076 dynamic object, but we're not including those sections. We have to
1077 change the definition to something the rest of the link can
1081 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1082 struct elf_link_hash_entry
*h
)
1084 struct riscv_elf_link_hash_table
*htab
;
1085 struct riscv_elf_link_hash_entry
* eh
;
1089 htab
= riscv_elf_hash_table (info
);
1090 BFD_ASSERT (htab
!= NULL
);
1092 dynobj
= htab
->elf
.dynobj
;
1094 /* Make sure we know what is going on here. */
1095 BFD_ASSERT (dynobj
!= NULL
1097 || h
->type
== STT_GNU_IFUNC
1101 && !h
->def_regular
)));
1103 /* If this is a function, put it in the procedure linkage table. We
1104 will fill in the contents of the procedure linkage table later
1105 (although we could actually do it here). */
1106 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1108 if (h
->plt
.refcount
<= 0
1109 || (h
->type
!= STT_GNU_IFUNC
1110 && (SYMBOL_CALLS_LOCAL (info
, h
)
1111 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1112 && h
->root
.type
== bfd_link_hash_undefweak
))))
1114 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1115 input file, but the symbol was never referred to by a dynamic
1116 object, or if all references were garbage collected. In such
1117 a case, we don't actually need to build a PLT entry. */
1118 h
->plt
.offset
= (bfd_vma
) -1;
1125 h
->plt
.offset
= (bfd_vma
) -1;
1127 /* If this is a weak symbol, and there is a real definition, the
1128 processor independent code will have arranged for us to see the
1129 real definition first, and we can just use the same value. */
1130 if (h
->is_weakalias
)
1132 struct elf_link_hash_entry
*def
= weakdef (h
);
1133 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1134 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1135 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1139 /* This is a reference to a symbol defined by a dynamic object which
1140 is not a function. */
1142 /* If we are creating a shared library, we must presume that the
1143 only references to the symbol are via the global offset table.
1144 For such cases we need not do anything here; the relocations will
1145 be handled correctly by relocate_section. */
1146 if (bfd_link_pic (info
))
1149 /* If there are no references to this symbol that do not use the
1150 GOT, we don't need to generate a copy reloc. */
1151 if (!h
->non_got_ref
)
1154 /* If -z nocopyreloc was given, we won't generate them either. */
1155 if (info
->nocopyreloc
)
1161 /* If we don't find any dynamic relocs in read-only sections, then
1162 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1163 if (!_bfd_elf_readonly_dynrelocs (h
))
1169 /* We must allocate the symbol in our .dynbss section, which will
1170 become part of the .bss section of the executable. There will be
1171 an entry for this symbol in the .dynsym section. The dynamic
1172 object will contain position independent code, so all references
1173 from the dynamic object to this symbol will go through the global
1174 offset table. The dynamic linker will use the .dynsym entry to
1175 determine the address it must put in the global offset table, so
1176 both the dynamic object and the regular object will refer to the
1177 same memory location for the variable. */
1179 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1180 to copy the initial value out of the dynamic object and into the
1181 runtime process image. We need to remember the offset into the
1182 .rel.bss section we are going to use. */
1183 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1184 if (eh
->tls_type
& ~GOT_NORMAL
)
1186 s
= htab
->sdyntdata
;
1187 srel
= htab
->elf
.srelbss
;
1189 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1191 s
= htab
->elf
.sdynrelro
;
1192 srel
= htab
->elf
.sreldynrelro
;
1196 s
= htab
->elf
.sdynbss
;
1197 srel
= htab
->elf
.srelbss
;
1199 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1201 srel
->size
+= sizeof (ElfNN_External_Rela
);
1205 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1208 /* Allocate space in .plt, .got and associated reloc sections for
1212 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1214 struct bfd_link_info
*info
;
1215 struct riscv_elf_link_hash_table
*htab
;
1216 struct elf_dyn_relocs
*p
;
1218 if (h
->root
.type
== bfd_link_hash_indirect
)
1221 info
= (struct bfd_link_info
*) inf
;
1222 htab
= riscv_elf_hash_table (info
);
1223 BFD_ASSERT (htab
!= NULL
);
1225 /* When we are generating pde, make sure gp symbol is output as a
1226 dynamic symbol. Then ld.so can set the gp register earlier, before
1227 resolving the ifunc. */
1228 if (!bfd_link_pic (info
)
1229 && htab
->elf
.dynamic_sections_created
1230 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1231 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1234 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1235 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1236 if they are defined and referenced in a non-shared object. */
1237 if (h
->type
== STT_GNU_IFUNC
1240 else if (htab
->elf
.dynamic_sections_created
1241 && h
->plt
.refcount
> 0)
1243 /* Make sure this symbol is output as a dynamic symbol.
1244 Undefined weak syms won't yet be marked as dynamic. */
1245 if (h
->dynindx
== -1
1246 && !h
->forced_local
)
1248 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1252 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1254 asection
*s
= htab
->elf
.splt
;
1257 s
->size
= PLT_HEADER_SIZE
;
1259 h
->plt
.offset
= s
->size
;
1261 /* Make room for this entry. */
1262 s
->size
+= PLT_ENTRY_SIZE
;
1264 /* We also need to make an entry in the .got.plt section. */
1265 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1267 /* We also need to make an entry in the .rela.plt section. */
1268 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1270 /* If this symbol is not defined in a regular file, and we are
1271 not generating a shared library, then set the symbol to this
1272 location in the .plt. This is required to make function
1273 pointers compare as equal between the normal executable and
1274 the shared library. */
1275 if (! bfd_link_pic (info
)
1278 h
->root
.u
.def
.section
= s
;
1279 h
->root
.u
.def
.value
= h
->plt
.offset
;
1282 /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1283 variant_cc flag of riscv_elf_link_hash_table. */
1284 if (h
->other
& STO_RISCV_VARIANT_CC
)
1285 htab
->variant_cc
= 1;
1289 h
->plt
.offset
= (bfd_vma
) -1;
1295 h
->plt
.offset
= (bfd_vma
) -1;
1299 if (h
->got
.refcount
> 0)
1303 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1305 /* Make sure this symbol is output as a dynamic symbol.
1306 Undefined weak syms won't yet be marked as dynamic. */
1307 if (h
->dynindx
== -1
1308 && !h
->forced_local
)
1310 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1315 h
->got
.offset
= s
->size
;
1316 dyn
= htab
->elf
.dynamic_sections_created
;
1317 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1320 bool need_reloc
= false;
1321 RISCV_TLS_GD_IE_NEED_DYN_RELOC(info
, dyn
, h
, indx
, need_reloc
);
1323 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1324 if (tls_type
& GOT_TLS_GD
)
1326 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1328 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1331 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1332 if (tls_type
& GOT_TLS_IE
)
1334 s
->size
+= RISCV_ELF_WORD_BYTES
;
1336 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1341 s
->size
+= RISCV_ELF_WORD_BYTES
;
1342 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1343 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1344 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1348 h
->got
.offset
= (bfd_vma
) -1;
1350 if (h
->dyn_relocs
== NULL
)
1353 /* In the shared -Bsymbolic case, discard space allocated for
1354 dynamic pc-relative relocs against symbols which turn out to be
1355 defined in regular objects. For the normal shared case, discard
1356 space for pc-relative relocs that have become local due to symbol
1357 visibility changes. */
1359 if (bfd_link_pic (info
))
1361 if (SYMBOL_CALLS_LOCAL (info
, h
))
1363 struct elf_dyn_relocs
**pp
;
1365 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1367 p
->count
-= p
->pc_count
;
1376 /* Also discard relocs on undefined weak syms with non-default
1378 if (h
->dyn_relocs
!= NULL
1379 && h
->root
.type
== bfd_link_hash_undefweak
)
1381 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1382 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1383 h
->dyn_relocs
= NULL
;
1385 /* Make sure undefined weak symbols are output as a dynamic
1387 else if (h
->dynindx
== -1
1388 && !h
->forced_local
)
1390 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1397 /* For the non-shared case, discard space for relocs against
1398 symbols which turn out to need copy relocs or are not
1404 || (htab
->elf
.dynamic_sections_created
1405 && (h
->root
.type
== bfd_link_hash_undefweak
1406 || h
->root
.type
== bfd_link_hash_undefined
))))
1408 /* Make sure this symbol is output as a dynamic symbol.
1409 Undefined weak syms won't yet be marked as dynamic. */
1410 if (h
->dynindx
== -1
1411 && !h
->forced_local
)
1413 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1417 /* If that succeeded, we know we'll be keeping all the
1419 if (h
->dynindx
!= -1)
1423 h
->dyn_relocs
= NULL
;
1428 /* Finally, allocate space. */
1429 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1431 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1432 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1438 /* Allocate space in .plt, .got and associated reloc sections for
1439 ifunc dynamic relocs. */
1442 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1445 struct bfd_link_info
*info
;
1447 if (h
->root
.type
== bfd_link_hash_indirect
)
1450 if (h
->root
.type
== bfd_link_hash_warning
)
1451 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1453 info
= (struct bfd_link_info
*) inf
;
1455 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1456 here if it is defined and referenced in a non-shared object. */
1457 if (h
->type
== STT_GNU_IFUNC
1459 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1468 /* Allocate space in .plt, .got and associated reloc sections for
1469 local ifunc dynamic relocs. */
1472 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1474 struct elf_link_hash_entry
*h
1475 = (struct elf_link_hash_entry
*) *slot
;
1477 if (h
->type
!= STT_GNU_IFUNC
1481 || h
->root
.type
!= bfd_link_hash_defined
)
1484 return allocate_ifunc_dynrelocs (h
, inf
);
1488 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1490 struct riscv_elf_link_hash_table
*htab
;
1495 htab
= riscv_elf_hash_table (info
);
1496 BFD_ASSERT (htab
!= NULL
);
1497 dynobj
= htab
->elf
.dynobj
;
1498 BFD_ASSERT (dynobj
!= NULL
);
1500 if (elf_hash_table (info
)->dynamic_sections_created
)
1502 /* Set the contents of the .interp section to the interpreter. */
1503 if (bfd_link_executable (info
) && !info
->nointerp
)
1505 s
= bfd_get_linker_section (dynobj
, ".interp");
1506 BFD_ASSERT (s
!= NULL
);
1507 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1508 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1512 /* Set up .got offsets for local syms, and space for local dynamic
1514 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1516 bfd_signed_vma
*local_got
;
1517 bfd_signed_vma
*end_local_got
;
1518 char *local_tls_type
;
1519 bfd_size_type locsymcount
;
1520 Elf_Internal_Shdr
*symtab_hdr
;
1523 if (! is_riscv_elf (ibfd
))
1526 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1528 struct elf_dyn_relocs
*p
;
1530 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1532 if (!bfd_is_abs_section (p
->sec
)
1533 && bfd_is_abs_section (p
->sec
->output_section
))
1535 /* Input section has been discarded, either because
1536 it is a copy of a linkonce section or due to
1537 linker script /DISCARD/, so we'll be discarding
1540 else if (p
->count
!= 0)
1542 srel
= elf_section_data (p
->sec
)->sreloc
;
1543 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1544 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1545 info
->flags
|= DF_TEXTREL
;
1550 local_got
= elf_local_got_refcounts (ibfd
);
1554 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1555 locsymcount
= symtab_hdr
->sh_info
;
1556 end_local_got
= local_got
+ locsymcount
;
1557 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1559 srel
= htab
->elf
.srelgot
;
1560 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1564 *local_got
= s
->size
;
1565 s
->size
+= RISCV_ELF_WORD_BYTES
;
1566 if (*local_tls_type
& GOT_TLS_GD
)
1567 s
->size
+= RISCV_ELF_WORD_BYTES
;
1568 if (bfd_link_pic (info
)
1569 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1570 srel
->size
+= sizeof (ElfNN_External_Rela
);
1573 *local_got
= (bfd_vma
) -1;
1577 /* Allocate .plt and .got entries and space dynamic relocs for
1579 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1581 /* Allocate .plt and .got entries and space dynamic relocs for
1582 global ifunc symbols. */
1583 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1585 /* Allocate .plt and .got entries and space dynamic relocs for
1586 local ifunc symbols. */
1587 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1589 /* Used to resolve the dynamic relocs overwite problems when
1590 generating static executable. */
1591 if (htab
->elf
.irelplt
)
1592 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1594 if (htab
->elf
.sgotplt
)
1596 struct elf_link_hash_entry
*got
;
1597 got
= elf_link_hash_lookup (elf_hash_table (info
),
1598 "_GLOBAL_OFFSET_TABLE_",
1599 false, false, false);
1601 /* Don't allocate .got.plt section if there are no GOT nor PLT
1602 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1604 || !got
->ref_regular_nonweak
)
1605 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1606 && (htab
->elf
.splt
== NULL
1607 || htab
->elf
.splt
->size
== 0)
1608 && (htab
->elf
.sgot
== NULL
1609 || (htab
->elf
.sgot
->size
1610 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1611 htab
->elf
.sgotplt
->size
= 0;
1614 /* The check_relocs and adjust_dynamic_symbol entry points have
1615 determined the sizes of the various dynamic sections. Allocate
1617 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1619 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1622 if (s
== htab
->elf
.splt
1623 || s
== htab
->elf
.sgot
1624 || s
== htab
->elf
.sgotplt
1625 || s
== htab
->elf
.iplt
1626 || s
== htab
->elf
.igotplt
1627 || s
== htab
->elf
.sdynbss
1628 || s
== htab
->elf
.sdynrelro
1629 || s
== htab
->sdyntdata
)
1631 /* Strip this section if we don't need it; see the
1634 else if (startswith (s
->name
, ".rela"))
1638 /* We use the reloc_count field as a counter if we need
1639 to copy relocs into the output file. */
1645 /* It's not one of our sections. */
1651 /* If we don't need this section, strip it from the
1652 output file. This is mostly to handle .rela.bss and
1653 .rela.plt. We must create both sections in
1654 create_dynamic_sections, because they must be created
1655 before the linker maps input sections to output
1656 sections. The linker does that before
1657 adjust_dynamic_symbol is called, and it is that
1658 function which decides whether anything needs to go
1659 into these sections. */
1660 s
->flags
|= SEC_EXCLUDE
;
1664 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1667 /* Allocate memory for the section contents. Zero the memory
1668 for the benefit of .rela.plt, which has 4 unused entries
1669 at the beginning, and we don't want garbage. */
1670 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1671 if (s
->contents
== NULL
)
1675 /* Add dynamic entries. */
1676 if (elf_hash_table (info
)->dynamic_sections_created
)
1678 if (!_bfd_elf_add_dynamic_tags (output_bfd
, info
, true))
1681 if (htab
->variant_cc
1682 && !_bfd_elf_add_dynamic_entry (info
, DT_RISCV_VARIANT_CC
, 0))
1690 #define DTP_OFFSET 0x800
1692 /* Return the relocation value for a TLS dtp-relative reloc. */
1695 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1697 /* If tls_sec is NULL, we should have signalled an error already. */
1698 if (elf_hash_table (info
)->tls_sec
== NULL
)
1700 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1703 /* Return the relocation value for a static TLS tp-relative relocation. */
1706 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1708 /* If tls_sec is NULL, we should have signalled an error already. */
1709 if (elf_hash_table (info
)->tls_sec
== NULL
)
1711 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1714 /* Return the global pointer's value, or 0 if it is not in use. */
1717 riscv_global_pointer_value (struct bfd_link_info
*info
)
1719 struct bfd_link_hash_entry
*h
;
1721 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1722 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1725 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1728 /* Emplace a static relocation. */
1730 static bfd_reloc_status_type
1731 perform_relocation (const reloc_howto_type
*howto
,
1732 const Elf_Internal_Rela
*rel
,
1734 asection
*input_section
,
1738 if (howto
->pc_relative
)
1739 value
-= sec_addr (input_section
) + rel
->r_offset
;
1741 switch (ELFNN_R_TYPE (rel
->r_info
))
1748 case R_RISCV_SUB_ULEB128
:
1749 value
-= rel
->r_addend
;
1752 value
+= rel
->r_addend
;
1755 switch (ELFNN_R_TYPE (rel
->r_info
))
1758 case R_RISCV_TPREL_HI20
:
1759 case R_RISCV_PCREL_HI20
:
1760 case R_RISCV_GOT_HI20
:
1761 case R_RISCV_TLS_GOT_HI20
:
1762 case R_RISCV_TLS_GD_HI20
:
1763 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1764 return bfd_reloc_overflow
;
1765 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1768 case R_RISCV_LO12_I
:
1769 case R_RISCV_GPREL_I
:
1770 case R_RISCV_TPREL_LO12_I
:
1771 case R_RISCV_TPREL_I
:
1772 case R_RISCV_PCREL_LO12_I
:
1773 value
= ENCODE_ITYPE_IMM (value
);
1776 case R_RISCV_LO12_S
:
1777 case R_RISCV_GPREL_S
:
1778 case R_RISCV_TPREL_LO12_S
:
1779 case R_RISCV_TPREL_S
:
1780 case R_RISCV_PCREL_LO12_S
:
1781 value
= ENCODE_STYPE_IMM (value
);
1785 case R_RISCV_CALL_PLT
:
1786 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1787 return bfd_reloc_overflow
;
1788 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1789 | (ENCODE_ITYPE_IMM (value
) << 32);
1793 if (!VALID_JTYPE_IMM (value
))
1794 return bfd_reloc_overflow
;
1795 value
= ENCODE_JTYPE_IMM (value
);
1798 case R_RISCV_BRANCH
:
1799 if (!VALID_BTYPE_IMM (value
))
1800 return bfd_reloc_overflow
;
1801 value
= ENCODE_BTYPE_IMM (value
);
1804 case R_RISCV_RVC_BRANCH
:
1805 if (!VALID_CBTYPE_IMM (value
))
1806 return bfd_reloc_overflow
;
1807 value
= ENCODE_CBTYPE_IMM (value
);
1810 case R_RISCV_RVC_JUMP
:
1811 if (!VALID_CJTYPE_IMM (value
))
1812 return bfd_reloc_overflow
;
1813 value
= ENCODE_CJTYPE_IMM (value
);
1816 case R_RISCV_RVC_LUI
:
1817 if (RISCV_CONST_HIGH_PART (value
) == 0)
1819 /* Linker relaxation can convert an address equal to or greater than
1820 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1821 valid immediate. We can fix this by converting it to a C.LI. */
1822 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1823 contents
+ rel
->r_offset
);
1824 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1825 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1826 value
= ENCODE_CITYPE_IMM (0);
1828 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1829 return bfd_reloc_overflow
;
1831 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1834 /* R_RISCV_SET_ULEB128 won't go into here. */
1835 case R_RISCV_SUB_ULEB128
:
1837 unsigned int len
= 0;
1838 _bfd_read_unsigned_leb128 (input_bfd
, contents
+ rel
->r_offset
, &len
);
1840 /* Clean the contents value to zero (0x80), but keep the original
1842 bfd_byte
*p
= contents
+ rel
->r_offset
;
1843 bfd_byte
*endp
= p
+ len
- 1;
1844 memset (p
, 0x80, len
- 1);
1847 /* Make sure the length of the new uleb128 value within the
1848 original (available) length. */
1849 unsigned int new_len
= 0;
1850 unsigned int val_t
= value
;
1860 (_("final size of uleb128 value at offset 0x%lx in %pA from "
1861 "%pB exceeds available space"),
1862 (long) rel
->r_offset
, input_section
, input_bfd
);
1863 return bfd_reloc_dangerous
;
1867 p
= _bfd_write_unsigned_leb128 (p
, endp
, value
);
1870 /* If the length of the value is reduced and shorter than the
1871 original uleb128 length, then _bfd_write_unsigned_leb128 may
1872 clear the 0x80 to 0x0 for the last byte that was written.
1873 So reset it to keep the the original uleb128 length. */
1877 return bfd_reloc_ok
;
1895 case R_RISCV_32_PCREL
:
1896 case R_RISCV_TLS_DTPREL32
:
1897 case R_RISCV_TLS_DTPREL64
:
1900 case R_RISCV_DELETE
:
1901 return bfd_reloc_ok
;
1904 return bfd_reloc_notsupported
;
1908 if (riscv_is_insn_reloc (howto
))
1909 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1911 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1912 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1913 if (riscv_is_insn_reloc (howto
))
1914 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1916 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1918 return bfd_reloc_ok
;
1921 /* Remember all PC-relative high-part relocs we've encountered to help us
1922 later resolve the corresponding low-part relocs. */
1928 /* Relocation value with addend. */
1930 /* Original reloc type. */
1932 } riscv_pcrel_hi_reloc
;
1934 typedef struct riscv_pcrel_lo_reloc
1936 /* PC value of auipc. */
1938 /* Internal relocation. */
1939 const Elf_Internal_Rela
*reloc
;
1940 /* Record the following information helps to resolve the %pcrel
1941 which cross different input section. For now we build a hash
1942 for pcrel at the start of riscv_elf_relocate_section, and then
1943 free the hash at the end. But riscv_elf_relocate_section only
1944 handles an input section at a time, so that means we can only
1945 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1946 section. Otherwise, we will report dangerous relocation errors
1947 for those %pcrel which are not in the same input section. */
1948 asection
*input_section
;
1949 struct bfd_link_info
*info
;
1950 reloc_howto_type
*howto
;
1952 /* The next riscv_pcrel_lo_reloc. */
1953 struct riscv_pcrel_lo_reloc
*next
;
1954 } riscv_pcrel_lo_reloc
;
1958 /* Hash table for riscv_pcrel_hi_reloc. */
1960 /* Linked list for riscv_pcrel_lo_reloc. */
1961 riscv_pcrel_lo_reloc
*lo_relocs
;
1962 } riscv_pcrel_relocs
;
1965 riscv_pcrel_reloc_hash (const void *entry
)
1967 const riscv_pcrel_hi_reloc
*e
= entry
;
1968 return (hashval_t
)(e
->address
>> 2);
1972 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1974 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1975 return e1
->address
== e2
->address
;
1979 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1981 p
->lo_relocs
= NULL
;
1982 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1983 riscv_pcrel_reloc_eq
, free
);
1984 return p
->hi_relocs
!= NULL
;
1988 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1990 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1994 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1999 htab_delete (p
->hi_relocs
);
2003 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
2004 struct bfd_link_info
*info
,
2008 const reloc_howto_type
*howto
)
2010 /* We may need to reference low addreses in PC-relative modes even when the
2011 PC is far away from these addresses. For example, undefweak references
2012 need to produce the address 0 when linked. As 0 is far from the arbitrary
2013 addresses that we can link PC-relative programs at, the linker can't
2014 actually relocate references to those symbols. In order to allow these
2015 programs to work we simply convert the PC-relative auipc sequences to
2016 0-relative lui sequences. */
2017 if (bfd_link_pic (info
))
2020 /* If it's possible to reference the symbol using auipc we do so, as that's
2021 more in the spirit of the PC-relative relocations we're processing. */
2022 bfd_vma offset
= addr
- pc
;
2023 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
2026 /* If it's impossible to reference this with a LUI-based offset then don't
2027 bother to convert it at all so users still see the PC-relative relocation
2028 in the truncation message. */
2029 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
2032 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
2034 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
2035 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
2036 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
2041 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
2047 bfd_vma offset
= absolute
? value
: value
- addr
;
2048 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
};
2049 riscv_pcrel_hi_reloc
**slot
=
2050 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
2052 BFD_ASSERT (*slot
== NULL
);
2053 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
2061 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
2063 const Elf_Internal_Rela
*reloc
,
2064 asection
*input_section
,
2065 struct bfd_link_info
*info
,
2066 reloc_howto_type
*howto
,
2069 riscv_pcrel_lo_reloc
*entry
;
2070 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
2073 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
2074 howto
, contents
, p
->lo_relocs
};
2075 p
->lo_relocs
= entry
;
2080 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
2082 riscv_pcrel_lo_reloc
*r
;
2084 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
2086 bfd
*input_bfd
= r
->input_section
->owner
;
2088 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0};
2089 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
2090 /* There may be a risk if the %pcrel_lo with addend refers to
2091 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
2092 so the corresponding %pcrel_lo with addend looks wrong. */
2093 char *string
= NULL
;
2095 string
= _("%pcrel_lo missing matching %pcrel_hi");
2096 else if (entry
->type
== R_RISCV_GOT_HI20
2097 && r
->reloc
->r_addend
!= 0)
2098 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
2099 else if (RISCV_CONST_HIGH_PART (entry
->value
)
2100 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
2102 /* Check the overflow when adding reloc addend. */
2103 string
= bfd_asprintf (_("%%pcrel_lo overflow with an addend,"
2104 " the value of %%pcrel_hi is 0x%" PRIx64
2105 " without any addend, but may be 0x%" PRIx64
2106 " after adding the %%pcrel_lo addend"),
2107 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
2108 (int64_t) RISCV_CONST_HIGH_PART
2109 (entry
->value
+ r
->reloc
->r_addend
));
2111 string
= _("%pcrel_lo overflow with an addend");
2116 (*r
->info
->callbacks
->reloc_dangerous
)
2117 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
2121 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
2122 input_bfd
, r
->contents
);
2128 /* Relocate a RISC-V ELF section.
2130 The RELOCATE_SECTION function is called by the new ELF backend linker
2131 to handle the relocations for a section.
2133 The relocs are always passed as Rela structures.
2135 This function is responsible for adjusting the section contents as
2136 necessary, and (if generating a relocatable output file) adjusting
2137 the reloc addend as necessary.
2139 This function does not have to worry about setting the reloc
2140 address or the reloc symbol index.
2142 LOCAL_SYMS is a pointer to the swapped in local symbols.
2144 LOCAL_SECTIONS is an array giving the section in the input file
2145 corresponding to the st_shndx field of each local symbol.
2147 The global hash table entry for the global symbols can be found
2148 via elf_sym_hashes (input_bfd).
2150 When generating relocatable output, this function must handle
2151 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2152 going to be the section symbol corresponding to the output
2153 section, which means that the addend must be adjusted
2157 riscv_elf_relocate_section (bfd
*output_bfd
,
2158 struct bfd_link_info
*info
,
2160 asection
*input_section
,
2162 Elf_Internal_Rela
*relocs
,
2163 Elf_Internal_Sym
*local_syms
,
2164 asection
**local_sections
)
2166 Elf_Internal_Rela
*rel
;
2167 Elf_Internal_Rela
*relend
;
2168 riscv_pcrel_relocs pcrel_relocs
;
2170 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2171 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2172 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
2173 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
2174 bfd_vma uleb128_set_vma
= 0;
2175 Elf_Internal_Rela
*uleb128_set_rel
= NULL
;
2178 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
2181 relend
= relocs
+ input_section
->reloc_count
;
2182 for (rel
= relocs
; rel
< relend
; rel
++)
2184 unsigned long r_symndx
;
2185 struct elf_link_hash_entry
*h
;
2186 Elf_Internal_Sym
*sym
;
2189 bfd_reloc_status_type r
= bfd_reloc_ok
;
2190 const char *name
= NULL
;
2191 bfd_vma off
, ie_off
;
2192 bool unresolved_reloc
, is_ie
= false;
2193 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
2194 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
2195 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2196 const char *msg
= NULL
;
2197 bool resolved_to_zero
;
2202 /* This is a final link. */
2203 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2207 unresolved_reloc
= false;
2208 if (r_symndx
< symtab_hdr
->sh_info
)
2210 sym
= local_syms
+ r_symndx
;
2211 sec
= local_sections
[r_symndx
];
2212 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2214 /* Relocate against local STT_GNU_IFUNC symbol. */
2215 if (!bfd_link_relocatable (info
)
2216 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2218 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2222 /* Set STT_GNU_IFUNC symbol value. */
2223 h
->root
.u
.def
.value
= sym
->st_value
;
2224 h
->root
.u
.def
.section
= sec
;
2229 bool warned
, ignored
;
2231 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2232 r_symndx
, symtab_hdr
, sym_hashes
,
2234 unresolved_reloc
, warned
, ignored
);
2237 /* To avoid generating warning messages about truncated
2238 relocations, set the relocation's address to be the same as
2239 the start of this section. */
2240 if (input_section
->output_section
!= NULL
)
2241 relocation
= input_section
->output_section
->vma
;
2247 if (sec
!= NULL
&& discarded_section (sec
))
2248 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2249 rel
, 1, relend
, howto
, 0, contents
);
2251 if (bfd_link_relocatable (info
))
2254 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2255 it here if it is defined in a non-shared object. */
2257 && h
->type
== STT_GNU_IFUNC
2260 asection
*plt
, *base_got
;
2262 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2264 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2265 STT_GNU_IFUNC symbol as STT_FUNC. */
2266 if (elf_section_type (input_section
) == SHT_NOTE
)
2269 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2270 sections because such sections are not SEC_ALLOC and
2271 thus ld.so will not process them. */
2272 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2277 else if (h
->plt
.offset
== (bfd_vma
) -1
2278 /* The following relocation may not need the .plt entries
2279 when all references to a STT_GNU_IFUNC symbols are done
2280 via GOT or static function pointers. */
2281 && r_type
!= R_RISCV_32
2282 && r_type
!= R_RISCV_64
2283 && r_type
!= R_RISCV_HI20
2284 && r_type
!= R_RISCV_GOT_HI20
2285 && r_type
!= R_RISCV_LO12_I
2286 && r_type
!= R_RISCV_LO12_S
)
2287 goto bad_ifunc_reloc
;
2289 /* STT_GNU_IFUNC symbol must go through PLT. */
2290 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2291 relocation
= plt
->output_section
->vma
2292 + plt
->output_offset
2299 if (rel
->r_addend
!= 0)
2301 if (h
->root
.root
.string
)
2302 name
= h
->root
.root
.string
;
2304 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2307 /* xgettext:c-format */
2308 (_("%pB: relocation %s against STT_GNU_IFUNC "
2309 "symbol `%s' has non-zero addend: %" PRId64
),
2310 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2311 bfd_set_error (bfd_error_bad_value
);
2315 /* Generate dynamic relocation only when there is a non-GOT
2316 reference in a shared object or there is no PLT. */
2317 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2318 || h
->plt
.offset
== (bfd_vma
) -1)
2320 Elf_Internal_Rela outrel
;
2323 /* Need a dynamic relocation to get the real function
2325 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2329 if (outrel
.r_offset
== (bfd_vma
) -1
2330 || outrel
.r_offset
== (bfd_vma
) -2)
2333 outrel
.r_offset
+= input_section
->output_section
->vma
2334 + input_section
->output_offset
;
2336 if (h
->dynindx
== -1
2338 || bfd_link_executable (info
))
2340 info
->callbacks
->minfo
2341 (_("Local IFUNC function `%s' in %pB\n"),
2342 h
->root
.root
.string
,
2343 h
->root
.u
.def
.section
->owner
);
2345 /* This symbol is resolved locally. */
2346 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2347 outrel
.r_addend
= h
->root
.u
.def
.value
2348 + h
->root
.u
.def
.section
->output_section
->vma
2349 + h
->root
.u
.def
.section
->output_offset
;
2353 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2354 outrel
.r_addend
= 0;
2357 /* Dynamic relocations are stored in
2358 1. .rela.ifunc section in PIC object.
2359 2. .rela.got section in dynamic executable.
2360 3. .rela.iplt section in static executable. */
2361 if (bfd_link_pic (info
))
2362 sreloc
= htab
->elf
.irelifunc
;
2363 else if (htab
->elf
.splt
!= NULL
)
2364 sreloc
= htab
->elf
.srelgot
;
2366 sreloc
= htab
->elf
.irelplt
;
2368 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2370 /* If this reloc is against an external symbol, we
2371 do not want to fiddle with the addend. Otherwise,
2372 we need to include the symbol value so that it
2373 becomes an addend for the dynamic reloc. For an
2374 internal symbol, we have updated addend. */
2379 case R_RISCV_GOT_HI20
:
2380 base_got
= htab
->elf
.sgot
;
2381 off
= h
->got
.offset
;
2383 if (base_got
== NULL
)
2386 if (off
== (bfd_vma
) -1)
2390 /* We can't use h->got.offset here to save state, or
2391 even just remember the offset, as finish_dynamic_symbol
2392 would use that as offset into .got. */
2394 if (htab
->elf
.splt
!= NULL
)
2396 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2398 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2399 base_got
= htab
->elf
.sgotplt
;
2403 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2404 off
= plt_idx
* GOT_ENTRY_SIZE
;
2405 base_got
= htab
->elf
.igotplt
;
2408 if (h
->dynindx
== -1
2412 /* This references the local definition. We must
2413 initialize this entry in the global offset table.
2414 Since the offset must always be a multiple of 8,
2415 we use the least significant bit to record
2416 whether we have initialized it already.
2418 When doing a dynamic link, we create a .rela.got
2419 relocation entry to initialize the value. This
2420 is done in the finish_dynamic_symbol routine. */
2425 bfd_put_NN (output_bfd
, relocation
,
2426 base_got
->contents
+ off
);
2427 /* Note that this is harmless for the case,
2428 as -1 | 1 still is -1. */
2434 relocation
= base_got
->output_section
->vma
2435 + base_got
->output_offset
+ off
;
2437 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2440 r
= bfd_reloc_overflow
;
2444 case R_RISCV_CALL_PLT
:
2446 case R_RISCV_LO12_I
:
2447 case R_RISCV_LO12_S
:
2450 case R_RISCV_PCREL_HI20
:
2451 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2454 r
= bfd_reloc_overflow
;
2459 if (h
->root
.root
.string
)
2460 name
= h
->root
.root
.string
;
2462 /* The entry of local ifunc is fake in global hash table,
2463 we should find the name by the original local symbol. */
2464 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2467 /* xgettext:c-format */
2468 (_("%pB: relocation %s against STT_GNU_IFUNC "
2469 "symbol `%s' isn't supported"), input_bfd
,
2471 bfd_set_error (bfd_error_bad_value
);
2478 name
= h
->root
.root
.string
;
2481 name
= (bfd_elf_string_from_elf_section
2482 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2483 if (name
== NULL
|| *name
== '\0')
2484 name
= bfd_section_name (sec
);
2487 resolved_to_zero
= (h
!= NULL
2488 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2494 case R_RISCV_TPREL_ADD
:
2496 case R_RISCV_JUMP_SLOT
:
2497 case R_RISCV_RELATIVE
:
2498 /* These require nothing of us at all. */
2502 case R_RISCV_BRANCH
:
2503 case R_RISCV_RVC_BRANCH
:
2504 case R_RISCV_RVC_LUI
:
2505 case R_RISCV_LO12_I
:
2506 case R_RISCV_LO12_S
:
2511 case R_RISCV_32_PCREL
:
2512 case R_RISCV_DELETE
:
2513 /* These require no special handling beyond perform_relocation. */
2516 case R_RISCV_SET_ULEB128
:
2517 if (uleb128_set_rel
== NULL
)
2519 /* Saved for later usage. */
2520 uleb128_set_vma
= relocation
;
2521 uleb128_set_rel
= rel
;
2526 msg
= ("Mismatched R_RISCV_SET_ULEB128, it must be paired with"
2527 "and applied before R_RISCV_SUB_ULEB128");
2528 r
= bfd_reloc_dangerous
;
2532 case R_RISCV_SUB_ULEB128
:
2533 if (uleb128_set_rel
!= NULL
2534 && uleb128_set_rel
->r_offset
== rel
->r_offset
)
2536 relocation
= uleb128_set_vma
- relocation
+ uleb128_set_rel
->r_addend
;
2537 uleb128_set_vma
= 0;
2538 uleb128_set_rel
= NULL
;
2542 msg
= ("Mismatched R_RISCV_SUB_ULEB128, it must be paired with"
2543 "and applied after R_RISCV_SET_ULEB128");
2544 r
= bfd_reloc_dangerous
;
2548 case R_RISCV_GOT_HI20
:
2551 off
= h
->got
.offset
;
2552 BFD_ASSERT (off
!= (bfd_vma
) -1);
2554 if (RISCV_RESOLVED_LOCALLY (info
, h
))
2556 /* We must initialize this entry in the global offset table.
2557 Since the offset must always be a multiple of the word
2558 size, we use the least significant bit to record whether
2559 we have initialized it already.
2561 When doing a dynamic link, we create a .rela.got
2562 relocation entry to initialize the value. This
2563 is done in the finish_dynamic_symbol routine. */
2568 bfd_put_NN (output_bfd
, relocation
,
2569 htab
->elf
.sgot
->contents
+ off
);
2574 unresolved_reloc
= false;
2578 BFD_ASSERT (local_got_offsets
!= NULL
2579 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2581 off
= local_got_offsets
[r_symndx
];
2583 /* The offset must always be a multiple of the word size.
2584 So, we can use the least significant bit to record
2585 whether we have already processed this entry. */
2590 if (bfd_link_pic (info
))
2593 Elf_Internal_Rela outrel
;
2595 /* We need to generate a R_RISCV_RELATIVE reloc
2596 for the dynamic linker. */
2597 s
= htab
->elf
.srelgot
;
2598 BFD_ASSERT (s
!= NULL
);
2600 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2602 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2603 outrel
.r_addend
= relocation
;
2605 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2608 bfd_put_NN (output_bfd
, relocation
,
2609 htab
->elf
.sgot
->contents
+ off
);
2610 local_got_offsets
[r_symndx
] |= 1;
2614 if (rel
->r_addend
!= 0)
2616 msg
= _("The addend isn't allowed for R_RISCV_GOT_HI20");
2617 r
= bfd_reloc_dangerous
;
2621 /* Address of got entry. */
2622 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2623 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
,
2624 relocation
, contents
,
2626 /* Update howto if relocation is changed. */
2627 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2628 ELFNN_R_TYPE (rel
->r_info
));
2630 r
= bfd_reloc_notsupported
;
2631 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2634 r
= bfd_reloc_overflow
;
2643 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2644 contents
+ rel
->r_offset
);
2645 relocation
= old_value
+ relocation
;
2651 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2652 contents
+ rel
->r_offset
);
2653 relocation
= (old_value
& ~howto
->dst_mask
)
2654 | (((old_value
& howto
->dst_mask
) - relocation
)
2664 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2665 contents
+ rel
->r_offset
);
2666 relocation
= old_value
- relocation
;
2671 case R_RISCV_CALL_PLT
:
2672 /* Handle a call to an undefined weak function. This won't be
2673 relaxed, so we have to handle it here. */
2674 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2675 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2677 /* We can use x0 as the base register. */
2678 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2679 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2680 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2681 /* Set the relocation value so that we get 0 after the pc
2682 relative adjustment. */
2683 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2688 case R_RISCV_RVC_JUMP
:
2689 if (bfd_link_pic (info
) && h
!= NULL
)
2691 if (h
->plt
.offset
!= MINUS_ONE
)
2693 /* Refer to the PLT entry. This check has to match the
2694 check in _bfd_riscv_relax_section. */
2695 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2696 unresolved_reloc
= false;
2698 else if (!SYMBOL_REFERENCES_LOCAL (info
, h
)
2699 && (input_section
->flags
& SEC_ALLOC
) != 0
2700 && (input_section
->flags
& SEC_READONLY
) != 0
2701 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2703 /* PR 28509, when generating the shared object, these
2704 referenced symbols may bind externally, which means
2705 they will be exported to the dynamic symbol table,
2706 and are preemptible by default. These symbols cannot
2707 be referenced by the non-pic relocations, like
2708 R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2710 However, consider that linker may relax the R_RISCV_CALL
2711 relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2712 these relocations are relocated to the plt entries,
2713 then we won't report error for them.
2715 Perhaps we also need the similar checks for the
2716 R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
2717 msg
= bfd_asprintf (_("%%X%%P: relocation %s against `%s'"
2718 " which may bind externally"
2720 " when making a shared object;"
2721 " recompile with -fPIC\n"),
2722 howto
->name
, h
->root
.root
.string
);
2723 r
= bfd_reloc_notsupported
;
2728 case R_RISCV_TPREL_HI20
:
2729 relocation
= tpoff (info
, relocation
);
2732 case R_RISCV_TPREL_LO12_I
:
2733 case R_RISCV_TPREL_LO12_S
:
2734 relocation
= tpoff (info
, relocation
);
2737 case R_RISCV_TPREL_I
:
2738 case R_RISCV_TPREL_S
:
2739 relocation
= tpoff (info
, relocation
);
2740 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2742 /* We can use tp as the base register. */
2743 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2744 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2745 insn
|= X_TP
<< OP_SH_RS1
;
2746 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2749 r
= bfd_reloc_overflow
;
2752 case R_RISCV_GPREL_I
:
2753 case R_RISCV_GPREL_S
:
2755 bfd_vma gp
= riscv_global_pointer_value (info
);
2756 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2757 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2759 /* We can use x0 or gp as the base register. */
2760 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2761 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2764 rel
->r_addend
-= gp
;
2765 insn
|= X_GP
<< OP_SH_RS1
;
2767 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2770 r
= bfd_reloc_overflow
;
2774 case R_RISCV_PCREL_HI20
:
2775 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, relocation
,
2777 /* Update howto if relocation is changed. */
2778 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2779 ELFNN_R_TYPE (rel
->r_info
));
2781 r
= bfd_reloc_notsupported
;
2782 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2783 relocation
+ rel
->r_addend
,
2785 r
= bfd_reloc_overflow
;
2788 case R_RISCV_PCREL_LO12_I
:
2789 case R_RISCV_PCREL_LO12_S
:
2790 /* We don't allow section symbols plus addends as the auipc address,
2791 because then riscv_relax_delete_bytes would have to search through
2792 all relocs to update these addends. This is also ambiguous, as
2793 we do allow offsets to be added to the target address, which are
2794 not to be used to find the auipc address. */
2795 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2796 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2799 msg
= _("%pcrel_lo section symbol with an addend");
2800 r
= bfd_reloc_dangerous
;
2804 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2805 input_section
, info
, howto
,
2808 r
= bfd_reloc_overflow
;
2811 case R_RISCV_TLS_DTPREL32
:
2812 case R_RISCV_TLS_DTPREL64
:
2813 relocation
= dtpoff (info
, relocation
);
2817 /* Non ABS symbol should be blocked in check_relocs. */
2823 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2826 if (RISCV_GENERATE_DYNAMIC_RELOC (howto
->pc_relative
, info
, h
,
2829 Elf_Internal_Rela outrel
;
2832 /* When generating a shared object, these relocations
2833 are copied into the output file to be resolved at run
2837 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2840 bool relocate
= false;
2841 if (outrel
.r_offset
== (bfd_vma
) -1)
2843 else if (outrel
.r_offset
== (bfd_vma
) -2)
2848 else if (h
!= NULL
&& bfd_is_abs_symbol (&h
->root
))
2850 /* Don't need dynamic reloc when the ABS symbol is
2851 non-dynamic or forced to local. Maybe just use
2852 SYMBOL_REFERENCES_LOCAL to check? */
2853 skip
= (h
->forced_local
|| (h
->dynindx
== -1));
2857 outrel
.r_offset
+= sec_addr (input_section
);
2860 memset (&outrel
, 0, sizeof outrel
); /* R_RISCV_NONE. */
2861 else if (RISCV_COPY_INPUT_RELOC (info
, h
))
2863 /* Maybe just use !SYMBOL_REFERENCES_LOCAL to check? */
2864 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2865 outrel
.r_addend
= rel
->r_addend
;
2869 /* This symbol is local, or marked to become local. */
2870 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2871 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2874 sreloc
= elf_section_data (input_section
)->sreloc
;
2875 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2881 case R_RISCV_TLS_GOT_HI20
:
2885 case R_RISCV_TLS_GD_HI20
:
2888 off
= h
->got
.offset
;
2893 off
= local_got_offsets
[r_symndx
];
2894 local_got_offsets
[r_symndx
] |= 1;
2897 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2898 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2899 /* If this symbol is referenced by both GD and IE TLS, the IE
2900 reference's GOT slot follows the GD reference's slots. */
2902 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2903 ie_off
= 2 * GOT_ENTRY_SIZE
;
2909 Elf_Internal_Rela outrel
;
2911 bool need_relocs
= false;
2913 if (htab
->elf
.srelgot
== NULL
)
2916 bool dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2917 RISCV_TLS_GD_IE_NEED_DYN_RELOC (info
, dyn
, h
, indx
, need_relocs
);
2919 /* The GOT entries have not been initialized yet. Do it
2920 now, and emit any relocations. */
2921 if (tls_type
& GOT_TLS_GD
)
2925 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2926 outrel
.r_addend
= 0;
2927 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2928 bfd_put_NN (output_bfd
, 0,
2929 htab
->elf
.sgot
->contents
+ off
);
2930 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2933 BFD_ASSERT (! unresolved_reloc
);
2934 bfd_put_NN (output_bfd
,
2935 dtpoff (info
, relocation
),
2936 (htab
->elf
.sgot
->contents
2937 + off
+ RISCV_ELF_WORD_BYTES
));
2941 bfd_put_NN (output_bfd
, 0,
2942 (htab
->elf
.sgot
->contents
2943 + off
+ RISCV_ELF_WORD_BYTES
));
2944 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2945 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2946 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2951 /* If we are not emitting relocations for a
2952 general dynamic reference, then we must be in a
2953 static link or an executable link with the
2954 symbol binding locally. Mark it as belonging
2955 to module 1, the executable. */
2956 bfd_put_NN (output_bfd
, 1,
2957 htab
->elf
.sgot
->contents
+ off
);
2958 bfd_put_NN (output_bfd
,
2959 dtpoff (info
, relocation
),
2960 (htab
->elf
.sgot
->contents
2961 + off
+ RISCV_ELF_WORD_BYTES
));
2965 if (tls_type
& GOT_TLS_IE
)
2969 bfd_put_NN (output_bfd
, 0,
2970 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2971 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2973 outrel
.r_addend
= 0;
2975 outrel
.r_addend
= tpoff (info
, relocation
);
2976 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2977 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2981 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2982 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2987 BFD_ASSERT (off
< (bfd_vma
) -2);
2988 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2989 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2992 r
= bfd_reloc_overflow
;
2993 unresolved_reloc
= false;
2997 r
= bfd_reloc_notsupported
;
3000 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3001 because such sections are not SEC_ALLOC and thus ld.so will
3002 not process them. */
3003 if (unresolved_reloc
3004 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3006 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3007 rel
->r_offset
) != (bfd_vma
) -1)
3009 msg
= bfd_asprintf (_("%%X%%P: unresolvable %s relocation against "
3012 h
->root
.root
.string
);
3013 r
= bfd_reloc_notsupported
;
3017 if (r
== bfd_reloc_ok
)
3018 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
3019 input_bfd
, contents
);
3021 /* We should have already detected the error and set message before.
3022 If the error message isn't set since the linker runs out of memory
3023 or we don't set it before, then we should set the default message
3024 with the "internal error" string here. */
3030 case bfd_reloc_overflow
:
3031 info
->callbacks
->reloc_overflow
3032 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3033 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
3036 case bfd_reloc_undefined
:
3037 info
->callbacks
->undefined_symbol
3038 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
3042 case bfd_reloc_outofrange
:
3044 msg
= _("%X%P: internal error: out of range error\n");
3047 case bfd_reloc_notsupported
:
3049 msg
= _("%X%P: internal error: unsupported relocation error\n");
3052 case bfd_reloc_dangerous
:
3053 /* The error message should already be set. */
3055 msg
= _("dangerous relocation error");
3056 info
->callbacks
->reloc_dangerous
3057 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
3061 msg
= _("%X%P: internal error: unknown error\n");
3065 /* Do not report error message for the dangerous relocation again. */
3066 if (msg
&& r
!= bfd_reloc_dangerous
)
3067 info
->callbacks
->einfo (msg
);
3069 /* We already reported the error via a callback, so don't try to report
3070 it again by returning false. That leads to spurious errors. */
3075 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
3077 riscv_free_pcrel_relocs (&pcrel_relocs
);
3081 /* Finish up dynamic symbol handling. We set the contents of various
3082 dynamic sections here. */
3085 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
3086 struct bfd_link_info
*info
,
3087 struct elf_link_hash_entry
*h
,
3088 Elf_Internal_Sym
*sym
)
3090 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3091 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3093 if (h
->plt
.offset
!= (bfd_vma
) -1)
3095 /* We've decided to create a PLT entry for this symbol. */
3097 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
3098 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
3099 Elf_Internal_Rela rela
;
3100 asection
*plt
, *gotplt
, *relplt
;
3102 /* When building a static executable, use .iplt, .igot.plt and
3103 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3104 if (htab
->elf
.splt
!= NULL
)
3106 plt
= htab
->elf
.splt
;
3107 gotplt
= htab
->elf
.sgotplt
;
3108 relplt
= htab
->elf
.srelplt
;
3112 plt
= htab
->elf
.iplt
;
3113 gotplt
= htab
->elf
.igotplt
;
3114 relplt
= htab
->elf
.irelplt
;
3117 /* This symbol has an entry in the procedure linkage table. Set
3119 if ((h
->dynindx
== -1
3120 && !((h
->forced_local
|| bfd_link_executable (info
))
3122 && h
->type
== STT_GNU_IFUNC
))
3128 /* Calculate the address of the PLT header. */
3129 header_address
= sec_addr (plt
);
3131 /* Calculate the index of the entry and the offset of .got.plt entry.
3132 For static executables, we don't reserve anything. */
3133 if (plt
== htab
->elf
.splt
)
3135 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
3136 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
3140 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
3141 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
3144 /* Calculate the address of the .got.plt entry. */
3145 got_address
= sec_addr (gotplt
) + got_offset
;
3147 /* Find out where the .plt entry should go. */
3148 loc
= plt
->contents
+ h
->plt
.offset
;
3150 /* Fill in the PLT entry itself. */
3151 if (! riscv_make_plt_entry (output_bfd
, got_address
,
3152 header_address
+ h
->plt
.offset
,
3156 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
3157 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
3159 /* Fill in the initial value of the .got.plt entry. */
3160 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
3161 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
3163 rela
.r_offset
= got_address
;
3165 if (h
->dynindx
== -1
3166 || ((bfd_link_executable (info
)
3167 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3169 && h
->type
== STT_GNU_IFUNC
))
3171 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3172 h
->root
.root
.string
,
3173 h
->root
.u
.def
.section
->owner
);
3175 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3176 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
3177 asection
*sec
= h
->root
.u
.def
.section
;
3178 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3179 rela
.r_addend
= h
->root
.u
.def
.value
3180 + sec
->output_section
->vma
3181 + sec
->output_offset
;
3185 /* Fill in the entry in the .rela.plt section. */
3186 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
3190 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
3191 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3193 if (!h
->def_regular
)
3195 /* Mark the symbol as undefined, rather than as defined in
3196 the .plt section. Leave the value alone. */
3197 sym
->st_shndx
= SHN_UNDEF
;
3198 /* If the symbol is weak, we do need to clear the value.
3199 Otherwise, the PLT entry would provide a definition for
3200 the symbol even if the symbol wasn't defined anywhere,
3201 and so the symbol would never be NULL. */
3202 if (!h
->ref_regular_nonweak
)
3207 if (h
->got
.offset
!= (bfd_vma
) -1
3208 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
3209 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3213 Elf_Internal_Rela rela
;
3214 bool use_elf_append_rela
= true;
3216 /* This symbol has an entry in the GOT. Set it up. */
3218 sgot
= htab
->elf
.sgot
;
3219 srela
= htab
->elf
.srelgot
;
3220 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3222 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3224 /* Handle the ifunc symbol in GOT entry. */
3226 && h
->type
== STT_GNU_IFUNC
)
3228 if (h
->plt
.offset
== (bfd_vma
) -1)
3230 /* STT_GNU_IFUNC is referenced without PLT. */
3232 if (htab
->elf
.splt
== NULL
)
3234 /* Use .rela.iplt section to store .got relocations
3235 in static executable. */
3236 srela
= htab
->elf
.irelplt
;
3238 /* Do not use riscv_elf_append_rela to add dynamic
3240 use_elf_append_rela
= false;
3243 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3245 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3246 h
->root
.root
.string
,
3247 h
->root
.u
.def
.section
->owner
);
3249 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3250 rela
.r_addend
= (h
->root
.u
.def
.value
3251 + h
->root
.u
.def
.section
->output_section
->vma
3252 + h
->root
.u
.def
.section
->output_offset
);
3256 /* Generate R_RISCV_NN. */
3257 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3258 BFD_ASSERT (h
->dynindx
!= -1);
3259 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3263 else if (bfd_link_pic (info
))
3265 /* Generate R_RISCV_NN. */
3266 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3267 BFD_ASSERT (h
->dynindx
!= -1);
3268 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3275 if (!h
->pointer_equality_needed
)
3278 /* For non-shared object, we can't use .got.plt, which
3279 contains the real function address if we need pointer
3280 equality. We load the GOT entry with the PLT entry. */
3281 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3282 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3283 + plt
->output_offset
3285 htab
->elf
.sgot
->contents
3286 + (h
->got
.offset
& ~(bfd_vma
) 1));
3290 else if (bfd_link_pic (info
)
3291 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3293 /* If this is a local symbol reference, we just want to emit
3294 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3295 or a pie link, or the symbol was forced to be local because
3296 of a version file. The entry in the global offset table will
3297 already have been initialized in the relocate_section function. */
3298 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3299 asection
*sec
= h
->root
.u
.def
.section
;
3300 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3301 rela
.r_addend
= (h
->root
.u
.def
.value
3302 + sec
->output_section
->vma
3303 + sec
->output_offset
);
3307 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3308 BFD_ASSERT (h
->dynindx
!= -1);
3309 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3313 bfd_put_NN (output_bfd
, 0,
3314 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3316 if (use_elf_append_rela
)
3317 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3320 /* Use riscv_elf_append_rela to add the dynamic relocs into
3321 .rela.iplt may cause the overwrite problems. Since we insert
3322 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3323 but the riscv_elf_append_rela adds the relocs to the place
3324 that are calculated from the reloc_index (in seqential).
3326 One solution is that add these dynamic relocs (GOT IFUNC)
3327 from the last of .rela.iplt section. */
3328 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3329 bfd_byte
*loc
= srela
->contents
3330 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3331 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3337 Elf_Internal_Rela rela
;
3340 /* This symbols needs a copy reloc. Set it up. */
3341 BFD_ASSERT (h
->dynindx
!= -1);
3343 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3344 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3346 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3347 s
= htab
->elf
.sreldynrelro
;
3349 s
= htab
->elf
.srelbss
;
3350 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3353 /* Mark some specially defined symbols as absolute. */
3354 if (h
== htab
->elf
.hdynamic
3355 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3356 sym
->st_shndx
= SHN_ABS
;
3361 /* Finish up local dynamic symbol handling. We set the contents of
3362 various dynamic sections here. */
3365 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3367 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3368 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3370 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3373 /* Finish up the dynamic sections. */
3376 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3377 bfd
*dynobj
, asection
*sdyn
)
3379 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3380 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3381 size_t dynsize
= bed
->s
->sizeof_dyn
;
3382 bfd_byte
*dyncon
, *dynconend
;
3384 dynconend
= sdyn
->contents
+ sdyn
->size
;
3385 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3387 Elf_Internal_Dyn dyn
;
3390 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3395 s
= htab
->elf
.sgotplt
;
3396 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3399 s
= htab
->elf
.srelplt
;
3400 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3403 s
= htab
->elf
.srelplt
;
3404 dyn
.d_un
.d_val
= s
->size
;
3410 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3416 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3417 struct bfd_link_info
*info
)
3421 struct riscv_elf_link_hash_table
*htab
;
3423 htab
= riscv_elf_hash_table (info
);
3424 BFD_ASSERT (htab
!= NULL
);
3425 dynobj
= htab
->elf
.dynobj
;
3427 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3429 if (elf_hash_table (info
)->dynamic_sections_created
)
3434 splt
= htab
->elf
.splt
;
3435 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3437 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3442 /* Fill in the head and tail entries in the procedure linkage table. */
3446 uint32_t plt_header
[PLT_HEADER_INSNS
];
3447 ret
= riscv_make_plt_header (output_bfd
,
3448 sec_addr (htab
->elf
.sgotplt
),
3449 sec_addr (splt
), plt_header
);
3453 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3454 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3456 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3461 if (htab
->elf
.sgotplt
)
3463 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3465 if (bfd_is_abs_section (output_section
))
3467 (*_bfd_error_handler
)
3468 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3472 if (htab
->elf
.sgotplt
->size
> 0)
3474 /* Write the first two entries in .got.plt, needed for the dynamic
3476 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3477 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3478 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3481 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3486 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3488 if (htab
->elf
.sgot
->size
> 0)
3490 /* Set the first entry in the global offset table to the address of
3491 the dynamic section. */
3492 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3493 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3496 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3499 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3500 htab_traverse (htab
->loc_hash_table
,
3501 riscv_elf_finish_local_dynamic_symbol
,
3507 /* Return address for Ith PLT stub in section PLT, for relocation REL
3508 or (bfd_vma) -1 if it should not be included. */
3511 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3512 const arelent
*rel ATTRIBUTE_UNUSED
)
3514 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3517 static enum elf_reloc_type_class
3518 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3519 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3520 const Elf_Internal_Rela
*rela
)
3522 switch (ELFNN_R_TYPE (rela
->r_info
))
3524 case R_RISCV_RELATIVE
:
3525 return reloc_class_relative
;
3526 case R_RISCV_JUMP_SLOT
:
3527 return reloc_class_plt
;
3529 return reloc_class_copy
;
3531 return reloc_class_normal
;
3535 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3539 riscv_float_abi_string (flagword flags
)
3541 switch (flags
& EF_RISCV_FLOAT_ABI
)
3543 case EF_RISCV_FLOAT_ABI_SOFT
:
3544 return "soft-float";
3546 case EF_RISCV_FLOAT_ABI_SINGLE
:
3547 return "single-float";
3549 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3550 return "double-float";
3552 case EF_RISCV_FLOAT_ABI_QUAD
:
3553 return "quad-float";
3560 /* The information of architecture elf attributes. */
3561 static riscv_subset_list_t in_subsets
;
3562 static riscv_subset_list_t out_subsets
;
3563 static riscv_subset_list_t merged_subsets
;
3565 /* Predicator for standard extension. */
3568 riscv_std_ext_p (const char *name
)
3570 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3573 /* Update the output subset's version to match the input when the input
3574 subset's version is newer. */
3577 riscv_update_subset_version (struct riscv_subset_t
*in
,
3578 struct riscv_subset_t
*out
)
3580 if (in
== NULL
|| out
== NULL
)
3583 /* Update the output ISA versions to the newest ones, but otherwise don't
3584 provide any errors or warnings about mis-matched ISA versions as it's
3585 generally too tricky to check for these at link time. */
3586 if ((in
->major_version
> out
->major_version
)
3587 || (in
->major_version
== out
->major_version
3588 && in
->minor_version
> out
->minor_version
)
3589 || (out
->major_version
== RISCV_UNKNOWN_VERSION
))
3591 out
->major_version
= in
->major_version
;
3592 out
->minor_version
= in
->minor_version
;
3596 /* Return true if subset is 'i' or 'e'. */
3599 riscv_i_or_e_p (bfd
*ibfd
,
3601 struct riscv_subset_t
*subset
)
3603 if ((strcasecmp (subset
->name
, "e") != 0)
3604 && (strcasecmp (subset
->name
, "i") != 0))
3607 (_("error: %pB: corrupted ISA string '%s'. "
3608 "First letter should be 'i' or 'e' but got '%s'"),
3609 ibfd
, arch
, subset
->name
);
3615 /* Merge standard extensions.
3618 Return FALSE if failed to merge.
3622 `in_arch`: Raw ISA string for input object.
3623 `out_arch`: Raw ISA string for output object.
3624 `pin`: Subset list for input object.
3625 `pout`: Subset list for output object. */
3628 riscv_merge_std_ext (bfd
*ibfd
,
3629 const char *in_arch
,
3630 const char *out_arch
,
3631 struct riscv_subset_t
**pin
,
3632 struct riscv_subset_t
**pout
)
3634 const char *standard_exts
= "mafdqlcbjtpvnh";
3636 struct riscv_subset_t
*in
= *pin
;
3637 struct riscv_subset_t
*out
= *pout
;
3639 /* First letter should be 'i' or 'e'. */
3640 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3643 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3646 if (strcasecmp (in
->name
, out
->name
) != 0)
3648 /* TODO: We might allow merge 'i' with 'e'. */
3650 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3651 ibfd
, in
->name
, out
->name
);
3655 riscv_update_subset_version(in
, out
);
3656 riscv_add_subset (&merged_subsets
,
3657 out
->name
, out
->major_version
, out
->minor_version
);
3662 /* Handle standard extension first. */
3663 for (p
= standard_exts
; *p
; ++p
)
3665 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3666 char find_ext
[2] = {*p
, '\0'};
3667 bool find_in
, find_out
;
3669 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3670 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3672 if (!find_in
&& !find_out
)
3675 if (find_in
&& find_out
)
3676 riscv_update_subset_version(ext_in
, ext_out
);
3678 ext_merged
= find_out
? ext_out
: ext_in
;
3679 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3680 ext_merged
->major_version
, ext_merged
->minor_version
);
3683 /* Skip all standard extensions. */
3684 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3685 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3693 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3694 object subset list. Likewise for POUT and the output object. Return TRUE
3695 on success and FALSE when a conflict is found. */
3698 riscv_merge_multi_letter_ext (riscv_subset_t
**pin
,
3699 riscv_subset_t
**pout
)
3701 riscv_subset_t
*in
= *pin
;
3702 riscv_subset_t
*out
= *pout
;
3703 riscv_subset_t
*tail
;
3709 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3713 /* `in' comes before `out', append `in' and increment. */
3714 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3720 /* `out' comes before `in', append `out' and increment. */
3721 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3722 out
->minor_version
);
3727 /* Both present, check version and increment both. */
3728 riscv_update_subset_version (in
, out
);
3730 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3731 out
->minor_version
);
3739 /* If we're here, either `in' or `out' is running longer than
3740 the other. So, we need to append the corresponding tail. */
3741 tail
= in
? in
: out
;
3744 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3745 tail
->minor_version
);
3753 /* Merge Tag_RISCV_arch attribute. */
3756 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3758 riscv_subset_t
*in
, *out
;
3759 char *merged_arch_str
;
3761 unsigned xlen_in
, xlen_out
;
3762 merged_subsets
.head
= NULL
;
3763 merged_subsets
.tail
= NULL
;
3765 riscv_parse_subset_t riscv_rps_ld_in
=
3766 {&in_subsets
, _bfd_error_handler
, &xlen_in
, NULL
, false};
3767 riscv_parse_subset_t riscv_rps_ld_out
=
3768 {&out_subsets
, _bfd_error_handler
, &xlen_out
, NULL
, false};
3770 if (in_arch
== NULL
&& out_arch
== NULL
)
3772 if (in_arch
== NULL
&& out_arch
!= NULL
)
3774 if (in_arch
!= NULL
&& out_arch
== NULL
)
3777 /* Parse subset from ISA string. */
3778 if (!riscv_parse_subset (&riscv_rps_ld_in
, in_arch
))
3780 if (!riscv_parse_subset (&riscv_rps_ld_out
, out_arch
))
3783 /* Checking XLEN. */
3784 if (xlen_out
!= xlen_in
)
3787 (_("error: %pB: ISA string of input (%s) doesn't match "
3788 "output (%s)"), ibfd
, in_arch
, out_arch
);
3792 /* Merge subset list. */
3793 in
= in_subsets
.head
;
3794 out
= out_subsets
.head
;
3796 /* Merge standard extension. */
3797 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3800 /* Merge all non-single letter extensions with single call. */
3801 if (!riscv_merge_multi_letter_ext (&in
, &out
))
3804 if (xlen_in
!= xlen_out
)
3807 (_("error: %pB: XLEN of input (%u) doesn't match "
3808 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3812 if (xlen_in
!= ARCH_SIZE
)
3815 (_("error: %pB: unsupported XLEN (%u), you might be "
3816 "using wrong emulation"), ibfd
, xlen_in
);
3820 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3822 /* Release the subset lists. */
3823 riscv_release_subset_list (&in_subsets
);
3824 riscv_release_subset_list (&out_subsets
);
3825 riscv_release_subset_list (&merged_subsets
);
3827 return merged_arch_str
;
3830 /* Merge object attributes from IBFD into output_bfd of INFO.
3831 Raise an error if there are conflicting attributes. */
3834 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3836 bfd
*obfd
= info
->output_bfd
;
3837 obj_attribute
*in_attr
;
3838 obj_attribute
*out_attr
;
3840 bool priv_attrs_merged
= false;
3841 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3844 /* Skip linker created files. */
3845 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3848 /* Skip any input that doesn't have an attribute section.
3849 This enables to link object files without attribute section with
3851 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3854 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3856 /* This is the first object. Copy the attributes. */
3857 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3859 out_attr
= elf_known_obj_attributes_proc (obfd
);
3861 /* Use the Tag_null value to indicate the attributes have been
3868 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3869 out_attr
= elf_known_obj_attributes_proc (obfd
);
3871 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3875 case Tag_RISCV_arch
:
3876 if (!out_attr
[Tag_RISCV_arch
].s
)
3877 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3878 else if (in_attr
[Tag_RISCV_arch
].s
3879 && out_attr
[Tag_RISCV_arch
].s
)
3881 /* Check compatible. */
3883 riscv_merge_arch_attr_info (ibfd
,
3884 in_attr
[Tag_RISCV_arch
].s
,
3885 out_attr
[Tag_RISCV_arch
].s
);
3886 if (merged_arch
== NULL
)
3889 out_attr
[Tag_RISCV_arch
].s
= "";
3892 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3896 case Tag_RISCV_priv_spec
:
3897 case Tag_RISCV_priv_spec_minor
:
3898 case Tag_RISCV_priv_spec_revision
:
3899 /* If we have handled the privileged elf attributes, then skip it. */
3900 if (!priv_attrs_merged
)
3902 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3903 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3904 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3905 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3906 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3908 /* Get the privileged spec class from elf attributes. */
3909 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3913 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3918 /* Allow to link the object without the privileged specs. */
3919 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3921 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3922 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3923 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3925 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3926 && in_priv_spec
!= out_priv_spec
)
3929 (_("warning: %pB use privileged spec version %u.%u.%u but "
3930 "the output use version %u.%u.%u"),
3939 /* The privileged spec v1.9.1 can not be linked with others
3940 since the conflicts, so we plan to drop it in a year or
3942 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3943 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3946 (_("warning: privileged spec version 1.9.1 can not be "
3947 "linked with other spec versions"));
3950 /* Update the output privileged spec to the newest one. */
3951 if (in_priv_spec
> out_priv_spec
)
3953 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3954 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3955 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3958 priv_attrs_merged
= true;
3962 case Tag_RISCV_unaligned_access
:
3963 out_attr
[i
].i
|= in_attr
[i
].i
;
3966 case Tag_RISCV_stack_align
:
3967 if (out_attr
[i
].i
== 0)
3968 out_attr
[i
].i
= in_attr
[i
].i
;
3969 else if (in_attr
[i
].i
!= 0
3970 && out_attr
[i
].i
!= 0
3971 && out_attr
[i
].i
!= in_attr
[i
].i
)
3974 (_("error: %pB use %u-byte stack aligned but the output "
3975 "use %u-byte stack aligned"),
3976 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3982 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3985 /* If out_attr was copied from in_attr then it won't have a type yet. */
3986 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3987 out_attr
[i
].type
= in_attr
[i
].type
;
3990 /* Merge Tag_compatibility attributes and any common GNU ones. */
3991 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3994 /* Check for any attributes not known on RISC-V. */
3995 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
4000 /* Merge backend specific data from an object file to the output
4001 object file when linking. */
4004 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
4006 bfd
*obfd
= info
->output_bfd
;
4007 flagword new_flags
, old_flags
;
4009 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
4012 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
4014 (*_bfd_error_handler
)
4015 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
4016 " target emulation `%s' does not match `%s'"),
4017 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
4021 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
4024 if (!riscv_merge_attributes (ibfd
, info
))
4027 /* Check to see if the input BFD actually contains any sections. If not,
4028 its flags may not have been initialized either, but it cannot actually
4029 cause any incompatibility. Do not short-circuit dynamic objects; their
4030 section list may be emptied by elf_link_add_object_symbols.
4032 Also check to see if there are no code sections in the input. In this
4033 case, there is no need to check for code specific flags. */
4034 if (!(ibfd
->flags
& DYNAMIC
))
4036 bool null_input_bfd
= true;
4037 bool only_data_sections
= true;
4040 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4042 null_input_bfd
= false;
4044 if ((bfd_section_flags (sec
)
4045 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
4046 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
4048 only_data_sections
= false;
4053 if (null_input_bfd
|| only_data_sections
)
4057 new_flags
= elf_elfheader (ibfd
)->e_flags
;
4058 old_flags
= elf_elfheader (obfd
)->e_flags
;
4060 if (!elf_flags_init (obfd
))
4062 elf_flags_init (obfd
) = true;
4063 elf_elfheader (obfd
)->e_flags
= new_flags
;
4067 /* Disallow linking different float ABIs. */
4068 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
4070 (*_bfd_error_handler
)
4071 (_("%pB: can't link %s modules with %s modules"), ibfd
,
4072 riscv_float_abi_string (new_flags
),
4073 riscv_float_abi_string (old_flags
));
4077 /* Disallow linking RVE and non-RVE. */
4078 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
4080 (*_bfd_error_handler
)
4081 (_("%pB: can't link RVE with other target"), ibfd
);
4085 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
4086 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
4088 /* Allow linking TSO and non-TSO, and keep the TSO flag. */
4089 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_TSO
;
4094 bfd_set_error (bfd_error_bad_value
);
4098 /* A second format for recording PC-relative hi relocations. This stores the
4099 information required to relax them to GP-relative addresses. */
4101 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
4102 struct riscv_pcgp_hi_reloc
4109 bool undefined_weak
;
4110 riscv_pcgp_hi_reloc
*next
;
4113 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
4114 struct riscv_pcgp_lo_reloc
4117 riscv_pcgp_lo_reloc
*next
;
4122 riscv_pcgp_hi_reloc
*hi
;
4123 riscv_pcgp_lo_reloc
*lo
;
4124 } riscv_pcgp_relocs
;
4126 /* Initialize the pcgp reloc info in P. */
4129 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
4136 /* Free the pcgp reloc info in P. */
4139 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
4140 bfd
*abfd ATTRIBUTE_UNUSED
,
4141 asection
*sec ATTRIBUTE_UNUSED
)
4143 riscv_pcgp_hi_reloc
*c
;
4144 riscv_pcgp_lo_reloc
*l
;
4146 for (c
= p
->hi
; c
!= NULL
; )
4148 riscv_pcgp_hi_reloc
*next
= c
->next
;
4153 for (l
= p
->lo
; l
!= NULL
; )
4155 riscv_pcgp_lo_reloc
*next
= l
->next
;
4161 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4162 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4163 relax the corresponding lo part reloc. */
4166 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4167 bfd_vma hi_addend
, bfd_vma hi_addr
,
4168 unsigned hi_sym
, asection
*sym_sec
,
4169 bool undefined_weak
)
4171 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
4174 new->hi_sec_off
= hi_sec_off
;
4175 new->hi_addend
= hi_addend
;
4176 new->hi_addr
= hi_addr
;
4177 new->hi_sym
= hi_sym
;
4178 new->sym_sec
= sym_sec
;
4179 new->undefined_weak
= undefined_weak
;
4185 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4186 This is used by a lo part reloc to find the corresponding hi part reloc. */
4188 static riscv_pcgp_hi_reloc
*
4189 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4191 riscv_pcgp_hi_reloc
*c
;
4193 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4194 if (c
->hi_sec_off
== hi_sec_off
)
4199 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4200 This is used to record relocs that can't be relaxed. */
4203 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4205 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4208 new->hi_sec_off
= hi_sec_off
;
4214 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4215 This is used by a hi part reloc to find the corresponding lo part reloc. */
4218 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4220 riscv_pcgp_lo_reloc
*c
;
4222 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4223 if (c
->hi_sec_off
== hi_sec_off
)
4229 riscv_update_pcgp_relocs (riscv_pcgp_relocs
*p
, asection
*deleted_sec
,
4230 bfd_vma deleted_addr
, size_t deleted_count
)
4232 /* Bytes have already been deleted and toaddr should match the old section
4233 size for our checks, so adjust it here. */
4234 bfd_vma toaddr
= deleted_sec
->size
+ deleted_count
;
4235 riscv_pcgp_lo_reloc
*l
;
4236 riscv_pcgp_hi_reloc
*h
;
4238 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4239 entries where they occur after the deleted bytes. */
4240 for (l
= p
->lo
; l
!= NULL
; l
= l
->next
)
4241 if (l
->hi_sec_off
> deleted_addr
4242 && l
->hi_sec_off
< toaddr
)
4243 l
->hi_sec_off
-= deleted_count
;
4245 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4246 these values occur after the deleted bytes. */
4247 for (h
= p
->hi
; h
!= NULL
; h
= h
->next
)
4249 if (h
->hi_sec_off
> deleted_addr
4250 && h
->hi_sec_off
< toaddr
)
4251 h
->hi_sec_off
-= deleted_count
;
4252 if (h
->sym_sec
== deleted_sec
4253 && h
->hi_addr
> deleted_addr
4254 && h
->hi_addr
< toaddr
)
4255 h
->hi_addr
-= deleted_count
;
4259 /* Delete some bytes, adjust relcocations and symbol table from a section. */
4262 _riscv_relax_delete_bytes (bfd
*abfd
,
4266 struct bfd_link_info
*link_info
,
4267 riscv_pcgp_relocs
*p
,
4268 bfd_vma delete_total
,
4271 unsigned int i
, symcount
;
4272 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
4273 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4274 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4275 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4276 bfd_byte
*contents
= data
->this_hdr
.contents
;
4277 size_t bytes_to_move
= toaddr
- addr
- count
;
4279 /* Actually delete the bytes. */
4281 memmove (contents
+ addr
, contents
+ addr
+ count
+ delete_total
, bytes_to_move
);
4283 /* Still adjust relocations and symbols in non-linear times. */
4284 toaddr
= sec
->size
+ count
;
4286 /* Adjust the location of all of the relocs. Note that we need not
4287 adjust the addends, since all PC-relative references must be against
4288 symbols, which we will adjust below. */
4289 for (i
= 0; i
< sec
->reloc_count
; i
++)
4290 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
4291 data
->relocs
[i
].r_offset
-= count
;
4293 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4294 table for which these values occur after the deleted bytes. */
4296 riscv_update_pcgp_relocs (p
, sec
, addr
, count
);
4298 /* Adjust the local symbols defined in this section. */
4299 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4301 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
4302 if (sym
->st_shndx
== sec_shndx
)
4304 /* If the symbol is in the range of memory we just moved, we
4305 have to adjust its value. */
4306 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
4307 sym
->st_value
-= count
;
4309 /* If the symbol *spans* the bytes we just deleted (i.e. its
4310 *end* is in the moved bytes but its *start* isn't), then we
4311 must adjust its size.
4313 This test needs to use the original value of st_value, otherwise
4314 we might accidentally decrease size when deleting bytes right
4315 before the symbol. But since deleted relocs can't span across
4316 symbols, we can't have both a st_value and a st_size decrease,
4317 so it is simpler to just use an else. */
4318 else if (sym
->st_value
<= addr
4319 && sym
->st_value
+ sym
->st_size
> addr
4320 && sym
->st_value
+ sym
->st_size
<= toaddr
)
4321 sym
->st_size
-= count
;
4325 /* Now adjust the global symbols defined in this section. */
4326 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
4327 - symtab_hdr
->sh_info
);
4329 for (i
= 0; i
< symcount
; i
++)
4331 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
4333 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4334 containing the definition of __wrap_SYMBOL, includes a direct
4335 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4336 the same symbol (which is __wrap_SYMBOL), but still exist as two
4337 different symbols in 'sym_hashes', we don't want to adjust
4338 the global symbol __wrap_SYMBOL twice.
4340 The same problem occurs with symbols that are versioned_hidden, as
4341 foo becomes an alias for foo@BAR, and hence they need the same
4343 if (link_info
->wrap_hash
!= NULL
4344 || sym_hash
->versioned
!= unversioned
)
4346 struct elf_link_hash_entry
**cur_sym_hashes
;
4348 /* Loop only over the symbols which have already been checked. */
4349 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4352 /* If the current symbol is identical to 'sym_hash', that means
4353 the symbol was already adjusted (or at least checked). */
4354 if (*cur_sym_hashes
== sym_hash
)
4357 /* Don't adjust the symbol again. */
4358 if (cur_sym_hashes
< &sym_hashes
[i
])
4362 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4363 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4364 && sym_hash
->root
.u
.def
.section
== sec
)
4366 /* As above, adjust the value if needed. */
4367 if (sym_hash
->root
.u
.def
.value
> addr
4368 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4369 sym_hash
->root
.u
.def
.value
-= count
;
4371 /* As above, adjust the size if needed. */
4372 else if (sym_hash
->root
.u
.def
.value
<= addr
4373 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4374 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4375 sym_hash
->size
-= count
;
4382 typedef bool (*relax_delete_t
) (bfd
*, asection
*,
4384 struct bfd_link_info
*,
4385 riscv_pcgp_relocs
*,
4386 Elf_Internal_Rela
*);
4388 static relax_delete_t riscv_relax_delete_bytes
;
4390 /* Do not delete some bytes from a section while relaxing.
4391 Just mark the deleted bytes as R_RISCV_DELETE. */
4394 _riscv_relax_delete_piecewise (bfd
*abfd ATTRIBUTE_UNUSED
,
4395 asection
*sec ATTRIBUTE_UNUSED
,
4398 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
,
4399 riscv_pcgp_relocs
*p ATTRIBUTE_UNUSED
,
4400 Elf_Internal_Rela
*rel
)
4404 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4405 rel
->r_offset
= addr
;
4406 rel
->r_addend
= count
;
4410 /* Delete some bytes from a section while relaxing. */
4413 _riscv_relax_delete_immediate (bfd
*abfd
,
4417 struct bfd_link_info
*link_info
,
4418 riscv_pcgp_relocs
*p
,
4419 Elf_Internal_Rela
*rel
)
4422 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4423 return _riscv_relax_delete_bytes (abfd
, sec
, addr
, count
,
4424 link_info
, p
, 0, sec
->size
);
4427 /* Delete the bytes for R_RISCV_DELETE relocs. */
4430 riscv_relax_resolve_delete_relocs (bfd
*abfd
,
4432 struct bfd_link_info
*link_info
,
4433 Elf_Internal_Rela
*relocs
)
4435 bfd_vma delete_total
= 0;
4438 for (i
= 0; i
< sec
->reloc_count
; i
++)
4440 Elf_Internal_Rela
*rel
= relocs
+ i
;
4441 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_DELETE
)
4444 /* Find the next R_RISCV_DELETE reloc if possible. */
4445 Elf_Internal_Rela
*rel_next
= NULL
;
4446 unsigned int start
= rel
- relocs
;
4447 for (i
= start
; i
< sec
->reloc_count
; i
++)
4449 /* Since we only replace existing relocs and don't add new relocs, the
4450 relocs are in sequential order. We can skip the relocs prior to this
4451 one, making this search linear time. */
4452 rel_next
= relocs
+ i
;
4453 if (ELFNN_R_TYPE ((rel_next
)->r_info
) == R_RISCV_DELETE
4454 && (rel_next
)->r_offset
> rel
->r_offset
)
4456 BFD_ASSERT (rel_next
- rel
> 0);
4463 bfd_vma toaddr
= rel_next
== NULL
? sec
->size
: rel_next
->r_offset
;
4464 if (!_riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4465 link_info
, NULL
, delete_total
, toaddr
))
4468 delete_total
+= rel
->r_addend
;
4469 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4471 /* Skip ahead to the next delete reloc. */
4472 i
= rel_next
!= NULL
? (unsigned int) (rel_next
- relocs
- 1)
4479 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4480 struct bfd_link_info
*,
4481 Elf_Internal_Rela
*,
4482 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4483 riscv_pcgp_relocs
*,
4484 bool undefined_weak
);
4486 /* Relax AUIPC + JALR into JAL. */
4489 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4490 struct bfd_link_info
*link_info
,
4491 Elf_Internal_Rela
*rel
,
4493 bfd_vma max_alignment
,
4494 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4496 riscv_pcgp_relocs
*pcgp_relocs
,
4497 bool undefined_weak ATTRIBUTE_UNUSED
)
4499 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4500 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4501 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4502 bfd_vma auipc
, jalr
;
4503 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4505 /* If the call crosses section boundaries, an alignment directive could
4506 cause the PC-relative offset to later increase, so we need to add in the
4507 max alignment of any section inclusive from the call to the target.
4508 Otherwise, we only need to use the alignment of the current section. */
4509 if (VALID_JTYPE_IMM (foff
))
4511 if (sym_sec
->output_section
== sec
->output_section
4512 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4513 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4514 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4517 /* See if this function call can be shortened. */
4518 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4521 /* Shorten the function call. */
4522 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4524 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4525 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4526 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4527 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4529 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4530 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4534 /* Relax to C.J[AL] rd, addr. */
4535 r_type
= R_RISCV_RVC_JUMP
;
4536 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4539 else if (VALID_JTYPE_IMM (foff
))
4541 /* Relax to JAL rd, addr. */
4542 r_type
= R_RISCV_JAL
;
4543 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4547 /* Near zero, relax to JALR rd, x0, addr. */
4548 r_type
= R_RISCV_LO12_I
;
4549 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4552 /* Replace the R_RISCV_CALL reloc. */
4553 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4554 /* Replace the AUIPC. */
4555 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4557 /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */
4559 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4560 link_info
, pcgp_relocs
, rel
+ 1);
4563 /* Traverse all output sections and return the max alignment.
4565 If gp is zero, then all the output section alignments are
4566 possible candidates; Otherwise, only the output sections
4567 which are in the [gp-2K, gp+2K) range need to be considered. */
4570 _bfd_riscv_get_max_alignment (asection
*sec
, bfd_vma gp
)
4572 unsigned int max_alignment_power
= 0;
4575 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4579 && !(VALID_ITYPE_IMM (sec_addr (o
) - gp
)
4580 || VALID_ITYPE_IMM (sec_addr (o
) + o
->size
- gp
)))
4583 if (valid
&& o
->alignment_power
> max_alignment_power
)
4584 max_alignment_power
= o
->alignment_power
;
4587 return (bfd_vma
) 1 << max_alignment_power
;
4590 /* Relax non-PIC global variable references to GP-relative references. */
4593 _bfd_riscv_relax_lui (bfd
*abfd
,
4596 struct bfd_link_info
*link_info
,
4597 Elf_Internal_Rela
*rel
,
4599 bfd_vma max_alignment
,
4600 bfd_vma reserve_size
,
4602 riscv_pcgp_relocs
*pcgp_relocs
,
4603 bool undefined_weak
)
4605 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (link_info
);
4606 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4607 /* Can relax to x0 even when gp relaxation is disabled. */
4608 bfd_vma gp
= htab
->params
->relax_gp
4609 ? riscv_global_pointer_value (link_info
)
4611 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4613 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4615 if (!undefined_weak
&& gp
)
4617 /* If gp and the symbol are in the same output section, which is not the
4618 abs section, then consider only that output section's alignment. */
4619 struct bfd_link_hash_entry
*h
=
4620 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4622 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4623 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4624 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4627 /* Consider output section alignments which are in [gp-2K, gp+2K). */
4628 max_alignment
= htab
->max_alignment_for_gp
;
4629 if (max_alignment
== (bfd_vma
) -1)
4631 max_alignment
= _bfd_riscv_get_max_alignment (sec
, gp
);
4632 htab
->max_alignment_for_gp
= max_alignment
;
4637 /* Is the reference in range of x0 or gp?
4638 Valid gp range conservatively because of alignment issue.
4640 Should we also consider the alignment issue for x0 base? */
4642 || VALID_ITYPE_IMM (symval
)
4644 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4646 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
)))
4648 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4649 switch (ELFNN_R_TYPE (rel
->r_info
))
4651 case R_RISCV_LO12_I
:
4652 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4655 case R_RISCV_LO12_S
:
4656 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4660 /* Delete unnecessary LUI and reuse the reloc. */
4662 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4663 link_info
, pcgp_relocs
, rel
);
4670 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4671 account for this assuming page alignment at worst. In the presence of
4672 RELRO segment the linker aligns it by one page size, therefore sections
4673 after the segment can be moved more than one page. */
4676 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4677 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4678 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4679 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4680 : ELF_MAXPAGESIZE
)))
4682 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4683 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4684 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4685 if (rd
== 0 || rd
== X_SP
)
4688 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4689 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4691 /* Replace the R_RISCV_HI20 reloc. */
4692 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4694 /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */
4696 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4697 link_info
, pcgp_relocs
, rel
+ 1);
4703 /* Relax non-PIC TLS references to TP-relative references. */
4706 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4708 asection
*sym_sec ATTRIBUTE_UNUSED
,
4709 struct bfd_link_info
*link_info
,
4710 Elf_Internal_Rela
*rel
,
4712 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4713 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4715 riscv_pcgp_relocs
*pcgp_relocs
,
4716 bool undefined_weak ATTRIBUTE_UNUSED
)
4718 /* See if this symbol is in range of tp. */
4719 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4722 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4723 switch (ELFNN_R_TYPE (rel
->r_info
))
4725 case R_RISCV_TPREL_LO12_I
:
4726 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4729 case R_RISCV_TPREL_LO12_S
:
4730 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4733 case R_RISCV_TPREL_HI20
:
4734 case R_RISCV_TPREL_ADD
:
4735 /* Delete unnecessary instruction and reuse the reloc. */
4737 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4745 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4746 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4749 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4751 struct bfd_link_info
*link_info
,
4752 Elf_Internal_Rela
*rel
,
4754 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4755 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4756 bool *again ATTRIBUTE_UNUSED
,
4757 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4758 bool undefined_weak ATTRIBUTE_UNUSED
)
4760 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4761 bfd_vma alignment
= 1, pos
;
4762 while (alignment
<= rel
->r_addend
)
4765 symval
-= rel
->r_addend
;
4766 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4767 bfd_vma nop_bytes
= aligned_addr
- symval
;
4769 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4770 sec
->sec_flg0
= true;
4772 /* Make sure there are enough NOPs to actually achieve the alignment. */
4773 if (rel
->r_addend
< nop_bytes
)
4776 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4777 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4778 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4779 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4780 bfd_set_error (bfd_error_bad_value
);
4784 /* Delete the reloc. */
4785 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4787 /* If the number of NOPs is already correct, there's nothing to do. */
4788 if (nop_bytes
== rel
->r_addend
)
4791 /* Write as many RISC-V NOPs as we need. */
4792 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4793 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4795 /* Write a final RVC NOP if need be. */
4796 if (nop_bytes
% 4 != 0)
4797 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4799 /* Delete excess bytes. */
4800 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4801 rel
->r_addend
- nop_bytes
, link_info
,
4805 /* Relax PC-relative references to GP-relative references. */
4808 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4811 struct bfd_link_info
*link_info
,
4812 Elf_Internal_Rela
*rel
,
4814 bfd_vma max_alignment
,
4815 bfd_vma reserve_size
,
4817 riscv_pcgp_relocs
*pcgp_relocs
,
4818 bool undefined_weak
)
4820 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (link_info
);
4821 /* Can relax to x0 even when gp relaxation is disabled. */
4822 bfd_vma gp
= htab
->params
->relax_gp
4823 ? riscv_global_pointer_value (link_info
)
4826 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4828 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4829 actual target address. */
4830 riscv_pcgp_hi_reloc hi_reloc
;
4831 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4832 switch (ELFNN_R_TYPE (rel
->r_info
))
4834 case R_RISCV_PCREL_LO12_I
:
4835 case R_RISCV_PCREL_LO12_S
:
4837 /* If the %lo has an addend, it isn't for the label pointing at the
4838 hi part instruction, but rather for the symbol pointed at by the
4839 hi part instruction. So we must subtract it here for the lookup.
4840 It is still used below in the final symbol address. */
4841 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4842 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4846 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4851 symval
= hi_reloc
.hi_addr
;
4852 sym_sec
= hi_reloc
.sym_sec
;
4854 /* We can not know whether the undefined weak symbol is referenced
4855 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4856 we have to record the 'undefined_weak' flag when handling the
4857 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4858 undefined_weak
= hi_reloc
.undefined_weak
;
4862 case R_RISCV_PCREL_HI20
:
4863 /* Mergeable symbols and code might later move out of range. */
4864 if (! undefined_weak
4865 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4868 /* If the cooresponding lo relocation has already been seen then it's not
4869 safe to relax this relocation. */
4870 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4879 if (!undefined_weak
&& gp
)
4881 /* If gp and the symbol are in the same output section, which is not the
4882 abs section, then consider only that output section's alignment. */
4883 struct bfd_link_hash_entry
*h
=
4884 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4886 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4887 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4888 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4891 /* Consider output section alignments which are in [gp-2K, gp+2K). */
4892 max_alignment
= htab
->max_alignment_for_gp
;
4893 if (max_alignment
== (bfd_vma
) -1)
4895 max_alignment
= _bfd_riscv_get_max_alignment (sec
, gp
);
4896 htab
->max_alignment_for_gp
= max_alignment
;
4901 /* Is the reference in range of x0 or gp?
4902 Valid gp range conservatively because of alignment issue.
4904 Should we also consider the alignment issue for x0 base? */
4906 || VALID_ITYPE_IMM (symval
)
4908 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4910 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
)))
4912 unsigned sym
= hi_reloc
.hi_sym
;
4913 switch (ELFNN_R_TYPE (rel
->r_info
))
4915 case R_RISCV_PCREL_LO12_I
:
4916 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4917 rel
->r_addend
+= hi_reloc
.hi_addend
;
4920 case R_RISCV_PCREL_LO12_S
:
4921 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4922 rel
->r_addend
+= hi_reloc
.hi_addend
;
4925 case R_RISCV_PCREL_HI20
:
4926 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4930 ELFNN_R_SYM(rel
->r_info
),
4933 /* Delete unnecessary AUIPC and reuse the reloc. */
4935 riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4947 /* Called by after_allocation to set the information of data segment
4951 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
4952 int *data_segment_phase
)
4954 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4955 htab
->data_segment_phase
= data_segment_phase
;
4960 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs and
4961 deletes the obsolete bytes.
4962 Pass 1: Which cannot be disabled, handles code alignment directives. */
4965 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4966 struct bfd_link_info
*info
,
4969 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4970 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4971 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4972 Elf_Internal_Rela
*relocs
;
4975 bfd_vma max_alignment
, reserve_size
= 0;
4976 riscv_pcgp_relocs pcgp_relocs
;
4977 static asection
*first_section
= NULL
;
4981 if (bfd_link_relocatable (info
)
4983 || sec
->reloc_count
== 0
4984 || (sec
->flags
& SEC_RELOC
) == 0
4985 || (sec
->flags
& SEC_HAS_CONTENTS
) == 0
4986 || (info
->disable_target_specific_optimizations
4987 && info
->relax_pass
== 0)
4988 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4989 and defined in ld/ldexp.h. */
4990 || *(htab
->data_segment_phase
) == 4)
4993 /* Record the first relax section, so that we can reset the
4994 max_alignment_for_gp for the repeated relax passes. */
4995 if (first_section
== NULL
)
4996 first_section
= sec
;
4997 else if (first_section
== sec
)
4998 htab
->max_alignment_for_gp
= -1;
5000 riscv_init_pcgp_relocs (&pcgp_relocs
);
5002 /* Read this BFD's relocs if we haven't done so already. */
5004 relocs
= data
->relocs
;
5005 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
5006 info
->keep_memory
)))
5009 /* Estimate the maximum alignment for all output sections once time
5010 should be enough. */
5011 max_alignment
= htab
->max_alignment
;
5012 if (max_alignment
== (bfd_vma
) -1)
5014 max_alignment
= _bfd_riscv_get_max_alignment (sec
, 0/* gp */);
5015 htab
->max_alignment
= max_alignment
;
5018 /* Examine and consider relaxing each reloc. */
5019 for (i
= 0; i
< sec
->reloc_count
; i
++)
5022 Elf_Internal_Rela
*rel
= relocs
+ i
;
5023 relax_func_t relax_func
;
5024 int type
= ELFNN_R_TYPE (rel
->r_info
);
5027 bool undefined_weak
= false;
5030 riscv_relax_delete_bytes
= NULL
;
5031 if (info
->relax_pass
== 0)
5033 if (type
== R_RISCV_CALL
5034 || type
== R_RISCV_CALL_PLT
)
5035 relax_func
= _bfd_riscv_relax_call
;
5036 else if (type
== R_RISCV_HI20
5037 || type
== R_RISCV_LO12_I
5038 || type
== R_RISCV_LO12_S
)
5039 relax_func
= _bfd_riscv_relax_lui
;
5040 else if (type
== R_RISCV_TPREL_HI20
5041 || type
== R_RISCV_TPREL_ADD
5042 || type
== R_RISCV_TPREL_LO12_I
5043 || type
== R_RISCV_TPREL_LO12_S
)
5044 relax_func
= _bfd_riscv_relax_tls_le
;
5045 else if (!bfd_link_pic (info
)
5046 && (type
== R_RISCV_PCREL_HI20
5047 || type
== R_RISCV_PCREL_LO12_I
5048 || type
== R_RISCV_PCREL_LO12_S
))
5049 relax_func
= _bfd_riscv_relax_pc
;
5052 riscv_relax_delete_bytes
= _riscv_relax_delete_piecewise
;
5054 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
5055 if (i
== sec
->reloc_count
- 1
5056 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
5057 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
5060 /* Skip over the R_RISCV_RELAX. */
5063 else if (info
->relax_pass
== 1 && type
== R_RISCV_ALIGN
)
5065 relax_func
= _bfd_riscv_relax_align
;
5066 riscv_relax_delete_bytes
= _riscv_relax_delete_immediate
;
5071 data
->relocs
= relocs
;
5073 /* Read this BFD's contents if we haven't done so already. */
5074 if (!data
->this_hdr
.contents
5075 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
5078 /* Read this BFD's symbols if we haven't done so already. */
5079 if (symtab_hdr
->sh_info
!= 0
5080 && !symtab_hdr
->contents
5081 && !(symtab_hdr
->contents
=
5082 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
5083 symtab_hdr
->sh_info
,
5084 0, NULL
, NULL
, NULL
)))
5087 /* Get the value of the symbol referred to by the reloc. */
5088 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
5090 /* A local symbol. */
5091 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
5092 + ELFNN_R_SYM (rel
->r_info
));
5093 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
5094 ? 0 : isym
->st_size
- rel
->r_addend
;
5096 /* Relocate against local STT_GNU_IFUNC symbol. we have created
5097 a fake global symbol entry for this, so deal with the local ifunc
5099 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
5102 if (isym
->st_shndx
== SHN_UNDEF
)
5103 sym_sec
= sec
, symval
= rel
->r_offset
;
5106 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
5107 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
5109 /* The purpose of this code is unknown. It breaks linker scripts
5110 for embedded development that place sections at address zero.
5111 This code is believed to be unnecessary. Disabling it but not
5112 yet removing it, in case something breaks. */
5113 if (sec_addr (sym_sec
) == 0)
5116 symval
= isym
->st_value
;
5118 symtype
= ELF_ST_TYPE (isym
->st_info
);
5123 struct elf_link_hash_entry
*h
;
5125 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
5126 h
= elf_sym_hashes (abfd
)[indx
];
5128 while (h
->root
.type
== bfd_link_hash_indirect
5129 || h
->root
.type
== bfd_link_hash_warning
)
5130 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5132 /* Disable the relaxation for ifunc. */
5133 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
5136 /* Maybe we should check UNDEFWEAK_NO_DYNAMIC_RELOC here? But that
5137 will break the undefweak relaxation testcases, so just make sure
5138 we won't do relaxations for linker_def symbols in short-term. */
5139 if (h
->root
.type
== bfd_link_hash_undefweak
5140 /* The linker_def symbol like __ehdr_start that may be undefweak
5141 for now, but will be guaranteed to be defined later. */
5142 && !h
->root
.linker_def
5143 && (relax_func
== _bfd_riscv_relax_lui
5144 || relax_func
== _bfd_riscv_relax_pc
))
5146 /* For the lui and auipc relaxations, since the symbol
5147 value of an undefined weak symbol is always be zero,
5148 we can optimize the patterns into a single LI/MV/ADDI
5151 Note that, creating shared libraries and pie output may
5152 break the rule above. Fortunately, since we do not relax
5153 pc relocs when creating shared libraries and pie output,
5154 and the absolute address access for R_RISCV_HI20 isn't
5155 allowed when "-fPIC" is set, the problem of creating shared
5156 libraries can not happen currently. Once we support the
5157 auipc relaxations when creating shared libraries, then we will
5158 need the more rigorous checking for this optimization. */
5159 undefined_weak
= true;
5162 /* This line has to match the check in riscv_elf_relocate_section
5163 in the R_RISCV_CALL[_PLT] case. */
5164 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
5166 sym_sec
= htab
->elf
.splt
;
5167 symval
= h
->plt
.offset
;
5169 else if (undefined_weak
)
5172 sym_sec
= bfd_und_section_ptr
;
5174 else if ((h
->root
.type
== bfd_link_hash_defined
5175 || h
->root
.type
== bfd_link_hash_defweak
)
5176 && h
->root
.u
.def
.section
!= NULL
5177 && h
->root
.u
.def
.section
->output_section
!= NULL
)
5179 symval
= h
->root
.u
.def
.value
;
5180 sym_sec
= h
->root
.u
.def
.section
;
5185 if (h
->type
!= STT_FUNC
)
5187 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
5191 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
5192 && (sym_sec
->flags
& SEC_MERGE
))
5194 /* At this stage in linking, no SEC_MERGE symbol has been
5195 adjusted, so all references to such symbols need to be
5196 passed through _bfd_merged_section_offset. (Later, in
5197 relocate_section, all SEC_MERGE symbols *except* for
5198 section symbols have been adjusted.)
5200 gas may reduce relocations against symbols in SEC_MERGE
5201 sections to a relocation against the section symbol when
5202 the original addend was zero. When the reloc is against
5203 a section symbol we should include the addend in the
5204 offset passed to _bfd_merged_section_offset, since the
5205 location of interest is the original symbol. On the
5206 other hand, an access to "sym+addend" where "sym" is not
5207 a section symbol should not include the addend; Such an
5208 access is presumed to be an offset from "sym"; The
5209 location of interest is just "sym". */
5210 if (symtype
== STT_SECTION
)
5211 symval
+= rel
->r_addend
;
5213 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
5214 elf_section_data (sym_sec
)->sec_info
,
5217 if (symtype
!= STT_SECTION
)
5218 symval
+= rel
->r_addend
;
5221 symval
+= rel
->r_addend
;
5223 symval
+= sec_addr (sym_sec
);
5225 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
5226 max_alignment
, reserve_size
, again
,
5227 &pcgp_relocs
, undefined_weak
))
5231 /* Resolve R_RISCV_DELETE relocations. */
5232 if (!riscv_relax_resolve_delete_relocs (abfd
, sec
, info
, relocs
))
5238 if (relocs
!= data
->relocs
)
5240 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
5246 # define PRSTATUS_SIZE 204
5247 # define PRSTATUS_OFFSET_PR_CURSIG 12
5248 # define PRSTATUS_OFFSET_PR_PID 24
5249 # define PRSTATUS_OFFSET_PR_REG 72
5250 # define ELF_GREGSET_T_SIZE 128
5251 # define PRPSINFO_SIZE 128
5252 # define PRPSINFO_OFFSET_PR_PID 16
5253 # define PRPSINFO_OFFSET_PR_FNAME 32
5254 # define PRPSINFO_OFFSET_PR_PSARGS 48
5255 # define PRPSINFO_PR_FNAME_LENGTH 16
5256 # define PRPSINFO_PR_PSARGS_LENGTH 80
5258 # define PRSTATUS_SIZE 376
5259 # define PRSTATUS_OFFSET_PR_CURSIG 12
5260 # define PRSTATUS_OFFSET_PR_PID 32
5261 # define PRSTATUS_OFFSET_PR_REG 112
5262 # define ELF_GREGSET_T_SIZE 256
5263 # define PRPSINFO_SIZE 136
5264 # define PRPSINFO_OFFSET_PR_PID 24
5265 # define PRPSINFO_OFFSET_PR_FNAME 40
5266 # define PRPSINFO_OFFSET_PR_PSARGS 56
5267 # define PRPSINFO_PR_FNAME_LENGTH 16
5268 # define PRPSINFO_PR_PSARGS_LENGTH 80
5271 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
5272 before the generic code in elf.c. By checking the compiler defines we
5273 only perform any action here if the generic code would otherwise not be
5274 able to help us. The intention is that bare metal core dumps (where the
5275 prstatus_t and/or prpsinfo_t might not be available) will use this code,
5276 while non bare metal tools will use the generic elf code. */
5279 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
5280 char *buf ATTRIBUTE_UNUSED
,
5281 int *bufsiz ATTRIBUTE_UNUSED
,
5282 int note_type ATTRIBUTE_UNUSED
, ...)
5289 #if !defined (HAVE_PRPSINFO_T)
5292 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5295 va_start (ap
, note_type
);
5296 memset (data
, 0, sizeof (data
));
5297 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5298 PRPSINFO_PR_FNAME_LENGTH
);
5299 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5301 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5302 -Wstringop-truncation:
5303 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5305 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5307 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5308 PRPSINFO_PR_PSARGS_LENGTH
);
5309 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5313 return elfcore_write_note (abfd
, buf
, bufsiz
,
5314 "CORE", note_type
, data
, sizeof (data
));
5316 #endif /* !HAVE_PRPSINFO_T */
5318 #if !defined (HAVE_PRSTATUS_T)
5321 char data
[PRSTATUS_SIZE
];
5327 va_start (ap
, note_type
);
5328 memset (data
, 0, sizeof(data
));
5329 pid
= va_arg (ap
, long);
5330 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5331 cursig
= va_arg (ap
, int);
5332 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5333 greg
= va_arg (ap
, const void *);
5334 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5335 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5337 return elfcore_write_note (abfd
, buf
, bufsiz
,
5338 "CORE", note_type
, data
, sizeof (data
));
5340 #endif /* !HAVE_PRSTATUS_T */
5344 /* Support for core dump NOTE sections. */
5347 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5349 switch (note
->descsz
)
5354 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5356 elf_tdata (abfd
)->core
->signal
5357 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5360 elf_tdata (abfd
)->core
->lwpid
5361 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5365 /* Make a ".reg/999" section. */
5366 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5367 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5371 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5373 switch (note
->descsz
)
5378 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5380 elf_tdata (abfd
)->core
->pid
5381 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5384 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5385 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5386 PRPSINFO_PR_FNAME_LENGTH
);
5389 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5390 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5391 PRPSINFO_PR_PSARGS_LENGTH
);
5395 /* Note that for some reason, a spurious space is tacked
5396 onto the end of the args in some (at least one anyway)
5397 implementations, so strip it off if it exists. */
5400 char *command
= elf_tdata (abfd
)->core
->command
;
5401 int n
= strlen (command
);
5403 if (0 < n
&& command
[n
- 1] == ' ')
5404 command
[n
- 1] = '\0';
5410 /* Set the right mach type. */
5413 riscv_elf_object_p (bfd
*abfd
)
5415 /* There are only two mach types in RISCV currently. */
5416 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5417 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5418 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5420 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5425 /* Determine whether an object attribute tag takes an integer, a
5429 riscv_elf_obj_attrs_arg_type (int tag
)
5431 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5434 /* Do not choose mapping symbols as a function name. */
5436 static bfd_size_type
5437 riscv_maybe_function_sym (const asymbol
*sym
,
5441 if (sym
->flags
& BSF_LOCAL
5442 && riscv_elf_is_mapping_symbols (sym
->name
))
5445 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5448 /* Treat the following cases as target special symbols, they are
5452 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5454 /* PR27584, local and empty symbols. Since they are usually
5455 generated for pcrel relocations. */
5456 return (!strcmp (sym
->name
, "")
5457 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5458 /* PR27916, mapping symbols. */
5459 || riscv_elf_is_mapping_symbols (sym
->name
));
5463 riscv_elf_additional_program_headers (bfd
*abfd
,
5464 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5468 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5469 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5476 riscv_elf_modify_segment_map (bfd
*abfd
,
5477 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5480 struct elf_segment_map
*m
, **pm
;
5483 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5485 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5488 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5489 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5491 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5496 m
= bfd_zalloc (abfd
, amt
);
5500 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5504 /* We want to put it after the PHDR and INTERP segments. */
5505 pm
= &elf_seg_map (abfd
);
5507 && ((*pm
)->p_type
== PT_PHDR
5508 || (*pm
)->p_type
== PT_INTERP
))
5519 /* Merge non-visibility st_other attributes. */
5522 riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
5523 unsigned int st_other
,
5524 bool definition ATTRIBUTE_UNUSED
,
5525 bool dynamic ATTRIBUTE_UNUSED
)
5527 unsigned int isym_sto
= st_other
& ~ELF_ST_VISIBILITY (-1);
5528 unsigned int h_sto
= h
->other
& ~ELF_ST_VISIBILITY (-1);
5530 if (isym_sto
== h_sto
)
5533 if (isym_sto
& ~STO_RISCV_VARIANT_CC
)
5534 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5535 h
->root
.root
.string
, isym_sto
);
5537 if (isym_sto
& STO_RISCV_VARIANT_CC
)
5538 h
->other
|= STO_RISCV_VARIANT_CC
;
5541 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5542 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5543 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5544 #define TARGET_BIG_NAME "elfNN-bigriscv"
5546 #define elf_backend_reloc_type_class riscv_reloc_type_class
5548 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5549 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5550 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5551 #define bfd_elfNN_bfd_merge_private_bfd_data \
5552 _bfd_riscv_elf_merge_private_bfd_data
5553 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5555 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5556 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5557 #define elf_backend_check_relocs riscv_elf_check_relocs
5558 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5559 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5560 #define elf_backend_relocate_section riscv_elf_relocate_section
5561 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5562 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5563 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5564 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5565 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5566 #define elf_backend_object_p riscv_elf_object_p
5567 #define elf_backend_write_core_note riscv_write_core_note
5568 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5569 #define elf_info_to_howto_rel NULL
5570 #define elf_info_to_howto riscv_info_to_howto_rela
5571 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5572 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5573 #define elf_backend_additional_program_headers \
5574 riscv_elf_additional_program_headers
5575 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5576 #define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
5578 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5580 #define elf_backend_can_gc_sections 1
5581 #define elf_backend_can_refcount 1
5582 #define elf_backend_want_got_plt 1
5583 #define elf_backend_plt_readonly 1
5584 #define elf_backend_plt_alignment 4
5585 #define elf_backend_want_plt_sym 1
5586 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5587 #define elf_backend_want_dynrelro 1
5588 #define elf_backend_rela_normal 1
5589 #define elf_backend_default_execstack 0
5591 #undef elf_backend_obj_attrs_vendor
5592 #define elf_backend_obj_attrs_vendor "riscv"
5593 #undef elf_backend_obj_attrs_arg_type
5594 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5595 #undef elf_backend_obj_attrs_section_type
5596 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5597 #undef elf_backend_obj_attrs_section
5598 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5600 #include "elfNN-target.h"