1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
41 /* Internal relocations used exclusively by the relaxation pass. */
42 #define R_RISCV_DELETE (R_RISCV_max + 1)
46 #define MINUS_ONE ((bfd_vma)0 - 1)
48 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
50 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
52 /* The name of the dynamic interpreter. This is put in the .interp
55 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
56 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
58 #define ELF_ARCH bfd_arch_riscv
59 #define ELF_TARGET_ID RISCV_ELF_DATA
60 #define ELF_MACHINE_CODE EM_RISCV
61 #define ELF_MAXPAGESIZE 0x1000
62 #define ELF_COMMONPAGESIZE 0x1000
64 #define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
66 /* RISC-V ELF linker hash entry. */
68 struct riscv_elf_link_hash_entry
70 struct elf_link_hash_entry elf
;
80 #define riscv_elf_hash_entry(ent) \
81 ((struct riscv_elf_link_hash_entry *) (ent))
83 struct _bfd_riscv_elf_obj_tdata
85 struct elf_obj_tdata root
;
87 /* tls_type for each local got entry. */
88 char *local_got_tls_type
;
91 #define _bfd_riscv_elf_tdata(abfd) \
92 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
94 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
95 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
97 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
98 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
99 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
101 #define is_riscv_elf(bfd) \
102 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
103 && elf_tdata (bfd) != NULL \
104 && elf_object_id (bfd) == RISCV_ELF_DATA)
107 elfNN_riscv_mkobject (bfd
*abfd
)
109 return bfd_elf_allocate_object (abfd
,
110 sizeof (struct _bfd_riscv_elf_obj_tdata
),
114 #include "elf/common.h"
115 #include "elf/internal.h"
117 struct riscv_elf_link_hash_table
119 struct elf_link_hash_table elf
;
121 /* Short-cuts to get to dynamic linker sections. */
124 /* The max alignment of output sections. */
125 bfd_vma max_alignment
;
127 /* Used by local STT_GNU_IFUNC symbols. */
128 htab_t loc_hash_table
;
129 void * loc_hash_memory
;
131 /* The index of the last unused .rel.iplt slot. */
132 bfd_vma last_iplt_index
;
134 /* The data segment phase, don't relax the section
135 when it is exp_seg_relro_adjust. */
136 int *data_segment_phase
;
138 /* Relocations for variant CC symbols may be present. */
142 /* Instruction access functions. */
143 #define riscv_get_insn(bits, ptr) \
144 ((bits) == 16 ? bfd_getl16 (ptr) \
145 : (bits) == 32 ? bfd_getl32 (ptr) \
146 : (bits) == 64 ? bfd_getl64 (ptr) \
147 : (abort (), (bfd_vma) - 1))
148 #define riscv_put_insn(bits, val, ptr) \
149 ((bits) == 16 ? bfd_putl16 (val, ptr) \
150 : (bits) == 32 ? bfd_putl32 (val, ptr) \
151 : (bits) == 64 ? bfd_putl64 (val, ptr) \
152 : (abort (), (void) 0))
154 /* Get the RISC-V ELF linker hash table from a link_info structure. */
155 #define riscv_elf_hash_table(p) \
156 ((is_elf_hash_table ((p)->hash) \
157 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
158 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
161 riscv_info_to_howto_rela (bfd
*abfd
,
163 Elf_Internal_Rela
*dst
)
165 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
166 return cache_ptr
->howto
!= NULL
;
170 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
172 const struct elf_backend_data
*bed
;
175 bed
= get_elf_backend_data (abfd
);
176 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
177 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
180 /* Return true if a relocation is modifying an instruction. */
183 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
185 /* Heuristic: A multibyte destination with a nontrivial mask
187 return (howto
->bitsize
> 8
188 && howto
->dst_mask
!= 0
189 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
190 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
194 #define PLT_HEADER_INSNS 8
195 #define PLT_ENTRY_INSNS 4
196 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
197 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
198 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
199 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
200 the other is used for link map. Other targets also reserve one more
201 entry used for runtime profile? */
202 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
204 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
207 # define MATCH_LREG MATCH_LW
209 # define MATCH_LREG MATCH_LD
212 /* Generate a PLT header. */
215 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
218 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
219 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
221 /* RVE has no t3 register, so this won't work, and is not supported. */
222 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
224 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
229 /* auipc t2, %hi(.got.plt)
230 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
231 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
232 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
233 addi t0, t2, %lo(.got.plt) # &.got.plt
234 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
235 l[w|d] t0, PTRSIZE(t0) # link map
238 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
239 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
240 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
241 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
242 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
243 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
244 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
245 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
250 /* Generate a PLT entry. */
253 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
256 /* RVE has no t3 register, so this won't work, and is not supported. */
257 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
259 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
264 /* auipc t3, %hi(.got.plt entry)
265 l[w|d] t3, %lo(.got.plt entry)(t3)
269 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
270 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
271 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
272 entry
[3] = RISCV_NOP
;
277 /* Create an entry in an RISC-V ELF linker hash table. */
279 static struct bfd_hash_entry
*
280 link_hash_newfunc (struct bfd_hash_entry
*entry
,
281 struct bfd_hash_table
*table
, const char *string
)
283 /* Allocate the structure if it has not already been allocated by a
288 bfd_hash_allocate (table
,
289 sizeof (struct riscv_elf_link_hash_entry
));
294 /* Call the allocation method of the superclass. */
295 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
298 struct riscv_elf_link_hash_entry
*eh
;
300 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
301 eh
->tls_type
= GOT_UNKNOWN
;
307 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
308 for local symbol so that we can handle local STT_GNU_IFUNC symbols
309 as global symbol. We reuse indx and dynstr_index for local symbol
310 hash since they aren't used by global symbols in this backend. */
313 riscv_elf_local_htab_hash (const void *ptr
)
315 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
316 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
319 /* Compare local hash entries. */
322 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
324 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
325 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
327 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
330 /* Find and/or create a hash entry for local symbol. */
332 static struct elf_link_hash_entry
*
333 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
334 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
337 struct riscv_elf_link_hash_entry eh
, *ret
;
338 asection
*sec
= abfd
->sections
;
339 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
340 ELFNN_R_SYM (rel
->r_info
));
343 eh
.elf
.indx
= sec
->id
;
344 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
345 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
346 create
? INSERT
: NO_INSERT
);
353 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
357 ret
= (struct riscv_elf_link_hash_entry
*)
358 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
359 sizeof (struct riscv_elf_link_hash_entry
));
362 memset (ret
, 0, sizeof (*ret
));
363 ret
->elf
.indx
= sec
->id
;
364 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
365 ret
->elf
.dynindx
= -1;
371 /* Destroy a RISC-V elf linker hash table. */
374 riscv_elf_link_hash_table_free (bfd
*obfd
)
376 struct riscv_elf_link_hash_table
*ret
377 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
379 if (ret
->loc_hash_table
)
380 htab_delete (ret
->loc_hash_table
);
381 if (ret
->loc_hash_memory
)
382 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
384 _bfd_elf_link_hash_table_free (obfd
);
387 /* Create a RISC-V ELF linker hash table. */
389 static struct bfd_link_hash_table
*
390 riscv_elf_link_hash_table_create (bfd
*abfd
)
392 struct riscv_elf_link_hash_table
*ret
;
393 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
395 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
399 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
400 sizeof (struct riscv_elf_link_hash_entry
),
407 ret
->max_alignment
= (bfd_vma
) -1;
409 /* Create hash table for local ifunc. */
410 ret
->loc_hash_table
= htab_try_create (1024,
411 riscv_elf_local_htab_hash
,
412 riscv_elf_local_htab_eq
,
414 ret
->loc_hash_memory
= objalloc_create ();
415 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
417 riscv_elf_link_hash_table_free (abfd
);
420 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
422 return &ret
->elf
.root
;
425 /* Create the .got section. */
428 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
432 struct elf_link_hash_entry
*h
;
433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
434 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
436 /* This function may be called more than once. */
437 if (htab
->sgot
!= NULL
)
440 flags
= bed
->dynamic_sec_flags
;
442 s
= bfd_make_section_anyway_with_flags (abfd
,
443 (bed
->rela_plts_and_copies_p
444 ? ".rela.got" : ".rel.got"),
445 (bed
->dynamic_sec_flags
448 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
452 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
454 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
458 /* The first bit of the global offset table is the header. */
459 s
->size
+= bed
->got_header_size
;
461 if (bed
->want_got_plt
)
463 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
465 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
469 /* Reserve room for the header. */
470 s
->size
+= GOTPLT_HEADER_SIZE
;
473 if (bed
->want_got_sym
)
475 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
476 section. We don't do this in the linker script because we don't want
477 to define the symbol if we are not creating a global offset
479 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
480 "_GLOBAL_OFFSET_TABLE_");
481 elf_hash_table (info
)->hgot
= h
;
489 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
490 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
494 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
495 struct bfd_link_info
*info
)
497 struct riscv_elf_link_hash_table
*htab
;
499 htab
= riscv_elf_hash_table (info
);
500 BFD_ASSERT (htab
!= NULL
);
502 if (!riscv_elf_create_got_section (dynobj
, info
))
505 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
508 if (!bfd_link_pic (info
))
510 /* Technically, this section doesn't have contents. It is used as the
511 target of TLS copy relocs, to copy TLS data from shared libraries into
512 the executable. However, if we don't mark it as loadable, then it
513 matches the IS_TBSS test in ldlang.c, and there is no run-time address
514 space allocated for it even though it has SEC_ALLOC. That test is
515 correct for .tbss, but not correct for this section. There is also
516 a second problem that having a section with no contents can only work
517 if it comes after all sections with contents in the same segment,
518 but the linker script does not guarantee that. This is just mixed in
519 with other .tdata.* sections. We can fix both problems by lying and
520 saying that there are contents. This section is expected to be small
521 so this should not cause a significant extra program startup cost. */
523 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
524 (SEC_ALLOC
| SEC_THREAD_LOCAL
525 | SEC_LOAD
| SEC_DATA
527 | SEC_LINKER_CREATED
));
530 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
531 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
537 /* Copy the extra info we tack onto an elf_link_hash_entry. */
540 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
541 struct elf_link_hash_entry
*dir
,
542 struct elf_link_hash_entry
*ind
)
544 struct riscv_elf_link_hash_entry
*edir
, *eind
;
546 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
547 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
549 if (ind
->root
.type
== bfd_link_hash_indirect
550 && dir
->got
.refcount
<= 0)
552 edir
->tls_type
= eind
->tls_type
;
553 eind
->tls_type
= GOT_UNKNOWN
;
555 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
559 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
560 unsigned long symndx
, char tls_type
)
562 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
564 *new_tls_type
|= tls_type
;
565 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
567 (*_bfd_error_handler
)
568 (_("%pB: `%s' accessed both as normal and thread local symbol"),
569 abfd
, h
? h
->root
.root
.string
: "<local>");
576 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
577 struct elf_link_hash_entry
*h
, long symndx
)
579 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
580 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
582 if (htab
->elf
.sgot
== NULL
)
584 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
590 h
->got
.refcount
+= 1;
594 /* This is a global offset table entry for a local symbol. */
595 if (elf_local_got_refcounts (abfd
) == NULL
)
597 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
598 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
600 _bfd_riscv_elf_local_got_tls_type (abfd
)
601 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
603 elf_local_got_refcounts (abfd
) [symndx
] += 1;
609 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
611 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
613 /* We propably can improve the information to tell users that they
614 should be recompile the code with -fPIC or -fPIE, just like what
616 (*_bfd_error_handler
)
617 (_("%pB: relocation %s against `%s' can not be used when making a shared "
618 "object; recompile with -fPIC"),
619 abfd
, r
? r
->name
: _("<unknown>"),
620 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
621 bfd_set_error (bfd_error_bad_value
);
625 /* Look through the relocs for a section during the first phase, and
626 allocate space in the global offset table or procedure linkage
630 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
631 asection
*sec
, const Elf_Internal_Rela
*relocs
)
633 struct riscv_elf_link_hash_table
*htab
;
634 Elf_Internal_Shdr
*symtab_hdr
;
635 struct elf_link_hash_entry
**sym_hashes
;
636 const Elf_Internal_Rela
*rel
;
637 asection
*sreloc
= NULL
;
639 if (bfd_link_relocatable (info
))
642 htab
= riscv_elf_hash_table (info
);
643 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
644 sym_hashes
= elf_sym_hashes (abfd
);
646 if (htab
->elf
.dynobj
== NULL
)
647 htab
->elf
.dynobj
= abfd
;
649 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
652 unsigned int r_symndx
;
653 struct elf_link_hash_entry
*h
;
655 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
656 r_type
= ELFNN_R_TYPE (rel
->r_info
);
658 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
660 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
665 if (r_symndx
< symtab_hdr
->sh_info
)
667 /* A local symbol. */
668 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
673 /* Check relocation against local STT_GNU_IFUNC symbol. */
674 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
676 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
680 /* Fake STT_GNU_IFUNC global symbol. */
681 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
683 h
->type
= STT_GNU_IFUNC
;
687 h
->root
.type
= bfd_link_hash_defined
;
694 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
695 while (h
->root
.type
== bfd_link_hash_indirect
696 || h
->root
.type
== bfd_link_hash_warning
)
697 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
707 case R_RISCV_CALL_PLT
:
709 case R_RISCV_GOT_HI20
:
710 case R_RISCV_PCREL_HI20
:
711 /* Create the ifunc sections, iplt and ipltgot, for static
713 if (h
->type
== STT_GNU_IFUNC
714 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
722 /* It is referenced by a non-shared object. */
728 case R_RISCV_TLS_GD_HI20
:
729 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
730 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
734 case R_RISCV_TLS_GOT_HI20
:
735 if (bfd_link_pic (info
))
736 info
->flags
|= DF_STATIC_TLS
;
737 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
738 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
742 case R_RISCV_GOT_HI20
:
743 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
744 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
749 case R_RISCV_CALL_PLT
:
750 /* These symbol requires a procedure linkage table entry.
751 We actually build the entry in adjust_dynamic_symbol,
752 because these might be a case of linking PIC code without
753 linking in any dynamic objects, in which case we don't
754 need to generate a procedure linkage table after all. */
756 /* If it is a local symbol, then we resolve it directly
757 without creating a PLT entry. */
762 h
->plt
.refcount
+= 1;
765 case R_RISCV_PCREL_HI20
:
767 && h
->type
== STT_GNU_IFUNC
)
770 h
->pointer_equality_needed
= 1;
772 /* We don't use the PCREL_HI20 in the data section,
773 so we always need the plt when it refers to
775 h
->plt
.refcount
+= 1;
781 case R_RISCV_RVC_BRANCH
:
782 case R_RISCV_RVC_JUMP
:
783 /* In shared libraries and pie, these relocs are known
785 if (bfd_link_pic (info
))
789 case R_RISCV_TPREL_HI20
:
790 if (!bfd_link_executable (info
))
791 return bad_static_reloc (abfd
, r_type
, h
);
793 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
797 if (bfd_link_pic (info
))
798 return bad_static_reloc (abfd
, r_type
, h
);
802 case R_RISCV_JUMP_SLOT
:
803 case R_RISCV_RELATIVE
:
811 && (!bfd_link_pic (info
)
812 || h
->type
== STT_GNU_IFUNC
))
814 /* This reloc might not bind locally. */
816 h
->pointer_equality_needed
= 1;
819 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
821 /* We may need a .plt entry if the symbol is a function
822 defined in a shared lib or is a function referenced
823 from the code or read-only section. */
824 h
->plt
.refcount
+= 1;
828 /* If we are creating a shared library, and this is a reloc
829 against a global symbol, or a non PC relative reloc
830 against a local symbol, then we need to copy the reloc
831 into the shared library. However, if we are linking with
832 -Bsymbolic, we do not need to copy a reloc against a
833 global symbol which is defined in an object we are
834 including in the link (i.e., DEF_REGULAR is set). At
835 this point we have not seen all the input files, so it is
836 possible that DEF_REGULAR is not set now but will be set
837 later (it is never cleared). In case of a weak definition,
838 DEF_REGULAR may be cleared later by a strong definition in
839 a shared library. We account for that possibility below by
840 storing information in the relocs_copied field of the hash
841 table entry. A similar situation occurs when creating
842 shared libraries and symbol visibility changes render the
845 If on the other hand, we are creating an executable, we
846 may need to keep relocations for symbols satisfied by a
847 dynamic library if we manage to avoid copy relocs for the
850 Generate dynamic pointer relocation against STT_GNU_IFUNC
851 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
852 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
854 if ((bfd_link_pic (info
)
855 && (sec
->flags
& SEC_ALLOC
) != 0
856 && ((r
!= NULL
&& !r
->pc_relative
)
859 || h
->root
.type
== bfd_link_hash_defweak
860 || !h
->def_regular
))))
861 || (!bfd_link_pic (info
)
862 && (sec
->flags
& SEC_ALLOC
) != 0
864 && (h
->root
.type
== bfd_link_hash_defweak
866 || (!bfd_link_pic (info
)
868 && h
->type
== STT_GNU_IFUNC
869 && (sec
->flags
& SEC_CODE
) == 0))
871 struct elf_dyn_relocs
*p
;
872 struct elf_dyn_relocs
**head
;
874 /* When creating a shared object, we must copy these
875 relocs into the output file. We create a reloc
876 section in dynobj and make room for the reloc. */
879 sreloc
= _bfd_elf_make_dynamic_reloc_section
880 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
881 abfd
, /*rela?*/ true);
887 /* If this is a global symbol, we count the number of
888 relocations we need for this symbol. */
890 head
= &h
->dyn_relocs
;
893 /* Track dynamic relocs needed for local syms too.
894 We really need local syms available to do this
899 Elf_Internal_Sym
*isym
;
901 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
906 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
910 vpp
= &elf_section_data (s
)->local_dynrel
;
911 head
= (struct elf_dyn_relocs
**) vpp
;
915 if (p
== NULL
|| p
->sec
!= sec
)
917 size_t amt
= sizeof *p
;
918 p
= ((struct elf_dyn_relocs
*)
919 bfd_alloc (htab
->elf
.dynobj
, amt
));
930 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
943 /* Adjust a symbol defined by a dynamic object and referenced by a
944 regular object. The current definition is in some section of the
945 dynamic object, but we're not including those sections. We have to
946 change the definition to something the rest of the link can
950 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
951 struct elf_link_hash_entry
*h
)
953 struct riscv_elf_link_hash_table
*htab
;
954 struct riscv_elf_link_hash_entry
* eh
;
958 htab
= riscv_elf_hash_table (info
);
959 BFD_ASSERT (htab
!= NULL
);
961 dynobj
= htab
->elf
.dynobj
;
963 /* Make sure we know what is going on here. */
964 BFD_ASSERT (dynobj
!= NULL
966 || h
->type
== STT_GNU_IFUNC
970 && !h
->def_regular
)));
972 /* If this is a function, put it in the procedure linkage table. We
973 will fill in the contents of the procedure linkage table later
974 (although we could actually do it here). */
975 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
977 if (h
->plt
.refcount
<= 0
978 || (h
->type
!= STT_GNU_IFUNC
979 && (SYMBOL_CALLS_LOCAL (info
, h
)
980 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
981 && h
->root
.type
== bfd_link_hash_undefweak
))))
983 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
984 input file, but the symbol was never referred to by a dynamic
985 object, or if all references were garbage collected. In such
986 a case, we don't actually need to build a PLT entry. */
987 h
->plt
.offset
= (bfd_vma
) -1;
994 h
->plt
.offset
= (bfd_vma
) -1;
996 /* If this is a weak symbol, and there is a real definition, the
997 processor independent code will have arranged for us to see the
998 real definition first, and we can just use the same value. */
1001 struct elf_link_hash_entry
*def
= weakdef (h
);
1002 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1003 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1004 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1008 /* This is a reference to a symbol defined by a dynamic object which
1009 is not a function. */
1011 /* If we are creating a shared library, we must presume that the
1012 only references to the symbol are via the global offset table.
1013 For such cases we need not do anything here; the relocations will
1014 be handled correctly by relocate_section. */
1015 if (bfd_link_pic (info
))
1018 /* If there are no references to this symbol that do not use the
1019 GOT, we don't need to generate a copy reloc. */
1020 if (!h
->non_got_ref
)
1023 /* If -z nocopyreloc was given, we won't generate them either. */
1024 if (info
->nocopyreloc
)
1030 /* If we don't find any dynamic relocs in read-only sections, then
1031 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1032 if (!_bfd_elf_readonly_dynrelocs (h
))
1038 /* We must allocate the symbol in our .dynbss section, which will
1039 become part of the .bss section of the executable. There will be
1040 an entry for this symbol in the .dynsym section. The dynamic
1041 object will contain position independent code, so all references
1042 from the dynamic object to this symbol will go through the global
1043 offset table. The dynamic linker will use the .dynsym entry to
1044 determine the address it must put in the global offset table, so
1045 both the dynamic object and the regular object will refer to the
1046 same memory location for the variable. */
1048 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1049 to copy the initial value out of the dynamic object and into the
1050 runtime process image. We need to remember the offset into the
1051 .rel.bss section we are going to use. */
1052 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1053 if (eh
->tls_type
& ~GOT_NORMAL
)
1055 s
= htab
->sdyntdata
;
1056 srel
= htab
->elf
.srelbss
;
1058 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1060 s
= htab
->elf
.sdynrelro
;
1061 srel
= htab
->elf
.sreldynrelro
;
1065 s
= htab
->elf
.sdynbss
;
1066 srel
= htab
->elf
.srelbss
;
1068 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1070 srel
->size
+= sizeof (ElfNN_External_Rela
);
1074 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1077 /* Allocate space in .plt, .got and associated reloc sections for
1081 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1083 struct bfd_link_info
*info
;
1084 struct riscv_elf_link_hash_table
*htab
;
1085 struct elf_dyn_relocs
*p
;
1087 if (h
->root
.type
== bfd_link_hash_indirect
)
1090 info
= (struct bfd_link_info
*) inf
;
1091 htab
= riscv_elf_hash_table (info
);
1092 BFD_ASSERT (htab
!= NULL
);
1094 /* When we are generating pde, make sure gp symbol is output as a
1095 dynamic symbol. Then ld.so can set the gp register earlier, before
1096 resolving the ifunc. */
1097 if (!bfd_link_pic (info
)
1098 && htab
->elf
.dynamic_sections_created
1099 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1100 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1103 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1104 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1105 if they are defined and referenced in a non-shared object. */
1106 if (h
->type
== STT_GNU_IFUNC
1109 else if (htab
->elf
.dynamic_sections_created
1110 && h
->plt
.refcount
> 0)
1112 /* Make sure this symbol is output as a dynamic symbol.
1113 Undefined weak syms won't yet be marked as dynamic. */
1114 if (h
->dynindx
== -1
1115 && !h
->forced_local
)
1117 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1121 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1123 asection
*s
= htab
->elf
.splt
;
1126 s
->size
= PLT_HEADER_SIZE
;
1128 h
->plt
.offset
= s
->size
;
1130 /* Make room for this entry. */
1131 s
->size
+= PLT_ENTRY_SIZE
;
1133 /* We also need to make an entry in the .got.plt section. */
1134 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1136 /* We also need to make an entry in the .rela.plt section. */
1137 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1139 /* If this symbol is not defined in a regular file, and we are
1140 not generating a shared library, then set the symbol to this
1141 location in the .plt. This is required to make function
1142 pointers compare as equal between the normal executable and
1143 the shared library. */
1144 if (! bfd_link_pic (info
)
1147 h
->root
.u
.def
.section
= s
;
1148 h
->root
.u
.def
.value
= h
->plt
.offset
;
1151 /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1152 variant_cc flag of riscv_elf_link_hash_table. */
1153 if (h
->other
& STO_RISCV_VARIANT_CC
)
1154 htab
->variant_cc
= 1;
1158 h
->plt
.offset
= (bfd_vma
) -1;
1164 h
->plt
.offset
= (bfd_vma
) -1;
1168 if (h
->got
.refcount
> 0)
1172 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1174 /* Make sure this symbol is output as a dynamic symbol.
1175 Undefined weak syms won't yet be marked as dynamic. */
1176 if (h
->dynindx
== -1
1177 && !h
->forced_local
)
1179 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1184 h
->got
.offset
= s
->size
;
1185 dyn
= htab
->elf
.dynamic_sections_created
;
1186 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1188 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1189 if (tls_type
& GOT_TLS_GD
)
1191 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1192 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1195 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1196 if (tls_type
& GOT_TLS_IE
)
1198 s
->size
+= RISCV_ELF_WORD_BYTES
;
1199 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1204 s
->size
+= RISCV_ELF_WORD_BYTES
;
1205 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1206 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1207 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1211 h
->got
.offset
= (bfd_vma
) -1;
1213 if (h
->dyn_relocs
== NULL
)
1216 /* In the shared -Bsymbolic case, discard space allocated for
1217 dynamic pc-relative relocs against symbols which turn out to be
1218 defined in regular objects. For the normal shared case, discard
1219 space for pc-relative relocs that have become local due to symbol
1220 visibility changes. */
1222 if (bfd_link_pic (info
))
1224 if (SYMBOL_CALLS_LOCAL (info
, h
))
1226 struct elf_dyn_relocs
**pp
;
1228 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1230 p
->count
-= p
->pc_count
;
1239 /* Also discard relocs on undefined weak syms with non-default
1241 if (h
->dyn_relocs
!= NULL
1242 && h
->root
.type
== bfd_link_hash_undefweak
)
1244 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1245 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1246 h
->dyn_relocs
= NULL
;
1248 /* Make sure undefined weak symbols are output as a dynamic
1250 else if (h
->dynindx
== -1
1251 && !h
->forced_local
)
1253 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1260 /* For the non-shared case, discard space for relocs against
1261 symbols which turn out to need copy relocs or are not
1267 || (htab
->elf
.dynamic_sections_created
1268 && (h
->root
.type
== bfd_link_hash_undefweak
1269 || h
->root
.type
== bfd_link_hash_undefined
))))
1271 /* Make sure this symbol is output as a dynamic symbol.
1272 Undefined weak syms won't yet be marked as dynamic. */
1273 if (h
->dynindx
== -1
1274 && !h
->forced_local
)
1276 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1280 /* If that succeeded, we know we'll be keeping all the
1282 if (h
->dynindx
!= -1)
1286 h
->dyn_relocs
= NULL
;
1291 /* Finally, allocate space. */
1292 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1294 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1295 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1301 /* Allocate space in .plt, .got and associated reloc sections for
1302 ifunc dynamic relocs. */
1305 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1308 struct bfd_link_info
*info
;
1310 if (h
->root
.type
== bfd_link_hash_indirect
)
1313 if (h
->root
.type
== bfd_link_hash_warning
)
1314 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1316 info
= (struct bfd_link_info
*) inf
;
1318 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1319 here if it is defined and referenced in a non-shared object. */
1320 if (h
->type
== STT_GNU_IFUNC
1322 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1331 /* Allocate space in .plt, .got and associated reloc sections for
1332 local ifunc dynamic relocs. */
1335 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1337 struct elf_link_hash_entry
*h
1338 = (struct elf_link_hash_entry
*) *slot
;
1340 if (h
->type
!= STT_GNU_IFUNC
1344 || h
->root
.type
!= bfd_link_hash_defined
)
1347 return allocate_ifunc_dynrelocs (h
, inf
);
1351 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1353 struct riscv_elf_link_hash_table
*htab
;
1358 htab
= riscv_elf_hash_table (info
);
1359 BFD_ASSERT (htab
!= NULL
);
1360 dynobj
= htab
->elf
.dynobj
;
1361 BFD_ASSERT (dynobj
!= NULL
);
1363 if (elf_hash_table (info
)->dynamic_sections_created
)
1365 /* Set the contents of the .interp section to the interpreter. */
1366 if (bfd_link_executable (info
) && !info
->nointerp
)
1368 s
= bfd_get_linker_section (dynobj
, ".interp");
1369 BFD_ASSERT (s
!= NULL
);
1370 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1371 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1375 /* Set up .got offsets for local syms, and space for local dynamic
1377 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1379 bfd_signed_vma
*local_got
;
1380 bfd_signed_vma
*end_local_got
;
1381 char *local_tls_type
;
1382 bfd_size_type locsymcount
;
1383 Elf_Internal_Shdr
*symtab_hdr
;
1386 if (! is_riscv_elf (ibfd
))
1389 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1391 struct elf_dyn_relocs
*p
;
1393 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1395 if (!bfd_is_abs_section (p
->sec
)
1396 && bfd_is_abs_section (p
->sec
->output_section
))
1398 /* Input section has been discarded, either because
1399 it is a copy of a linkonce section or due to
1400 linker script /DISCARD/, so we'll be discarding
1403 else if (p
->count
!= 0)
1405 srel
= elf_section_data (p
->sec
)->sreloc
;
1406 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1407 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1408 info
->flags
|= DF_TEXTREL
;
1413 local_got
= elf_local_got_refcounts (ibfd
);
1417 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1418 locsymcount
= symtab_hdr
->sh_info
;
1419 end_local_got
= local_got
+ locsymcount
;
1420 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1422 srel
= htab
->elf
.srelgot
;
1423 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1427 *local_got
= s
->size
;
1428 s
->size
+= RISCV_ELF_WORD_BYTES
;
1429 if (*local_tls_type
& GOT_TLS_GD
)
1430 s
->size
+= RISCV_ELF_WORD_BYTES
;
1431 if (bfd_link_pic (info
)
1432 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1433 srel
->size
+= sizeof (ElfNN_External_Rela
);
1436 *local_got
= (bfd_vma
) -1;
1440 /* Allocate .plt and .got entries and space dynamic relocs for
1442 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1444 /* Allocate .plt and .got entries and space dynamic relocs for
1445 global ifunc symbols. */
1446 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1448 /* Allocate .plt and .got entries and space dynamic relocs for
1449 local ifunc symbols. */
1450 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1452 /* Used to resolve the dynamic relocs overwite problems when
1453 generating static executable. */
1454 if (htab
->elf
.irelplt
)
1455 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1457 if (htab
->elf
.sgotplt
)
1459 struct elf_link_hash_entry
*got
;
1460 got
= elf_link_hash_lookup (elf_hash_table (info
),
1461 "_GLOBAL_OFFSET_TABLE_",
1462 false, false, false);
1464 /* Don't allocate .got.plt section if there are no GOT nor PLT
1465 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1467 || !got
->ref_regular_nonweak
)
1468 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1469 && (htab
->elf
.splt
== NULL
1470 || htab
->elf
.splt
->size
== 0)
1471 && (htab
->elf
.sgot
== NULL
1472 || (htab
->elf
.sgot
->size
1473 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1474 htab
->elf
.sgotplt
->size
= 0;
1477 /* The check_relocs and adjust_dynamic_symbol entry points have
1478 determined the sizes of the various dynamic sections. Allocate
1480 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1482 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1485 if (s
== htab
->elf
.splt
1486 || s
== htab
->elf
.sgot
1487 || s
== htab
->elf
.sgotplt
1488 || s
== htab
->elf
.iplt
1489 || s
== htab
->elf
.igotplt
1490 || s
== htab
->elf
.sdynbss
1491 || s
== htab
->elf
.sdynrelro
1492 || s
== htab
->sdyntdata
)
1494 /* Strip this section if we don't need it; see the
1497 else if (startswith (s
->name
, ".rela"))
1501 /* We use the reloc_count field as a counter if we need
1502 to copy relocs into the output file. */
1508 /* It's not one of our sections. */
1514 /* If we don't need this section, strip it from the
1515 output file. This is mostly to handle .rela.bss and
1516 .rela.plt. We must create both sections in
1517 create_dynamic_sections, because they must be created
1518 before the linker maps input sections to output
1519 sections. The linker does that before
1520 adjust_dynamic_symbol is called, and it is that
1521 function which decides whether anything needs to go
1522 into these sections. */
1523 s
->flags
|= SEC_EXCLUDE
;
1527 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1530 /* Allocate memory for the section contents. Zero the memory
1531 for the benefit of .rela.plt, which has 4 unused entries
1532 at the beginning, and we don't want garbage. */
1533 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1534 if (s
->contents
== NULL
)
1538 /* Add dynamic entries. */
1539 if (elf_hash_table (info
)->dynamic_sections_created
)
1541 if (!_bfd_elf_add_dynamic_tags (output_bfd
, info
, true))
1544 if (htab
->variant_cc
1545 && !_bfd_elf_add_dynamic_entry (info
, DT_RISCV_VARIANT_CC
, 0))
1553 #define DTP_OFFSET 0x800
1555 /* Return the relocation value for a TLS dtp-relative reloc. */
1558 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1560 /* If tls_sec is NULL, we should have signalled an error already. */
1561 if (elf_hash_table (info
)->tls_sec
== NULL
)
1563 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1566 /* Return the relocation value for a static TLS tp-relative relocation. */
1569 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1571 /* If tls_sec is NULL, we should have signalled an error already. */
1572 if (elf_hash_table (info
)->tls_sec
== NULL
)
1574 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1577 /* Return the global pointer's value, or 0 if it is not in use. */
1580 riscv_global_pointer_value (struct bfd_link_info
*info
)
1582 struct bfd_link_hash_entry
*h
;
1584 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1585 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1588 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1591 /* Emplace a static relocation. */
1593 static bfd_reloc_status_type
1594 perform_relocation (const reloc_howto_type
*howto
,
1595 const Elf_Internal_Rela
*rel
,
1597 asection
*input_section
,
1601 if (howto
->pc_relative
)
1602 value
-= sec_addr (input_section
) + rel
->r_offset
;
1603 value
+= rel
->r_addend
;
1605 switch (ELFNN_R_TYPE (rel
->r_info
))
1608 case R_RISCV_TPREL_HI20
:
1609 case R_RISCV_PCREL_HI20
:
1610 case R_RISCV_GOT_HI20
:
1611 case R_RISCV_TLS_GOT_HI20
:
1612 case R_RISCV_TLS_GD_HI20
:
1613 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1614 return bfd_reloc_overflow
;
1615 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1618 case R_RISCV_LO12_I
:
1619 case R_RISCV_GPREL_I
:
1620 case R_RISCV_TPREL_LO12_I
:
1621 case R_RISCV_TPREL_I
:
1622 case R_RISCV_PCREL_LO12_I
:
1623 value
= ENCODE_ITYPE_IMM (value
);
1626 case R_RISCV_LO12_S
:
1627 case R_RISCV_GPREL_S
:
1628 case R_RISCV_TPREL_LO12_S
:
1629 case R_RISCV_TPREL_S
:
1630 case R_RISCV_PCREL_LO12_S
:
1631 value
= ENCODE_STYPE_IMM (value
);
1635 case R_RISCV_CALL_PLT
:
1636 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1637 return bfd_reloc_overflow
;
1638 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1639 | (ENCODE_ITYPE_IMM (value
) << 32);
1643 if (!VALID_JTYPE_IMM (value
))
1644 return bfd_reloc_overflow
;
1645 value
= ENCODE_JTYPE_IMM (value
);
1648 case R_RISCV_BRANCH
:
1649 if (!VALID_BTYPE_IMM (value
))
1650 return bfd_reloc_overflow
;
1651 value
= ENCODE_BTYPE_IMM (value
);
1654 case R_RISCV_RVC_BRANCH
:
1655 if (!VALID_CBTYPE_IMM (value
))
1656 return bfd_reloc_overflow
;
1657 value
= ENCODE_CBTYPE_IMM (value
);
1660 case R_RISCV_RVC_JUMP
:
1661 if (!VALID_CJTYPE_IMM (value
))
1662 return bfd_reloc_overflow
;
1663 value
= ENCODE_CJTYPE_IMM (value
);
1666 case R_RISCV_RVC_LUI
:
1667 if (RISCV_CONST_HIGH_PART (value
) == 0)
1669 /* Linker relaxation can convert an address equal to or greater than
1670 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1671 valid immediate. We can fix this by converting it to a C.LI. */
1672 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1673 contents
+ rel
->r_offset
);
1674 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1675 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1676 value
= ENCODE_CITYPE_IMM (0);
1678 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1679 return bfd_reloc_overflow
;
1681 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1699 case R_RISCV_32_PCREL
:
1700 case R_RISCV_TLS_DTPREL32
:
1701 case R_RISCV_TLS_DTPREL64
:
1704 case R_RISCV_DELETE
:
1705 return bfd_reloc_ok
;
1708 return bfd_reloc_notsupported
;
1712 if (riscv_is_insn_reloc (howto
))
1713 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1715 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1716 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1717 if (riscv_is_insn_reloc (howto
))
1718 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1720 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1722 return bfd_reloc_ok
;
1725 /* Remember all PC-relative high-part relocs we've encountered to help us
1726 later resolve the corresponding low-part relocs. */
1732 /* Relocation value with addend. */
1734 /* Original reloc type. */
1736 } riscv_pcrel_hi_reloc
;
1738 typedef struct riscv_pcrel_lo_reloc
1740 /* PC value of auipc. */
1742 /* Internal relocation. */
1743 const Elf_Internal_Rela
*reloc
;
1744 /* Record the following information helps to resolve the %pcrel
1745 which cross different input section. For now we build a hash
1746 for pcrel at the start of riscv_elf_relocate_section, and then
1747 free the hash at the end. But riscv_elf_relocate_section only
1748 handles an input section at a time, so that means we can only
1749 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1750 section. Otherwise, we will report dangerous relocation errors
1751 for those %pcrel which are not in the same input section. */
1752 asection
*input_section
;
1753 struct bfd_link_info
*info
;
1754 reloc_howto_type
*howto
;
1756 /* The next riscv_pcrel_lo_reloc. */
1757 struct riscv_pcrel_lo_reloc
*next
;
1758 } riscv_pcrel_lo_reloc
;
1762 /* Hash table for riscv_pcrel_hi_reloc. */
1764 /* Linked list for riscv_pcrel_lo_reloc. */
1765 riscv_pcrel_lo_reloc
*lo_relocs
;
1766 } riscv_pcrel_relocs
;
1769 riscv_pcrel_reloc_hash (const void *entry
)
1771 const riscv_pcrel_hi_reloc
*e
= entry
;
1772 return (hashval_t
)(e
->address
>> 2);
1776 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1778 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1779 return e1
->address
== e2
->address
;
1783 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1785 p
->lo_relocs
= NULL
;
1786 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1787 riscv_pcrel_reloc_eq
, free
);
1788 return p
->hi_relocs
!= NULL
;
1792 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1794 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1798 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1803 htab_delete (p
->hi_relocs
);
1807 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1808 struct bfd_link_info
*info
,
1812 const reloc_howto_type
*howto
)
1814 /* We may need to reference low addreses in PC-relative modes even when the
1815 PC is far away from these addresses. For example, undefweak references
1816 need to produce the address 0 when linked. As 0 is far from the arbitrary
1817 addresses that we can link PC-relative programs at, the linker can't
1818 actually relocate references to those symbols. In order to allow these
1819 programs to work we simply convert the PC-relative auipc sequences to
1820 0-relative lui sequences. */
1821 if (bfd_link_pic (info
))
1824 /* If it's possible to reference the symbol using auipc we do so, as that's
1825 more in the spirit of the PC-relative relocations we're processing. */
1826 bfd_vma offset
= addr
- pc
;
1827 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1830 /* If it's impossible to reference this with a LUI-based offset then don't
1831 bother to convert it at all so users still see the PC-relative relocation
1832 in the truncation message. */
1833 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1836 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
1838 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1839 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1840 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1845 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
1851 bfd_vma offset
= absolute
? value
: value
- addr
;
1852 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
};
1853 riscv_pcrel_hi_reloc
**slot
=
1854 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1856 BFD_ASSERT (*slot
== NULL
);
1857 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1865 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1867 const Elf_Internal_Rela
*reloc
,
1868 asection
*input_section
,
1869 struct bfd_link_info
*info
,
1870 reloc_howto_type
*howto
,
1873 riscv_pcrel_lo_reloc
*entry
;
1874 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1877 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
1878 howto
, contents
, p
->lo_relocs
};
1879 p
->lo_relocs
= entry
;
1884 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1886 riscv_pcrel_lo_reloc
*r
;
1888 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1890 bfd
*input_bfd
= r
->input_section
->owner
;
1892 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0};
1893 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1894 /* There may be a risk if the %pcrel_lo with addend refers to
1895 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
1896 so the corresponding %pcrel_lo with addend looks wrong. */
1897 char *string
= NULL
;
1899 string
= _("%pcrel_lo missing matching %pcrel_hi");
1900 else if (entry
->type
== R_RISCV_GOT_HI20
1901 && r
->reloc
->r_addend
!= 0)
1902 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
1903 else if (RISCV_CONST_HIGH_PART (entry
->value
)
1904 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
1906 /* Check the overflow when adding reloc addend. */
1907 if (asprintf (&string
,
1908 _("%%pcrel_lo overflow with an addend, the "
1909 "value of %%pcrel_hi is 0x%" PRIx64
" without "
1910 "any addend, but may be 0x%" PRIx64
" after "
1911 "adding the %%pcrel_lo addend"),
1912 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
1913 (int64_t) RISCV_CONST_HIGH_PART
1914 (entry
->value
+ r
->reloc
->r_addend
)) == -1)
1915 string
= _("%pcrel_lo overflow with an addend");
1920 (*r
->info
->callbacks
->reloc_dangerous
)
1921 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1925 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1926 input_bfd
, r
->contents
);
1932 /* Relocate a RISC-V ELF section.
1934 The RELOCATE_SECTION function is called by the new ELF backend linker
1935 to handle the relocations for a section.
1937 The relocs are always passed as Rela structures.
1939 This function is responsible for adjusting the section contents as
1940 necessary, and (if generating a relocatable output file) adjusting
1941 the reloc addend as necessary.
1943 This function does not have to worry about setting the reloc
1944 address or the reloc symbol index.
1946 LOCAL_SYMS is a pointer to the swapped in local symbols.
1948 LOCAL_SECTIONS is an array giving the section in the input file
1949 corresponding to the st_shndx field of each local symbol.
1951 The global hash table entry for the global symbols can be found
1952 via elf_sym_hashes (input_bfd).
1954 When generating relocatable output, this function must handle
1955 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1956 going to be the section symbol corresponding to the output
1957 section, which means that the addend must be adjusted
1961 riscv_elf_relocate_section (bfd
*output_bfd
,
1962 struct bfd_link_info
*info
,
1964 asection
*input_section
,
1966 Elf_Internal_Rela
*relocs
,
1967 Elf_Internal_Sym
*local_syms
,
1968 asection
**local_sections
)
1970 Elf_Internal_Rela
*rel
;
1971 Elf_Internal_Rela
*relend
;
1972 riscv_pcrel_relocs pcrel_relocs
;
1974 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1975 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1976 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1977 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1980 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1983 relend
= relocs
+ input_section
->reloc_count
;
1984 for (rel
= relocs
; rel
< relend
; rel
++)
1986 unsigned long r_symndx
;
1987 struct elf_link_hash_entry
*h
;
1988 Elf_Internal_Sym
*sym
;
1991 bfd_reloc_status_type r
= bfd_reloc_ok
;
1992 const char *name
= NULL
;
1993 bfd_vma off
, ie_off
;
1994 bool unresolved_reloc
, is_ie
= false;
1995 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1996 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1997 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1998 const char *msg
= NULL
;
1999 char *msg_buf
= NULL
;
2000 bool resolved_to_zero
;
2005 /* This is a final link. */
2006 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2010 unresolved_reloc
= false;
2011 if (r_symndx
< symtab_hdr
->sh_info
)
2013 sym
= local_syms
+ r_symndx
;
2014 sec
= local_sections
[r_symndx
];
2015 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2017 /* Relocate against local STT_GNU_IFUNC symbol. */
2018 if (!bfd_link_relocatable (info
)
2019 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2021 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2025 /* Set STT_GNU_IFUNC symbol value. */
2026 h
->root
.u
.def
.value
= sym
->st_value
;
2027 h
->root
.u
.def
.section
= sec
;
2032 bool warned
, ignored
;
2034 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2035 r_symndx
, symtab_hdr
, sym_hashes
,
2037 unresolved_reloc
, warned
, ignored
);
2040 /* To avoid generating warning messages about truncated
2041 relocations, set the relocation's address to be the same as
2042 the start of this section. */
2043 if (input_section
->output_section
!= NULL
)
2044 relocation
= input_section
->output_section
->vma
;
2050 if (sec
!= NULL
&& discarded_section (sec
))
2051 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2052 rel
, 1, relend
, howto
, 0, contents
);
2054 if (bfd_link_relocatable (info
))
2057 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2058 it here if it is defined in a non-shared object. */
2060 && h
->type
== STT_GNU_IFUNC
2063 asection
*plt
, *base_got
;
2065 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2067 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2068 STT_GNU_IFUNC symbol as STT_FUNC. */
2069 if (elf_section_type (input_section
) == SHT_NOTE
)
2072 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2073 sections because such sections are not SEC_ALLOC and
2074 thus ld.so will not process them. */
2075 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2080 else if (h
->plt
.offset
== (bfd_vma
) -1
2081 /* The following relocation may not need the .plt entries
2082 when all references to a STT_GNU_IFUNC symbols are done
2083 via GOT or static function pointers. */
2084 && r_type
!= R_RISCV_32
2085 && r_type
!= R_RISCV_64
2086 && r_type
!= R_RISCV_HI20
2087 && r_type
!= R_RISCV_GOT_HI20
2088 && r_type
!= R_RISCV_LO12_I
2089 && r_type
!= R_RISCV_LO12_S
)
2090 goto bad_ifunc_reloc
;
2092 /* STT_GNU_IFUNC symbol must go through PLT. */
2093 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2094 relocation
= plt
->output_section
->vma
2095 + plt
->output_offset
2102 if (rel
->r_addend
!= 0)
2104 if (h
->root
.root
.string
)
2105 name
= h
->root
.root
.string
;
2107 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2110 /* xgettext:c-format */
2111 (_("%pB: relocation %s against STT_GNU_IFUNC "
2112 "symbol `%s' has non-zero addend: %" PRId64
),
2113 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2114 bfd_set_error (bfd_error_bad_value
);
2118 /* Generate dynamic relocation only when there is a non-GOT
2119 reference in a shared object or there is no PLT. */
2120 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2121 || h
->plt
.offset
== (bfd_vma
) -1)
2123 Elf_Internal_Rela outrel
;
2126 /* Need a dynamic relocation to get the real function
2128 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2132 if (outrel
.r_offset
== (bfd_vma
) -1
2133 || outrel
.r_offset
== (bfd_vma
) -2)
2136 outrel
.r_offset
+= input_section
->output_section
->vma
2137 + input_section
->output_offset
;
2139 if (h
->dynindx
== -1
2141 || bfd_link_executable (info
))
2143 info
->callbacks
->minfo
2144 (_("Local IFUNC function `%s' in %pB\n"),
2145 h
->root
.root
.string
,
2146 h
->root
.u
.def
.section
->owner
);
2148 /* This symbol is resolved locally. */
2149 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2150 outrel
.r_addend
= h
->root
.u
.def
.value
2151 + h
->root
.u
.def
.section
->output_section
->vma
2152 + h
->root
.u
.def
.section
->output_offset
;
2156 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2157 outrel
.r_addend
= 0;
2160 /* Dynamic relocations are stored in
2161 1. .rela.ifunc section in PIC object.
2162 2. .rela.got section in dynamic executable.
2163 3. .rela.iplt section in static executable. */
2164 if (bfd_link_pic (info
))
2165 sreloc
= htab
->elf
.irelifunc
;
2166 else if (htab
->elf
.splt
!= NULL
)
2167 sreloc
= htab
->elf
.srelgot
;
2169 sreloc
= htab
->elf
.irelplt
;
2171 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2173 /* If this reloc is against an external symbol, we
2174 do not want to fiddle with the addend. Otherwise,
2175 we need to include the symbol value so that it
2176 becomes an addend for the dynamic reloc. For an
2177 internal symbol, we have updated addend. */
2182 case R_RISCV_GOT_HI20
:
2183 base_got
= htab
->elf
.sgot
;
2184 off
= h
->got
.offset
;
2186 if (base_got
== NULL
)
2189 if (off
== (bfd_vma
) -1)
2193 /* We can't use h->got.offset here to save state, or
2194 even just remember the offset, as finish_dynamic_symbol
2195 would use that as offset into .got. */
2197 if (htab
->elf
.splt
!= NULL
)
2199 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2201 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2202 base_got
= htab
->elf
.sgotplt
;
2206 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2207 off
= plt_idx
* GOT_ENTRY_SIZE
;
2208 base_got
= htab
->elf
.igotplt
;
2211 if (h
->dynindx
== -1
2215 /* This references the local definition. We must
2216 initialize this entry in the global offset table.
2217 Since the offset must always be a multiple of 8,
2218 we use the least significant bit to record
2219 whether we have initialized it already.
2221 When doing a dynamic link, we create a .rela.got
2222 relocation entry to initialize the value. This
2223 is done in the finish_dynamic_symbol routine. */
2228 bfd_put_NN (output_bfd
, relocation
,
2229 base_got
->contents
+ off
);
2230 /* Note that this is harmless for the case,
2231 as -1 | 1 still is -1. */
2237 relocation
= base_got
->output_section
->vma
2238 + base_got
->output_offset
+ off
;
2240 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2243 r
= bfd_reloc_overflow
;
2247 case R_RISCV_CALL_PLT
:
2249 case R_RISCV_LO12_I
:
2250 case R_RISCV_LO12_S
:
2253 case R_RISCV_PCREL_HI20
:
2254 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2257 r
= bfd_reloc_overflow
;
2262 if (h
->root
.root
.string
)
2263 name
= h
->root
.root
.string
;
2265 /* The entry of local ifunc is fake in global hash table,
2266 we should find the name by the original local symbol. */
2267 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2270 /* xgettext:c-format */
2271 (_("%pB: relocation %s against STT_GNU_IFUNC "
2272 "symbol `%s' isn't supported"), input_bfd
,
2274 bfd_set_error (bfd_error_bad_value
);
2281 name
= h
->root
.root
.string
;
2284 name
= (bfd_elf_string_from_elf_section
2285 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2286 if (name
== NULL
|| *name
== '\0')
2287 name
= bfd_section_name (sec
);
2290 resolved_to_zero
= (h
!= NULL
2291 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2297 case R_RISCV_TPREL_ADD
:
2299 case R_RISCV_JUMP_SLOT
:
2300 case R_RISCV_RELATIVE
:
2301 /* These require nothing of us at all. */
2305 case R_RISCV_BRANCH
:
2306 case R_RISCV_RVC_BRANCH
:
2307 case R_RISCV_RVC_LUI
:
2308 case R_RISCV_LO12_I
:
2309 case R_RISCV_LO12_S
:
2314 case R_RISCV_32_PCREL
:
2315 case R_RISCV_DELETE
:
2316 /* These require no special handling beyond perform_relocation. */
2319 case R_RISCV_GOT_HI20
:
2324 off
= h
->got
.offset
;
2325 BFD_ASSERT (off
!= (bfd_vma
) -1);
2326 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2327 pic
= bfd_link_pic (info
);
2329 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2330 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
2332 /* This is actually a static link, or it is a
2333 -Bsymbolic link and the symbol is defined
2334 locally, or the symbol was forced to be local
2335 because of a version file. We must initialize
2336 this entry in the global offset table. Since the
2337 offset must always be a multiple of the word size,
2338 we use the least significant bit to record whether
2339 we have initialized it already.
2341 When doing a dynamic link, we create a .rela.got
2342 relocation entry to initialize the value. This
2343 is done in the finish_dynamic_symbol routine. */
2348 bfd_put_NN (output_bfd
, relocation
,
2349 htab
->elf
.sgot
->contents
+ off
);
2354 unresolved_reloc
= false;
2358 BFD_ASSERT (local_got_offsets
!= NULL
2359 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2361 off
= local_got_offsets
[r_symndx
];
2363 /* The offset must always be a multiple of the word size.
2364 So, we can use the least significant bit to record
2365 whether we have already processed this entry. */
2370 if (bfd_link_pic (info
))
2373 Elf_Internal_Rela outrel
;
2375 /* We need to generate a R_RISCV_RELATIVE reloc
2376 for the dynamic linker. */
2377 s
= htab
->elf
.srelgot
;
2378 BFD_ASSERT (s
!= NULL
);
2380 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2382 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2383 outrel
.r_addend
= relocation
;
2385 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2388 bfd_put_NN (output_bfd
, relocation
,
2389 htab
->elf
.sgot
->contents
+ off
);
2390 local_got_offsets
[r_symndx
] |= 1;
2394 if (rel
->r_addend
!= 0)
2396 msg
= _("The addend isn't allowed for R_RISCV_GOT_HI20");
2397 r
= bfd_reloc_dangerous
;
2401 /* Address of got entry. */
2402 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2403 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
,
2404 relocation
, contents
,
2406 /* Update howto if relocation is changed. */
2407 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2408 ELFNN_R_TYPE (rel
->r_info
));
2410 r
= bfd_reloc_notsupported
;
2411 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2414 r
= bfd_reloc_overflow
;
2423 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2424 contents
+ rel
->r_offset
);
2425 relocation
= old_value
+ relocation
;
2435 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2436 contents
+ rel
->r_offset
);
2437 relocation
= old_value
- relocation
;
2442 case R_RISCV_CALL_PLT
:
2443 /* Handle a call to an undefined weak function. This won't be
2444 relaxed, so we have to handle it here. */
2445 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2446 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2448 /* We can use x0 as the base register. */
2449 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2450 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2451 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2452 /* Set the relocation value so that we get 0 after the pc
2453 relative adjustment. */
2454 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2459 case R_RISCV_RVC_JUMP
:
2460 if (bfd_link_pic (info
) && h
!= NULL
)
2462 if (h
->plt
.offset
!= MINUS_ONE
)
2464 /* Refer to the PLT entry. This check has to match the
2465 check in _bfd_riscv_relax_section. */
2466 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2467 unresolved_reloc
= false;
2469 else if (!SYMBOL_REFERENCES_LOCAL (info
, h
)
2470 && (input_section
->flags
& SEC_ALLOC
) != 0
2471 && (input_section
->flags
& SEC_READONLY
) != 0
2472 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
2474 /* PR 28509, when generating the shared object, these
2475 referenced symbols may bind externally, which means
2476 they will be exported to the dynamic symbol table,
2477 and are preemptible by default. These symbols cannot
2478 be referenced by the non-pic relocations, like
2479 R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2481 However, consider that linker may relax the R_RISCV_CALL
2482 relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2483 these relocations are relocated to the plt entries,
2484 then we won't report error for them.
2486 Perhaps we also need the similar checks for the
2487 R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
2488 if (asprintf (&msg_buf
,
2489 _("%%X%%P: relocation %s against `%s' which "
2490 "may bind externally can not be used when "
2491 "making a shared object; recompile "
2493 howto
->name
, h
->root
.root
.string
) == -1)
2496 r
= bfd_reloc_notsupported
;
2501 case R_RISCV_TPREL_HI20
:
2502 relocation
= tpoff (info
, relocation
);
2505 case R_RISCV_TPREL_LO12_I
:
2506 case R_RISCV_TPREL_LO12_S
:
2507 relocation
= tpoff (info
, relocation
);
2510 case R_RISCV_TPREL_I
:
2511 case R_RISCV_TPREL_S
:
2512 relocation
= tpoff (info
, relocation
);
2513 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2515 /* We can use tp as the base register. */
2516 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2517 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2518 insn
|= X_TP
<< OP_SH_RS1
;
2519 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2522 r
= bfd_reloc_overflow
;
2525 case R_RISCV_GPREL_I
:
2526 case R_RISCV_GPREL_S
:
2528 bfd_vma gp
= riscv_global_pointer_value (info
);
2529 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2530 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2532 /* We can use x0 or gp as the base register. */
2533 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2534 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2537 rel
->r_addend
-= gp
;
2538 insn
|= X_GP
<< OP_SH_RS1
;
2540 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2543 r
= bfd_reloc_overflow
;
2547 case R_RISCV_PCREL_HI20
:
2548 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, relocation
,
2550 /* Update howto if relocation is changed. */
2551 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2552 ELFNN_R_TYPE (rel
->r_info
));
2554 r
= bfd_reloc_notsupported
;
2555 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2556 relocation
+ rel
->r_addend
,
2558 r
= bfd_reloc_overflow
;
2561 case R_RISCV_PCREL_LO12_I
:
2562 case R_RISCV_PCREL_LO12_S
:
2563 /* We don't allow section symbols plus addends as the auipc address,
2564 because then riscv_relax_delete_bytes would have to search through
2565 all relocs to update these addends. This is also ambiguous, as
2566 we do allow offsets to be added to the target address, which are
2567 not to be used to find the auipc address. */
2568 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2569 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2572 msg
= _("%pcrel_lo section symbol with an addend");
2573 r
= bfd_reloc_dangerous
;
2577 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2578 input_section
, info
, howto
,
2581 r
= bfd_reloc_overflow
;
2584 case R_RISCV_TLS_DTPREL32
:
2585 case R_RISCV_TLS_DTPREL64
:
2586 relocation
= dtpoff (info
, relocation
);
2591 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2594 if ((bfd_link_pic (info
)
2596 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2597 && !resolved_to_zero
)
2598 || h
->root
.type
!= bfd_link_hash_undefweak
)
2599 && (!howto
->pc_relative
2600 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2601 || (!bfd_link_pic (info
)
2607 || h
->root
.type
== bfd_link_hash_undefweak
2608 || h
->root
.type
== bfd_link_hash_undefined
)))
2610 Elf_Internal_Rela outrel
;
2612 bool skip_static_relocation
, skip_dynamic_relocation
;
2614 /* When generating a shared object, these relocations
2615 are copied into the output file to be resolved at run
2619 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2621 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2622 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2623 outrel
.r_offset
+= sec_addr (input_section
);
2625 if (skip_dynamic_relocation
)
2626 memset (&outrel
, 0, sizeof outrel
);
2627 else if (h
!= NULL
&& h
->dynindx
!= -1
2628 && !(bfd_link_pic (info
)
2629 && SYMBOLIC_BIND (info
, h
)
2632 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2633 outrel
.r_addend
= rel
->r_addend
;
2637 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2638 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2641 sreloc
= elf_section_data (input_section
)->sreloc
;
2642 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2643 if (skip_static_relocation
)
2648 case R_RISCV_TLS_GOT_HI20
:
2652 case R_RISCV_TLS_GD_HI20
:
2655 off
= h
->got
.offset
;
2660 off
= local_got_offsets
[r_symndx
];
2661 local_got_offsets
[r_symndx
] |= 1;
2664 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2665 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2666 /* If this symbol is referenced by both GD and IE TLS, the IE
2667 reference's GOT slot follows the GD reference's slots. */
2669 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2670 ie_off
= 2 * GOT_ENTRY_SIZE
;
2676 Elf_Internal_Rela outrel
;
2678 bool need_relocs
= false;
2680 if (htab
->elf
.srelgot
== NULL
)
2686 dyn
= htab
->elf
.dynamic_sections_created
;
2687 pic
= bfd_link_pic (info
);
2689 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2690 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2694 /* The GOT entries have not been initialized yet. Do it
2695 now, and emit any relocations. */
2696 if ((bfd_link_pic (info
) || indx
!= 0)
2698 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2699 || h
->root
.type
!= bfd_link_hash_undefweak
))
2702 if (tls_type
& GOT_TLS_GD
)
2706 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2707 outrel
.r_addend
= 0;
2708 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2709 bfd_put_NN (output_bfd
, 0,
2710 htab
->elf
.sgot
->contents
+ off
);
2711 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2714 BFD_ASSERT (! unresolved_reloc
);
2715 bfd_put_NN (output_bfd
,
2716 dtpoff (info
, relocation
),
2717 (htab
->elf
.sgot
->contents
2718 + off
+ RISCV_ELF_WORD_BYTES
));
2722 bfd_put_NN (output_bfd
, 0,
2723 (htab
->elf
.sgot
->contents
2724 + off
+ RISCV_ELF_WORD_BYTES
));
2725 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2726 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2727 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2732 /* If we are not emitting relocations for a
2733 general dynamic reference, then we must be in a
2734 static link or an executable link with the
2735 symbol binding locally. Mark it as belonging
2736 to module 1, the executable. */
2737 bfd_put_NN (output_bfd
, 1,
2738 htab
->elf
.sgot
->contents
+ off
);
2739 bfd_put_NN (output_bfd
,
2740 dtpoff (info
, relocation
),
2741 (htab
->elf
.sgot
->contents
2742 + off
+ RISCV_ELF_WORD_BYTES
));
2746 if (tls_type
& GOT_TLS_IE
)
2750 bfd_put_NN (output_bfd
, 0,
2751 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2752 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2754 outrel
.r_addend
= 0;
2756 outrel
.r_addend
= tpoff (info
, relocation
);
2757 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2758 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2762 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2763 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2768 BFD_ASSERT (off
< (bfd_vma
) -2);
2769 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2770 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2773 r
= bfd_reloc_overflow
;
2774 unresolved_reloc
= false;
2778 r
= bfd_reloc_notsupported
;
2781 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2782 because such sections are not SEC_ALLOC and thus ld.so will
2783 not process them. */
2784 if (unresolved_reloc
2785 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2787 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2788 rel
->r_offset
) != (bfd_vma
) -1)
2790 if (asprintf (&msg_buf
,
2791 _("%%X%%P: unresolvable %s relocation against "
2794 h
->root
.root
.string
) == -1)
2797 r
= bfd_reloc_notsupported
;
2801 if (r
== bfd_reloc_ok
)
2802 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2803 input_bfd
, contents
);
2805 /* We should have already detected the error and set message before.
2806 If the error message isn't set since the linker runs out of memory
2807 or we don't set it before, then we should set the default message
2808 with the "internal error" string here. */
2814 case bfd_reloc_overflow
:
2815 info
->callbacks
->reloc_overflow
2816 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2817 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2820 case bfd_reloc_undefined
:
2821 info
->callbacks
->undefined_symbol
2822 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2826 case bfd_reloc_outofrange
:
2828 msg
= _("%X%P: internal error: out of range error\n");
2831 case bfd_reloc_notsupported
:
2833 msg
= _("%X%P: internal error: unsupported relocation error\n");
2836 case bfd_reloc_dangerous
:
2837 /* The error message should already be set. */
2839 msg
= _("dangerous relocation error");
2840 info
->callbacks
->reloc_dangerous
2841 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2845 msg
= _("%X%P: internal error: unknown error\n");
2849 /* Do not report error message for the dangerous relocation again. */
2850 if (msg
&& r
!= bfd_reloc_dangerous
)
2851 info
->callbacks
->einfo (msg
);
2853 /* Free the unused `msg_buf`. */
2856 /* We already reported the error via a callback, so don't try to report
2857 it again by returning false. That leads to spurious errors. */
2862 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2864 riscv_free_pcrel_relocs (&pcrel_relocs
);
2868 /* Finish up dynamic symbol handling. We set the contents of various
2869 dynamic sections here. */
2872 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2873 struct bfd_link_info
*info
,
2874 struct elf_link_hash_entry
*h
,
2875 Elf_Internal_Sym
*sym
)
2877 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2878 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2880 if (h
->plt
.offset
!= (bfd_vma
) -1)
2882 /* We've decided to create a PLT entry for this symbol. */
2884 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2885 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2886 Elf_Internal_Rela rela
;
2887 asection
*plt
, *gotplt
, *relplt
;
2889 /* When building a static executable, use .iplt, .igot.plt and
2890 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2891 if (htab
->elf
.splt
!= NULL
)
2893 plt
= htab
->elf
.splt
;
2894 gotplt
= htab
->elf
.sgotplt
;
2895 relplt
= htab
->elf
.srelplt
;
2899 plt
= htab
->elf
.iplt
;
2900 gotplt
= htab
->elf
.igotplt
;
2901 relplt
= htab
->elf
.irelplt
;
2904 /* This symbol has an entry in the procedure linkage table. Set
2906 if ((h
->dynindx
== -1
2907 && !((h
->forced_local
|| bfd_link_executable (info
))
2909 && h
->type
== STT_GNU_IFUNC
))
2915 /* Calculate the address of the PLT header. */
2916 header_address
= sec_addr (plt
);
2918 /* Calculate the index of the entry and the offset of .got.plt entry.
2919 For static executables, we don't reserve anything. */
2920 if (plt
== htab
->elf
.splt
)
2922 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2923 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2927 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2928 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2931 /* Calculate the address of the .got.plt entry. */
2932 got_address
= sec_addr (gotplt
) + got_offset
;
2934 /* Find out where the .plt entry should go. */
2935 loc
= plt
->contents
+ h
->plt
.offset
;
2937 /* Fill in the PLT entry itself. */
2938 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2939 header_address
+ h
->plt
.offset
,
2943 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2944 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
2946 /* Fill in the initial value of the .got.plt entry. */
2947 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2948 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2950 rela
.r_offset
= got_address
;
2952 if (h
->dynindx
== -1
2953 || ((bfd_link_executable (info
)
2954 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2956 && h
->type
== STT_GNU_IFUNC
))
2958 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2959 h
->root
.root
.string
,
2960 h
->root
.u
.def
.section
->owner
);
2962 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2963 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2964 asection
*sec
= h
->root
.u
.def
.section
;
2965 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2966 rela
.r_addend
= h
->root
.u
.def
.value
2967 + sec
->output_section
->vma
2968 + sec
->output_offset
;
2972 /* Fill in the entry in the .rela.plt section. */
2973 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2977 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2978 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2980 if (!h
->def_regular
)
2982 /* Mark the symbol as undefined, rather than as defined in
2983 the .plt section. Leave the value alone. */
2984 sym
->st_shndx
= SHN_UNDEF
;
2985 /* If the symbol is weak, we do need to clear the value.
2986 Otherwise, the PLT entry would provide a definition for
2987 the symbol even if the symbol wasn't defined anywhere,
2988 and so the symbol would never be NULL. */
2989 if (!h
->ref_regular_nonweak
)
2994 if (h
->got
.offset
!= (bfd_vma
) -1
2995 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2996 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
3000 Elf_Internal_Rela rela
;
3001 bool use_elf_append_rela
= true;
3003 /* This symbol has an entry in the GOT. Set it up. */
3005 sgot
= htab
->elf
.sgot
;
3006 srela
= htab
->elf
.srelgot
;
3007 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3009 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3011 /* Handle the ifunc symbol in GOT entry. */
3013 && h
->type
== STT_GNU_IFUNC
)
3015 if (h
->plt
.offset
== (bfd_vma
) -1)
3017 /* STT_GNU_IFUNC is referenced without PLT. */
3019 if (htab
->elf
.splt
== NULL
)
3021 /* Use .rela.iplt section to store .got relocations
3022 in static executable. */
3023 srela
= htab
->elf
.irelplt
;
3025 /* Do not use riscv_elf_append_rela to add dynamic
3027 use_elf_append_rela
= false;
3030 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3032 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3033 h
->root
.root
.string
,
3034 h
->root
.u
.def
.section
->owner
);
3036 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3037 rela
.r_addend
= (h
->root
.u
.def
.value
3038 + h
->root
.u
.def
.section
->output_section
->vma
3039 + h
->root
.u
.def
.section
->output_offset
);
3043 /* Generate R_RISCV_NN. */
3044 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3045 BFD_ASSERT (h
->dynindx
!= -1);
3046 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3050 else if (bfd_link_pic (info
))
3052 /* Generate R_RISCV_NN. */
3053 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3054 BFD_ASSERT (h
->dynindx
!= -1);
3055 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3062 if (!h
->pointer_equality_needed
)
3065 /* For non-shared object, we can't use .got.plt, which
3066 contains the real function address if we need pointer
3067 equality. We load the GOT entry with the PLT entry. */
3068 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3069 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3070 + plt
->output_offset
3072 htab
->elf
.sgot
->contents
3073 + (h
->got
.offset
& ~(bfd_vma
) 1));
3077 else if (bfd_link_pic (info
)
3078 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3080 /* If this is a local symbol reference, we just want to emit
3081 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3082 or a pie link, or the symbol was forced to be local because
3083 of a version file. The entry in the global offset table will
3084 already have been initialized in the relocate_section function. */
3085 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3086 asection
*sec
= h
->root
.u
.def
.section
;
3087 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3088 rela
.r_addend
= (h
->root
.u
.def
.value
3089 + sec
->output_section
->vma
3090 + sec
->output_offset
);
3094 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3095 BFD_ASSERT (h
->dynindx
!= -1);
3096 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3100 bfd_put_NN (output_bfd
, 0,
3101 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3103 if (use_elf_append_rela
)
3104 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3107 /* Use riscv_elf_append_rela to add the dynamic relocs into
3108 .rela.iplt may cause the overwrite problems. Since we insert
3109 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3110 but the riscv_elf_append_rela adds the relocs to the place
3111 that are calculated from the reloc_index (in seqential).
3113 One solution is that add these dynamic relocs (GOT IFUNC)
3114 from the last of .rela.iplt section. */
3115 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3116 bfd_byte
*loc
= srela
->contents
3117 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3118 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3124 Elf_Internal_Rela rela
;
3127 /* This symbols needs a copy reloc. Set it up. */
3128 BFD_ASSERT (h
->dynindx
!= -1);
3130 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3131 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3133 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3134 s
= htab
->elf
.sreldynrelro
;
3136 s
= htab
->elf
.srelbss
;
3137 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3140 /* Mark some specially defined symbols as absolute. */
3141 if (h
== htab
->elf
.hdynamic
3142 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3143 sym
->st_shndx
= SHN_ABS
;
3148 /* Finish up local dynamic symbol handling. We set the contents of
3149 various dynamic sections here. */
3152 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3154 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3155 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3157 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3160 /* Finish up the dynamic sections. */
3163 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3164 bfd
*dynobj
, asection
*sdyn
)
3166 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3167 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3168 size_t dynsize
= bed
->s
->sizeof_dyn
;
3169 bfd_byte
*dyncon
, *dynconend
;
3171 dynconend
= sdyn
->contents
+ sdyn
->size
;
3172 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3174 Elf_Internal_Dyn dyn
;
3177 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3182 s
= htab
->elf
.sgotplt
;
3183 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3186 s
= htab
->elf
.srelplt
;
3187 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3190 s
= htab
->elf
.srelplt
;
3191 dyn
.d_un
.d_val
= s
->size
;
3197 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3203 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3204 struct bfd_link_info
*info
)
3208 struct riscv_elf_link_hash_table
*htab
;
3210 htab
= riscv_elf_hash_table (info
);
3211 BFD_ASSERT (htab
!= NULL
);
3212 dynobj
= htab
->elf
.dynobj
;
3214 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3216 if (elf_hash_table (info
)->dynamic_sections_created
)
3221 splt
= htab
->elf
.splt
;
3222 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3224 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3229 /* Fill in the head and tail entries in the procedure linkage table. */
3233 uint32_t plt_header
[PLT_HEADER_INSNS
];
3234 ret
= riscv_make_plt_header (output_bfd
,
3235 sec_addr (htab
->elf
.sgotplt
),
3236 sec_addr (splt
), plt_header
);
3240 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3241 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3243 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3248 if (htab
->elf
.sgotplt
)
3250 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3252 if (bfd_is_abs_section (output_section
))
3254 (*_bfd_error_handler
)
3255 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3259 if (htab
->elf
.sgotplt
->size
> 0)
3261 /* Write the first two entries in .got.plt, needed for the dynamic
3263 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3264 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3265 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3268 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3273 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3275 if (htab
->elf
.sgot
->size
> 0)
3277 /* Set the first entry in the global offset table to the address of
3278 the dynamic section. */
3279 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3280 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3283 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3286 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3287 htab_traverse (htab
->loc_hash_table
,
3288 riscv_elf_finish_local_dynamic_symbol
,
3294 /* Return address for Ith PLT stub in section PLT, for relocation REL
3295 or (bfd_vma) -1 if it should not be included. */
3298 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3299 const arelent
*rel ATTRIBUTE_UNUSED
)
3301 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3304 static enum elf_reloc_type_class
3305 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3306 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3307 const Elf_Internal_Rela
*rela
)
3309 switch (ELFNN_R_TYPE (rela
->r_info
))
3311 case R_RISCV_RELATIVE
:
3312 return reloc_class_relative
;
3313 case R_RISCV_JUMP_SLOT
:
3314 return reloc_class_plt
;
3316 return reloc_class_copy
;
3318 return reloc_class_normal
;
3322 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3326 riscv_float_abi_string (flagword flags
)
3328 switch (flags
& EF_RISCV_FLOAT_ABI
)
3330 case EF_RISCV_FLOAT_ABI_SOFT
:
3331 return "soft-float";
3333 case EF_RISCV_FLOAT_ABI_SINGLE
:
3334 return "single-float";
3336 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3337 return "double-float";
3339 case EF_RISCV_FLOAT_ABI_QUAD
:
3340 return "quad-float";
3347 /* The information of architecture elf attributes. */
3348 static riscv_subset_list_t in_subsets
;
3349 static riscv_subset_list_t out_subsets
;
3350 static riscv_subset_list_t merged_subsets
;
3352 /* Predicator for standard extension. */
3355 riscv_std_ext_p (const char *name
)
3357 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3360 /* Update the output subset's version to match the input when the input
3361 subset's version is newer. */
3364 riscv_update_subset_version (struct riscv_subset_t
*in
,
3365 struct riscv_subset_t
*out
)
3367 if (in
== NULL
|| out
== NULL
)
3370 /* Update the output ISA versions to the newest ones, but otherwise don't
3371 provide any errors or warnings about mis-matched ISA versions as it's
3372 generally too tricky to check for these at link time. */
3373 if ((in
->major_version
> out
->major_version
)
3374 || (in
->major_version
== out
->major_version
3375 && in
->minor_version
> out
->minor_version
)
3376 || (out
->major_version
== RISCV_UNKNOWN_VERSION
))
3378 out
->major_version
= in
->major_version
;
3379 out
->minor_version
= in
->minor_version
;
3383 /* Return true if subset is 'i' or 'e'. */
3386 riscv_i_or_e_p (bfd
*ibfd
,
3388 struct riscv_subset_t
*subset
)
3390 if ((strcasecmp (subset
->name
, "e") != 0)
3391 && (strcasecmp (subset
->name
, "i") != 0))
3394 (_("error: %pB: corrupted ISA string '%s'. "
3395 "First letter should be 'i' or 'e' but got '%s'"),
3396 ibfd
, arch
, subset
->name
);
3402 /* Merge standard extensions.
3405 Return FALSE if failed to merge.
3409 `in_arch`: Raw ISA string for input object.
3410 `out_arch`: Raw ISA string for output object.
3411 `pin`: Subset list for input object.
3412 `pout`: Subset list for output object. */
3415 riscv_merge_std_ext (bfd
*ibfd
,
3416 const char *in_arch
,
3417 const char *out_arch
,
3418 struct riscv_subset_t
**pin
,
3419 struct riscv_subset_t
**pout
)
3421 const char *standard_exts
= "mafdqlcbjtpvn";
3423 struct riscv_subset_t
*in
= *pin
;
3424 struct riscv_subset_t
*out
= *pout
;
3426 /* First letter should be 'i' or 'e'. */
3427 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3430 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3433 if (strcasecmp (in
->name
, out
->name
) != 0)
3435 /* TODO: We might allow merge 'i' with 'e'. */
3437 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3438 ibfd
, in
->name
, out
->name
);
3442 riscv_update_subset_version(in
, out
);
3443 riscv_add_subset (&merged_subsets
,
3444 out
->name
, out
->major_version
, out
->minor_version
);
3449 /* Handle standard extension first. */
3450 for (p
= standard_exts
; *p
; ++p
)
3452 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3453 char find_ext
[2] = {*p
, '\0'};
3454 bool find_in
, find_out
;
3456 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3457 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3459 if (!find_in
&& !find_out
)
3462 if (find_in
&& find_out
)
3463 riscv_update_subset_version(ext_in
, ext_out
);
3465 ext_merged
= find_out
? ext_out
: ext_in
;
3466 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3467 ext_merged
->major_version
, ext_merged
->minor_version
);
3470 /* Skip all standard extensions. */
3471 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3472 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3480 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3481 object subset list. Likewise for POUT and the output object. Return TRUE
3482 on success and FALSE when a conflict is found. */
3485 riscv_merge_multi_letter_ext (riscv_subset_t
**pin
,
3486 riscv_subset_t
**pout
)
3488 riscv_subset_t
*in
= *pin
;
3489 riscv_subset_t
*out
= *pout
;
3490 riscv_subset_t
*tail
;
3496 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3500 /* `in' comes before `out', append `in' and increment. */
3501 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3507 /* `out' comes before `in', append `out' and increment. */
3508 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3509 out
->minor_version
);
3514 /* Both present, check version and increment both. */
3515 riscv_update_subset_version (in
, out
);
3517 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3518 out
->minor_version
);
3526 /* If we're here, either `in' or `out' is running longer than
3527 the other. So, we need to append the corresponding tail. */
3528 tail
= in
? in
: out
;
3531 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3532 tail
->minor_version
);
3540 /* Merge Tag_RISCV_arch attribute. */
3543 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3545 riscv_subset_t
*in
, *out
;
3546 char *merged_arch_str
;
3548 unsigned xlen_in
, xlen_out
;
3549 merged_subsets
.head
= NULL
;
3550 merged_subsets
.tail
= NULL
;
3552 riscv_parse_subset_t riscv_rps_ld_in
=
3553 {&in_subsets
, _bfd_error_handler
, &xlen_in
, NULL
, false};
3554 riscv_parse_subset_t riscv_rps_ld_out
=
3555 {&out_subsets
, _bfd_error_handler
, &xlen_out
, NULL
, false};
3557 if (in_arch
== NULL
&& out_arch
== NULL
)
3559 if (in_arch
== NULL
&& out_arch
!= NULL
)
3561 if (in_arch
!= NULL
&& out_arch
== NULL
)
3564 /* Parse subset from ISA string. */
3565 if (!riscv_parse_subset (&riscv_rps_ld_in
, in_arch
))
3567 if (!riscv_parse_subset (&riscv_rps_ld_out
, out_arch
))
3570 /* Checking XLEN. */
3571 if (xlen_out
!= xlen_in
)
3574 (_("error: %pB: ISA string of input (%s) doesn't match "
3575 "output (%s)"), ibfd
, in_arch
, out_arch
);
3579 /* Merge subset list. */
3580 in
= in_subsets
.head
;
3581 out
= out_subsets
.head
;
3583 /* Merge standard extension. */
3584 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3587 /* Merge all non-single letter extensions with single call. */
3588 if (!riscv_merge_multi_letter_ext (&in
, &out
))
3591 if (xlen_in
!= xlen_out
)
3594 (_("error: %pB: XLEN of input (%u) doesn't match "
3595 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3599 if (xlen_in
!= ARCH_SIZE
)
3602 (_("error: %pB: unsupported XLEN (%u), you might be "
3603 "using wrong emulation"), ibfd
, xlen_in
);
3607 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3609 /* Release the subset lists. */
3610 riscv_release_subset_list (&in_subsets
);
3611 riscv_release_subset_list (&out_subsets
);
3612 riscv_release_subset_list (&merged_subsets
);
3614 return merged_arch_str
;
3617 /* Merge object attributes from IBFD into output_bfd of INFO.
3618 Raise an error if there are conflicting attributes. */
3621 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3623 bfd
*obfd
= info
->output_bfd
;
3624 obj_attribute
*in_attr
;
3625 obj_attribute
*out_attr
;
3627 bool priv_attrs_merged
= false;
3628 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3631 /* Skip linker created files. */
3632 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3635 /* Skip any input that doesn't have an attribute section.
3636 This enables to link object files without attribute section with
3638 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3641 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3643 /* This is the first object. Copy the attributes. */
3644 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3646 out_attr
= elf_known_obj_attributes_proc (obfd
);
3648 /* Use the Tag_null value to indicate the attributes have been
3655 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3656 out_attr
= elf_known_obj_attributes_proc (obfd
);
3658 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3662 case Tag_RISCV_arch
:
3663 if (!out_attr
[Tag_RISCV_arch
].s
)
3664 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3665 else if (in_attr
[Tag_RISCV_arch
].s
3666 && out_attr
[Tag_RISCV_arch
].s
)
3668 /* Check compatible. */
3670 riscv_merge_arch_attr_info (ibfd
,
3671 in_attr
[Tag_RISCV_arch
].s
,
3672 out_attr
[Tag_RISCV_arch
].s
);
3673 if (merged_arch
== NULL
)
3676 out_attr
[Tag_RISCV_arch
].s
= "";
3679 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3683 case Tag_RISCV_priv_spec
:
3684 case Tag_RISCV_priv_spec_minor
:
3685 case Tag_RISCV_priv_spec_revision
:
3686 /* If we have handled the privileged elf attributes, then skip it. */
3687 if (!priv_attrs_merged
)
3689 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3690 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3691 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3692 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3693 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3695 /* Get the privileged spec class from elf attributes. */
3696 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3700 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3705 /* Allow to link the object without the privileged specs. */
3706 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3708 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3709 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3710 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3712 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3713 && in_priv_spec
!= out_priv_spec
)
3716 (_("warning: %pB use privileged spec version %u.%u.%u but "
3717 "the output use version %u.%u.%u"),
3726 /* The privileged spec v1.9.1 can not be linked with others
3727 since the conflicts, so we plan to drop it in a year or
3729 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3730 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3733 (_("warning: privileged spec version 1.9.1 can not be "
3734 "linked with other spec versions"));
3737 /* Update the output privileged spec to the newest one. */
3738 if (in_priv_spec
> out_priv_spec
)
3740 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3741 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3742 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3745 priv_attrs_merged
= true;
3749 case Tag_RISCV_unaligned_access
:
3750 out_attr
[i
].i
|= in_attr
[i
].i
;
3753 case Tag_RISCV_stack_align
:
3754 if (out_attr
[i
].i
== 0)
3755 out_attr
[i
].i
= in_attr
[i
].i
;
3756 else if (in_attr
[i
].i
!= 0
3757 && out_attr
[i
].i
!= 0
3758 && out_attr
[i
].i
!= in_attr
[i
].i
)
3761 (_("error: %pB use %u-byte stack aligned but the output "
3762 "use %u-byte stack aligned"),
3763 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3769 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3772 /* If out_attr was copied from in_attr then it won't have a type yet. */
3773 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3774 out_attr
[i
].type
= in_attr
[i
].type
;
3777 /* Merge Tag_compatibility attributes and any common GNU ones. */
3778 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3781 /* Check for any attributes not known on RISC-V. */
3782 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3787 /* Merge backend specific data from an object file to the output
3788 object file when linking. */
3791 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3793 bfd
*obfd
= info
->output_bfd
;
3794 flagword new_flags
, old_flags
;
3796 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3799 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3801 (*_bfd_error_handler
)
3802 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3803 " target emulation `%s' does not match `%s'"),
3804 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3808 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3811 if (!riscv_merge_attributes (ibfd
, info
))
3814 /* Check to see if the input BFD actually contains any sections. If not,
3815 its flags may not have been initialized either, but it cannot actually
3816 cause any incompatibility. Do not short-circuit dynamic objects; their
3817 section list may be emptied by elf_link_add_object_symbols.
3819 Also check to see if there are no code sections in the input. In this
3820 case, there is no need to check for code specific flags. */
3821 if (!(ibfd
->flags
& DYNAMIC
))
3823 bool null_input_bfd
= true;
3824 bool only_data_sections
= true;
3827 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3829 null_input_bfd
= false;
3831 if ((bfd_section_flags (sec
)
3832 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3833 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3835 only_data_sections
= false;
3840 if (null_input_bfd
|| only_data_sections
)
3844 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3845 old_flags
= elf_elfheader (obfd
)->e_flags
;
3847 if (!elf_flags_init (obfd
))
3849 elf_flags_init (obfd
) = true;
3850 elf_elfheader (obfd
)->e_flags
= new_flags
;
3854 /* Disallow linking different float ABIs. */
3855 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3857 (*_bfd_error_handler
)
3858 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3859 riscv_float_abi_string (new_flags
),
3860 riscv_float_abi_string (old_flags
));
3864 /* Disallow linking RVE and non-RVE. */
3865 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3867 (*_bfd_error_handler
)
3868 (_("%pB: can't link RVE with other target"), ibfd
);
3872 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3873 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3875 /* Allow linking TSO and non-TSO, and keep the TSO flag. */
3876 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_TSO
;
3881 bfd_set_error (bfd_error_bad_value
);
3885 /* A second format for recording PC-relative hi relocations. This stores the
3886 information required to relax them to GP-relative addresses. */
3888 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3889 struct riscv_pcgp_hi_reloc
3896 bool undefined_weak
;
3897 riscv_pcgp_hi_reloc
*next
;
3900 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3901 struct riscv_pcgp_lo_reloc
3904 riscv_pcgp_lo_reloc
*next
;
3909 riscv_pcgp_hi_reloc
*hi
;
3910 riscv_pcgp_lo_reloc
*lo
;
3911 } riscv_pcgp_relocs
;
3913 /* Initialize the pcgp reloc info in P. */
3916 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3923 /* Free the pcgp reloc info in P. */
3926 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3927 bfd
*abfd ATTRIBUTE_UNUSED
,
3928 asection
*sec ATTRIBUTE_UNUSED
)
3930 riscv_pcgp_hi_reloc
*c
;
3931 riscv_pcgp_lo_reloc
*l
;
3933 for (c
= p
->hi
; c
!= NULL
; )
3935 riscv_pcgp_hi_reloc
*next
= c
->next
;
3940 for (l
= p
->lo
; l
!= NULL
; )
3942 riscv_pcgp_lo_reloc
*next
= l
->next
;
3948 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3949 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3950 relax the corresponding lo part reloc. */
3953 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3954 bfd_vma hi_addend
, bfd_vma hi_addr
,
3955 unsigned hi_sym
, asection
*sym_sec
,
3956 bool undefined_weak
)
3958 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
3961 new->hi_sec_off
= hi_sec_off
;
3962 new->hi_addend
= hi_addend
;
3963 new->hi_addr
= hi_addr
;
3964 new->hi_sym
= hi_sym
;
3965 new->sym_sec
= sym_sec
;
3966 new->undefined_weak
= undefined_weak
;
3972 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3973 This is used by a lo part reloc to find the corresponding hi part reloc. */
3975 static riscv_pcgp_hi_reloc
*
3976 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3978 riscv_pcgp_hi_reloc
*c
;
3980 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3981 if (c
->hi_sec_off
== hi_sec_off
)
3986 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3987 This is used to record relocs that can't be relaxed. */
3990 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3992 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
3995 new->hi_sec_off
= hi_sec_off
;
4001 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4002 This is used by a hi part reloc to find the corresponding lo part reloc. */
4005 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4007 riscv_pcgp_lo_reloc
*c
;
4009 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4010 if (c
->hi_sec_off
== hi_sec_off
)
4016 riscv_update_pcgp_relocs (riscv_pcgp_relocs
*p
, asection
*deleted_sec
,
4017 bfd_vma deleted_addr
, size_t deleted_count
)
4019 /* Bytes have already been deleted and toaddr should match the old section
4020 size for our checks, so adjust it here. */
4021 bfd_vma toaddr
= deleted_sec
->size
+ deleted_count
;
4022 riscv_pcgp_lo_reloc
*l
;
4023 riscv_pcgp_hi_reloc
*h
;
4025 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4026 entries where they occur after the deleted bytes. */
4027 for (l
= p
->lo
; l
!= NULL
; l
= l
->next
)
4028 if (l
->hi_sec_off
> deleted_addr
4029 && l
->hi_sec_off
< toaddr
)
4030 l
->hi_sec_off
-= deleted_count
;
4032 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4033 these values occur after the deleted bytes. */
4034 for (h
= p
->hi
; h
!= NULL
; h
= h
->next
)
4036 if (h
->hi_sec_off
> deleted_addr
4037 && h
->hi_sec_off
< toaddr
)
4038 h
->hi_sec_off
-= deleted_count
;
4039 if (h
->sym_sec
== deleted_sec
4040 && h
->hi_addr
> deleted_addr
4041 && h
->hi_addr
< toaddr
)
4042 h
->hi_addr
-= deleted_count
;
4046 /* Delete some bytes, adjust relcocations and symbol table from a section. */
4049 _riscv_relax_delete_bytes (bfd
*abfd
,
4053 struct bfd_link_info
*link_info
,
4054 riscv_pcgp_relocs
*p
,
4055 bfd_vma delete_total
,
4058 unsigned int i
, symcount
;
4059 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
4060 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
4061 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
4062 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4063 bfd_byte
*contents
= data
->this_hdr
.contents
;
4064 size_t bytes_to_move
= toaddr
- addr
- count
;
4066 /* Actually delete the bytes. */
4068 memmove (contents
+ addr
, contents
+ addr
+ count
+ delete_total
, bytes_to_move
);
4070 /* Still adjust relocations and symbols in non-linear times. */
4071 toaddr
= sec
->size
+ count
;
4073 /* Adjust the location of all of the relocs. Note that we need not
4074 adjust the addends, since all PC-relative references must be against
4075 symbols, which we will adjust below. */
4076 for (i
= 0; i
< sec
->reloc_count
; i
++)
4077 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
4078 data
->relocs
[i
].r_offset
-= count
;
4080 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4081 table for which these values occur after the deleted bytes. */
4083 riscv_update_pcgp_relocs (p
, sec
, addr
, count
);
4085 /* Adjust the local symbols defined in this section. */
4086 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
4088 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
4089 if (sym
->st_shndx
== sec_shndx
)
4091 /* If the symbol is in the range of memory we just moved, we
4092 have to adjust its value. */
4093 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
4094 sym
->st_value
-= count
;
4096 /* If the symbol *spans* the bytes we just deleted (i.e. its
4097 *end* is in the moved bytes but its *start* isn't), then we
4098 must adjust its size.
4100 This test needs to use the original value of st_value, otherwise
4101 we might accidentally decrease size when deleting bytes right
4102 before the symbol. But since deleted relocs can't span across
4103 symbols, we can't have both a st_value and a st_size decrease,
4104 so it is simpler to just use an else. */
4105 else if (sym
->st_value
<= addr
4106 && sym
->st_value
+ sym
->st_size
> addr
4107 && sym
->st_value
+ sym
->st_size
<= toaddr
)
4108 sym
->st_size
-= count
;
4112 /* Now adjust the global symbols defined in this section. */
4113 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
4114 - symtab_hdr
->sh_info
);
4116 for (i
= 0; i
< symcount
; i
++)
4118 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
4120 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4121 containing the definition of __wrap_SYMBOL, includes a direct
4122 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4123 the same symbol (which is __wrap_SYMBOL), but still exist as two
4124 different symbols in 'sym_hashes', we don't want to adjust
4125 the global symbol __wrap_SYMBOL twice.
4127 The same problem occurs with symbols that are versioned_hidden, as
4128 foo becomes an alias for foo@BAR, and hence they need the same
4130 if (link_info
->wrap_hash
!= NULL
4131 || sym_hash
->versioned
!= unversioned
)
4133 struct elf_link_hash_entry
**cur_sym_hashes
;
4135 /* Loop only over the symbols which have already been checked. */
4136 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4139 /* If the current symbol is identical to 'sym_hash', that means
4140 the symbol was already adjusted (or at least checked). */
4141 if (*cur_sym_hashes
== sym_hash
)
4144 /* Don't adjust the symbol again. */
4145 if (cur_sym_hashes
< &sym_hashes
[i
])
4149 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4150 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4151 && sym_hash
->root
.u
.def
.section
== sec
)
4153 /* As above, adjust the value if needed. */
4154 if (sym_hash
->root
.u
.def
.value
> addr
4155 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4156 sym_hash
->root
.u
.def
.value
-= count
;
4158 /* As above, adjust the size if needed. */
4159 else if (sym_hash
->root
.u
.def
.value
<= addr
4160 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4161 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4162 sym_hash
->size
-= count
;
4169 typedef bool (*relax_delete_t
) (bfd
*, asection
*,
4171 struct bfd_link_info
*,
4172 riscv_pcgp_relocs
*,
4173 Elf_Internal_Rela
*);
4175 static relax_delete_t riscv_relax_delete_bytes
;
4177 /* Do not delete some bytes from a section while relaxing.
4178 Just mark the deleted bytes as R_RISCV_DELETE. */
4181 _riscv_relax_delete_piecewise (bfd
*abfd ATTRIBUTE_UNUSED
,
4182 asection
*sec ATTRIBUTE_UNUSED
,
4185 struct bfd_link_info
*link_info ATTRIBUTE_UNUSED
,
4186 riscv_pcgp_relocs
*p ATTRIBUTE_UNUSED
,
4187 Elf_Internal_Rela
*rel
)
4191 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4192 rel
->r_offset
= addr
;
4193 rel
->r_addend
= count
;
4197 /* Delete some bytes from a section while relaxing. */
4200 _riscv_relax_delete_immediate (bfd
*abfd
,
4204 struct bfd_link_info
*link_info
,
4205 riscv_pcgp_relocs
*p
,
4206 Elf_Internal_Rela
*rel
)
4209 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4210 return _riscv_relax_delete_bytes (abfd
, sec
, addr
, count
,
4211 link_info
, p
, 0, sec
->size
);
4214 /* Delete the bytes for R_RISCV_DELETE relocs. */
4217 riscv_relax_resolve_delete_relocs (bfd
*abfd
,
4219 struct bfd_link_info
*link_info
,
4220 Elf_Internal_Rela
*relocs
)
4222 bfd_vma delete_total
= 0;
4225 for (i
= 0; i
< sec
->reloc_count
; i
++)
4227 Elf_Internal_Rela
*rel
= relocs
+ i
;
4228 if (ELFNN_R_TYPE (rel
->r_info
) != R_RISCV_DELETE
)
4231 /* Find the next R_RISCV_DELETE reloc if possible. */
4232 Elf_Internal_Rela
*rel_next
= NULL
;
4233 unsigned int start
= rel
- relocs
;
4234 for (i
= start
; i
< sec
->reloc_count
; i
++)
4236 /* Since we only replace existing relocs and don't add new relocs, the
4237 relocs are in sequential order. We can skip the relocs prior to this
4238 one, making this search linear time. */
4239 rel_next
= relocs
+ i
;
4240 if (ELFNN_R_TYPE ((rel_next
)->r_info
) == R_RISCV_DELETE
4241 && (rel_next
)->r_offset
> rel
->r_offset
)
4243 BFD_ASSERT (rel_next
- rel
> 0);
4250 bfd_vma toaddr
= rel_next
== NULL
? sec
->size
: rel_next
->r_offset
;
4251 if (!_riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4252 link_info
, NULL
, delete_total
, toaddr
))
4255 delete_total
+= rel
->r_addend
;
4256 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4258 /* Skip ahead to the next delete reloc. */
4259 i
= rel_next
!= NULL
? (unsigned int) (rel_next
- relocs
- 1)
4266 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4267 struct bfd_link_info
*,
4268 Elf_Internal_Rela
*,
4269 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4270 riscv_pcgp_relocs
*,
4271 bool undefined_weak
);
4273 /* Relax AUIPC + JALR into JAL. */
4276 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4277 struct bfd_link_info
*link_info
,
4278 Elf_Internal_Rela
*rel
,
4280 bfd_vma max_alignment
,
4281 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4283 riscv_pcgp_relocs
*pcgp_relocs
,
4284 bool undefined_weak ATTRIBUTE_UNUSED
)
4286 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4287 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4288 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4289 bfd_vma auipc
, jalr
;
4290 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4292 /* If the call crosses section boundaries, an alignment directive could
4293 cause the PC-relative offset to later increase, so we need to add in the
4294 max alignment of any section inclusive from the call to the target.
4295 Otherwise, we only need to use the alignment of the current section. */
4296 if (VALID_JTYPE_IMM (foff
))
4298 if (sym_sec
->output_section
== sec
->output_section
4299 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4300 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4301 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4304 /* See if this function call can be shortened. */
4305 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4308 /* Shorten the function call. */
4309 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4311 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4312 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4313 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4314 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4316 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4317 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4321 /* Relax to C.J[AL] rd, addr. */
4322 r_type
= R_RISCV_RVC_JUMP
;
4323 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4326 else if (VALID_JTYPE_IMM (foff
))
4328 /* Relax to JAL rd, addr. */
4329 r_type
= R_RISCV_JAL
;
4330 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4334 /* Near zero, relax to JALR rd, x0, addr. */
4335 r_type
= R_RISCV_LO12_I
;
4336 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4339 /* Replace the R_RISCV_CALL reloc. */
4340 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4341 /* Replace the AUIPC. */
4342 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4344 /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */
4346 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4347 link_info
, pcgp_relocs
, rel
+ 1);
4350 /* Traverse all output sections and return the max alignment. */
4353 _bfd_riscv_get_max_alignment (asection
*sec
)
4355 unsigned int max_alignment_power
= 0;
4358 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4360 if (o
->alignment_power
> max_alignment_power
)
4361 max_alignment_power
= o
->alignment_power
;
4364 return (bfd_vma
) 1 << max_alignment_power
;
4367 /* Relax non-PIC global variable references to GP-relative references. */
4370 _bfd_riscv_relax_lui (bfd
*abfd
,
4373 struct bfd_link_info
*link_info
,
4374 Elf_Internal_Rela
*rel
,
4376 bfd_vma max_alignment
,
4377 bfd_vma reserve_size
,
4379 riscv_pcgp_relocs
*pcgp_relocs
,
4380 bool undefined_weak
)
4382 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4383 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4384 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4386 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4390 /* If gp and the symbol are in the same output section, which is not the
4391 abs section, then consider only that output section's alignment. */
4392 struct bfd_link_hash_entry
*h
=
4393 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4395 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4396 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4397 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4400 /* Is the reference in range of x0 or gp?
4401 Valid gp range conservatively because of alignment issue. */
4403 || (VALID_ITYPE_IMM (symval
)
4405 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4407 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4409 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4410 switch (ELFNN_R_TYPE (rel
->r_info
))
4412 case R_RISCV_LO12_I
:
4415 /* Change the RS1 to zero. */
4416 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4417 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4418 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4421 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4424 case R_RISCV_LO12_S
:
4427 /* Change the RS1 to zero. */
4428 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4429 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4430 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4433 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4437 /* Delete unnecessary LUI and reuse the reloc. */
4439 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4440 link_info
, pcgp_relocs
, rel
);
4447 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4448 account for this assuming page alignment at worst. In the presence of
4449 RELRO segment the linker aligns it by one page size, therefore sections
4450 after the segment can be moved more than one page. */
4453 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4454 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4455 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4456 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4457 : ELF_MAXPAGESIZE
)))
4459 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4460 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4461 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4462 if (rd
== 0 || rd
== X_SP
)
4465 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4466 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4468 /* Replace the R_RISCV_HI20 reloc. */
4469 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4471 /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */
4473 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4474 link_info
, pcgp_relocs
, rel
+ 1);
4480 /* Relax non-PIC TLS references to TP-relative references. */
4483 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4485 asection
*sym_sec ATTRIBUTE_UNUSED
,
4486 struct bfd_link_info
*link_info
,
4487 Elf_Internal_Rela
*rel
,
4489 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4490 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4492 riscv_pcgp_relocs
*pcgp_relocs
,
4493 bool undefined_weak ATTRIBUTE_UNUSED
)
4495 /* See if this symbol is in range of tp. */
4496 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4499 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4500 switch (ELFNN_R_TYPE (rel
->r_info
))
4502 case R_RISCV_TPREL_LO12_I
:
4503 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4506 case R_RISCV_TPREL_LO12_S
:
4507 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4510 case R_RISCV_TPREL_HI20
:
4511 case R_RISCV_TPREL_ADD
:
4512 /* Delete unnecessary instruction and reuse the reloc. */
4514 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4522 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4523 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4526 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4528 struct bfd_link_info
*link_info
,
4529 Elf_Internal_Rela
*rel
,
4531 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4532 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4533 bool *again ATTRIBUTE_UNUSED
,
4534 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4535 bool undefined_weak ATTRIBUTE_UNUSED
)
4537 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4538 bfd_vma alignment
= 1, pos
;
4539 while (alignment
<= rel
->r_addend
)
4542 symval
-= rel
->r_addend
;
4543 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4544 bfd_vma nop_bytes
= aligned_addr
- symval
;
4546 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4547 sec
->sec_flg0
= true;
4549 /* Make sure there are enough NOPs to actually achieve the alignment. */
4550 if (rel
->r_addend
< nop_bytes
)
4553 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4554 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4555 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4556 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4557 bfd_set_error (bfd_error_bad_value
);
4561 /* Delete the reloc. */
4562 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4564 /* If the number of NOPs is already correct, there's nothing to do. */
4565 if (nop_bytes
== rel
->r_addend
)
4568 /* Write as many RISC-V NOPs as we need. */
4569 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4570 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4572 /* Write a final RVC NOP if need be. */
4573 if (nop_bytes
% 4 != 0)
4574 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4576 /* Delete excess bytes. */
4577 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4578 rel
->r_addend
- nop_bytes
, link_info
,
4582 /* Relax PC-relative references to GP-relative references. */
4585 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4588 struct bfd_link_info
*link_info
,
4589 Elf_Internal_Rela
*rel
,
4591 bfd_vma max_alignment
,
4592 bfd_vma reserve_size
,
4594 riscv_pcgp_relocs
*pcgp_relocs
,
4595 bool undefined_weak
)
4597 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4598 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4600 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4602 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4603 actual target address. */
4604 riscv_pcgp_hi_reloc hi_reloc
;
4605 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4606 switch (ELFNN_R_TYPE (rel
->r_info
))
4608 case R_RISCV_PCREL_LO12_I
:
4609 case R_RISCV_PCREL_LO12_S
:
4611 /* If the %lo has an addend, it isn't for the label pointing at the
4612 hi part instruction, but rather for the symbol pointed at by the
4613 hi part instruction. So we must subtract it here for the lookup.
4614 It is still used below in the final symbol address. */
4615 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4616 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4620 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4625 symval
= hi_reloc
.hi_addr
;
4626 sym_sec
= hi_reloc
.sym_sec
;
4628 /* We can not know whether the undefined weak symbol is referenced
4629 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4630 we have to record the 'undefined_weak' flag when handling the
4631 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4632 undefined_weak
= hi_reloc
.undefined_weak
;
4636 case R_RISCV_PCREL_HI20
:
4637 /* Mergeable symbols and code might later move out of range. */
4638 if (! undefined_weak
4639 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4642 /* If the cooresponding lo relocation has already been seen then it's not
4643 safe to relax this relocation. */
4644 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4655 /* If gp and the symbol are in the same output section, which is not the
4656 abs section, then consider only that output section's alignment. */
4657 struct bfd_link_hash_entry
*h
=
4658 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4660 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4661 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4662 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4665 /* Is the reference in range of x0 or gp?
4666 Valid gp range conservatively because of alignment issue. */
4668 || (VALID_ITYPE_IMM (symval
)
4670 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4672 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4674 unsigned sym
= hi_reloc
.hi_sym
;
4675 switch (ELFNN_R_TYPE (rel
->r_info
))
4677 case R_RISCV_PCREL_LO12_I
:
4680 /* Change the RS1 to zero, and then modify the relocation
4681 type to R_RISCV_LO12_I. */
4682 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4683 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4684 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4685 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4686 rel
->r_addend
= hi_reloc
.hi_addend
;
4690 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4691 rel
->r_addend
+= hi_reloc
.hi_addend
;
4695 case R_RISCV_PCREL_LO12_S
:
4698 /* Change the RS1 to zero, and then modify the relocation
4699 type to R_RISCV_LO12_S. */
4700 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4701 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4702 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4703 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4704 rel
->r_addend
= hi_reloc
.hi_addend
;
4708 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4709 rel
->r_addend
+= hi_reloc
.hi_addend
;
4713 case R_RISCV_PCREL_HI20
:
4714 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4718 ELFNN_R_SYM(rel
->r_info
),
4721 /* Delete unnecessary AUIPC and reuse the reloc. */
4723 riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
,
4735 /* Called by after_allocation to set the information of data segment
4739 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
4740 int *data_segment_phase
)
4742 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4743 htab
->data_segment_phase
= data_segment_phase
;
4748 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs.
4749 Pass 1: Deletes the bytes that PCREL relaxation in pass 0 made obsolete.
4750 Pass 2: Which cannot be disabled, handles code alignment directives. */
4753 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4754 struct bfd_link_info
*info
,
4757 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4758 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4759 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4760 Elf_Internal_Rela
*relocs
;
4763 bfd_vma max_alignment
, reserve_size
= 0;
4764 riscv_pcgp_relocs pcgp_relocs
;
4768 if (bfd_link_relocatable (info
)
4770 || (sec
->flags
& SEC_RELOC
) == 0
4771 || sec
->reloc_count
== 0
4772 || (info
->disable_target_specific_optimizations
4773 && info
->relax_pass
== 0)
4774 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4775 and defined in ld/ldexp.h. */
4776 || *(htab
->data_segment_phase
) == 4)
4779 riscv_init_pcgp_relocs (&pcgp_relocs
);
4781 /* Read this BFD's relocs if we haven't done so already. */
4783 relocs
= data
->relocs
;
4784 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4785 info
->keep_memory
)))
4790 max_alignment
= htab
->max_alignment
;
4791 if (max_alignment
== (bfd_vma
) -1)
4793 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4794 htab
->max_alignment
= max_alignment
;
4798 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4800 /* Examine and consider relaxing each reloc. */
4801 for (i
= 0; i
< sec
->reloc_count
; i
++)
4804 Elf_Internal_Rela
*rel
= relocs
+ i
;
4805 relax_func_t relax_func
;
4806 int type
= ELFNN_R_TYPE (rel
->r_info
);
4809 bool undefined_weak
= false;
4812 riscv_relax_delete_bytes
= NULL
;
4813 if (info
->relax_pass
== 0)
4815 if (type
== R_RISCV_CALL
4816 || type
== R_RISCV_CALL_PLT
)
4817 relax_func
= _bfd_riscv_relax_call
;
4818 else if (type
== R_RISCV_HI20
4819 || type
== R_RISCV_LO12_I
4820 || type
== R_RISCV_LO12_S
)
4821 relax_func
= _bfd_riscv_relax_lui
;
4822 else if (type
== R_RISCV_TPREL_HI20
4823 || type
== R_RISCV_TPREL_ADD
4824 || type
== R_RISCV_TPREL_LO12_I
4825 || type
== R_RISCV_TPREL_LO12_S
)
4826 relax_func
= _bfd_riscv_relax_tls_le
;
4827 else if (!bfd_link_pic (info
)
4828 && (type
== R_RISCV_PCREL_HI20
4829 || type
== R_RISCV_PCREL_LO12_I
4830 || type
== R_RISCV_PCREL_LO12_S
))
4831 relax_func
= _bfd_riscv_relax_pc
;
4834 riscv_relax_delete_bytes
= _riscv_relax_delete_piecewise
;
4836 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4837 if (i
== sec
->reloc_count
- 1
4838 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4839 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4842 /* Skip over the R_RISCV_RELAX. */
4845 else if (info
->relax_pass
== 1 && type
== R_RISCV_ALIGN
)
4847 relax_func
= _bfd_riscv_relax_align
;
4848 riscv_relax_delete_bytes
= _riscv_relax_delete_immediate
;
4853 data
->relocs
= relocs
;
4855 /* Read this BFD's contents if we haven't done so already. */
4856 if (!data
->this_hdr
.contents
4857 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4860 /* Read this BFD's symbols if we haven't done so already. */
4861 if (symtab_hdr
->sh_info
!= 0
4862 && !symtab_hdr
->contents
4863 && !(symtab_hdr
->contents
=
4864 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4865 symtab_hdr
->sh_info
,
4866 0, NULL
, NULL
, NULL
)))
4869 /* Get the value of the symbol referred to by the reloc. */
4870 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4872 /* A local symbol. */
4873 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4874 + ELFNN_R_SYM (rel
->r_info
));
4875 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4876 ? 0 : isym
->st_size
- rel
->r_addend
;
4878 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4879 a fake global symbol entry for this, so deal with the local ifunc
4881 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4884 if (isym
->st_shndx
== SHN_UNDEF
)
4885 sym_sec
= sec
, symval
= rel
->r_offset
;
4888 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4889 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4891 /* The purpose of this code is unknown. It breaks linker scripts
4892 for embedded development that place sections at address zero.
4893 This code is believed to be unnecessary. Disabling it but not
4894 yet removing it, in case something breaks. */
4895 if (sec_addr (sym_sec
) == 0)
4898 symval
= isym
->st_value
;
4900 symtype
= ELF_ST_TYPE (isym
->st_info
);
4905 struct elf_link_hash_entry
*h
;
4907 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4908 h
= elf_sym_hashes (abfd
)[indx
];
4910 while (h
->root
.type
== bfd_link_hash_indirect
4911 || h
->root
.type
== bfd_link_hash_warning
)
4912 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4914 /* Disable the relaxation for ifunc. */
4915 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4918 if (h
->root
.type
== bfd_link_hash_undefweak
4919 && (relax_func
== _bfd_riscv_relax_lui
4920 || relax_func
== _bfd_riscv_relax_pc
))
4922 /* For the lui and auipc relaxations, since the symbol
4923 value of an undefined weak symbol is always be zero,
4924 we can optimize the patterns into a single LI/MV/ADDI
4927 Note that, creating shared libraries and pie output may
4928 break the rule above. Fortunately, since we do not relax
4929 pc relocs when creating shared libraries and pie output,
4930 and the absolute address access for R_RISCV_HI20 isn't
4931 allowed when "-fPIC" is set, the problem of creating shared
4932 libraries can not happen currently. Once we support the
4933 auipc relaxations when creating shared libraries, then we will
4934 need the more rigorous checking for this optimization. */
4935 undefined_weak
= true;
4938 /* This line has to match the check in riscv_elf_relocate_section
4939 in the R_RISCV_CALL[_PLT] case. */
4940 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4942 sym_sec
= htab
->elf
.splt
;
4943 symval
= h
->plt
.offset
;
4945 else if (undefined_weak
)
4948 sym_sec
= bfd_und_section_ptr
;
4950 else if ((h
->root
.type
== bfd_link_hash_defined
4951 || h
->root
.type
== bfd_link_hash_defweak
)
4952 && h
->root
.u
.def
.section
!= NULL
4953 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4955 symval
= h
->root
.u
.def
.value
;
4956 sym_sec
= h
->root
.u
.def
.section
;
4961 if (h
->type
!= STT_FUNC
)
4963 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4967 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4968 && (sym_sec
->flags
& SEC_MERGE
))
4970 /* At this stage in linking, no SEC_MERGE symbol has been
4971 adjusted, so all references to such symbols need to be
4972 passed through _bfd_merged_section_offset. (Later, in
4973 relocate_section, all SEC_MERGE symbols *except* for
4974 section symbols have been adjusted.)
4976 gas may reduce relocations against symbols in SEC_MERGE
4977 sections to a relocation against the section symbol when
4978 the original addend was zero. When the reloc is against
4979 a section symbol we should include the addend in the
4980 offset passed to _bfd_merged_section_offset, since the
4981 location of interest is the original symbol. On the
4982 other hand, an access to "sym+addend" where "sym" is not
4983 a section symbol should not include the addend; Such an
4984 access is presumed to be an offset from "sym"; The
4985 location of interest is just "sym". */
4986 if (symtype
== STT_SECTION
)
4987 symval
+= rel
->r_addend
;
4989 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4990 elf_section_data (sym_sec
)->sec_info
,
4993 if (symtype
!= STT_SECTION
)
4994 symval
+= rel
->r_addend
;
4997 symval
+= rel
->r_addend
;
4999 symval
+= sec_addr (sym_sec
);
5001 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
5002 max_alignment
, reserve_size
, again
,
5003 &pcgp_relocs
, undefined_weak
))
5007 /* Resolve R_RISCV_DELETE relocations. */
5008 if (!riscv_relax_resolve_delete_relocs (abfd
, sec
, info
, relocs
))
5014 if (relocs
!= data
->relocs
)
5016 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
5022 # define PRSTATUS_SIZE 204
5023 # define PRSTATUS_OFFSET_PR_CURSIG 12
5024 # define PRSTATUS_OFFSET_PR_PID 24
5025 # define PRSTATUS_OFFSET_PR_REG 72
5026 # define ELF_GREGSET_T_SIZE 128
5027 # define PRPSINFO_SIZE 128
5028 # define PRPSINFO_OFFSET_PR_PID 16
5029 # define PRPSINFO_OFFSET_PR_FNAME 32
5030 # define PRPSINFO_OFFSET_PR_PSARGS 48
5031 # define PRPSINFO_PR_FNAME_LENGTH 16
5032 # define PRPSINFO_PR_PSARGS_LENGTH 80
5034 # define PRSTATUS_SIZE 376
5035 # define PRSTATUS_OFFSET_PR_CURSIG 12
5036 # define PRSTATUS_OFFSET_PR_PID 32
5037 # define PRSTATUS_OFFSET_PR_REG 112
5038 # define ELF_GREGSET_T_SIZE 256
5039 # define PRPSINFO_SIZE 136
5040 # define PRPSINFO_OFFSET_PR_PID 24
5041 # define PRPSINFO_OFFSET_PR_FNAME 40
5042 # define PRPSINFO_OFFSET_PR_PSARGS 56
5043 # define PRPSINFO_PR_FNAME_LENGTH 16
5044 # define PRPSINFO_PR_PSARGS_LENGTH 80
5047 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
5048 before the generic code in elf.c. By checking the compiler defines we
5049 only perform any action here if the generic code would otherwise not be
5050 able to help us. The intention is that bare metal core dumps (where the
5051 prstatus_t and/or prpsinfo_t might not be available) will use this code,
5052 while non bare metal tools will use the generic elf code. */
5055 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
5056 char *buf ATTRIBUTE_UNUSED
,
5057 int *bufsiz ATTRIBUTE_UNUSED
,
5058 int note_type ATTRIBUTE_UNUSED
, ...)
5065 #if !defined (HAVE_PRPSINFO_T)
5068 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5071 va_start (ap
, note_type
);
5072 memset (data
, 0, sizeof (data
));
5073 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5074 PRPSINFO_PR_FNAME_LENGTH
);
5075 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5077 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5078 -Wstringop-truncation:
5079 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5081 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5083 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5084 PRPSINFO_PR_PSARGS_LENGTH
);
5085 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5089 return elfcore_write_note (abfd
, buf
, bufsiz
,
5090 "CORE", note_type
, data
, sizeof (data
));
5092 #endif /* !HAVE_PRPSINFO_T */
5094 #if !defined (HAVE_PRSTATUS_T)
5097 char data
[PRSTATUS_SIZE
];
5103 va_start (ap
, note_type
);
5104 memset (data
, 0, sizeof(data
));
5105 pid
= va_arg (ap
, long);
5106 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5107 cursig
= va_arg (ap
, int);
5108 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5109 greg
= va_arg (ap
, const void *);
5110 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5111 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5113 return elfcore_write_note (abfd
, buf
, bufsiz
,
5114 "CORE", note_type
, data
, sizeof (data
));
5116 #endif /* !HAVE_PRSTATUS_T */
5120 /* Support for core dump NOTE sections. */
5123 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5125 switch (note
->descsz
)
5130 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5132 elf_tdata (abfd
)->core
->signal
5133 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5136 elf_tdata (abfd
)->core
->lwpid
5137 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5141 /* Make a ".reg/999" section. */
5142 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5143 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5147 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5149 switch (note
->descsz
)
5154 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5156 elf_tdata (abfd
)->core
->pid
5157 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5160 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5161 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5162 PRPSINFO_PR_FNAME_LENGTH
);
5165 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5166 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5167 PRPSINFO_PR_PSARGS_LENGTH
);
5171 /* Note that for some reason, a spurious space is tacked
5172 onto the end of the args in some (at least one anyway)
5173 implementations, so strip it off if it exists. */
5176 char *command
= elf_tdata (abfd
)->core
->command
;
5177 int n
= strlen (command
);
5179 if (0 < n
&& command
[n
- 1] == ' ')
5180 command
[n
- 1] = '\0';
5186 /* Set the right mach type. */
5189 riscv_elf_object_p (bfd
*abfd
)
5191 /* There are only two mach types in RISCV currently. */
5192 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5193 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5194 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5196 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5201 /* Determine whether an object attribute tag takes an integer, a
5205 riscv_elf_obj_attrs_arg_type (int tag
)
5207 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5210 /* Do not choose mapping symbols as a function name. */
5212 static bfd_size_type
5213 riscv_maybe_function_sym (const asymbol
*sym
,
5217 if (sym
->flags
& BSF_LOCAL
5218 && riscv_elf_is_mapping_symbols (sym
->name
))
5221 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5224 /* Treat the following cases as target special symbols, they are
5228 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5230 /* PR27584, local and empty symbols. Since they are usually
5231 generated for pcrel relocations. */
5232 return (!strcmp (sym
->name
, "")
5233 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5234 /* PR27916, mapping symbols. */
5235 || riscv_elf_is_mapping_symbols (sym
->name
));
5239 riscv_elf_additional_program_headers (bfd
*abfd
,
5240 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5244 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5245 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5252 riscv_elf_modify_segment_map (bfd
*abfd
,
5253 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5256 struct elf_segment_map
*m
, **pm
;
5259 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5261 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5264 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5265 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5267 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5272 m
= bfd_zalloc (abfd
, amt
);
5276 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5280 /* We want to put it after the PHDR and INTERP segments. */
5281 pm
= &elf_seg_map (abfd
);
5283 && ((*pm
)->p_type
== PT_PHDR
5284 || (*pm
)->p_type
== PT_INTERP
))
5295 /* Merge non-visibility st_other attributes. */
5298 riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
5299 unsigned int st_other
,
5300 bool definition ATTRIBUTE_UNUSED
,
5301 bool dynamic ATTRIBUTE_UNUSED
)
5303 unsigned int isym_sto
= st_other
& ~ELF_ST_VISIBILITY (-1);
5304 unsigned int h_sto
= h
->other
& ~ELF_ST_VISIBILITY (-1);
5306 if (isym_sto
== h_sto
)
5309 if (isym_sto
& ~STO_RISCV_VARIANT_CC
)
5310 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5311 h
->root
.root
.string
, isym_sto
);
5313 if (isym_sto
& STO_RISCV_VARIANT_CC
)
5314 h
->other
|= STO_RISCV_VARIANT_CC
;
5317 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5318 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5319 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5320 #define TARGET_BIG_NAME "elfNN-bigriscv"
5322 #define elf_backend_reloc_type_class riscv_reloc_type_class
5324 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5325 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5326 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5327 #define bfd_elfNN_bfd_merge_private_bfd_data \
5328 _bfd_riscv_elf_merge_private_bfd_data
5329 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5331 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5332 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5333 #define elf_backend_check_relocs riscv_elf_check_relocs
5334 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5335 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5336 #define elf_backend_relocate_section riscv_elf_relocate_section
5337 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5338 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5339 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5340 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5341 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5342 #define elf_backend_object_p riscv_elf_object_p
5343 #define elf_backend_write_core_note riscv_write_core_note
5344 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5345 #define elf_info_to_howto_rel NULL
5346 #define elf_info_to_howto riscv_info_to_howto_rela
5347 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5348 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5349 #define elf_backend_additional_program_headers \
5350 riscv_elf_additional_program_headers
5351 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5352 #define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
5354 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5356 #define elf_backend_can_gc_sections 1
5357 #define elf_backend_can_refcount 1
5358 #define elf_backend_want_got_plt 1
5359 #define elf_backend_plt_readonly 1
5360 #define elf_backend_plt_alignment 4
5361 #define elf_backend_want_plt_sym 1
5362 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5363 #define elf_backend_want_dynrelro 1
5364 #define elf_backend_rela_normal 1
5365 #define elf_backend_default_execstack 0
5367 #undef elf_backend_obj_attrs_vendor
5368 #define elf_backend_obj_attrs_vendor "riscv"
5369 #undef elf_backend_obj_attrs_arg_type
5370 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5371 #undef elf_backend_obj_attrs_section_type
5372 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5373 #undef elf_backend_obj_attrs_section
5374 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5376 #include "elfNN-target.h"