1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2025 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
41 /* True if dynamic relocation is needed. If we are creating a shared library,
42 and this is a reloc against a global symbol, or a non PC relative reloc
43 against a local symbol, then we need to copy the reloc into the shared
44 library. However, if we are linking with -Bsymbolic, we do not need to
45 copy a reloc against a global symbol which is defined in an object we are
46 including in the link (i.e., DEF_REGULAR is set).
48 At this point we have not seen all the input files, so it is possible that
49 DEF_REGULAR is not set now but will be set later (it is never cleared).
50 In case of a weak definition, DEF_REGULAR may be cleared later by a strong
51 definition in a shared library. We account for that possibility below by
52 storing information in the relocs_copied field of the hash table entry.
53 A similar situation occurs when creating shared libraries and symbol
54 visibility changes render the symbol local.
56 If on the other hand, we are creating an executable, we may need to keep
57 relocations for symbols satisfied by a dynamic library if we manage to
58 avoid copy relocs for the symbol.
60 Generate dynamic pointer relocation against STT_GNU_IFUNC symbol in the
61 non-code section (R_RISCV_32/R_RISCV_64). */
62 #define RISCV_NEED_DYNAMIC_RELOC(PCREL, INFO, H, SEC) \
63 ((bfd_link_pic (INFO) \
64 && ((SEC)->flags & SEC_ALLOC) != 0 \
67 && (!(INFO)->symbolic \
68 || (H)->root.type == bfd_link_hash_defweak \
69 || !(H)->def_regular)))) \
70 || (!bfd_link_pic (INFO) \
71 && ((SEC)->flags & SEC_ALLOC) != 0 \
73 && ((H)->root.type == bfd_link_hash_defweak \
74 || !(H)->def_regular)) \
75 || (!bfd_link_pic (INFO) \
77 && (H)->type == STT_GNU_IFUNC \
78 && ((SEC)->flags & SEC_CODE) == 0))
80 /* True if dynamic relocation should be generated. */
81 #define RISCV_GENERATE_DYNAMIC_RELOC(PCREL, INFO, H, RESOLVED_TO_ZERO) \
82 ((bfd_link_pic (INFO) \
84 || (ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT && !(RESOLVED_TO_ZERO)) \
85 || (H)->root.type != bfd_link_hash_undefweak) \
87 || !SYMBOL_CALLS_LOCAL ((INFO), (H)))) \
88 || (!bfd_link_pic (INFO) \
90 && (H)->dynindx != -1 \
91 && !(H)->non_got_ref \
92 && (((H)->def_dynamic && !(H)->def_regular) \
93 || (H)->root.type == bfd_link_hash_undefweak \
94 || (H)->root.type == bfd_link_hash_undefined)))
96 /* True if this input relocation should be copied to output. H->dynindx
97 may be -1 if this symbol was marked to become local. */
98 #define RISCV_COPY_INPUT_RELOC(INFO, H) \
100 && (H)->dynindx != -1 \
101 && (!bfd_link_pic (INFO) \
102 || !SYMBOLIC_BIND ((INFO), (H)) \
103 || !(H)->def_regular))
105 /* True if this is actually a static link, or it is a -Bsymbolic link
106 and the symbol is defined locally, or the symbol was forced to be
107 local because of a version file. */
108 #define RISCV_RESOLVED_LOCALLY(INFO, H) \
109 (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (elf_hash_table (INFO)->dynamic_sections_created, \
110 bfd_link_pic (INFO), (H)) \
111 || (bfd_link_pic (INFO) \
112 && SYMBOL_REFERENCES_LOCAL ((INFO), (H))))
114 /* Set NEED_RELOC to true if TLS GD/IE needs dynamic relocations, and INDX will
115 be the dynamic index. PR22263, use the same check in allocate_dynrelocs and
116 riscv_elf_relocate_section for TLS GD/IE. */
117 #define RISCV_TLS_GD_IE_NEED_DYN_RELOC(INFO, DYN, H, INDX, NEED_RELOC) \
121 && (H)->dynindx != -1 \
122 && WILL_CALL_FINISH_DYNAMIC_SYMBOL ((DYN), bfd_link_pic (INFO), (H)) \
123 && (bfd_link_dll (INFO) || !SYMBOL_REFERENCES_LOCAL ((INFO), (H)))) \
124 (INDX) = (H)->dynindx; \
125 if ((bfd_link_dll (INFO) || (INDX) != 0) \
127 || ELF_ST_VISIBILITY ((H)->other) == STV_DEFAULT \
128 || (H)->root.type != bfd_link_hash_undefweak)) \
129 (NEED_RELOC) = true; \
135 #define MINUS_ONE ((bfd_vma)0 - 1)
137 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
139 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
141 /* The name of the dynamic interpreter. This is put in the .interp
144 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
145 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
147 #define ELF_ARCH bfd_arch_riscv
148 #define ELF_TARGET_ID RISCV_ELF_DATA
149 #define ELF_MACHINE_CODE EM_RISCV
150 #define ELF_MAXPAGESIZE 0x1000
151 #define ELF_COMMONPAGESIZE 0x1000
153 #define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
155 /* RISC-V ELF linker hash entry. */
157 struct riscv_elf_link_hash_entry
159 struct elf_link_hash_entry elf
;
161 #define GOT_UNKNOWN 0
166 #define GOT_TLSDESC 16
170 #define riscv_elf_hash_entry(ent) \
171 ((struct riscv_elf_link_hash_entry *) (ent))
173 struct _bfd_riscv_elf_obj_tdata
175 struct elf_obj_tdata root
;
177 /* tls_type for each local got entry. */
178 char *local_got_tls_type
;
181 #define _bfd_riscv_elf_tdata(abfd) \
182 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
184 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
185 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
187 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
188 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
189 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
191 #define is_riscv_elf(bfd) \
192 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
193 && elf_tdata (bfd) != NULL \
194 && elf_object_id (bfd) == RISCV_ELF_DATA)
197 elfNN_riscv_mkobject (bfd
*abfd
)
199 return bfd_elf_allocate_object (abfd
,
200 sizeof (struct _bfd_riscv_elf_obj_tdata
));
203 #include "elf/common.h"
204 #include "elf/internal.h"
206 struct riscv_elf_link_hash_table
208 struct elf_link_hash_table elf
;
210 /* Various options and other info passed from the linker. */
211 struct riscv_elf_params
*params
;
213 /* Short-cuts to get to dynamic linker sections. */
216 /* The max alignment of output sections. */
217 bfd_vma max_alignment
;
219 /* The max alignment of output sections in [gp-2K, gp+2K) range. */
220 bfd_vma max_alignment_for_gp
;
222 /* Used by local STT_GNU_IFUNC symbols. */
223 htab_t loc_hash_table
;
224 void * loc_hash_memory
;
226 /* The index of the last unused .rel.iplt slot. */
227 bfd_vma last_iplt_index
;
229 /* The data segment phase, don't relax the section
230 when it is exp_seg_relro_adjust. */
231 int *data_segment_phase
;
233 /* Relocations for variant CC symbols may be present. */
237 /* Instruction access functions. */
238 #define riscv_get_insn(bits, ptr) \
239 ((bits) == 16 ? bfd_getl16 (ptr) \
240 : (bits) == 32 ? bfd_getl32 (ptr) \
241 : (bits) == 64 ? bfd_getl64 (ptr) \
242 : (abort (), (bfd_vma) - 1))
243 #define riscv_put_insn(bits, val, ptr) \
244 ((bits) == 16 ? bfd_putl16 (val, ptr) \
245 : (bits) == 32 ? bfd_putl32 (val, ptr) \
246 : (bits) == 64 ? bfd_putl64 (val, ptr) \
247 : (abort (), (void) 0))
249 /* Get the RISC-V ELF linker hash table from a link_info structure. */
250 #define riscv_elf_hash_table(p) \
251 ((is_elf_hash_table ((p)->hash) \
252 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
253 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
256 riscv_elfNN_set_options (struct bfd_link_info
*link_info
,
257 struct riscv_elf_params
*params
)
259 riscv_elf_hash_table (link_info
)->params
= params
;
263 riscv_info_to_howto_rela (bfd
*abfd
,
265 Elf_Internal_Rela
*dst
)
267 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
268 return cache_ptr
->howto
!= NULL
;
272 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
274 const struct elf_backend_data
*bed
;
277 bed
= get_elf_backend_data (abfd
);
278 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
279 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
282 /* Return true if a relocation is modifying an instruction. */
285 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
287 /* Heuristic: A multibyte destination with a nontrivial mask
289 return (howto
->bitsize
> 8
290 && howto
->dst_mask
!= 0
291 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
292 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
296 #define PLT_HEADER_INSNS 8
297 #define PLT_ENTRY_INSNS 4
298 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
299 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
300 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
301 #define TLS_GD_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
302 #define TLS_IE_GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
303 #define TLSDESC_GOT_ENTRY_SIZE (RISCV_ELF_WORD_BYTES * 2)
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
)))
511 ret
->max_alignment
= (bfd_vma
) -1;
512 ret
->max_alignment_for_gp
= (bfd_vma
) -1;
514 /* Create hash table for local ifunc. */
515 ret
->loc_hash_table
= htab_try_create (1024,
516 riscv_elf_local_htab_hash
,
517 riscv_elf_local_htab_eq
,
519 ret
->loc_hash_memory
= objalloc_create ();
520 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
522 riscv_elf_link_hash_table_free (abfd
);
525 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
527 return &ret
->elf
.root
;
530 /* Create the .got section. */
533 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
537 struct elf_link_hash_entry
*h
;
538 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
539 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
541 /* This function may be called more than once. */
542 if (htab
->sgot
!= NULL
)
545 flags
= bed
->dynamic_sec_flags
;
547 s
= bfd_make_section_anyway_with_flags (abfd
,
548 (bed
->rela_plts_and_copies_p
549 ? ".rela.got" : ".rel.got"),
550 (bed
->dynamic_sec_flags
553 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
557 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
559 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
563 /* The first bit of the global offset table is the header. */
564 s
->size
+= bed
->got_header_size
;
566 if (bed
->want_got_plt
)
568 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
570 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
574 /* Reserve room for the header. */
575 s
->size
+= GOTPLT_HEADER_SIZE
;
578 if (bed
->want_got_sym
)
580 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
581 section. We don't do this in the linker script because we don't want
582 to define the symbol if we are not creating a global offset
584 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
585 "_GLOBAL_OFFSET_TABLE_");
586 elf_hash_table (info
)->hgot
= h
;
594 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
595 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
599 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
600 struct bfd_link_info
*info
)
602 struct riscv_elf_link_hash_table
*htab
;
604 htab
= riscv_elf_hash_table (info
);
605 BFD_ASSERT (htab
!= NULL
);
607 if (!riscv_elf_create_got_section (dynobj
, info
))
610 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
613 if (!bfd_link_pic (info
))
615 /* Technically, this section doesn't have contents. It is used as the
616 target of TLS copy relocs, to copy TLS data from shared libraries into
617 the executable. However, if we don't mark it as loadable, then it
618 matches the IS_TBSS test in ldlang.c, and there is no run-time address
619 space allocated for it even though it has SEC_ALLOC. That test is
620 correct for .tbss, but not correct for this section. There is also
621 a second problem that having a section with no contents can only work
622 if it comes after all sections with contents in the same segment,
623 but the linker script does not guarantee that. This is just mixed in
624 with other .tdata.* sections. We can fix both problems by lying and
625 saying that there are contents. This section is expected to be small
626 so this should not cause a significant extra program startup cost. */
628 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
629 (SEC_ALLOC
| SEC_THREAD_LOCAL
630 | SEC_LOAD
| SEC_DATA
632 | SEC_LINKER_CREATED
));
635 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
636 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
642 /* Copy the extra info we tack onto an elf_link_hash_entry. */
645 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
646 struct elf_link_hash_entry
*dir
,
647 struct elf_link_hash_entry
*ind
)
649 struct riscv_elf_link_hash_entry
*edir
, *eind
;
651 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
652 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
654 if (ind
->root
.type
== bfd_link_hash_indirect
655 && dir
->got
.refcount
<= 0)
657 edir
->tls_type
= eind
->tls_type
;
658 eind
->tls_type
= GOT_UNKNOWN
;
660 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
664 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
665 unsigned long symndx
, char tls_type
)
667 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
669 *new_tls_type
|= tls_type
;
670 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
672 (*_bfd_error_handler
)
673 (_("%pB: `%s' accessed both as normal and thread local symbol"),
674 abfd
, h
? h
->root
.root
.string
: "<local>");
681 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
682 struct elf_link_hash_entry
*h
, long symndx
)
684 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
685 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
687 if (htab
->elf
.sgot
== NULL
)
689 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
695 h
->got
.refcount
+= 1;
699 /* This is a global offset table entry for a local symbol. */
700 if (elf_local_got_refcounts (abfd
) == NULL
)
702 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
703 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
705 _bfd_riscv_elf_local_got_tls_type (abfd
)
706 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
708 elf_local_got_refcounts (abfd
) [symndx
] += 1;
714 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
716 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
718 /* We propably can improve the information to tell users that they
719 should be recompile the code with -fPIC or -fPIE, just like what
721 (*_bfd_error_handler
)
722 (_("%pB: relocation %s against `%s' can not be used when making a shared "
723 "object; recompile with -fPIC"),
724 abfd
, r
? r
->name
: _("<unknown>"),
725 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
726 bfd_set_error (bfd_error_bad_value
);
730 /* Look through the relocs for a section during the first phase, and
731 allocate space in the global offset table or procedure linkage
735 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
736 asection
*sec
, const Elf_Internal_Rela
*relocs
)
738 struct riscv_elf_link_hash_table
*htab
;
739 Elf_Internal_Shdr
*symtab_hdr
;
740 struct elf_link_hash_entry
**sym_hashes
;
741 const Elf_Internal_Rela
*rel
;
742 asection
*sreloc
= NULL
;
744 if (bfd_link_relocatable (info
))
747 htab
= riscv_elf_hash_table (info
);
748 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
749 sym_hashes
= elf_sym_hashes (abfd
);
751 if (htab
->elf
.dynobj
== NULL
)
752 htab
->elf
.dynobj
= abfd
;
754 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
757 unsigned int r_symndx
;
758 struct elf_link_hash_entry
*h
;
759 bool is_abs_symbol
= false;
761 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
762 r_type
= ELFNN_R_TYPE (rel
->r_info
);
764 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
766 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
771 if (r_symndx
< symtab_hdr
->sh_info
)
773 /* A local symbol. */
774 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
779 is_abs_symbol
= isym
->st_shndx
== SHN_ABS
? true : false;
781 /* Check relocation against local STT_GNU_IFUNC symbol. */
782 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
784 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
788 /* Fake STT_GNU_IFUNC global symbol. */
789 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
791 h
->type
= STT_GNU_IFUNC
;
795 h
->root
.type
= bfd_link_hash_defined
;
802 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
803 while (h
->root
.type
== bfd_link_hash_indirect
804 || h
->root
.type
== bfd_link_hash_warning
)
805 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
807 is_abs_symbol
= bfd_is_abs_symbol (&h
->root
) ? true : false;
817 case R_RISCV_CALL_PLT
:
819 case R_RISCV_GOT_HI20
:
820 case R_RISCV_PCREL_HI20
:
821 /* Create the ifunc sections, iplt and ipltgot, for static
823 if (h
->type
== STT_GNU_IFUNC
824 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
832 /* It is referenced by a non-shared object. */
838 case R_RISCV_TLS_GD_HI20
:
839 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
840 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
844 case R_RISCV_TLS_GOT_HI20
:
845 if (bfd_link_dll (info
))
846 info
->flags
|= DF_STATIC_TLS
;
847 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
848 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
852 case R_RISCV_GOT_HI20
:
853 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
854 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
858 case R_RISCV_TLSDESC_HI20
:
859 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
860 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLSDESC
))
865 case R_RISCV_CALL_PLT
:
866 /* These symbol requires a procedure linkage table entry.
867 We actually build the entry in adjust_dynamic_symbol,
868 because these might be a case of linking PIC code without
869 linking in any dynamic objects, in which case we don't
870 need to generate a procedure linkage table after all. */
872 /* If it is a local symbol, then we resolve it directly
873 without creating a PLT entry. */
878 h
->plt
.refcount
+= 1;
881 case R_RISCV_PCREL_HI20
:
883 && h
->type
== STT_GNU_IFUNC
)
886 h
->pointer_equality_needed
= 1;
888 /* We don't use the PCREL_HI20 in the data section,
889 so we always need the plt when it refers to
891 h
->plt
.refcount
+= 1;
894 /* The non-preemptible absolute symbol shouldn't be referneced with
895 pc-relative relocation when generating shared object. However,
896 PCREL_HI20/LO12 relocs are always bind locally when generating
897 shared object, so all absolute symbol referenced need to be
898 disallowed, except they are defined in linker script.
900 Maybe we should add this check for all pc-relative relocations,
901 please see pr28789 and pr25749 for details. */
902 if (bfd_link_pic (info
)
903 /* (h == NULL || SYMBOL_REFERENCES_LOCAL (info, h)) */
906 if (h
!= NULL
&& (h
)->root
.ldscript_def
)
907 /* Disallow the absolute symbol defined in linker script here
908 will cause the glibc-linux toolchain build failed, so regard
909 them as pc-relative symbols, just like what x86 did. */
914 if (h
->root
.root
.string
)
915 name
= h
->root
.root
.string
;
918 Elf_Internal_Sym
*sym
;
919 sym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
, abfd
,
921 name
= bfd_elf_sym_name (abfd
, symtab_hdr
, sym
, NULL
);
924 reloc_howto_type
*r_t
=
925 riscv_elf_rtype_to_howto (abfd
, r_type
);
927 (_("%pB: relocation %s against absolute symbol `%s' can "
928 "not be used when making a shared object"),
929 abfd
, r_t
? r_t
->name
: _("<unknown>"), name
);
930 bfd_set_error (bfd_error_bad_value
);
938 case R_RISCV_RVC_BRANCH
:
939 case R_RISCV_RVC_JUMP
:
940 /* In shared libraries and pie, these relocs are known
942 if (bfd_link_pic (info
))
946 case R_RISCV_TPREL_HI20
:
947 /* This is not allowed in the pic, but okay in pie. */
948 if (!bfd_link_executable (info
))
949 return bad_static_reloc (abfd
, r_type
, h
);
951 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
955 if (bfd_link_pic (info
))
956 return bad_static_reloc (abfd
, r_type
, h
);
961 && bfd_link_pic (info
)
962 && (sec
->flags
& SEC_ALLOC
) != 0)
967 reloc_howto_type
*r_t
= riscv_elf_rtype_to_howto (abfd
, r_type
);
969 (_("%pB: relocation %s against non-absolute symbol `%s' can "
970 "not be used in RVNN when making a shared object"),
971 abfd
, r_t
? r_t
->name
: _("<unknown>"),
972 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
973 bfd_set_error (bfd_error_bad_value
);
979 case R_RISCV_JUMP_SLOT
:
980 case R_RISCV_RELATIVE
:
987 && (!bfd_link_pic (info
)
988 || h
->type
== STT_GNU_IFUNC
))
990 /* This reloc might not bind locally. */
992 h
->pointer_equality_needed
= 1;
995 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
997 /* We may need a .plt entry if the symbol is a function
998 defined in a shared lib or is a function referenced
999 from the code or read-only section. */
1000 h
->plt
.refcount
+= 1;
1004 reloc_howto_type
*r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
1005 if (RISCV_NEED_DYNAMIC_RELOC (r
->pc_relative
, info
, h
, sec
))
1007 struct elf_dyn_relocs
*p
;
1008 struct elf_dyn_relocs
**head
;
1010 /* When creating a shared object, we must copy these
1011 relocs into the output file. We create a reloc
1012 section in dynobj and make room for the reloc. */
1015 sreloc
= _bfd_elf_make_dynamic_reloc_section
1016 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
1017 abfd
, /*rela?*/ true);
1023 /* If this is a global symbol, we count the number of
1024 relocations we need for this symbol. */
1026 head
= &h
->dyn_relocs
;
1029 /* Track dynamic relocs needed for local syms too.
1030 We really need local syms available to do this
1035 Elf_Internal_Sym
*isym
;
1037 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
1042 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1046 vpp
= &elf_section_data (s
)->local_dynrel
;
1047 head
= (struct elf_dyn_relocs
**) vpp
;
1051 if (p
== NULL
|| p
->sec
!= sec
)
1053 size_t amt
= sizeof *p
;
1054 p
= ((struct elf_dyn_relocs
*)
1055 bfd_alloc (htab
->elf
.dynobj
, amt
));
1066 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
1079 /* Adjust a symbol defined by a dynamic object and referenced by a
1080 regular object. The current definition is in some section of the
1081 dynamic object, but we're not including those sections. We have to
1082 change the definition to something the rest of the link can
1086 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
1087 struct elf_link_hash_entry
*h
)
1089 struct riscv_elf_link_hash_table
*htab
;
1090 struct riscv_elf_link_hash_entry
* eh
;
1094 htab
= riscv_elf_hash_table (info
);
1095 BFD_ASSERT (htab
!= NULL
);
1097 dynobj
= htab
->elf
.dynobj
;
1099 /* Make sure we know what is going on here. */
1100 BFD_ASSERT (dynobj
!= NULL
1102 || h
->type
== STT_GNU_IFUNC
1106 && !h
->def_regular
)));
1108 /* If this is a function, put it in the procedure linkage table. We
1109 will fill in the contents of the procedure linkage table later
1110 (although we could actually do it here). */
1111 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1113 if (h
->plt
.refcount
<= 0
1114 || (h
->type
!= STT_GNU_IFUNC
1115 && (SYMBOL_CALLS_LOCAL (info
, h
)
1116 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1117 && h
->root
.type
== bfd_link_hash_undefweak
))))
1119 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1120 input file, but the symbol was never referred to by a dynamic
1121 object, or if all references were garbage collected. In such
1122 a case, we don't actually need to build a PLT entry. */
1123 h
->plt
.offset
= (bfd_vma
) -1;
1130 h
->plt
.offset
= (bfd_vma
) -1;
1132 /* If this is a weak symbol, and there is a real definition, the
1133 processor independent code will have arranged for us to see the
1134 real definition first, and we can just use the same value. */
1135 if (h
->is_weakalias
)
1137 struct elf_link_hash_entry
*def
= weakdef (h
);
1138 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1139 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1140 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1144 /* This is a reference to a symbol defined by a dynamic object which
1145 is not a function. */
1147 /* If we are creating a shared library, we must presume that the
1148 only references to the symbol are via the global offset table.
1149 For such cases we need not do anything here; the relocations will
1150 be handled correctly by relocate_section. */
1151 if (bfd_link_pic (info
))
1154 /* If there are no references to this symbol that do not use the
1155 GOT, we don't need to generate a copy reloc. */
1156 if (!h
->non_got_ref
)
1159 /* If -z nocopyreloc was given, we won't generate them either. */
1160 if (info
->nocopyreloc
)
1166 /* If we don't find any dynamic relocs in read-only sections, then
1167 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1168 if (!_bfd_elf_readonly_dynrelocs (h
))
1174 /* We must allocate the symbol in our .dynbss section, which will
1175 become part of the .bss section of the executable. There will be
1176 an entry for this symbol in the .dynsym section. The dynamic
1177 object will contain position independent code, so all references
1178 from the dynamic object to this symbol will go through the global
1179 offset table. The dynamic linker will use the .dynsym entry to
1180 determine the address it must put in the global offset table, so
1181 both the dynamic object and the regular object will refer to the
1182 same memory location for the variable. */
1184 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1185 to copy the initial value out of the dynamic object and into the
1186 runtime process image. We need to remember the offset into the
1187 .rel.bss section we are going to use. */
1188 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1189 if (eh
->tls_type
& ~GOT_NORMAL
)
1191 s
= htab
->sdyntdata
;
1192 srel
= htab
->elf
.srelbss
;
1194 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1196 s
= htab
->elf
.sdynrelro
;
1197 srel
= htab
->elf
.sreldynrelro
;
1201 s
= htab
->elf
.sdynbss
;
1202 srel
= htab
->elf
.srelbss
;
1204 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1206 srel
->size
+= sizeof (ElfNN_External_Rela
);
1210 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1213 /* Allocate space in .plt, .got and associated reloc sections for
1217 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1219 struct bfd_link_info
*info
;
1220 struct riscv_elf_link_hash_table
*htab
;
1221 struct elf_dyn_relocs
*p
;
1223 if (h
->root
.type
== bfd_link_hash_indirect
)
1226 info
= (struct bfd_link_info
*) inf
;
1227 htab
= riscv_elf_hash_table (info
);
1228 BFD_ASSERT (htab
!= NULL
);
1230 /* When we are generating pde, make sure gp symbol is output as a
1231 dynamic symbol. Then ld.so can set the gp register earlier, before
1232 resolving the ifunc. */
1233 if (!bfd_link_pic (info
)
1234 && htab
->elf
.dynamic_sections_created
1235 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1236 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1239 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1240 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1241 if they are defined and referenced in a non-shared object. */
1242 if (h
->type
== STT_GNU_IFUNC
1245 else if (htab
->elf
.dynamic_sections_created
1246 && h
->plt
.refcount
> 0)
1248 /* Make sure this symbol is output as a dynamic symbol.
1249 Undefined weak syms won't yet be marked as dynamic. */
1250 if (h
->dynindx
== -1
1251 && !h
->forced_local
)
1253 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1257 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1259 asection
*s
= htab
->elf
.splt
;
1262 s
->size
= PLT_HEADER_SIZE
;
1264 h
->plt
.offset
= s
->size
;
1266 /* Make room for this entry. */
1267 s
->size
+= PLT_ENTRY_SIZE
;
1269 /* We also need to make an entry in the .got.plt section. */
1270 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1272 /* We also need to make an entry in the .rela.plt section. */
1273 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1275 /* If this symbol is not defined in a regular file, and we are
1276 not generating a shared library, then set the symbol to this
1277 location in the .plt. This is required to make function
1278 pointers compare as equal between the normal executable and
1279 the shared library. */
1280 if (! bfd_link_pic (info
)
1283 h
->root
.u
.def
.section
= s
;
1284 h
->root
.u
.def
.value
= h
->plt
.offset
;
1287 /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1288 variant_cc flag of riscv_elf_link_hash_table. */
1289 if (h
->other
& STO_RISCV_VARIANT_CC
)
1290 htab
->variant_cc
= 1;
1294 h
->plt
.offset
= (bfd_vma
) -1;
1300 h
->plt
.offset
= (bfd_vma
) -1;
1304 if (h
->got
.refcount
> 0)
1308 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1310 /* Make sure this symbol is output as a dynamic symbol.
1311 Undefined weak syms won't yet be marked as dynamic. */
1312 if (h
->dynindx
== -1
1313 && !h
->forced_local
)
1315 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1320 h
->got
.offset
= s
->size
;
1321 dyn
= htab
->elf
.dynamic_sections_created
;
1322 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
| GOT_TLSDESC
))
1325 bool need_reloc
= false;
1326 RISCV_TLS_GD_IE_NEED_DYN_RELOC(info
, dyn
, h
, indx
, need_reloc
);
1328 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1329 if (tls_type
& GOT_TLS_GD
)
1331 s
->size
+= TLS_GD_GOT_ENTRY_SIZE
;
1333 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1336 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1337 if (tls_type
& GOT_TLS_IE
)
1339 s
->size
+= TLS_IE_GOT_ENTRY_SIZE
;
1341 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1344 /* TLSDESC needs one dynamic reloc and two GOT slots. */
1345 if (tls_type
& GOT_TLSDESC
)
1347 s
->size
+= TLSDESC_GOT_ENTRY_SIZE
;
1348 /* TLSDESC always use dynamic relocs. */
1349 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1354 s
->size
+= GOT_ENTRY_SIZE
;
1355 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1356 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1357 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1361 h
->got
.offset
= (bfd_vma
) -1;
1363 if (h
->dyn_relocs
== NULL
)
1366 /* In the shared -Bsymbolic case, discard space allocated for
1367 dynamic pc-relative relocs against symbols which turn out to be
1368 defined in regular objects. For the normal shared case, discard
1369 space for pc-relative relocs that have become local due to symbol
1370 visibility changes. */
1372 if (bfd_link_pic (info
))
1374 if (SYMBOL_CALLS_LOCAL (info
, h
))
1376 struct elf_dyn_relocs
**pp
;
1378 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1380 p
->count
-= p
->pc_count
;
1389 /* Also discard relocs on undefined weak syms with non-default
1391 if (h
->dyn_relocs
!= NULL
1392 && h
->root
.type
== bfd_link_hash_undefweak
)
1394 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1395 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1396 h
->dyn_relocs
= NULL
;
1398 /* Make sure undefined weak symbols are output as a dynamic
1400 else if (h
->dynindx
== -1
1401 && !h
->forced_local
)
1403 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1410 /* For the non-shared case, discard space for relocs against
1411 symbols which turn out to need copy relocs or are not
1417 || (htab
->elf
.dynamic_sections_created
1418 && (h
->root
.type
== bfd_link_hash_undefweak
1419 || h
->root
.type
== bfd_link_hash_undefined
))))
1421 /* Make sure this symbol is output as a dynamic symbol.
1422 Undefined weak syms won't yet be marked as dynamic. */
1423 if (h
->dynindx
== -1
1424 && !h
->forced_local
)
1426 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1430 /* If that succeeded, we know we'll be keeping all the
1432 if (h
->dynindx
!= -1)
1436 h
->dyn_relocs
= NULL
;
1441 /* Finally, allocate space. */
1442 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1444 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1445 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1451 /* Allocate space in .plt, .got and associated reloc sections for
1452 ifunc dynamic relocs. */
1455 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1458 struct bfd_link_info
*info
;
1460 if (h
->root
.type
== bfd_link_hash_indirect
)
1463 if (h
->root
.type
== bfd_link_hash_warning
)
1464 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1466 info
= (struct bfd_link_info
*) inf
;
1468 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1469 here if it is defined and referenced in a non-shared object. */
1470 if (h
->type
== STT_GNU_IFUNC
1472 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1481 /* Allocate space in .plt, .got and associated reloc sections for
1482 local ifunc dynamic relocs. */
1485 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1487 struct elf_link_hash_entry
*h
1488 = (struct elf_link_hash_entry
*) *slot
;
1490 if (h
->type
!= STT_GNU_IFUNC
1494 || h
->root
.type
!= bfd_link_hash_defined
)
1497 return allocate_ifunc_dynrelocs (h
, inf
);
1501 riscv_elf_late_size_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1503 struct riscv_elf_link_hash_table
*htab
;
1508 htab
= riscv_elf_hash_table (info
);
1509 BFD_ASSERT (htab
!= NULL
);
1510 dynobj
= htab
->elf
.dynobj
;
1514 if (elf_hash_table (info
)->dynamic_sections_created
)
1516 /* Set the contents of the .interp section to the interpreter. */
1517 if (bfd_link_executable (info
) && !info
->nointerp
)
1519 s
= bfd_get_linker_section (dynobj
, ".interp");
1520 BFD_ASSERT (s
!= NULL
);
1521 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1522 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1527 /* Set up .got offsets for local syms, and space for local dynamic
1529 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1531 bfd_signed_vma
*local_got
;
1532 bfd_signed_vma
*end_local_got
;
1533 char *local_tls_type
;
1534 bfd_size_type locsymcount
;
1535 Elf_Internal_Shdr
*symtab_hdr
;
1538 if (! is_riscv_elf (ibfd
))
1541 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1543 struct elf_dyn_relocs
*p
;
1545 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1547 if (!bfd_is_abs_section (p
->sec
)
1548 && bfd_is_abs_section (p
->sec
->output_section
))
1550 /* Input section has been discarded, either because
1551 it is a copy of a linkonce section or due to
1552 linker script /DISCARD/, so we'll be discarding
1555 else if (p
->count
!= 0)
1557 srel
= elf_section_data (p
->sec
)->sreloc
;
1558 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1559 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1560 info
->flags
|= DF_TEXTREL
;
1565 local_got
= elf_local_got_refcounts (ibfd
);
1569 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1570 locsymcount
= symtab_hdr
->sh_info
;
1571 end_local_got
= local_got
+ locsymcount
;
1572 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1574 srel
= htab
->elf
.srelgot
;
1575 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1579 *local_got
= s
->size
;
1580 if (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
| GOT_TLSDESC
))
1582 if (*local_tls_type
& GOT_TLS_GD
)
1584 s
->size
+= TLS_GD_GOT_ENTRY_SIZE
;
1585 if (bfd_link_dll (info
))
1586 srel
->size
+= sizeof (ElfNN_External_Rela
);
1588 if (*local_tls_type
& GOT_TLS_IE
)
1590 s
->size
+= TLS_IE_GOT_ENTRY_SIZE
;
1591 if (bfd_link_dll (info
))
1592 srel
->size
+= sizeof (ElfNN_External_Rela
);
1594 if (*local_tls_type
& GOT_TLSDESC
)
1596 s
->size
+= TLSDESC_GOT_ENTRY_SIZE
;
1597 srel
->size
+= sizeof (ElfNN_External_Rela
);
1602 s
->size
+= GOT_ENTRY_SIZE
;
1603 if (bfd_link_pic (info
))
1604 srel
->size
+= sizeof (ElfNN_External_Rela
);
1608 *local_got
= (bfd_vma
) -1;
1612 /* Allocate .plt and .got entries and space dynamic relocs for
1614 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1616 /* Allocate .plt and .got entries and space dynamic relocs for
1617 global ifunc symbols. */
1618 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1620 /* Allocate .plt and .got entries and space dynamic relocs for
1621 local ifunc symbols. */
1622 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1624 /* Used to resolve the dynamic relocs overwite problems when
1625 generating static executable. */
1626 if (htab
->elf
.irelplt
)
1627 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1629 if (htab
->elf
.sgotplt
)
1631 struct elf_link_hash_entry
*got
;
1632 got
= elf_link_hash_lookup (elf_hash_table (info
),
1633 "_GLOBAL_OFFSET_TABLE_",
1634 false, false, false);
1636 /* Don't allocate .got.plt section if there are no GOT nor PLT
1637 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1639 || !got
->ref_regular_nonweak
)
1640 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1641 && (htab
->elf
.splt
== NULL
1642 || htab
->elf
.splt
->size
== 0)
1643 && (htab
->elf
.sgot
== NULL
1644 || (htab
->elf
.sgot
->size
1645 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1646 htab
->elf
.sgotplt
->size
= 0;
1649 /* The check_relocs and adjust_dynamic_symbol entry points have
1650 determined the sizes of the various dynamic sections. Allocate
1652 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1654 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1657 if (s
== htab
->elf
.splt
1658 || s
== htab
->elf
.sgot
1659 || s
== htab
->elf
.sgotplt
1660 || s
== htab
->elf
.iplt
1661 || s
== htab
->elf
.igotplt
1662 || s
== htab
->elf
.sdynbss
1663 || s
== htab
->elf
.sdynrelro
1664 || s
== htab
->sdyntdata
)
1666 /* Strip this section if we don't need it; see the
1669 else if (startswith (s
->name
, ".rela"))
1673 /* We use the reloc_count field as a counter if we need
1674 to copy relocs into the output file. */
1680 /* It's not one of our sections. */
1686 /* If we don't need this section, strip it from the
1687 output file. This is mostly to handle .rela.bss and
1688 .rela.plt. We must create both sections in
1689 create_dynamic_sections, because they must be created
1690 before the linker maps input sections to output
1691 sections. The linker does that before
1692 adjust_dynamic_symbol is called, and it is that
1693 function which decides whether anything needs to go
1694 into these sections. */
1695 s
->flags
|= SEC_EXCLUDE
;
1699 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1702 /* Allocate memory for the section contents. Zero the memory
1703 for the benefit of .rela.plt, which has 4 unused entries
1704 at the beginning, and we don't want garbage. */
1705 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1706 if (s
->contents
== NULL
)
1711 /* Add dynamic entries. */
1712 if (elf_hash_table (info
)->dynamic_sections_created
)
1714 if (!_bfd_elf_add_dynamic_tags (output_bfd
, info
, true))
1717 if (htab
->variant_cc
1718 && !_bfd_elf_add_dynamic_entry (info
, DT_RISCV_VARIANT_CC
, 0))
1726 #define DTP_OFFSET 0x800
1728 /* Return the relocation value for a TLS dtp-relative reloc. */
1731 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1733 /* If tls_sec is NULL, we should have signalled an error already. */
1734 if (elf_hash_table (info
)->tls_sec
== NULL
)
1736 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1739 /* Return the relocation value for a static TLS tp-relative relocation. */
1742 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1744 /* If tls_sec is NULL, we should have signalled an error already. */
1745 if (elf_hash_table (info
)->tls_sec
== NULL
)
1747 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1750 /* Return the relocation value for a static TLSDESC relocation. */
1753 tlsdescoff (struct bfd_link_info
*info
, bfd_vma address
)
1755 /* If tls_sec is NULL, we should have signalled an error already. */
1756 if (elf_hash_table (info
)->tls_sec
== NULL
)
1758 return address
- elf_hash_table (info
)->tls_sec
->vma
;
1761 /* Return the global pointer's value, or 0 if it is not in use. */
1764 riscv_global_pointer_value (struct bfd_link_info
*info
)
1766 struct bfd_link_hash_entry
*h
;
1768 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1769 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1772 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1775 /* Emplace a static relocation. */
1777 static bfd_reloc_status_type
1778 perform_relocation (const reloc_howto_type
*howto
,
1779 const Elf_Internal_Rela
*rel
,
1781 asection
*input_section
,
1785 if (howto
->pc_relative
)
1786 value
-= sec_addr (input_section
) + rel
->r_offset
;
1788 /* PR31179, ignore the non-zero addend of R_RISCV_SUB_ULEB128. */
1789 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_SUB_ULEB128
)
1790 value
+= rel
->r_addend
;
1792 switch (ELFNN_R_TYPE (rel
->r_info
))
1795 case R_RISCV_TPREL_HI20
:
1796 case R_RISCV_PCREL_HI20
:
1797 case R_RISCV_GOT_HI20
:
1798 case R_RISCV_TLS_GOT_HI20
:
1799 case R_RISCV_TLS_GD_HI20
:
1800 case R_RISCV_TLSDESC_HI20
:
1801 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1802 return bfd_reloc_overflow
;
1803 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1806 case R_RISCV_LO12_I
:
1807 case R_RISCV_GPREL_I
:
1808 case R_RISCV_TPREL_LO12_I
:
1809 case R_RISCV_TPREL_I
:
1810 case R_RISCV_PCREL_LO12_I
:
1811 case R_RISCV_TLSDESC_LOAD_LO12
:
1812 case R_RISCV_TLSDESC_ADD_LO12
:
1813 value
= ENCODE_ITYPE_IMM (value
);
1816 case R_RISCV_LO12_S
:
1817 case R_RISCV_GPREL_S
:
1818 case R_RISCV_TPREL_LO12_S
:
1819 case R_RISCV_TPREL_S
:
1820 case R_RISCV_PCREL_LO12_S
:
1821 value
= ENCODE_STYPE_IMM (value
);
1825 case R_RISCV_CALL_PLT
:
1826 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1827 return bfd_reloc_overflow
;
1828 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1829 | (ENCODE_ITYPE_IMM (value
) << 32);
1833 if (!VALID_JTYPE_IMM (value
))
1834 return bfd_reloc_overflow
;
1835 value
= ENCODE_JTYPE_IMM (value
);
1838 case R_RISCV_BRANCH
:
1839 if (!VALID_BTYPE_IMM (value
))
1840 return bfd_reloc_overflow
;
1841 value
= ENCODE_BTYPE_IMM (value
);
1844 case R_RISCV_RVC_BRANCH
:
1845 if (!VALID_CBTYPE_IMM (value
))
1846 return bfd_reloc_overflow
;
1847 value
= ENCODE_CBTYPE_IMM (value
);
1850 case R_RISCV_RVC_JUMP
:
1851 if (!VALID_CJTYPE_IMM (value
))
1852 return bfd_reloc_overflow
;
1853 value
= ENCODE_CJTYPE_IMM (value
);
1856 case R_RISCV_RVC_LUI
:
1857 if (RISCV_CONST_HIGH_PART (value
) == 0)
1859 /* Linker relaxation can convert an address equal to or greater than
1860 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1861 valid immediate. We can fix this by converting it to a C.LI. */
1862 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1863 contents
+ rel
->r_offset
);
1864 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1865 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1866 value
= ENCODE_CITYPE_IMM (0);
1868 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1869 return bfd_reloc_overflow
;
1871 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1874 /* R_RISCV_SET_ULEB128 won't go into here. */
1875 case R_RISCV_SUB_ULEB128
:
1877 unsigned int len
= 0;
1878 _bfd_read_unsigned_leb128 (input_bfd
, contents
+ rel
->r_offset
, &len
);
1880 /* Clean the contents value to zero (0x80), but keep the original
1882 bfd_byte
*p
= contents
+ rel
->r_offset
;
1883 bfd_byte
*endp
= p
+ len
- 1;
1884 memset (p
, 0x80, len
- 1);
1887 /* Make sure the length of the new uleb128 value within the
1888 original (available) length. */
1889 unsigned int new_len
= 0;
1890 unsigned int val_t
= value
;
1900 (_("final size of uleb128 value at offset 0x%lx in %pA from "
1901 "%pB exceeds available space"),
1902 (long) rel
->r_offset
, input_section
, input_bfd
);
1903 return bfd_reloc_dangerous
;
1907 p
= _bfd_write_unsigned_leb128 (p
, endp
, value
);
1910 /* If the length of the value is reduced and shorter than the
1911 original uleb128 length, then _bfd_write_unsigned_leb128 may
1912 clear the 0x80 to 0x0 for the last byte that was written.
1913 So reset it to keep the the original uleb128 length. */
1917 return bfd_reloc_ok
;
1935 case R_RISCV_32_PCREL
:
1936 case R_RISCV_TLS_DTPREL32
:
1937 case R_RISCV_TLS_DTPREL64
:
1940 case R_RISCV_DELETE
:
1941 return bfd_reloc_ok
;
1944 return bfd_reloc_notsupported
;
1948 if (riscv_is_insn_reloc (howto
))
1949 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1951 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1952 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1953 if (riscv_is_insn_reloc (howto
))
1954 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1956 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1958 return bfd_reloc_ok
;
1961 /* Remember all PC-relative high-part relocs we've encountered to help us
1962 later resolve the corresponding low-part relocs. */
1968 /* Relocation value with addend. */
1970 /* Original reloc type. */
1972 /* True if changed to R_RISCV_HI20. */
1974 } riscv_pcrel_hi_reloc
;
1976 typedef struct riscv_pcrel_lo_reloc
1978 /* PC value of auipc. */
1980 /* Internal relocation. */
1981 Elf_Internal_Rela
*reloc
;
1982 /* Record the following information helps to resolve the %pcrel
1983 which cross different input section. For now we build a hash
1984 for pcrel at the start of riscv_elf_relocate_section, and then
1985 free the hash at the end. But riscv_elf_relocate_section only
1986 handles an input section at a time, so that means we can only
1987 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1988 section. Otherwise, we will report dangerous relocation errors
1989 for those %pcrel which are not in the same input section. */
1990 asection
*input_section
;
1991 struct bfd_link_info
*info
;
1992 reloc_howto_type
*howto
;
1994 /* The next riscv_pcrel_lo_reloc. */
1995 struct riscv_pcrel_lo_reloc
*next
;
1996 } riscv_pcrel_lo_reloc
;
2000 /* Hash table for riscv_pcrel_hi_reloc. */
2002 /* Linked list for riscv_pcrel_lo_reloc. */
2003 riscv_pcrel_lo_reloc
*lo_relocs
;
2004 } riscv_pcrel_relocs
;
2007 riscv_pcrel_reloc_hash (const void *entry
)
2009 const riscv_pcrel_hi_reloc
*e
= entry
;
2010 return (hashval_t
)(e
->address
>> 2);
2014 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
2016 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
2017 return e1
->address
== e2
->address
;
2021 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
2023 p
->lo_relocs
= NULL
;
2024 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
2025 riscv_pcrel_reloc_eq
, free
);
2026 return p
->hi_relocs
!= NULL
;
2030 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
2032 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
2036 riscv_pcrel_lo_reloc
*next
= cur
->next
;
2041 htab_delete (p
->hi_relocs
);
2045 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
2046 struct bfd_link_info
*info
,
2050 const reloc_howto_type
*howto
)
2052 /* We may need to reference low addreses in PC-relative modes even when the
2053 PC is far away from these addresses. For example, undefweak references
2054 need to produce the address 0 when linked. As 0 is far from the arbitrary
2055 addresses that we can link PC-relative programs at, the linker can't
2056 actually relocate references to those symbols. In order to allow these
2057 programs to work we simply convert the PC-relative auipc sequences to
2058 0-relative lui sequences. */
2059 if (bfd_link_pic (info
))
2062 /* If it's possible to reference the symbol using auipc we do so, as that's
2063 more in the spirit of the PC-relative relocations we're processing. */
2064 bfd_vma offset
= *addr
- pc
;
2065 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
2068 /* If it's impossible to reference this with a LUI-based offset then don't
2069 bother to convert it at all so users still see the PC-relative relocation
2070 in the truncation message. */
2071 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (*addr
)))
2074 /* PR27180, encode target absolute address into r_addend rather than
2075 r_sym. Clear the ADDR to avoid duplicate relocate in the
2076 perform_relocation. */
2077 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_HI20
);
2078 rel
->r_addend
+= *addr
;
2081 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
2082 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
2083 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
2088 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
2094 bfd_vma offset
= absolute
? value
: value
- addr
;
2095 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
, absolute
};
2096 riscv_pcrel_hi_reloc
**slot
=
2097 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
2099 BFD_ASSERT (*slot
== NULL
);
2100 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
2108 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
2110 Elf_Internal_Rela
*reloc
,
2111 asection
*input_section
,
2112 struct bfd_link_info
*info
,
2113 reloc_howto_type
*howto
,
2116 riscv_pcrel_lo_reloc
*entry
;
2117 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
2120 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
2121 howto
, contents
, p
->lo_relocs
};
2122 p
->lo_relocs
= entry
;
2127 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
2129 riscv_pcrel_lo_reloc
*r
;
2131 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
2133 bfd
*input_bfd
= r
->input_section
->owner
;
2135 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0, 0};
2136 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
2137 /* There may be a risk if the %pcrel_lo with addend refers to
2138 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
2139 so the corresponding %pcrel_lo with addend looks wrong. */
2140 char *string
= NULL
;
2142 string
= _("%pcrel_lo missing matching %pcrel_hi");
2143 else if (entry
->type
== R_RISCV_GOT_HI20
2144 && r
->reloc
->r_addend
!= 0)
2145 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
2146 else if (RISCV_CONST_HIGH_PART (entry
->value
)
2147 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
2149 /* Check the overflow when adding reloc addend. */
2150 string
= bfd_asprintf (_("%%pcrel_lo overflow with an addend,"
2151 " the value of %%pcrel_hi is 0x%" PRIx64
2152 " without any addend, but may be 0x%" PRIx64
2153 " after adding the %%pcrel_lo addend"),
2154 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
2155 (int64_t) RISCV_CONST_HIGH_PART
2156 (entry
->value
+ r
->reloc
->r_addend
));
2158 string
= _("%pcrel_lo overflow with an addend");
2163 (*r
->info
->callbacks
->reloc_dangerous
)
2164 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
2168 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
2169 input_bfd
, r
->contents
);
2171 /* The corresponding R_RISCV_GOT_PCREL_HI20 and R_RISCV_PCREL_HI20 are
2172 converted to R_RISCV_HI20, so try to convert R_RISCV_PCREL_LO12_I/S
2173 to R_RISCV_LO12_I/S. */
2174 if (entry
->absolute
)
2176 switch (ELFNN_R_TYPE (r
->reloc
->r_info
))
2178 case R_RISCV_PCREL_LO12_I
:
2179 r
->reloc
->r_info
= ELFNN_R_INFO (0, R_RISCV_LO12_I
);
2180 r
->reloc
->r_addend
+= entry
->value
;
2182 case R_RISCV_PCREL_LO12_S
:
2183 r
->reloc
->r_info
= ELFNN_R_INFO (0, R_RISCV_LO12_S
);
2184 r
->reloc
->r_addend
+= entry
->value
;
2187 /* This shouldn't happen, so just skip it. */
2196 /* Relocate a RISC-V ELF section.
2198 The RELOCATE_SECTION function is called by the new ELF backend linker
2199 to handle the relocations for a section.
2201 The relocs are always passed as Rela structures.
2203 This function is responsible for adjusting the section contents as
2204 necessary, and (if generating a relocatable output file) adjusting
2205 the reloc addend as necessary.
2207 This function does not have to worry about setting the reloc
2208 address or the reloc symbol index.
2210 LOCAL_SYMS is a pointer to the swapped in local symbols.
2212 LOCAL_SECTIONS is an array giving the section in the input file
2213 corresponding to the st_shndx field of each local symbol.
2215 The global hash table entry for the global symbols can be found
2216 via elf_sym_hashes (input_bfd).
2218 When generating relocatable output, this function must handle
2219 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
2220 going to be the section symbol corresponding to the output
2221 section, which means that the addend must be adjusted
2225 riscv_elf_relocate_section (bfd
*output_bfd
,
2226 struct bfd_link_info
*info
,
2228 asection
*input_section
,
2230 Elf_Internal_Rela
*relocs
,
2231 Elf_Internal_Sym
*local_syms
,
2232 asection
**local_sections
)
2234 Elf_Internal_Rela
*rel
;
2235 Elf_Internal_Rela
*relend
;
2236 riscv_pcrel_relocs pcrel_relocs
;
2238 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2239 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
2240 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
2241 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
2242 bfd_vma uleb128_set_vma
= 0;
2243 Elf_Internal_Rela
*uleb128_set_rel
= NULL
;
2246 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
2249 relend
= relocs
+ input_section
->reloc_count
;
2250 for (rel
= relocs
; rel
< relend
; rel
++)
2252 unsigned long r_symndx
;
2253 struct elf_link_hash_entry
*h
;
2254 Elf_Internal_Sym
*sym
;
2257 bfd_reloc_status_type r
= bfd_reloc_ok
;
2258 const char *name
= NULL
;
2259 bfd_vma off
, ie_off
, desc_off
;
2260 bool unresolved_reloc
, is_ie
= false, is_desc
= false;
2261 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
2262 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
2263 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2264 const char *msg
= NULL
;
2265 bool resolved_to_zero
;
2270 /* This is a final link. */
2271 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2275 unresolved_reloc
= false;
2276 if (r_symndx
< symtab_hdr
->sh_info
)
2278 sym
= local_syms
+ r_symndx
;
2279 sec
= local_sections
[r_symndx
];
2280 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2282 /* Relocate against local STT_GNU_IFUNC symbol. */
2283 if (!bfd_link_relocatable (info
)
2284 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2286 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2290 /* Set STT_GNU_IFUNC symbol value. */
2291 h
->root
.u
.def
.value
= sym
->st_value
;
2292 h
->root
.u
.def
.section
= sec
;
2297 bool warned
, ignored
;
2299 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2300 r_symndx
, symtab_hdr
, sym_hashes
,
2302 unresolved_reloc
, warned
, ignored
);
2305 /* To avoid generating warning messages about truncated
2306 relocations, set the relocation's address to be the same as
2307 the start of this section. */
2308 if (input_section
->output_section
!= NULL
)
2309 relocation
= input_section
->output_section
->vma
;
2315 if (sec
!= NULL
&& discarded_section (sec
))
2316 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2317 rel
, 1, relend
, howto
, 0, contents
);
2319 if (bfd_link_relocatable (info
))
2322 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2323 it here if it is defined in a non-shared object. */
2325 && h
->type
== STT_GNU_IFUNC
2328 asection
*plt
, *base_got
;
2330 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2332 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2333 STT_GNU_IFUNC symbol as STT_FUNC. */
2334 if (elf_section_type (input_section
) == SHT_NOTE
)
2337 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2338 sections because such sections are not SEC_ALLOC and
2339 thus ld.so will not process them. */
2340 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2345 else if (h
->plt
.offset
== (bfd_vma
) -1
2346 /* The following relocation may not need the .plt entries
2347 when all references to a STT_GNU_IFUNC symbols are done
2348 via GOT or static function pointers. */
2349 && r_type
!= R_RISCV_32
2350 && r_type
!= R_RISCV_64
2351 && r_type
!= R_RISCV_HI20
2352 && r_type
!= R_RISCV_GOT_HI20
2353 && r_type
!= R_RISCV_LO12_I
2354 && r_type
!= R_RISCV_LO12_S
)
2355 goto bad_ifunc_reloc
;
2357 /* STT_GNU_IFUNC symbol must go through PLT. */
2358 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2359 relocation
= plt
->output_section
->vma
2360 + plt
->output_offset
2367 if (rel
->r_addend
!= 0)
2369 if (h
->root
.root
.string
)
2370 name
= h
->root
.root
.string
;
2372 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2375 /* xgettext:c-format */
2376 (_("%pB: relocation %s against STT_GNU_IFUNC "
2377 "symbol `%s' has non-zero addend: %" PRId64
),
2378 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2379 bfd_set_error (bfd_error_bad_value
);
2383 /* Generate dynamic relocation only when there is a non-GOT
2384 reference in a shared object or there is no PLT. */
2385 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2386 || h
->plt
.offset
== (bfd_vma
) -1)
2388 Elf_Internal_Rela outrel
;
2390 /* Need a dynamic relocation to get the real function
2392 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2396 if (outrel
.r_offset
== (bfd_vma
) -1
2397 || outrel
.r_offset
== (bfd_vma
) -2)
2400 outrel
.r_offset
+= input_section
->output_section
->vma
2401 + input_section
->output_offset
;
2403 if (h
->dynindx
== -1
2405 || bfd_link_executable (info
))
2407 info
->callbacks
->minfo
2408 (_("Local IFUNC function `%s' in %pB\n"),
2409 h
->root
.root
.string
,
2410 h
->root
.u
.def
.section
->owner
);
2412 /* This symbol is resolved locally. */
2413 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2414 outrel
.r_addend
= h
->root
.u
.def
.value
2415 + h
->root
.u
.def
.section
->output_section
->vma
2416 + h
->root
.u
.def
.section
->output_offset
;
2420 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2421 outrel
.r_addend
= 0;
2424 /* Dynamic relocations are stored in
2425 1. .rela.ifunc section in PIC object.
2426 2. .rela.got section in dynamic executable.
2427 3. .rela.iplt section in static executable. */
2428 if (bfd_link_pic (info
))
2429 riscv_elf_append_rela (output_bfd
, htab
->elf
.irelifunc
,
2431 else if (htab
->elf
.splt
!= NULL
)
2432 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
,
2436 /* Do not use riscv_elf_append_rela to add dynamic
2437 relocs into .rela.iplt, since it may cause the
2438 overwrite problems. This is same as what we did
2439 in the riscv_elf_finish_dynamic_symbol. */
2440 const struct elf_backend_data
*bed
=
2441 get_elf_backend_data (output_bfd
);
2442 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
2443 bfd_byte
*loc
= htab
->elf
.irelplt
->contents
2444 + iplt_idx
* sizeof (ElfNN_External_Rela
);
2445 bed
->s
->swap_reloca_out (output_bfd
, &outrel
, loc
);
2448 /* If this reloc is against an external symbol, we
2449 do not want to fiddle with the addend. Otherwise,
2450 we need to include the symbol value so that it
2451 becomes an addend for the dynamic reloc. For an
2452 internal symbol, we have updated addend. */
2457 case R_RISCV_GOT_HI20
:
2458 base_got
= htab
->elf
.sgot
;
2459 off
= h
->got
.offset
;
2461 if (base_got
== NULL
)
2464 if (off
== (bfd_vma
) -1)
2468 /* We can't use h->got.offset here to save state, or
2469 even just remember the offset, as finish_dynamic_symbol
2470 would use that as offset into .got. */
2472 if (htab
->elf
.splt
!= NULL
)
2474 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2476 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2477 base_got
= htab
->elf
.sgotplt
;
2481 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2482 off
= plt_idx
* GOT_ENTRY_SIZE
;
2483 base_got
= htab
->elf
.igotplt
;
2486 if (h
->dynindx
== -1
2490 /* This references the local definition. We must
2491 initialize this entry in the global offset table.
2492 Since the offset must always be a multiple of 8,
2493 we use the least significant bit to record
2494 whether we have initialized it already.
2496 When doing a dynamic link, we create a .rela.got
2497 relocation entry to initialize the value. This
2498 is done in the finish_dynamic_symbol routine. */
2503 bfd_put_NN (output_bfd
, relocation
,
2504 base_got
->contents
+ off
);
2505 /* Note that this is harmless for the case,
2506 as -1 | 1 still is -1. */
2512 relocation
= base_got
->output_section
->vma
2513 + base_got
->output_offset
+ off
;
2515 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2518 r
= bfd_reloc_overflow
;
2522 case R_RISCV_CALL_PLT
:
2524 case R_RISCV_LO12_I
:
2525 case R_RISCV_LO12_S
:
2528 case R_RISCV_PCREL_HI20
:
2529 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2532 r
= bfd_reloc_overflow
;
2537 if (h
->root
.root
.string
)
2538 name
= h
->root
.root
.string
;
2540 /* The entry of local ifunc is fake in global hash table,
2541 we should find the name by the original local symbol. */
2542 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2545 /* xgettext:c-format */
2546 (_("%pB: relocation %s against STT_GNU_IFUNC "
2547 "symbol `%s' isn't supported"), input_bfd
,
2549 bfd_set_error (bfd_error_bad_value
);
2556 name
= h
->root
.root
.string
;
2559 name
= (bfd_elf_string_from_elf_section
2560 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2561 if (name
== NULL
|| *name
== '\0')
2562 name
= bfd_section_name (sec
);
2565 resolved_to_zero
= (h
!= NULL
2566 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2572 case R_RISCV_TPREL_ADD
:
2573 case R_RISCV_TLSDESC_CALL
:
2575 case R_RISCV_JUMP_SLOT
:
2576 case R_RISCV_RELATIVE
:
2577 /* These require nothing of us at all. */
2581 case R_RISCV_BRANCH
:
2582 case R_RISCV_RVC_BRANCH
:
2583 case R_RISCV_RVC_LUI
:
2584 case R_RISCV_LO12_I
:
2585 case R_RISCV_LO12_S
:
2590 case R_RISCV_32_PCREL
:
2591 case R_RISCV_DELETE
:
2592 /* These require no special handling beyond perform_relocation. */
2595 case R_RISCV_SET_ULEB128
:
2596 if (uleb128_set_rel
== NULL
)
2598 /* Saved for later usage. */
2599 uleb128_set_vma
= relocation
;
2600 uleb128_set_rel
= rel
;
2605 msg
= ("Mismatched R_RISCV_SET_ULEB128, it must be paired with"
2606 " and applied before R_RISCV_SUB_ULEB128");
2607 r
= bfd_reloc_dangerous
;
2611 case R_RISCV_SUB_ULEB128
:
2612 if (uleb128_set_rel
!= NULL
2613 && uleb128_set_rel
->r_offset
== rel
->r_offset
)
2615 relocation
= uleb128_set_vma
- relocation
2616 + uleb128_set_rel
->r_addend
;
2617 uleb128_set_vma
= 0;
2618 uleb128_set_rel
= NULL
;
2620 /* PR31179, the addend of SUB_ULEB128 should be zero if using
2621 .uleb128, but we make it non-zero by accident in assembler,
2622 so just ignore it in perform_relocation, and make assembler
2623 continue doing the right thing. Don't reset the addend of
2624 SUB_ULEB128 to zero here since it will break the --emit-reloc,
2625 even though the non-zero addend is unexpected.
2627 We encourage people to rebuild their stuff to get the
2628 non-zero addend of SUB_ULEB128, but that might need some
2629 times, so report warnings to inform people need to rebuild
2630 if --check-uleb128 is enabled. However, since the failed
2631 .reloc cases for ADD/SET/SUB/ULEB128 are rarely to use, it
2632 may acceptable that stop supproting them until people rebuld
2633 their stuff, maybe half-year or one year later. I believe
2634 this might be the least harmful option that we should go.
2636 Or maybe we should teach people that don't write the
2637 .reloc R_RISCV_SUB* with non-zero constant, and report
2638 warnings/errors in assembler. */
2639 if (htab
->params
->check_uleb128
2640 && rel
->r_addend
!= 0)
2641 _bfd_error_handler (_("%pB: warning: R_RISCV_SUB_ULEB128 with"
2642 " non-zero addend, please rebuild by"
2643 " binutils 2.42 or up"), input_bfd
);
2647 msg
= ("Mismatched R_RISCV_SUB_ULEB128, it must be paired with"
2648 " and applied after R_RISCV_SET_ULEB128");
2649 r
= bfd_reloc_dangerous
;
2653 case R_RISCV_GOT_HI20
:
2656 off
= h
->got
.offset
;
2657 BFD_ASSERT (off
!= (bfd_vma
) -1);
2659 if (RISCV_RESOLVED_LOCALLY (info
, h
))
2661 /* We must initialize this entry in the global offset table.
2662 Since the offset must always be a multiple of the word
2663 size, we use the least significant bit to record whether
2664 we have initialized it already.
2666 When doing a dynamic link, we create a .rela.got
2667 relocation entry to initialize the value. This
2668 is done in the finish_dynamic_symbol routine. */
2673 bfd_put_NN (output_bfd
, relocation
,
2674 htab
->elf
.sgot
->contents
+ off
);
2679 unresolved_reloc
= false;
2683 BFD_ASSERT (local_got_offsets
!= NULL
2684 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2686 off
= local_got_offsets
[r_symndx
];
2688 /* The offset must always be a multiple of the word size.
2689 So, we can use the least significant bit to record
2690 whether we have already processed this entry. */
2695 if (bfd_link_pic (info
))
2698 Elf_Internal_Rela outrel
;
2700 /* We need to generate a R_RISCV_RELATIVE reloc
2701 for the dynamic linker. */
2702 s
= htab
->elf
.srelgot
;
2703 BFD_ASSERT (s
!= NULL
);
2705 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2707 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2708 outrel
.r_addend
= relocation
;
2710 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2713 bfd_put_NN (output_bfd
, relocation
,
2714 htab
->elf
.sgot
->contents
+ off
);
2715 local_got_offsets
[r_symndx
] |= 1;
2719 if (rel
->r_addend
!= 0)
2721 msg
= _("The addend isn't allowed for R_RISCV_GOT_HI20");
2722 r
= bfd_reloc_dangerous
;
2726 /* Address of got entry. */
2727 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2728 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
,
2729 &relocation
, contents
,
2731 /* Update howto if relocation is changed. */
2732 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2733 ELFNN_R_TYPE (rel
->r_info
));
2735 r
= bfd_reloc_notsupported
;
2736 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2737 relocation
+ rel
->r_addend
,
2739 r
= bfd_reloc_overflow
;
2748 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2749 contents
+ rel
->r_offset
);
2750 relocation
= old_value
+ relocation
;
2756 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2757 contents
+ rel
->r_offset
);
2758 relocation
= (old_value
& ~howto
->dst_mask
)
2759 | (((old_value
& howto
->dst_mask
) - relocation
)
2769 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2770 contents
+ rel
->r_offset
);
2771 relocation
= old_value
- relocation
;
2776 case R_RISCV_CALL_PLT
:
2777 /* Handle a call to an undefined weak function. This won't be
2778 relaxed, so we have to handle it here. */
2779 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2780 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2782 /* We can use x0 as the base register. */
2783 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2784 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2785 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2786 /* Set the relocation value so that we get 0 after the pc
2787 relative adjustment. */
2788 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2793 case R_RISCV_RVC_JUMP
:
2794 if (bfd_link_pic (info
) && h
!= NULL
)
2796 if (h
->plt
.offset
!= MINUS_ONE
)
2798 /* Refer to the PLT entry. This check has to match the
2799 check in _bfd_riscv_relax_section. */
2800 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2801 unresolved_reloc
= false;
2803 else if (!SYMBOL_REFERENCES_LOCAL (info
, h
)
2804 && (input_section
->flags
& SEC_ALLOC
) != 0
2805 && (input_section
->flags
& SEC_READONLY
) != 0
2806 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2808 /* PR 28509, when generating the shared object, these
2809 referenced symbols may bind externally, which means
2810 they will be exported to the dynamic symbol table,
2811 and are preemptible by default. These symbols cannot
2812 be referenced by the non-pic relocations, like
2813 R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2815 However, consider that linker may relax the R_RISCV_CALL
2816 relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2817 these relocations are relocated to the plt entries,
2818 then we won't report error for them.
2820 Perhaps we also need the similar checks for the
2821 R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
2822 msg
= bfd_asprintf (_("%%X%%P: relocation %s against `%s'"
2823 " which may bind externally"
2825 " when making a shared object;"
2826 " recompile with -fPIC\n"),
2827 howto
->name
, h
->root
.root
.string
);
2828 r
= bfd_reloc_notsupported
;
2833 case R_RISCV_TPREL_HI20
:
2834 relocation
= tpoff (info
, relocation
);
2837 case R_RISCV_TPREL_LO12_I
:
2838 case R_RISCV_TPREL_LO12_S
:
2839 relocation
= tpoff (info
, relocation
);
2842 case R_RISCV_TPREL_I
:
2843 case R_RISCV_TPREL_S
:
2844 relocation
= tpoff (info
, relocation
);
2845 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2847 /* We can use tp as the base register. */
2848 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2849 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2850 insn
|= X_TP
<< OP_SH_RS1
;
2851 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2854 r
= bfd_reloc_overflow
;
2857 case R_RISCV_GPREL_I
:
2858 case R_RISCV_GPREL_S
:
2860 bfd_vma gp
= riscv_global_pointer_value (info
);
2861 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2862 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2864 /* We can use x0 or gp as the base register. */
2865 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2866 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2869 rel
->r_addend
-= gp
;
2870 insn
|= X_GP
<< OP_SH_RS1
;
2872 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2875 r
= bfd_reloc_overflow
;
2879 case R_RISCV_PCREL_HI20
:
2880 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, &relocation
,
2882 /* Update howto if relocation is changed. */
2883 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2884 ELFNN_R_TYPE (rel
->r_info
));
2886 r
= bfd_reloc_notsupported
;
2887 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2888 relocation
+ rel
->r_addend
,
2890 r
= bfd_reloc_overflow
;
2893 case R_RISCV_PCREL_LO12_I
:
2894 case R_RISCV_PCREL_LO12_S
:
2895 /* We don't allow section symbols plus addends as the auipc address,
2896 because then riscv_relax_delete_bytes would have to search through
2897 all relocs to update these addends. This is also ambiguous, as
2898 we do allow offsets to be added to the target address, which are
2899 not to be used to find the auipc address. */
2900 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2901 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2904 msg
= _("%pcrel_lo section symbol with an addend");
2905 r
= bfd_reloc_dangerous
;
2909 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2910 input_section
, info
, howto
,
2913 r
= bfd_reloc_overflow
;
2916 case R_RISCV_TLS_DTPREL32
:
2917 case R_RISCV_TLS_DTPREL64
:
2918 relocation
= dtpoff (info
, relocation
);
2921 case R_RISCV_TLSDESC_LOAD_LO12
:
2922 case R_RISCV_TLSDESC_ADD_LO12
:
2925 msg
= _("%tlsdesc_lo with addend");
2926 r
= bfd_reloc_dangerous
;
2930 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2931 input_section
, info
, howto
,
2934 r
= bfd_reloc_overflow
;
2938 /* Non ABS symbol should be blocked in check_relocs. */
2944 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2947 if (RISCV_GENERATE_DYNAMIC_RELOC (howto
->pc_relative
, info
, h
,
2950 Elf_Internal_Rela outrel
;
2953 /* When generating a shared object, these relocations
2954 are copied into the output file to be resolved at run
2958 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2961 bool relocate
= false;
2962 if (outrel
.r_offset
== (bfd_vma
) -1)
2964 else if (outrel
.r_offset
== (bfd_vma
) -2)
2969 else if (h
!= NULL
&& bfd_is_abs_symbol (&h
->root
))
2971 /* Don't need dynamic reloc when the ABS symbol is
2972 non-dynamic or forced to local. Maybe just use
2973 SYMBOL_REFERENCES_LOCAL to check? */
2974 skip
= (h
->forced_local
|| (h
->dynindx
== -1));
2978 outrel
.r_offset
+= sec_addr (input_section
);
2981 memset (&outrel
, 0, sizeof outrel
); /* R_RISCV_NONE. */
2982 else if (RISCV_COPY_INPUT_RELOC (info
, h
))
2984 /* Maybe just use !SYMBOL_REFERENCES_LOCAL to check? */
2985 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2986 outrel
.r_addend
= rel
->r_addend
;
2990 /* This symbol is local, or marked to become local. */
2991 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2992 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2995 sreloc
= elf_section_data (input_section
)->sreloc
;
2996 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
3002 case R_RISCV_TLSDESC_HI20
:
3006 case R_RISCV_TLS_GOT_HI20
:
3010 case R_RISCV_TLS_GD_HI20
:
3014 off
= h
->got
.offset
;
3019 off
= local_got_offsets
[r_symndx
];
3020 local_got_offsets
[r_symndx
] |= 1;
3023 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
3024 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
| GOT_TLSDESC
));
3025 /* When more than one TLS type is used, the GD slot comes first,
3026 then IE, then finally TLSDESC. */
3028 if (tls_type
& GOT_TLS_GD
)
3029 ie_off
+= TLS_GD_GOT_ENTRY_SIZE
;
3032 if (tls_type
& GOT_TLS_IE
)
3033 desc_off
+= TLS_IE_GOT_ENTRY_SIZE
;
3039 Elf_Internal_Rela outrel
;
3041 bool need_relocs
= false;
3043 if (htab
->elf
.srelgot
== NULL
)
3046 bool dyn
= elf_hash_table (info
)->dynamic_sections_created
;
3047 RISCV_TLS_GD_IE_NEED_DYN_RELOC (info
, dyn
, h
, indx
, need_relocs
);
3049 /* The GOT entries have not been initialized yet. Do it
3050 now, and emit any relocations. */
3051 if (tls_type
& GOT_TLS_GD
)
3055 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
3056 outrel
.r_addend
= 0;
3057 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
3058 bfd_put_NN (output_bfd
, 0,
3059 htab
->elf
.sgot
->contents
+ off
);
3060 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3063 BFD_ASSERT (! unresolved_reloc
);
3064 bfd_put_NN (output_bfd
,
3065 dtpoff (info
, relocation
),
3066 (htab
->elf
.sgot
->contents
3067 + off
+ RISCV_ELF_WORD_BYTES
));
3071 bfd_put_NN (output_bfd
, 0,
3072 (htab
->elf
.sgot
->contents
3073 + off
+ RISCV_ELF_WORD_BYTES
));
3074 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
3075 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
3076 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3081 /* If we are not emitting relocations for a
3082 general dynamic reference, then we must be in a
3083 static link or an executable link with the
3084 symbol binding locally. Mark it as belonging
3085 to module 1, the executable. */
3086 bfd_put_NN (output_bfd
, 1,
3087 htab
->elf
.sgot
->contents
+ off
);
3088 bfd_put_NN (output_bfd
,
3089 dtpoff (info
, relocation
),
3090 (htab
->elf
.sgot
->contents
3091 + off
+ RISCV_ELF_WORD_BYTES
));
3095 if (tls_type
& GOT_TLS_IE
)
3099 bfd_put_NN (output_bfd
, 0,
3100 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
3101 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
3103 outrel
.r_addend
= 0;
3105 outrel
.r_addend
= tpoff (info
, relocation
);
3106 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
3107 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3111 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
3112 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
3116 if (tls_type
& GOT_TLSDESC
)
3118 /* TLSDESC is always handled by the dynamic linker and always need
3120 bfd_put_NN (output_bfd
, 0,
3121 htab
->elf
.sgot
->contents
+ off
+ desc_off
);
3122 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
3124 outrel
.r_addend
= 0;
3126 outrel
.r_addend
= tlsdescoff (info
, relocation
);
3127 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLSDESC
);
3128 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
3132 BFD_ASSERT (off
< (bfd_vma
) -2);
3133 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
3135 relocation
+= ie_off
;
3137 relocation
+= desc_off
;
3138 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
3141 r
= bfd_reloc_overflow
;
3142 unresolved_reloc
= false;
3146 r
= bfd_reloc_notsupported
;
3149 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3150 because such sections are not SEC_ALLOC and thus ld.so will
3151 not process them. */
3152 if (unresolved_reloc
3153 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
3155 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
3156 rel
->r_offset
) != (bfd_vma
) -1)
3158 msg
= bfd_asprintf (_("%%X%%P: unresolvable %s relocation against "
3161 h
->root
.root
.string
);
3162 r
= bfd_reloc_notsupported
;
3166 if (r
== bfd_reloc_ok
)
3167 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
3168 input_bfd
, contents
);
3170 /* We should have already detected the error and set message before.
3171 If the error message isn't set since the linker runs out of memory
3172 or we don't set it before, then we should set the default message
3173 with the "internal error" string here. */
3179 case bfd_reloc_overflow
:
3180 info
->callbacks
->reloc_overflow
3181 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
3182 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
3185 case bfd_reloc_undefined
:
3186 info
->callbacks
->undefined_symbol
3187 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
3191 case bfd_reloc_outofrange
:
3193 msg
= _("%X%P: internal error: out of range error\n");
3196 case bfd_reloc_notsupported
:
3198 msg
= _("%X%P: internal error: unsupported relocation error\n");
3201 case bfd_reloc_dangerous
:
3202 /* The error message should already be set. */
3204 msg
= _("dangerous relocation error");
3205 info
->callbacks
->reloc_dangerous
3206 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
3210 msg
= _("%X%P: internal error: unknown error\n");
3214 /* Do not report error message for the dangerous relocation again. */
3215 if (msg
&& r
!= bfd_reloc_dangerous
)
3216 info
->callbacks
->einfo (msg
);
3218 /* We already reported the error via a callback, so don't try to report
3219 it again by returning false. That leads to spurious errors. */
3224 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
3226 riscv_free_pcrel_relocs (&pcrel_relocs
);
3230 /* Finish up dynamic symbol handling. We set the contents of various
3231 dynamic sections here. */
3234 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
3235 struct bfd_link_info
*info
,
3236 struct elf_link_hash_entry
*h
,
3237 Elf_Internal_Sym
*sym
)
3239 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3240 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3242 if (h
->plt
.offset
!= (bfd_vma
) -1)
3244 /* We've decided to create a PLT entry for this symbol. */
3246 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
3247 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
3248 Elf_Internal_Rela rela
;
3249 asection
*plt
, *gotplt
, *relplt
;
3251 /* When building a static executable, use .iplt, .igot.plt and
3252 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3253 if (htab
->elf
.splt
!= NULL
)
3255 plt
= htab
->elf
.splt
;
3256 gotplt
= htab
->elf
.sgotplt
;
3257 relplt
= htab
->elf
.srelplt
;
3261 plt
= htab
->elf
.iplt
;
3262 gotplt
= htab
->elf
.igotplt
;
3263 relplt
= htab
->elf
.irelplt
;
3266 /* This symbol has an entry in the procedure linkage table. Set
3268 if ((h
->dynindx
== -1
3269 && !((h
->forced_local
|| bfd_link_executable (info
))
3271 && h
->type
== STT_GNU_IFUNC
))
3277 /* Calculate the address of the PLT header. */
3278 header_address
= sec_addr (plt
);
3280 /* Calculate the index of the entry and the offset of .got.plt entry.
3281 For static executables, we don't reserve anything. */
3282 if (plt
== htab
->elf
.splt
)
3284 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
3285 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
3289 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
3290 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
3293 /* Calculate the address of the .got.plt entry. */
3294 got_address
= sec_addr (gotplt
) + got_offset
;
3296 /* Find out where the .plt entry should go. */
3297 loc
= plt
->contents
+ h
->plt
.offset
;
3299 /* Fill in the PLT entry itself. */
3300 if (! riscv_make_plt_entry (output_bfd
, got_address
,
3301 header_address
+ h
->plt
.offset
,
3305 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
3306 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
3308 /* Fill in the initial value of the .got.plt entry. */
3309 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
3310 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
3312 rela
.r_offset
= got_address
;
3314 if (h
->dynindx
== -1
3315 || ((bfd_link_executable (info
)
3316 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
3318 && h
->type
== STT_GNU_IFUNC
))
3320 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3321 h
->root
.root
.string
,
3322 h
->root
.u
.def
.section
->owner
);
3324 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3325 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
3326 asection
*sec
= h
->root
.u
.def
.section
;
3327 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3328 rela
.r_addend
= h
->root
.u
.def
.value
3329 + sec
->output_section
->vma
3330 + sec
->output_offset
;
3334 /* Fill in the entry in the .rela.plt section. */
3335 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
3339 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
3340 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3342 if (!h
->def_regular
)
3344 /* Mark the symbol as undefined, rather than as defined in
3345 the .plt section. Leave the value alone. */
3346 sym
->st_shndx
= SHN_UNDEF
;
3347 /* If the symbol is weak, we do need to clear the value.
3348 Otherwise, the PLT entry would provide a definition for
3349 the symbol even if the symbol wasn't defined anywhere,
3350 and so the symbol would never be NULL. */
3351 if (!h
->ref_regular_nonweak
)
3356 if (h
->got
.offset
!= (bfd_vma
) -1
3357 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
| GOT_TLSDESC
))
3358 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3362 Elf_Internal_Rela rela
;
3363 bool use_elf_append_rela
= true;
3365 /* This symbol has an entry in the GOT. Set it up. */
3367 sgot
= htab
->elf
.sgot
;
3368 srela
= htab
->elf
.srelgot
;
3369 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3371 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3373 /* Handle the ifunc symbol in GOT entry. */
3375 && h
->type
== STT_GNU_IFUNC
)
3377 if (h
->plt
.offset
== (bfd_vma
) -1)
3379 /* STT_GNU_IFUNC is referenced without PLT. */
3381 if (htab
->elf
.splt
== NULL
)
3383 /* Use .rela.iplt section to store .got relocations
3384 in static executable. */
3385 srela
= htab
->elf
.irelplt
;
3387 /* Do not use riscv_elf_append_rela to add dynamic
3389 use_elf_append_rela
= false;
3392 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3394 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3395 h
->root
.root
.string
,
3396 h
->root
.u
.def
.section
->owner
);
3398 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3399 rela
.r_addend
= (h
->root
.u
.def
.value
3400 + h
->root
.u
.def
.section
->output_section
->vma
3401 + h
->root
.u
.def
.section
->output_offset
);
3405 /* Generate R_RISCV_NN. */
3406 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3407 BFD_ASSERT (h
->dynindx
!= -1);
3408 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3412 else if (bfd_link_pic (info
))
3414 /* Generate R_RISCV_NN. */
3415 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3416 BFD_ASSERT (h
->dynindx
!= -1);
3417 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3424 if (!h
->pointer_equality_needed
)
3427 /* For non-shared object, we can't use .got.plt, which
3428 contains the real function address if we need pointer
3429 equality. We load the GOT entry with the PLT entry. */
3430 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3431 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3432 + plt
->output_offset
3434 htab
->elf
.sgot
->contents
3435 + (h
->got
.offset
& ~(bfd_vma
) 1));
3439 else if (bfd_link_pic (info
)
3440 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3442 /* If this is a local symbol reference, we just want to emit
3443 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3444 or a pie link, or the symbol was forced to be local because
3445 of a version file. The entry in the global offset table will
3446 already have been initialized in the relocate_section function. */
3447 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3448 asection
*sec
= h
->root
.u
.def
.section
;
3449 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3450 rela
.r_addend
= (h
->root
.u
.def
.value
3451 + sec
->output_section
->vma
3452 + sec
->output_offset
);
3456 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3457 BFD_ASSERT (h
->dynindx
!= -1);
3458 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3462 bfd_put_NN (output_bfd
, 0,
3463 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3465 if (use_elf_append_rela
)
3466 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3469 /* Use riscv_elf_append_rela to add the dynamic relocs into
3470 .rela.iplt may cause the overwrite problems. Since we insert
3471 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3472 but the riscv_elf_append_rela adds the relocs to the place
3473 that are calculated from the reloc_index (in seqential).
3475 One solution is that add these dynamic relocs (GOT IFUNC)
3476 from the last of .rela.iplt section. */
3477 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3478 bfd_byte
*loc
= srela
->contents
3479 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3480 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3486 Elf_Internal_Rela rela
;
3489 /* This symbols needs a copy reloc. Set it up. */
3490 BFD_ASSERT (h
->dynindx
!= -1);
3492 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3493 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3495 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3496 s
= htab
->elf
.sreldynrelro
;
3498 s
= htab
->elf
.srelbss
;
3499 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3502 /* Mark some specially defined symbols as absolute. */
3503 if (h
== htab
->elf
.hdynamic
3504 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3505 sym
->st_shndx
= SHN_ABS
;
3510 /* Finish up local dynamic symbol handling. We set the contents of
3511 various dynamic sections here. */
3514 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3516 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3517 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3519 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3522 /* Finish up the dynamic sections. */
3525 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3526 bfd
*dynobj
, asection
*sdyn
)
3528 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3529 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3530 size_t dynsize
= bed
->s
->sizeof_dyn
;
3531 bfd_byte
*dyncon
, *dynconend
;
3533 dynconend
= sdyn
->contents
+ sdyn
->size
;
3534 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3536 Elf_Internal_Dyn dyn
;
3539 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3544 s
= htab
->elf
.sgotplt
;
3545 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3548 s
= htab
->elf
.srelplt
;
3549 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3552 s
= htab
->elf
.srelplt
;
3553 dyn
.d_un
.d_val
= s
->size
;
3559 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3565 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3566 struct bfd_link_info
*info
)
3570 struct riscv_elf_link_hash_table
*htab
;
3572 htab
= riscv_elf_hash_table (info
);
3573 BFD_ASSERT (htab
!= NULL
);
3574 dynobj
= htab
->elf
.dynobj
;
3576 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3578 if (elf_hash_table (info
)->dynamic_sections_created
)
3583 splt
= htab
->elf
.splt
;
3584 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3586 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3591 /* Fill in the head and tail entries in the procedure linkage table. */
3595 uint32_t plt_header
[PLT_HEADER_INSNS
];
3596 ret
= riscv_make_plt_header (output_bfd
,
3597 sec_addr (htab
->elf
.sgotplt
),
3598 sec_addr (splt
), plt_header
);
3602 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3603 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3605 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3610 if (htab
->elf
.sgotplt
&& htab
->elf
.sgotplt
->size
> 0)
3612 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3614 if (bfd_is_abs_section (output_section
))
3616 (*_bfd_error_handler
)
3617 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3621 /* Write the first two entries in .got.plt, needed for the dynamic
3623 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3624 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3625 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3627 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3630 if (htab
->elf
.sgot
&& htab
->elf
.sgot
->size
> 0)
3632 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3634 if (!bfd_is_abs_section (output_section
))
3636 /* Set the first entry in the global offset table to the address of
3637 the dynamic section. */
3638 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3639 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3641 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3645 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3646 htab_traverse (htab
->loc_hash_table
,
3647 riscv_elf_finish_local_dynamic_symbol
,
3653 /* Return address for Ith PLT stub in section PLT, for relocation REL
3654 or (bfd_vma) -1 if it should not be included. */
3657 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3658 const arelent
*rel ATTRIBUTE_UNUSED
)
3660 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3663 /* Used to decide how to sort relocs in an optimal manner for the
3664 dynamic linker, before writing them out. */
3666 static enum elf_reloc_type_class
3667 riscv_reloc_type_class (const struct bfd_link_info
*info
,
3668 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3669 const Elf_Internal_Rela
*rela
)
3671 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3673 if (htab
->elf
.dynsym
!= NULL
3674 && htab
->elf
.dynsym
->contents
!= NULL
)
3676 /* Check relocation against STT_GNU_IFUNC symbol if there are
3678 bfd
*abfd
= info
->output_bfd
;
3679 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
3680 unsigned long r_symndx
= ELFNN_R_SYM (rela
->r_info
);
3681 if (r_symndx
!= STN_UNDEF
)
3683 Elf_Internal_Sym sym
;
3684 if (!bed
->s
->swap_symbol_in (abfd
,
3685 (htab
->elf
.dynsym
->contents
3686 + r_symndx
* bed
->s
->sizeof_sym
),
3689 /* xgettext:c-format */
3690 _bfd_error_handler (_("%pB symbol number %lu references"
3691 " nonexistent SHT_SYMTAB_SHNDX section"),
3693 /* Ideally an error class should be returned here. */
3695 else if (ELF_ST_TYPE (sym
.st_info
) == STT_GNU_IFUNC
)
3696 return reloc_class_ifunc
;
3700 switch (ELFNN_R_TYPE (rela
->r_info
))
3702 case R_RISCV_IRELATIVE
:
3703 return reloc_class_ifunc
;
3704 case R_RISCV_RELATIVE
:
3705 return reloc_class_relative
;
3706 case R_RISCV_JUMP_SLOT
:
3707 return reloc_class_plt
;
3709 return reloc_class_copy
;
3711 return reloc_class_normal
;
3715 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3719 riscv_float_abi_string (flagword flags
)
3721 switch (flags
& EF_RISCV_FLOAT_ABI
)
3723 case EF_RISCV_FLOAT_ABI_SOFT
:
3724 return "soft-float";
3726 case EF_RISCV_FLOAT_ABI_SINGLE
:
3727 return "single-float";
3729 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3730 return "double-float";
3732 case EF_RISCV_FLOAT_ABI_QUAD
:
3733 return "quad-float";
3740 /* The information of architecture elf attributes. */
3741 static riscv_subset_list_t in_subsets
;
3742 static riscv_subset_list_t out_subsets
;
3743 static riscv_subset_list_t merged_subsets
;
3745 /* Predicator for standard extension. */
3748 riscv_std_ext_p (const char *name
)
3750 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3753 /* Update the output subset's version to match the input when the input
3754 subset's version is newer. */
3757 riscv_update_subset_version (struct riscv_subset_t
*in
,
3758 struct riscv_subset_t
*out
)
3760 if (in
== NULL
|| out
== NULL
)
3763 /* Update the output ISA versions to the newest ones, but otherwise don't
3764 provide any errors or warnings about mis-matched ISA versions as it's
3765 generally too tricky to check for these at link time. */
3766 if ((in
->major_version
> out
->major_version
)
3767 || (in
->major_version
== out
->major_version
3768 && in
->minor_version
> out
->minor_version
)
3769 || (out
->major_version
== RISCV_UNKNOWN_VERSION
))
3771 out
->major_version
= in
->major_version
;
3772 out
->minor_version
= in
->minor_version
;
3776 /* Return true if subset is 'i' or 'e'. */
3779 riscv_i_or_e_p (bfd
*ibfd
,
3781 struct riscv_subset_t
*subset
)
3783 if ((strcasecmp (subset
->name
, "e") != 0)
3784 && (strcasecmp (subset
->name
, "i") != 0))
3787 (_("error: %pB: corrupted ISA string '%s'. "
3788 "First letter should be 'i' or 'e' but got '%s'"),
3789 ibfd
, arch
, subset
->name
);
3795 /* Merge standard extensions.
3798 Return FALSE if failed to merge.
3802 `in_arch`: Raw ISA string for input object.
3803 `out_arch`: Raw ISA string for output object.
3804 `pin`: Subset list for input object.
3805 `pout`: Subset list for output object. */
3808 riscv_merge_std_ext (bfd
*ibfd
,
3809 const char *in_arch
,
3810 const char *out_arch
,
3811 struct riscv_subset_t
**pin
,
3812 struct riscv_subset_t
**pout
)
3814 const char *standard_exts
= "mafdqlcbjtpvnh";
3816 struct riscv_subset_t
*in
= *pin
;
3817 struct riscv_subset_t
*out
= *pout
;
3819 /* First letter should be 'i' or 'e'. */
3820 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3823 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3826 if (strcasecmp (in
->name
, out
->name
) != 0)
3828 /* TODO: We might allow merge 'i' with 'e'. */
3830 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3831 ibfd
, in
->name
, out
->name
);
3835 riscv_update_subset_version(in
, out
);
3836 riscv_add_subset (&merged_subsets
,
3837 out
->name
, out
->major_version
, out
->minor_version
);
3842 /* Handle standard extension first. */
3843 for (p
= standard_exts
; *p
; ++p
)
3845 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3846 char find_ext
[2] = {*p
, '\0'};
3847 bool find_in
, find_out
;
3849 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3850 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3852 if (!find_in
&& !find_out
)
3855 if (find_in
&& find_out
)
3856 riscv_update_subset_version(ext_in
, ext_out
);
3858 ext_merged
= find_out
? ext_out
: ext_in
;
3859 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3860 ext_merged
->major_version
, ext_merged
->minor_version
);
3863 /* Skip all standard extensions. */
3864 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3865 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3873 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3874 object subset list. Likewise for POUT and the output object. Return TRUE
3875 on success and FALSE when a conflict is found. */
3878 riscv_merge_multi_letter_ext (riscv_subset_t
**pin
,
3879 riscv_subset_t
**pout
)
3881 riscv_subset_t
*in
= *pin
;
3882 riscv_subset_t
*out
= *pout
;
3883 riscv_subset_t
*tail
;
3889 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3893 /* `in' comes before `out', append `in' and increment. */
3894 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3900 /* `out' comes before `in', append `out' and increment. */
3901 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3902 out
->minor_version
);
3907 /* Both present, check version and increment both. */
3908 riscv_update_subset_version (in
, out
);
3910 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3911 out
->minor_version
);
3919 /* If we're here, either `in' or `out' is running longer than
3920 the other. So, we need to append the corresponding tail. */
3921 tail
= in
? in
: out
;
3924 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3925 tail
->minor_version
);
3933 /* Merge Tag_RISCV_arch attribute. */
3936 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3938 riscv_subset_t
*in
, *out
;
3939 char *merged_arch_str
;
3941 unsigned xlen_in
, xlen_out
;
3942 merged_subsets
.head
= NULL
;
3943 merged_subsets
.tail
= NULL
;
3945 riscv_parse_subset_t riscv_rps_ld_in
=
3946 {&in_subsets
, _bfd_error_handler
, &xlen_in
, NULL
, false};
3947 riscv_parse_subset_t riscv_rps_ld_out
=
3948 {&out_subsets
, _bfd_error_handler
, &xlen_out
, NULL
, false};
3950 if (in_arch
== NULL
&& out_arch
== NULL
)
3952 if (in_arch
== NULL
&& out_arch
!= NULL
)
3954 if (in_arch
!= NULL
&& out_arch
== NULL
)
3957 /* Parse subset from ISA string. */
3958 if (!riscv_parse_subset (&riscv_rps_ld_in
, in_arch
))
3960 if (!riscv_parse_subset (&riscv_rps_ld_out
, out_arch
))
3963 /* Checking XLEN. */
3964 if (xlen_out
!= xlen_in
)
3967 (_("error: %pB: ISA string of input (%s) doesn't match "
3968 "output (%s)"), ibfd
, in_arch
, out_arch
);
3972 /* Merge subset list. */
3973 in
= in_subsets
.head
;
3974 out
= out_subsets
.head
;
3976 /* Merge standard extension. */
3977 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3980 /* Merge all non-single letter extensions with single call. */
3981 if (!riscv_merge_multi_letter_ext (&in
, &out
))
3984 if (xlen_in
!= xlen_out
)
3987 (_("error: %pB: XLEN of input (%u) doesn't match "
3988 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3992 if (xlen_in
!= ARCH_SIZE
)
3995 (_("error: %pB: unsupported XLEN (%u), you might be "
3996 "using wrong emulation"), ibfd
, xlen_in
);
4000 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
4002 /* Release the subset lists. */
4003 riscv_release_subset_list (&in_subsets
);
4004 riscv_release_subset_list (&out_subsets
);
4005 riscv_release_subset_list (&merged_subsets
);
4007 return merged_arch_str
;
4010 /* Merge object attributes from IBFD into output_bfd of INFO.
4011 Raise an error if there are conflicting attributes. */
4014 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
4016 bfd
*obfd
= info
->output_bfd
;
4017 obj_attribute
*in_attr
;
4018 obj_attribute
*out_attr
;
4020 bool priv_attrs_merged
= false;
4021 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
4024 /* Skip linker created files. */
4025 if (ibfd
->flags
& BFD_LINKER_CREATED
)
4028 /* Skip any input that doesn't have an attribute section.
4029 This enables to link object files without attribute section with
4031 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
4034 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
4036 /* This is the first object. Copy the attributes. */
4037 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
4039 out_attr
= elf_known_obj_attributes_proc (obfd
);
4041 /* Use the Tag_null value to indicate the attributes have been
4048 in_attr
= elf_known_obj_attributes_proc (ibfd
);
4049 out_attr
= elf_known_obj_attributes_proc (obfd
);
4051 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
4055 case Tag_RISCV_arch
:
4056 if (!out_attr
[Tag_RISCV_arch
].s
)
4057 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
4058 else if (in_attr
[Tag_RISCV_arch
].s
4059 && out_attr
[Tag_RISCV_arch
].s
)
4061 /* Check compatible. */
4063 riscv_merge_arch_attr_info (ibfd
,
4064 in_attr
[Tag_RISCV_arch
].s
,
4065 out_attr
[Tag_RISCV_arch
].s
);
4066 if (merged_arch
== NULL
)
4069 out_attr
[Tag_RISCV_arch
].s
= "";
4072 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
4076 case Tag_RISCV_priv_spec
:
4077 case Tag_RISCV_priv_spec_minor
:
4078 case Tag_RISCV_priv_spec_revision
:
4079 /* If we have handled the privileged elf attributes, then skip it. */
4080 if (!priv_attrs_merged
)
4082 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
4083 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
4084 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
4085 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
4086 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
4088 /* Get the privileged spec class from elf attributes. */
4089 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
4093 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
4098 /* Allow to link the object without the privileged specs. */
4099 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
4101 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
4102 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
4103 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
4105 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
4106 && in_priv_spec
!= out_priv_spec
)
4108 /* The abandoned privileged spec v1.9.1 can not be linked with
4109 others since the conflicts. Keep the check since compatible
4111 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
4112 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
4115 (_("warning: privileged spec version 1.9.1 can not be "
4116 "linked with other spec versions"));
4119 /* Update the output privileged spec to the newest one. */
4120 if (in_priv_spec
> out_priv_spec
)
4122 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
4123 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
4124 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
4127 priv_attrs_merged
= true;
4131 case Tag_RISCV_unaligned_access
:
4132 out_attr
[i
].i
|= in_attr
[i
].i
;
4135 case Tag_RISCV_stack_align
:
4136 if (out_attr
[i
].i
== 0)
4137 out_attr
[i
].i
= in_attr
[i
].i
;
4138 else if (in_attr
[i
].i
!= 0
4139 && out_attr
[i
].i
!= 0
4140 && out_attr
[i
].i
!= in_attr
[i
].i
)
4143 (_("error: %pB use %u-byte stack aligned but the output "
4144 "use %u-byte stack aligned"),
4145 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
4151 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
4154 /* If out_attr was copied from in_attr then it won't have a type yet. */
4155 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
4156 out_attr
[i
].type
= in_attr
[i
].type
;
4159 /* Merge Tag_compatibility attributes and any common GNU ones. */
4160 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
4163 /* Check for any attributes not known on RISC-V. */
4164 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
4169 /* Merge backend specific data from an object file to the output
4170 object file when linking. */
4173 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
4175 bfd
*obfd
= info
->output_bfd
;
4176 flagword new_flags
, old_flags
;
4178 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
4181 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
4183 (*_bfd_error_handler
)
4184 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
4185 " target emulation `%s' does not match `%s'"),
4186 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
4190 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
4193 if (!riscv_merge_attributes (ibfd
, info
))
4196 /* Check to see if the input BFD actually contains any sections. If not,
4197 its flags may not have been initialized either, but it cannot actually
4198 cause any incompatibility. Do not short-circuit dynamic objects; their
4199 section list may be emptied by elf_link_add_object_symbols.
4201 Also check to see if there are no code sections in the input. In this
4202 case, there is no need to check for code specific flags. */
4203 if (!(ibfd
->flags
& DYNAMIC
))
4205 bool null_input_bfd
= true;
4206 bool only_data_sections
= true;
4209 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
4211 null_input_bfd
= false;
4213 if ((bfd_section_flags (sec
)
4214 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
4215 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
4217 only_data_sections
= false;
4222 if (null_input_bfd
|| only_data_sections
)
4226 new_flags
= elf_elfheader (ibfd
)->e_flags
;
4227 old_flags
= elf_elfheader (obfd
)->e_flags
;
4229 if (!elf_flags_init (obfd
))
4231 elf_flags_init (obfd
) = true;
4232 elf_elfheader (obfd
)->e_flags
= new_flags
;
4236 /* Disallow linking different float ABIs. */
4237 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
4239 (*_bfd_error_handler
)
4240 (_("%pB: can't link %s modules with %s modules"), ibfd
,
4241 riscv_float_abi_string (new_flags
),
4242 riscv_float_abi_string (old_flags
));
4246 /* Disallow linking RVE and non-RVE. */
4247 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
4249 (*_bfd_error_handler
)
4250 (_("%pB: can't link RVE with other target"), ibfd
);
4254 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
4255 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
4257 /* Allow linking TSO and non-TSO, and keep the TSO flag. */
4258 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_TSO
;
4263 bfd_set_error (bfd_error_bad_value
);
4267 /* Ignore and report warning for the unknwon elf attribute. */
4270 riscv_elf_obj_attrs_handle_unknown (bfd
*abfd
, int tag
)
4273 /* xgettext:c-format */
4274 (_("warning: %pB: unknown RISCV ABI object attribute %d"),
4279 /* A second format for recording PC-relative hi relocations. This stores the
4280 information required to relax them to GP-relative addresses. */
4282 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
4283 struct riscv_pcgp_hi_reloc
4290 bool undefined_weak
;
4291 riscv_pcgp_hi_reloc
*next
;
4294 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
4295 struct riscv_pcgp_lo_reloc
4298 riscv_pcgp_lo_reloc
*next
;
4303 riscv_pcgp_hi_reloc
*hi
;
4304 riscv_pcgp_lo_reloc
*lo
;
4305 } riscv_pcgp_relocs
;
4307 /* Initialize the pcgp reloc info in P. */
4310 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
4317 /* Free the pcgp reloc info in P. */
4320 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
4321 bfd
*abfd ATTRIBUTE_UNUSED
,
4322 asection
*sec ATTRIBUTE_UNUSED
)
4324 riscv_pcgp_hi_reloc
*c
;
4325 riscv_pcgp_lo_reloc
*l
;
4327 for (c
= p
->hi
; c
!= NULL
; )
4329 riscv_pcgp_hi_reloc
*next
= c
->next
;
4334 for (l
= p
->lo
; l
!= NULL
; )
4336 riscv_pcgp_lo_reloc
*next
= l
->next
;
4342 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4343 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4344 relax the corresponding lo part reloc. */
4347 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4348 bfd_vma hi_addend
, bfd_vma hi_addr
,
4349 unsigned hi_sym
, asection
*sym_sec
,
4350 bool undefined_weak
)
4352 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
4355 new->hi_sec_off
= hi_sec_off
;
4356 new->hi_addend
= hi_addend
;
4357 new->hi_addr
= hi_addr
;
4358 new->hi_sym
= hi_sym
;
4359 new->sym_sec
= sym_sec
;
4360 new->undefined_weak
= undefined_weak
;
4366 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4367 This is used by a lo part reloc to find the corresponding hi part reloc. */
4369 static riscv_pcgp_hi_reloc
*
4370 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4372 riscv_pcgp_hi_reloc
*c
;
4374 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4375 if (c
->hi_sec_off
== hi_sec_off
)
4380 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4381 This is used to record relocs that can't be relaxed. */
4384 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4386 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4389 new->hi_sec_off
= hi_sec_off
;
4395 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4396 This is used by a hi part reloc to find the corresponding lo part reloc. */
4399 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4401 riscv_pcgp_lo_reloc
*c
;
4403 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4404 if (c
->hi_sec_off
== hi_sec_off
)
4410 riscv_update_pcgp_relocs (riscv_pcgp_relocs
*p
, asection
*deleted_sec
,
4411 bfd_vma deleted_addr
, size_t deleted_count
)
4413 /* Bytes have already been deleted and toaddr should match the old section
4414 size for our checks, so adjust it here. */
4415 bfd_vma toaddr
= deleted_sec
->size
+ deleted_count
;
4416 riscv_pcgp_lo_reloc
*l
;
4417 riscv_pcgp_hi_reloc
*h
;
4419 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4420 entries where they occur after the deleted bytes. */
4421 for (l
= p
->lo
; l
!= NULL
; l
= l
->next
)
4422 if (l
->hi_sec_off
> deleted_addr
4423 && l
->hi_sec_off
< toaddr
)
4424 l
->hi_sec_off
-= deleted_count
;
4426 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4427 these values occur after the deleted bytes. */
4428 for (h
= p
->hi
; h
!= NULL
; h
= h
->next
)
4430 if (h
->hi_sec_off
> deleted_addr
4431 && h
->hi_sec_off
< toaddr
)
4432 h
->hi_sec_off
-= deleted_count
;
4433 if (h
->sym_sec
== deleted_sec
4434 && h
->hi_addr
> deleted_addr
4435 && h
->hi_addr
< toaddr
)
4436 h
->hi_addr
-= deleted_count
;
4440 /* Delete some bytes, adjust relcocations and symbol table from a section. */
4443 _riscv_relax_delete_bytes (bfd
*abfd
,
4447 struct bfd_link_info
*link_info
,
4448 riscv_pcgp_relocs
*p
,
4449 bfd_vma delete_total
,
4452 unsigned int i
, symcount
;
4453 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
4454 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4455 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4456 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4457 bfd_byte
*contents
= data
->this_hdr
.contents
;
4458 size_t bytes_to_move
= toaddr
- addr
- count
;
4460 /* Actually delete the bytes. */
4462 memmove (contents
+ addr
, contents
+ addr
+ count
+ delete_total
, bytes_to_move
);
4464 /* Still adjust relocations and symbols in non-linear times. */
4465 toaddr
= sec
->size
+ count
;
4467 /* Adjust the location of all of the relocs. Note that we need not
4468 adjust the addends, since all PC-relative references must be against
4469 symbols, which we will adjust below. */
4470 for (i
= 0; i
< sec
->reloc_count
; i
++)
4471 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
4472 data
->relocs
[i
].r_offset
-= count
;
4474 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4475 table for which these values occur after the deleted bytes. */
4477 riscv_update_pcgp_relocs (p
, sec
, addr
, count
);
4479 /* Adjust the local symbols defined in this section. */
4480 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4482 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
4483 if (sym
->st_shndx
== sec_shndx
)
4485 /* If the symbol is in the range of memory we just moved, we
4486 have to adjust its value. */
4487 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
4488 sym
->st_value
-= count
;
4490 /* If the symbol *spans* the bytes we just deleted (i.e. its
4491 *end* is in the moved bytes but its *start* isn't), then we
4492 must adjust its size.
4494 This test needs to use the original value of st_value, otherwise
4495 we might accidentally decrease size when deleting bytes right
4496 before the symbol. But since deleted relocs can't span across
4497 symbols, we can't have both a st_value and a st_size decrease,
4498 so it is simpler to just use an else. */
4499 else if (sym
->st_value
<= addr
4500 && sym
->st_value
+ sym
->st_size
> addr
4501 && sym
->st_value
+ sym
->st_size
<= toaddr
)
4502 sym
->st_size
-= count
;
4506 /* Now adjust the global symbols defined in this section. */
4507 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
4508 - symtab_hdr
->sh_info
);
4510 for (i
= 0; i
< symcount
; i
++)
4512 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
4514 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4515 containing the definition of __wrap_SYMBOL, includes a direct
4516 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4517 the same symbol (which is __wrap_SYMBOL), but still exist as two
4518 different symbols in 'sym_hashes', we don't want to adjust
4519 the global symbol __wrap_SYMBOL twice.
4521 The same problem occurs with symbols that are versioned_hidden, as
4522 foo becomes an alias for foo@BAR, and hence they need the same
4524 if (link_info
->wrap_hash
!= NULL
4525 || sym_hash
->versioned
!= unversioned
)
4527 struct elf_link_hash_entry
**cur_sym_hashes
;
4529 /* Loop only over the symbols which have already been checked. */
4530 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4533 /* If the current symbol is identical to 'sym_hash', that means
4534 the symbol was already adjusted (or at least checked). */
4535 if (*cur_sym_hashes
== sym_hash
)
4538 /* Don't adjust the symbol again. */
4539 if (cur_sym_hashes
< &sym_hashes
[i
])
4543 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4544 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4545 && sym_hash
->root
.u
.def
.section
== sec
)
4547 /* As above, adjust the value if needed. */
4548 if (sym_hash
->root
.u
.def
.value
> addr
4549 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4550 sym_hash
->root
.u
.def
.value
-= count
;
4552 /* As above, adjust the size if needed. */
4553 else if (sym_hash
->root
.u
.def
.value
<= addr
4554 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4555 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4556 sym_hash
->size
-= count
;
4563 typedef bool (*relax_delete_t
) (bfd
*, asection
*,
4565 struct bfd_link_info
*,
4566 riscv_pcgp_relocs
*,
4567 Elf_Internal_Rela
*);
4569 static relax_delete_t riscv_relax_delete_bytes
;
4571 /* Do not delete some bytes from a section while relaxing.
4572 Just mark the deleted bytes as R_RISCV_DELETE. */
4575 _riscv_relax_delete_piecewise (bfd
*abfd ATTRIBUTE_UNUSED
,
4576 asection
*sec ATTRIBUTE_UNUSED
,
4579 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
,
4580 riscv_pcgp_relocs
*p ATTRIBUTE_UNUSED
,
4581 Elf_Internal_Rela
*rel
)
4585 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4586 rel
->r_offset
= addr
;
4587 rel
->r_addend
= count
;
4591 /* Delete some bytes from a section while relaxing. */
4594 _riscv_relax_delete_immediate (bfd
*abfd
,
4598 struct bfd_link_info
*link_info
,
4599 riscv_pcgp_relocs
*p
,
4600 Elf_Internal_Rela
*rel
)
4603 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4604 return _riscv_relax_delete_bytes (abfd
, sec
, addr
, count
,
4605 link_info
, p
, 0, sec
->size
);
4608 /* Delete the bytes for R_RISCV_DELETE relocs. */
4611 riscv_relax_resolve_delete_relocs (bfd
*abfd
,
4613 struct bfd_link_info
*link_info
,
4614 Elf_Internal_Rela
*relocs
)
4616 bfd_vma delete_total
= 0;
4619 for (i
= 0; i
< sec
->reloc_count
; i
++)
4621 Elf_Internal_Rela
*rel
= relocs
+ i
;
4622 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_DELETE
)
4625 /* Find the next R_RISCV_DELETE reloc if possible. */
4626 Elf_Internal_Rela
*rel_next
= NULL
;
4627 unsigned int start
= rel
- relocs
;
4628 for (i
= start
; i
< sec
->reloc_count
; i
++)
4630 /* Since we only replace existing relocs and don't add new relocs, the
4631 relocs are in sequential order. We can skip the relocs prior to this
4632 one, making this search linear time. */
4633 rel_next
= relocs
+ i
;
4634 if (ELFNN_R_TYPE ((rel_next
)->r_info
) == R_RISCV_DELETE
4635 && (rel_next
)->r_offset
> rel
->r_offset
)
4637 BFD_ASSERT (rel_next
- rel
> 0);
4644 bfd_vma toaddr
= rel_next
== NULL
? sec
->size
: rel_next
->r_offset
;
4645 if (!_riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4646 link_info
, NULL
, delete_total
, toaddr
))
4649 delete_total
+= rel
->r_addend
;
4650 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4652 /* Skip ahead to the next delete reloc. */
4653 i
= rel_next
!= NULL
? (unsigned int) (rel_next
- relocs
- 1)
4660 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4661 struct bfd_link_info
*,
4662 Elf_Internal_Rela
*,
4663 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4664 riscv_pcgp_relocs
*,
4665 bool undefined_weak
);
4667 /* Relax AUIPC + JALR into JAL. */
4670 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4671 struct bfd_link_info
*link_info
,
4672 Elf_Internal_Rela
*rel
,
4674 bfd_vma max_alignment
,
4675 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4677 riscv_pcgp_relocs
*pcgp_relocs
,
4678 bool undefined_weak ATTRIBUTE_UNUSED
)
4680 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4681 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4682 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4683 bfd_vma auipc
, jalr
;
4684 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4686 /* If the call crosses section boundaries, an alignment directive could
4687 cause the PC-relative offset to later increase, so we need to add in the
4688 max alignment of any section inclusive from the call to the target.
4689 Otherwise, we only need to use the alignment of the current section. */
4690 if (VALID_JTYPE_IMM (foff
))
4692 if (sym_sec
->output_section
== sec
->output_section
4693 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4694 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4695 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4698 /* See if this function call can be shortened. */
4699 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4702 /* Shorten the function call. */
4703 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4705 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4706 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4707 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4708 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4710 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4711 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4715 /* Relax to C.J[AL] rd, addr. */
4716 r_type
= R_RISCV_RVC_JUMP
;
4717 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4720 else if (VALID_JTYPE_IMM (foff
))
4722 /* Relax to JAL rd, addr. */
4723 r_type
= R_RISCV_JAL
;
4724 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4728 /* Near zero, relax to JALR rd, x0, addr. */
4729 r_type
= R_RISCV_LO12_I
;
4730 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4733 /* Replace the R_RISCV_CALL reloc. */
4734 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4735 /* Replace the AUIPC. */
4736 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4738 /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */
4740 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4741 link_info
, pcgp_relocs
, rel
+ 1);
4744 /* Traverse all output sections and return the max alignment.
4746 If gp is zero, then all the output section alignments are
4747 possible candidates; Otherwise, only the output sections
4748 which are in the [gp-2K, gp+2K) range need to be considered. */
4751 _bfd_riscv_get_max_alignment (asection
*sec
, bfd_vma gp
)
4753 unsigned int max_alignment_power
= 0;
4756 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4760 && !(VALID_ITYPE_IMM (sec_addr (o
) - gp
)
4761 || VALID_ITYPE_IMM (sec_addr (o
) + o
->size
- gp
)))
4764 if (valid
&& o
->alignment_power
> max_alignment_power
)
4765 max_alignment_power
= o
->alignment_power
;
4768 return (bfd_vma
) 1 << max_alignment_power
;
4771 /* Relax non-PIC global variable references to GP-relative references. */
4774 _bfd_riscv_relax_lui (bfd
*abfd
,
4777 struct bfd_link_info
*link_info
,
4778 Elf_Internal_Rela
*rel
,
4780 bfd_vma max_alignment
,
4781 bfd_vma reserve_size
,
4783 riscv_pcgp_relocs
*pcgp_relocs
,
4784 bool undefined_weak
)
4786 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (link_info
);
4787 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4788 /* Can relax to x0 even when gp relaxation is disabled. */
4789 bfd_vma gp
= htab
->params
->relax_gp
4790 ? riscv_global_pointer_value (link_info
)
4792 bfd_vma data_segment_alignment
= link_info
->relro
4793 ? ELF_MAXPAGESIZE
+ ELF_COMMONPAGESIZE
4795 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4797 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4799 if (!undefined_weak
&& gp
)
4801 /* If gp and the symbol are in the same output section, which is not the
4802 abs section, then consider only that output section's alignment. */
4803 struct bfd_link_hash_entry
*h
=
4804 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4806 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4807 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4808 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4811 /* Consider output section alignments which are in [gp-2K, gp+2K). */
4812 max_alignment
= htab
->max_alignment_for_gp
;
4813 if (max_alignment
== (bfd_vma
) -1)
4815 max_alignment
= _bfd_riscv_get_max_alignment (sec
, gp
);
4816 htab
->max_alignment_for_gp
= max_alignment
;
4820 /* PR27566, for default linker script, if a symbol's value outsides the
4821 bounds of the defined section, then it may cross the data segment
4822 alignment, so we should reserve more size about MAXPAGESIZE and
4823 COMMONPAGESIZE, since the data segment alignment might move the
4825 if (symval
< sec_addr (sym_sec
)
4826 || symval
> (sec_addr (sym_sec
) + sym_sec
->size
))
4827 max_alignment
= data_segment_alignment
> max_alignment
4828 ? data_segment_alignment
: max_alignment
;
4831 /* Is the reference in range of x0 or gp?
4832 Valid gp range conservatively because of alignment issue.
4834 Should we also consider the alignment issue for x0 base? */
4836 || VALID_ITYPE_IMM (symval
)
4838 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4840 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
)))
4842 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4843 switch (ELFNN_R_TYPE (rel
->r_info
))
4845 case R_RISCV_LO12_I
:
4846 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4849 case R_RISCV_LO12_S
:
4850 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4854 /* Delete unnecessary LUI and reuse the reloc. */
4856 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4857 link_info
, pcgp_relocs
, rel
);
4864 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4865 account for this assuming page alignment at worst. In the presence of
4866 RELRO segment the linker aligns it by one page size, therefore sections
4867 after the segment can be moved more than one page. */
4870 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4871 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4872 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4873 + data_segment_alignment
))
4875 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4876 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4877 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4878 if (rd
== 0 || rd
== X_SP
)
4881 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4882 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4884 /* Replace the R_RISCV_HI20 reloc. */
4885 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4887 /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */
4889 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4890 link_info
, pcgp_relocs
, rel
+ 1);
4896 /* Relax non-PIC TLS references to TP-relative references. */
4899 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4901 asection
*sym_sec ATTRIBUTE_UNUSED
,
4902 struct bfd_link_info
*link_info
,
4903 Elf_Internal_Rela
*rel
,
4905 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4906 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4908 riscv_pcgp_relocs
*pcgp_relocs
,
4909 bool undefined_weak ATTRIBUTE_UNUSED
)
4911 /* See if this symbol is in range of tp. */
4912 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4915 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4916 switch (ELFNN_R_TYPE (rel
->r_info
))
4918 case R_RISCV_TPREL_LO12_I
:
4919 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4922 case R_RISCV_TPREL_LO12_S
:
4923 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4926 case R_RISCV_TPREL_HI20
:
4927 case R_RISCV_TPREL_ADD
:
4928 /* Delete unnecessary instruction and reuse the reloc. */
4930 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4938 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4939 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4942 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4944 struct bfd_link_info
*link_info
,
4945 Elf_Internal_Rela
*rel
,
4947 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4948 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4949 bool *again ATTRIBUTE_UNUSED
,
4950 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4951 bool undefined_weak ATTRIBUTE_UNUSED
)
4953 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4954 bfd_vma alignment
= 1, pos
;
4955 while (alignment
<= rel
->r_addend
)
4958 symval
-= rel
->r_addend
;
4959 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4960 bfd_vma nop_bytes
= aligned_addr
- symval
;
4962 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4963 sec
->sec_flg0
= true;
4965 /* Make sure there are enough NOPs to actually achieve the alignment. */
4966 if (rel
->r_addend
< nop_bytes
)
4969 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4970 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4971 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4972 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4973 bfd_set_error (bfd_error_bad_value
);
4977 /* Delete the reloc. */
4978 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4980 /* If the number of NOPs is already correct, there's nothing to do. */
4981 if (nop_bytes
== rel
->r_addend
)
4984 /* Write as many RISC-V NOPs as we need. */
4985 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4986 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4988 /* Write a final RVC NOP if need be. */
4989 if (nop_bytes
% 4 != 0)
4990 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4992 /* Delete excess bytes. */
4993 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4994 rel
->r_addend
- nop_bytes
, link_info
,
4998 /* Relax PC-relative references to GP-relative references. */
5001 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
5004 struct bfd_link_info
*link_info
,
5005 Elf_Internal_Rela
*rel
,
5007 bfd_vma max_alignment
,
5008 bfd_vma reserve_size
,
5010 riscv_pcgp_relocs
*pcgp_relocs
,
5011 bool undefined_weak
)
5013 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (link_info
);
5014 /* Can relax to x0 even when gp relaxation is disabled. */
5015 bfd_vma gp
= htab
->params
->relax_gp
5016 ? riscv_global_pointer_value (link_info
)
5018 bfd_vma data_segment_alignment
= link_info
->relro
5019 ? ELF_MAXPAGESIZE
+ ELF_COMMONPAGESIZE
5022 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
5024 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
5025 actual target address. */
5026 riscv_pcgp_hi_reloc hi_reloc
;
5027 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
5028 switch (ELFNN_R_TYPE (rel
->r_info
))
5030 case R_RISCV_PCREL_LO12_I
:
5031 case R_RISCV_PCREL_LO12_S
:
5033 /* If the %lo has an addend, it isn't for the label pointing at the
5034 hi part instruction, but rather for the symbol pointed at by the
5035 hi part instruction. So we must subtract it here for the lookup.
5036 It is still used below in the final symbol address. */
5037 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
5038 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
5042 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
5047 symval
= hi_reloc
.hi_addr
;
5048 sym_sec
= hi_reloc
.sym_sec
;
5050 /* We can not know whether the undefined weak symbol is referenced
5051 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
5052 we have to record the 'undefined_weak' flag when handling the
5053 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
5054 undefined_weak
= hi_reloc
.undefined_weak
;
5058 case R_RISCV_PCREL_HI20
:
5059 /* Mergeable symbols and code might later move out of range. */
5060 if (! undefined_weak
5061 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
5064 /* If the cooresponding lo relocation has already been seen then it's not
5065 safe to relax this relocation. */
5066 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
5075 if (!undefined_weak
&& gp
)
5077 /* If gp and the symbol are in the same output section, which is not the
5078 abs section, then consider only that output section's alignment. */
5079 struct bfd_link_hash_entry
*h
=
5080 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
5082 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
5083 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
5084 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
5087 /* Consider output section alignments which are in [gp-2K, gp+2K). */
5088 max_alignment
= htab
->max_alignment_for_gp
;
5089 if (max_alignment
== (bfd_vma
) -1)
5091 max_alignment
= _bfd_riscv_get_max_alignment (sec
, gp
);
5092 htab
->max_alignment_for_gp
= max_alignment
;
5096 /* PR27566, for default linker script, if a symbol's value outsides the
5097 bounds of the defined section, then it may cross the data segment
5098 alignment, so we should reserve more size about MAXPAGESIZE and
5099 COMMONPAGESIZE, since the data segment alignment might move the
5101 if (symval
< sec_addr (sym_sec
)
5102 || symval
> (sec_addr (sym_sec
) + sym_sec
->size
))
5103 max_alignment
= data_segment_alignment
> max_alignment
5104 ? data_segment_alignment
: max_alignment
;
5107 /* Is the reference in range of x0 or gp?
5108 Valid gp range conservatively because of alignment issue.
5110 Should we also consider the alignment issue for x0 base? */
5112 || VALID_ITYPE_IMM (symval
)
5114 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
5116 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
)))
5118 unsigned sym
= hi_reloc
.hi_sym
;
5119 switch (ELFNN_R_TYPE (rel
->r_info
))
5121 case R_RISCV_PCREL_LO12_I
:
5122 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
5123 rel
->r_addend
+= hi_reloc
.hi_addend
;
5126 case R_RISCV_PCREL_LO12_S
:
5127 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
5128 rel
->r_addend
+= hi_reloc
.hi_addend
;
5131 case R_RISCV_PCREL_HI20
:
5132 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
5136 ELFNN_R_SYM(rel
->r_info
),
5139 /* Delete unnecessary AUIPC and reuse the reloc. */
5141 riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
5153 /* Called by after_allocation to set the information of data segment
5157 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
5158 int *data_segment_phase
)
5160 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
5161 htab
->data_segment_phase
= data_segment_phase
;
5166 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs and
5167 deletes the obsolete bytes.
5168 Pass 1: Which cannot be disabled, handles code alignment directives. */
5171 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
5172 struct bfd_link_info
*info
,
5175 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
5176 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
5177 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
5178 Elf_Internal_Rela
*relocs
;
5181 bfd_vma max_alignment
, reserve_size
= 0;
5182 riscv_pcgp_relocs pcgp_relocs
;
5183 static asection
*first_section
= NULL
;
5187 if (bfd_link_relocatable (info
)
5189 || sec
->reloc_count
== 0
5190 || (sec
->flags
& SEC_RELOC
) == 0
5191 || (sec
->flags
& SEC_HAS_CONTENTS
) == 0
5192 || (info
->disable_target_specific_optimizations
5193 && info
->relax_pass
== 0)
5194 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
5195 and defined in ld/ldexp.h. */
5196 || *(htab
->data_segment_phase
) == 4)
5199 /* Record the first relax section, so that we can reset the
5200 max_alignment_for_gp for the repeated relax passes. */
5201 if (first_section
== NULL
)
5202 first_section
= sec
;
5203 else if (first_section
== sec
)
5204 htab
->max_alignment_for_gp
= -1;
5206 riscv_init_pcgp_relocs (&pcgp_relocs
);
5208 /* Read this BFD's relocs if we haven't done so already. */
5210 relocs
= data
->relocs
;
5211 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
5212 info
->keep_memory
)))
5215 /* Estimate the maximum alignment for all output sections once time
5216 should be enough. */
5217 max_alignment
= htab
->max_alignment
;
5218 if (max_alignment
== (bfd_vma
) -1)
5220 max_alignment
= _bfd_riscv_get_max_alignment (sec
, 0/* gp */);
5221 htab
->max_alignment
= max_alignment
;
5224 /* Examine and consider relaxing each reloc. */
5225 for (i
= 0; i
< sec
->reloc_count
; i
++)
5228 Elf_Internal_Rela
*rel
= relocs
+ i
;
5229 relax_func_t relax_func
;
5230 int type
= ELFNN_R_TYPE (rel
->r_info
);
5233 bool undefined_weak
= false;
5236 riscv_relax_delete_bytes
= NULL
;
5237 if (info
->relax_pass
== 0)
5239 if (type
== R_RISCV_CALL
5240 || type
== R_RISCV_CALL_PLT
)
5241 relax_func
= _bfd_riscv_relax_call
;
5242 else if (type
== R_RISCV_HI20
5243 || type
== R_RISCV_LO12_I
5244 || type
== R_RISCV_LO12_S
)
5245 relax_func
= _bfd_riscv_relax_lui
;
5246 else if (type
== R_RISCV_TPREL_HI20
5247 || type
== R_RISCV_TPREL_ADD
5248 || type
== R_RISCV_TPREL_LO12_I
5249 || type
== R_RISCV_TPREL_LO12_S
)
5250 relax_func
= _bfd_riscv_relax_tls_le
;
5251 else if (!bfd_link_pic (info
)
5252 && (type
== R_RISCV_PCREL_HI20
5253 || type
== R_RISCV_PCREL_LO12_I
5254 || type
== R_RISCV_PCREL_LO12_S
))
5255 relax_func
= _bfd_riscv_relax_pc
;
5258 riscv_relax_delete_bytes
= _riscv_relax_delete_piecewise
;
5260 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
5261 if (i
== sec
->reloc_count
- 1
5262 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
5263 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
5266 /* Skip over the R_RISCV_RELAX. */
5269 else if (info
->relax_pass
== 1 && type
== R_RISCV_ALIGN
)
5271 relax_func
= _bfd_riscv_relax_align
;
5272 riscv_relax_delete_bytes
= _riscv_relax_delete_immediate
;
5277 data
->relocs
= relocs
;
5279 /* Read this BFD's contents if we haven't done so already. */
5280 if (!data
->this_hdr
.contents
5281 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
5284 /* Read this BFD's symbols if we haven't done so already. */
5285 if (symtab_hdr
->sh_info
!= 0
5286 && !symtab_hdr
->contents
5287 && !(symtab_hdr
->contents
=
5288 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
5289 symtab_hdr
->sh_info
,
5290 0, NULL
, NULL
, NULL
)))
5293 /* Get the value of the symbol referred to by the reloc. */
5294 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
5296 /* A local symbol. */
5297 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
5298 + ELFNN_R_SYM (rel
->r_info
));
5299 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
5300 ? 0 : isym
->st_size
- rel
->r_addend
;
5302 /* Relocate against local STT_GNU_IFUNC symbol. we have created
5303 a fake global symbol entry for this, so deal with the local ifunc
5305 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
5308 if (isym
->st_shndx
== SHN_UNDEF
)
5309 sym_sec
= sec
, symval
= rel
->r_offset
;
5312 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
5313 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
5315 /* The purpose of this code is unknown. It breaks linker scripts
5316 for embedded development that place sections at address zero.
5317 This code is believed to be unnecessary. Disabling it but not
5318 yet removing it, in case something breaks. */
5319 if (sec_addr (sym_sec
) == 0)
5322 symval
= isym
->st_value
;
5324 symtype
= ELF_ST_TYPE (isym
->st_info
);
5329 struct elf_link_hash_entry
*h
;
5331 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
5332 h
= elf_sym_hashes (abfd
)[indx
];
5334 while (h
->root
.type
== bfd_link_hash_indirect
5335 || h
->root
.type
== bfd_link_hash_warning
)
5336 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
5338 /* Disable the relaxation for ifunc. */
5339 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
5342 /* Maybe we should check UNDEFWEAK_NO_DYNAMIC_RELOC here? But that
5343 will break the undefweak relaxation testcases, so just make sure
5344 we won't do relaxations for linker_def symbols in short-term. */
5345 if (h
->root
.type
== bfd_link_hash_undefweak
5346 /* The linker_def symbol like __ehdr_start that may be undefweak
5347 for now, but will be guaranteed to be defined later. */
5348 && !h
->root
.linker_def
5349 && (relax_func
== _bfd_riscv_relax_lui
5350 || relax_func
== _bfd_riscv_relax_pc
))
5352 /* For the lui and auipc relaxations, since the symbol
5353 value of an undefined weak symbol is always be zero,
5354 we can optimize the patterns into a single LI/MV/ADDI
5357 Note that, creating shared libraries and pie output may
5358 break the rule above. Fortunately, since we do not relax
5359 pc relocs when creating shared libraries and pie output,
5360 and the absolute address access for R_RISCV_HI20 isn't
5361 allowed when "-fPIC" is set, the problem of creating shared
5362 libraries can not happen currently. Once we support the
5363 auipc relaxations when creating shared libraries, then we will
5364 need the more rigorous checking for this optimization. */
5365 undefined_weak
= true;
5368 /* This line has to match the check in riscv_elf_relocate_section
5369 in the R_RISCV_CALL[_PLT] case. */
5370 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
5372 sym_sec
= htab
->elf
.splt
;
5373 symval
= h
->plt
.offset
;
5375 else if (undefined_weak
)
5378 sym_sec
= bfd_und_section_ptr
;
5380 else if ((h
->root
.type
== bfd_link_hash_defined
5381 || h
->root
.type
== bfd_link_hash_defweak
)
5382 && h
->root
.u
.def
.section
!= NULL
5383 && h
->root
.u
.def
.section
->output_section
!= NULL
)
5385 symval
= h
->root
.u
.def
.value
;
5386 sym_sec
= h
->root
.u
.def
.section
;
5391 if (h
->type
!= STT_FUNC
)
5393 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
5397 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
5398 && (sym_sec
->flags
& SEC_MERGE
))
5400 /* At this stage in linking, no SEC_MERGE symbol has been
5401 adjusted, so all references to such symbols need to be
5402 passed through _bfd_merged_section_offset. (Later, in
5403 relocate_section, all SEC_MERGE symbols *except* for
5404 section symbols have been adjusted.)
5406 gas may reduce relocations against symbols in SEC_MERGE
5407 sections to a relocation against the section symbol when
5408 the original addend was zero. When the reloc is against
5409 a section symbol we should include the addend in the
5410 offset passed to _bfd_merged_section_offset, since the
5411 location of interest is the original symbol. On the
5412 other hand, an access to "sym+addend" where "sym" is not
5413 a section symbol should not include the addend; Such an
5414 access is presumed to be an offset from "sym"; The
5415 location of interest is just "sym". */
5416 if (symtype
== STT_SECTION
)
5417 symval
+= rel
->r_addend
;
5419 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
5420 elf_section_data (sym_sec
)->sec_info
,
5423 if (symtype
!= STT_SECTION
)
5424 symval
+= rel
->r_addend
;
5427 symval
+= rel
->r_addend
;
5429 symval
+= sec_addr (sym_sec
);
5431 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
5432 max_alignment
, reserve_size
, again
,
5433 &pcgp_relocs
, undefined_weak
))
5437 /* Resolve R_RISCV_DELETE relocations. */
5438 if (!riscv_relax_resolve_delete_relocs (abfd
, sec
, info
, relocs
))
5444 if (relocs
!= data
->relocs
)
5446 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
5452 # define PRSTATUS_SIZE 204
5453 # define PRSTATUS_OFFSET_PR_CURSIG 12
5454 # define PRSTATUS_OFFSET_PR_PID 24
5455 # define PRSTATUS_OFFSET_PR_REG 72
5456 # define ELF_GREGSET_T_SIZE 128
5457 # define PRPSINFO_SIZE 128
5458 # define PRPSINFO_OFFSET_PR_PID 16
5459 # define PRPSINFO_OFFSET_PR_FNAME 32
5460 # define PRPSINFO_OFFSET_PR_PSARGS 48
5461 # define PRPSINFO_PR_FNAME_LENGTH 16
5462 # define PRPSINFO_PR_PSARGS_LENGTH 80
5464 # define PRSTATUS_SIZE 376
5465 # define PRSTATUS_OFFSET_PR_CURSIG 12
5466 # define PRSTATUS_OFFSET_PR_PID 32
5467 # define PRSTATUS_OFFSET_PR_REG 112
5468 # define ELF_GREGSET_T_SIZE 256
5469 # define PRPSINFO_SIZE 136
5470 # define PRPSINFO_OFFSET_PR_PID 24
5471 # define PRPSINFO_OFFSET_PR_FNAME 40
5472 # define PRPSINFO_OFFSET_PR_PSARGS 56
5473 # define PRPSINFO_PR_FNAME_LENGTH 16
5474 # define PRPSINFO_PR_PSARGS_LENGTH 80
5477 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
5478 before the generic code in elf.c. By checking the compiler defines we
5479 only perform any action here if the generic code would otherwise not be
5480 able to help us. The intention is that bare metal core dumps (where the
5481 prstatus_t and/or prpsinfo_t might not be available) will use this code,
5482 while non bare metal tools will use the generic elf code. */
5485 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
5486 char *buf ATTRIBUTE_UNUSED
,
5487 int *bufsiz ATTRIBUTE_UNUSED
,
5488 int note_type ATTRIBUTE_UNUSED
, ...)
5495 #if !defined (HAVE_PRPSINFO_T)
5498 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5501 va_start (ap
, note_type
);
5502 memset (data
, 0, sizeof (data
));
5503 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5504 PRPSINFO_PR_FNAME_LENGTH
);
5505 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5507 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5508 -Wstringop-truncation:
5509 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5511 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5513 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5514 PRPSINFO_PR_PSARGS_LENGTH
);
5515 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5519 return elfcore_write_note (abfd
, buf
, bufsiz
,
5520 "CORE", note_type
, data
, sizeof (data
));
5522 #endif /* !HAVE_PRPSINFO_T */
5524 #if !defined (HAVE_PRSTATUS_T)
5527 char data
[PRSTATUS_SIZE
];
5533 va_start (ap
, note_type
);
5534 memset (data
, 0, sizeof(data
));
5535 pid
= va_arg (ap
, long);
5536 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5537 cursig
= va_arg (ap
, int);
5538 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5539 greg
= va_arg (ap
, const void *);
5540 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5541 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5543 return elfcore_write_note (abfd
, buf
, bufsiz
,
5544 "CORE", note_type
, data
, sizeof (data
));
5546 #endif /* !HAVE_PRSTATUS_T */
5550 /* Support for core dump NOTE sections. */
5553 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5555 switch (note
->descsz
)
5560 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5562 elf_tdata (abfd
)->core
->signal
5563 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5566 elf_tdata (abfd
)->core
->lwpid
5567 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5571 /* Make a ".reg/999" section. */
5572 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5573 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5577 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5579 switch (note
->descsz
)
5584 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5586 elf_tdata (abfd
)->core
->pid
5587 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5590 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5591 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5592 PRPSINFO_PR_FNAME_LENGTH
);
5595 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5596 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5597 PRPSINFO_PR_PSARGS_LENGTH
);
5601 /* Note that for some reason, a spurious space is tacked
5602 onto the end of the args in some (at least one anyway)
5603 implementations, so strip it off if it exists. */
5606 char *command
= elf_tdata (abfd
)->core
->command
;
5607 int n
= strlen (command
);
5609 if (0 < n
&& command
[n
- 1] == ' ')
5610 command
[n
- 1] = '\0';
5616 /* Set the right mach type. */
5619 riscv_elf_object_p (bfd
*abfd
)
5621 /* There are only two mach types in RISCV currently. */
5622 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5623 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5624 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5626 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5631 /* Determine whether an object attribute tag takes an integer, a
5635 riscv_elf_obj_attrs_arg_type (int tag
)
5637 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5640 /* Do not choose mapping symbols as a function name. */
5642 static bfd_size_type
5643 riscv_maybe_function_sym (const asymbol
*sym
,
5647 if (sym
->flags
& BSF_LOCAL
5648 && (riscv_elf_is_mapping_symbols (sym
->name
)
5649 || _bfd_elf_is_local_label_name (sec
->owner
, sym
->name
)))
5652 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5655 /* Treat the following cases as target special symbols, they are
5659 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5661 /* PR27584, local and empty symbols. Since they are usually
5662 generated for pcrel relocations. */
5663 return (!sym
->name
[0]
5664 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5665 /* PR27916, mapping symbols. */
5666 || riscv_elf_is_mapping_symbols (sym
->name
));
5670 riscv_elf_additional_program_headers (bfd
*abfd
,
5671 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5675 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5676 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5683 riscv_elf_modify_segment_map (bfd
*abfd
,
5684 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5687 struct elf_segment_map
*m
, **pm
;
5690 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5692 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5695 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5696 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5698 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5703 m
= bfd_zalloc (abfd
, amt
);
5707 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5711 /* We want to put it after the PHDR and INTERP segments. */
5712 pm
= &elf_seg_map (abfd
);
5714 && ((*pm
)->p_type
== PT_PHDR
5715 || (*pm
)->p_type
== PT_INTERP
))
5726 /* Merge non-visibility st_other attributes. */
5729 riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
5730 unsigned int st_other
,
5731 bool definition ATTRIBUTE_UNUSED
,
5732 bool dynamic ATTRIBUTE_UNUSED
)
5734 unsigned int isym_sto
= st_other
& ~ELF_ST_VISIBILITY (-1);
5735 unsigned int h_sto
= h
->other
& ~ELF_ST_VISIBILITY (-1);
5737 if (isym_sto
== h_sto
)
5740 if (isym_sto
& ~STO_RISCV_VARIANT_CC
)
5741 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5742 h
->root
.root
.string
, isym_sto
);
5744 if (isym_sto
& STO_RISCV_VARIANT_CC
)
5745 h
->other
|= STO_RISCV_VARIANT_CC
;
5748 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5749 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5750 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5751 #define TARGET_BIG_NAME "elfNN-bigriscv"
5753 #define elf_backend_reloc_type_class riscv_reloc_type_class
5755 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5756 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5757 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5758 #define bfd_elfNN_bfd_merge_private_bfd_data \
5759 _bfd_riscv_elf_merge_private_bfd_data
5760 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5762 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5763 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5764 #define elf_backend_check_relocs riscv_elf_check_relocs
5765 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5766 #define elf_backend_late_size_sections riscv_elf_late_size_sections
5767 #define elf_backend_relocate_section riscv_elf_relocate_section
5768 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5769 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5770 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5771 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5772 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5773 #define elf_backend_object_p riscv_elf_object_p
5774 #define elf_backend_write_core_note riscv_write_core_note
5775 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5776 #define elf_info_to_howto_rel NULL
5777 #define elf_info_to_howto riscv_info_to_howto_rela
5778 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5779 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5780 #define elf_backend_additional_program_headers \
5781 riscv_elf_additional_program_headers
5782 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5783 #define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
5785 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5787 #define elf_backend_can_gc_sections 1
5788 #define elf_backend_can_refcount 1
5789 #define elf_backend_want_got_plt 1
5790 #define elf_backend_plt_readonly 1
5791 #define elf_backend_plt_alignment 4
5792 #define elf_backend_want_plt_sym 1
5793 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5794 #define elf_backend_want_dynrelro 1
5795 #define elf_backend_rela_normal 1
5796 #define elf_backend_default_execstack 0
5798 #undef elf_backend_obj_attrs_vendor
5799 #define elf_backend_obj_attrs_vendor "riscv"
5800 #undef elf_backend_obj_attrs_arg_type
5801 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5802 #undef elf_backend_obj_attrs_section_type
5803 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5804 #undef elf_backend_obj_attrs_section
5805 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5806 #define elf_backend_obj_attrs_handle_unknown riscv_elf_obj_attrs_handle_unknown
5808 #include "elfNN-target.h"