1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2021 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 /* Re-run the relaxations from relax pass 0 if TRUE. */
137 /* The data segment phase, don't relax the section
138 when it is exp_seg_relro_adjust. */
139 int *data_segment_phase
;
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;
408 ret
->restart_relax
= false;
410 /* Create hash table for local ifunc. */
411 ret
->loc_hash_table
= htab_try_create (1024,
412 riscv_elf_local_htab_hash
,
413 riscv_elf_local_htab_eq
,
415 ret
->loc_hash_memory
= objalloc_create ();
416 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
418 riscv_elf_link_hash_table_free (abfd
);
421 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
423 return &ret
->elf
.root
;
426 /* Create the .got section. */
429 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
433 struct elf_link_hash_entry
*h
;
434 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
435 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
437 /* This function may be called more than once. */
438 if (htab
->sgot
!= NULL
)
441 flags
= bed
->dynamic_sec_flags
;
443 s
= bfd_make_section_anyway_with_flags (abfd
,
444 (bed
->rela_plts_and_copies_p
445 ? ".rela.got" : ".rel.got"),
446 (bed
->dynamic_sec_flags
449 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
453 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
455 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
459 /* The first bit of the global offset table is the header. */
460 s
->size
+= bed
->got_header_size
;
462 if (bed
->want_got_plt
)
464 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
466 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
470 /* Reserve room for the header. */
471 s
->size
+= GOTPLT_HEADER_SIZE
;
474 if (bed
->want_got_sym
)
476 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
477 section. We don't do this in the linker script because we don't want
478 to define the symbol if we are not creating a global offset
480 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
481 "_GLOBAL_OFFSET_TABLE_");
482 elf_hash_table (info
)->hgot
= h
;
490 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
491 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
495 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
496 struct bfd_link_info
*info
)
498 struct riscv_elf_link_hash_table
*htab
;
500 htab
= riscv_elf_hash_table (info
);
501 BFD_ASSERT (htab
!= NULL
);
503 if (!riscv_elf_create_got_section (dynobj
, info
))
506 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
509 if (!bfd_link_pic (info
))
511 /* Technically, this section doesn't have contents. It is used as the
512 target of TLS copy relocs, to copy TLS data from shared libraries into
513 the executable. However, if we don't mark it as loadable, then it
514 matches the IS_TBSS test in ldlang.c, and there is no run-time address
515 space allocated for it even though it has SEC_ALLOC. That test is
516 correct for .tbss, but not correct for this section. There is also
517 a second problem that having a section with no contents can only work
518 if it comes after all sections with contents in the same segment,
519 but the linker script does not guarantee that. This is just mixed in
520 with other .tdata.* sections. We can fix both problems by lying and
521 saying that there are contents. This section is expected to be small
522 so this should not cause a significant extra program startup cost. */
524 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
525 (SEC_ALLOC
| SEC_THREAD_LOCAL
526 | SEC_LOAD
| SEC_DATA
528 | SEC_LINKER_CREATED
));
531 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
532 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
538 /* Copy the extra info we tack onto an elf_link_hash_entry. */
541 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
542 struct elf_link_hash_entry
*dir
,
543 struct elf_link_hash_entry
*ind
)
545 struct riscv_elf_link_hash_entry
*edir
, *eind
;
547 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
548 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
550 if (ind
->root
.type
== bfd_link_hash_indirect
551 && dir
->got
.refcount
<= 0)
553 edir
->tls_type
= eind
->tls_type
;
554 eind
->tls_type
= GOT_UNKNOWN
;
556 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
560 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
561 unsigned long symndx
, char tls_type
)
563 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
565 *new_tls_type
|= tls_type
;
566 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
568 (*_bfd_error_handler
)
569 (_("%pB: `%s' accessed both as normal and thread local symbol"),
570 abfd
, h
? h
->root
.root
.string
: "<local>");
577 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
578 struct elf_link_hash_entry
*h
, long symndx
)
580 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
581 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
583 if (htab
->elf
.sgot
== NULL
)
585 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
591 h
->got
.refcount
+= 1;
595 /* This is a global offset table entry for a local symbol. */
596 if (elf_local_got_refcounts (abfd
) == NULL
)
598 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
599 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
601 _bfd_riscv_elf_local_got_tls_type (abfd
)
602 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
604 elf_local_got_refcounts (abfd
) [symndx
] += 1;
610 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
612 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
614 /* We propably can improve the information to tell users that they
615 should be recompile the code with -fPIC or -fPIE, just like what
617 (*_bfd_error_handler
)
618 (_("%pB: relocation %s against `%s' can not be used when making a shared "
619 "object; recompile with -fPIC"),
620 abfd
, r
? r
->name
: _("<unknown>"),
621 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
622 bfd_set_error (bfd_error_bad_value
);
626 /* Look through the relocs for a section during the first phase, and
627 allocate space in the global offset table or procedure linkage
631 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
632 asection
*sec
, const Elf_Internal_Rela
*relocs
)
634 struct riscv_elf_link_hash_table
*htab
;
635 Elf_Internal_Shdr
*symtab_hdr
;
636 struct elf_link_hash_entry
**sym_hashes
;
637 const Elf_Internal_Rela
*rel
;
638 asection
*sreloc
= NULL
;
640 if (bfd_link_relocatable (info
))
643 htab
= riscv_elf_hash_table (info
);
644 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
645 sym_hashes
= elf_sym_hashes (abfd
);
647 if (htab
->elf
.dynobj
== NULL
)
648 htab
->elf
.dynobj
= abfd
;
650 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
653 unsigned int r_symndx
;
654 struct elf_link_hash_entry
*h
;
656 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
657 r_type
= ELFNN_R_TYPE (rel
->r_info
);
659 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
661 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
666 if (r_symndx
< symtab_hdr
->sh_info
)
668 /* A local symbol. */
669 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
674 /* Check relocation against local STT_GNU_IFUNC symbol. */
675 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
677 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
681 /* Fake STT_GNU_IFUNC global symbol. */
682 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
684 h
->type
= STT_GNU_IFUNC
;
688 h
->root
.type
= bfd_link_hash_defined
;
695 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
696 while (h
->root
.type
== bfd_link_hash_indirect
697 || h
->root
.type
== bfd_link_hash_warning
)
698 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
708 case R_RISCV_CALL_PLT
:
710 case R_RISCV_GOT_HI20
:
711 case R_RISCV_PCREL_HI20
:
712 /* Create the ifunc sections, iplt and ipltgot, for static
714 if (h
->type
== STT_GNU_IFUNC
715 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
723 /* It is referenced by a non-shared object. */
729 case R_RISCV_TLS_GD_HI20
:
730 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
731 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
735 case R_RISCV_TLS_GOT_HI20
:
736 if (bfd_link_pic (info
))
737 info
->flags
|= DF_STATIC_TLS
;
738 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
739 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
743 case R_RISCV_GOT_HI20
:
744 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
745 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
750 case R_RISCV_CALL_PLT
:
751 /* These symbol requires a procedure linkage table entry.
752 We actually build the entry in adjust_dynamic_symbol,
753 because these might be a case of linking PIC code without
754 linking in any dynamic objects, in which case we don't
755 need to generate a procedure linkage table after all. */
757 /* If it is a local symbol, then we resolve it directly
758 without creating a PLT entry. */
763 h
->plt
.refcount
+= 1;
766 case R_RISCV_PCREL_HI20
:
768 && h
->type
== STT_GNU_IFUNC
)
771 h
->pointer_equality_needed
= 1;
773 /* We don't use the PCREL_HI20 in the data section,
774 so we always need the plt when it refers to
776 h
->plt
.refcount
+= 1;
782 case R_RISCV_RVC_BRANCH
:
783 case R_RISCV_RVC_JUMP
:
784 /* In shared libraries and pie, these relocs are known
786 if (bfd_link_pic (info
))
790 case R_RISCV_TPREL_HI20
:
791 if (!bfd_link_executable (info
))
792 return bad_static_reloc (abfd
, r_type
, h
);
794 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
798 if (bfd_link_pic (info
))
799 return bad_static_reloc (abfd
, r_type
, h
);
803 case R_RISCV_JUMP_SLOT
:
804 case R_RISCV_RELATIVE
:
812 && (!bfd_link_pic (info
)
813 || h
->type
== STT_GNU_IFUNC
))
815 /* This reloc might not bind locally. */
817 h
->pointer_equality_needed
= 1;
820 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
822 /* We may need a .plt entry if the symbol is a function
823 defined in a shared lib or is a function referenced
824 from the code or read-only section. */
825 h
->plt
.refcount
+= 1;
829 /* If we are creating a shared library, and this is a reloc
830 against a global symbol, or a non PC relative reloc
831 against a local symbol, then we need to copy the reloc
832 into the shared library. However, if we are linking with
833 -Bsymbolic, we do not need to copy a reloc against a
834 global symbol which is defined in an object we are
835 including in the link (i.e., DEF_REGULAR is set). At
836 this point we have not seen all the input files, so it is
837 possible that DEF_REGULAR is not set now but will be set
838 later (it is never cleared). In case of a weak definition,
839 DEF_REGULAR may be cleared later by a strong definition in
840 a shared library. We account for that possibility below by
841 storing information in the relocs_copied field of the hash
842 table entry. A similar situation occurs when creating
843 shared libraries and symbol visibility changes render the
846 If on the other hand, we are creating an executable, we
847 may need to keep relocations for symbols satisfied by a
848 dynamic library if we manage to avoid copy relocs for the
851 Generate dynamic pointer relocation against STT_GNU_IFUNC
852 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
853 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
855 if ((bfd_link_pic (info
)
856 && (sec
->flags
& SEC_ALLOC
) != 0
857 && ((r
!= NULL
&& !r
->pc_relative
)
860 || h
->root
.type
== bfd_link_hash_defweak
861 || !h
->def_regular
))))
862 || (!bfd_link_pic (info
)
863 && (sec
->flags
& SEC_ALLOC
) != 0
865 && (h
->root
.type
== bfd_link_hash_defweak
867 || (!bfd_link_pic (info
)
869 && h
->type
== STT_GNU_IFUNC
870 && (sec
->flags
& SEC_CODE
) == 0))
872 struct elf_dyn_relocs
*p
;
873 struct elf_dyn_relocs
**head
;
875 /* When creating a shared object, we must copy these
876 relocs into the output file. We create a reloc
877 section in dynobj and make room for the reloc. */
880 sreloc
= _bfd_elf_make_dynamic_reloc_section
881 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
882 abfd
, /*rela?*/ true);
888 /* If this is a global symbol, we count the number of
889 relocations we need for this symbol. */
891 head
= &h
->dyn_relocs
;
894 /* Track dynamic relocs needed for local syms too.
895 We really need local syms available to do this
900 Elf_Internal_Sym
*isym
;
902 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
907 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
911 vpp
= &elf_section_data (s
)->local_dynrel
;
912 head
= (struct elf_dyn_relocs
**) vpp
;
916 if (p
== NULL
|| p
->sec
!= sec
)
918 size_t amt
= sizeof *p
;
919 p
= ((struct elf_dyn_relocs
*)
920 bfd_alloc (htab
->elf
.dynobj
, amt
));
931 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
936 case R_RISCV_GNU_VTINHERIT
:
937 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
941 case R_RISCV_GNU_VTENTRY
:
942 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
955 riscv_elf_gc_mark_hook (asection
*sec
,
956 struct bfd_link_info
*info
,
957 Elf_Internal_Rela
*rel
,
958 struct elf_link_hash_entry
*h
,
959 Elf_Internal_Sym
*sym
)
962 switch (ELFNN_R_TYPE (rel
->r_info
))
964 case R_RISCV_GNU_VTINHERIT
:
965 case R_RISCV_GNU_VTENTRY
:
969 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
972 /* Adjust a symbol defined by a dynamic object and referenced by a
973 regular object. The current definition is in some section of the
974 dynamic object, but we're not including those sections. We have to
975 change the definition to something the rest of the link can
979 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
980 struct elf_link_hash_entry
*h
)
982 struct riscv_elf_link_hash_table
*htab
;
983 struct riscv_elf_link_hash_entry
* eh
;
987 htab
= riscv_elf_hash_table (info
);
988 BFD_ASSERT (htab
!= NULL
);
990 dynobj
= htab
->elf
.dynobj
;
992 /* Make sure we know what is going on here. */
993 BFD_ASSERT (dynobj
!= NULL
995 || h
->type
== STT_GNU_IFUNC
999 && !h
->def_regular
)));
1001 /* If this is a function, put it in the procedure linkage table. We
1002 will fill in the contents of the procedure linkage table later
1003 (although we could actually do it here). */
1004 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1006 if (h
->plt
.refcount
<= 0
1007 || (h
->type
!= STT_GNU_IFUNC
1008 && (SYMBOL_CALLS_LOCAL (info
, h
)
1009 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1010 && h
->root
.type
== bfd_link_hash_undefweak
))))
1012 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1013 input file, but the symbol was never referred to by a dynamic
1014 object, or if all references were garbage collected. In such
1015 a case, we don't actually need to build a PLT entry. */
1016 h
->plt
.offset
= (bfd_vma
) -1;
1023 h
->plt
.offset
= (bfd_vma
) -1;
1025 /* If this is a weak symbol, and there is a real definition, the
1026 processor independent code will have arranged for us to see the
1027 real definition first, and we can just use the same value. */
1028 if (h
->is_weakalias
)
1030 struct elf_link_hash_entry
*def
= weakdef (h
);
1031 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1032 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1033 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1037 /* This is a reference to a symbol defined by a dynamic object which
1038 is not a function. */
1040 /* If we are creating a shared library, we must presume that the
1041 only references to the symbol are via the global offset table.
1042 For such cases we need not do anything here; the relocations will
1043 be handled correctly by relocate_section. */
1044 if (bfd_link_pic (info
))
1047 /* If there are no references to this symbol that do not use the
1048 GOT, we don't need to generate a copy reloc. */
1049 if (!h
->non_got_ref
)
1052 /* If -z nocopyreloc was given, we won't generate them either. */
1053 if (info
->nocopyreloc
)
1059 /* If we don't find any dynamic relocs in read-only sections, then
1060 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1061 if (!_bfd_elf_readonly_dynrelocs (h
))
1067 /* We must allocate the symbol in our .dynbss section, which will
1068 become part of the .bss section of the executable. There will be
1069 an entry for this symbol in the .dynsym section. The dynamic
1070 object will contain position independent code, so all references
1071 from the dynamic object to this symbol will go through the global
1072 offset table. The dynamic linker will use the .dynsym entry to
1073 determine the address it must put in the global offset table, so
1074 both the dynamic object and the regular object will refer to the
1075 same memory location for the variable. */
1077 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1078 to copy the initial value out of the dynamic object and into the
1079 runtime process image. We need to remember the offset into the
1080 .rel.bss section we are going to use. */
1081 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1082 if (eh
->tls_type
& ~GOT_NORMAL
)
1084 s
= htab
->sdyntdata
;
1085 srel
= htab
->elf
.srelbss
;
1087 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1089 s
= htab
->elf
.sdynrelro
;
1090 srel
= htab
->elf
.sreldynrelro
;
1094 s
= htab
->elf
.sdynbss
;
1095 srel
= htab
->elf
.srelbss
;
1097 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1099 srel
->size
+= sizeof (ElfNN_External_Rela
);
1103 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1106 /* Allocate space in .plt, .got and associated reloc sections for
1110 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1112 struct bfd_link_info
*info
;
1113 struct riscv_elf_link_hash_table
*htab
;
1114 struct elf_dyn_relocs
*p
;
1116 if (h
->root
.type
== bfd_link_hash_indirect
)
1119 info
= (struct bfd_link_info
*) inf
;
1120 htab
= riscv_elf_hash_table (info
);
1121 BFD_ASSERT (htab
!= NULL
);
1123 /* When we are generating pde, make sure gp symbol is output as a
1124 dynamic symbol. Then ld.so can set the gp register earlier, before
1125 resolving the ifunc. */
1126 if (!bfd_link_pic (info
)
1127 && htab
->elf
.dynamic_sections_created
1128 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1129 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1132 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1133 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1134 if they are defined and referenced in a non-shared object. */
1135 if (h
->type
== STT_GNU_IFUNC
1138 else if (htab
->elf
.dynamic_sections_created
1139 && h
->plt
.refcount
> 0)
1141 /* Make sure this symbol is output as a dynamic symbol.
1142 Undefined weak syms won't yet be marked as dynamic. */
1143 if (h
->dynindx
== -1
1144 && !h
->forced_local
)
1146 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1150 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1152 asection
*s
= htab
->elf
.splt
;
1155 s
->size
= PLT_HEADER_SIZE
;
1157 h
->plt
.offset
= s
->size
;
1159 /* Make room for this entry. */
1160 s
->size
+= PLT_ENTRY_SIZE
;
1162 /* We also need to make an entry in the .got.plt section. */
1163 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1165 /* We also need to make an entry in the .rela.plt section. */
1166 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1168 /* If this symbol is not defined in a regular file, and we are
1169 not generating a shared library, then set the symbol to this
1170 location in the .plt. This is required to make function
1171 pointers compare as equal between the normal executable and
1172 the shared library. */
1173 if (! bfd_link_pic (info
)
1176 h
->root
.u
.def
.section
= s
;
1177 h
->root
.u
.def
.value
= h
->plt
.offset
;
1182 h
->plt
.offset
= (bfd_vma
) -1;
1188 h
->plt
.offset
= (bfd_vma
) -1;
1192 if (h
->got
.refcount
> 0)
1196 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1198 /* Make sure this symbol is output as a dynamic symbol.
1199 Undefined weak syms won't yet be marked as dynamic. */
1200 if (h
->dynindx
== -1
1201 && !h
->forced_local
)
1203 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1208 h
->got
.offset
= s
->size
;
1209 dyn
= htab
->elf
.dynamic_sections_created
;
1210 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1212 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1213 if (tls_type
& GOT_TLS_GD
)
1215 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1216 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1219 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1220 if (tls_type
& GOT_TLS_IE
)
1222 s
->size
+= RISCV_ELF_WORD_BYTES
;
1223 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1228 s
->size
+= RISCV_ELF_WORD_BYTES
;
1229 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1230 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1231 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1235 h
->got
.offset
= (bfd_vma
) -1;
1237 if (h
->dyn_relocs
== NULL
)
1240 /* In the shared -Bsymbolic case, discard space allocated for
1241 dynamic pc-relative relocs against symbols which turn out to be
1242 defined in regular objects. For the normal shared case, discard
1243 space for pc-relative relocs that have become local due to symbol
1244 visibility changes. */
1246 if (bfd_link_pic (info
))
1248 if (SYMBOL_CALLS_LOCAL (info
, h
))
1250 struct elf_dyn_relocs
**pp
;
1252 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1254 p
->count
-= p
->pc_count
;
1263 /* Also discard relocs on undefined weak syms with non-default
1265 if (h
->dyn_relocs
!= NULL
1266 && h
->root
.type
== bfd_link_hash_undefweak
)
1268 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1269 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1270 h
->dyn_relocs
= NULL
;
1272 /* Make sure undefined weak symbols are output as a dynamic
1274 else if (h
->dynindx
== -1
1275 && !h
->forced_local
)
1277 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1284 /* For the non-shared case, discard space for relocs against
1285 symbols which turn out to need copy relocs or are not
1291 || (htab
->elf
.dynamic_sections_created
1292 && (h
->root
.type
== bfd_link_hash_undefweak
1293 || h
->root
.type
== bfd_link_hash_undefined
))))
1295 /* Make sure this symbol is output as a dynamic symbol.
1296 Undefined weak syms won't yet be marked as dynamic. */
1297 if (h
->dynindx
== -1
1298 && !h
->forced_local
)
1300 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1304 /* If that succeeded, we know we'll be keeping all the
1306 if (h
->dynindx
!= -1)
1310 h
->dyn_relocs
= NULL
;
1315 /* Finally, allocate space. */
1316 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1318 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1319 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1325 /* Allocate space in .plt, .got and associated reloc sections for
1326 ifunc dynamic relocs. */
1329 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1332 struct bfd_link_info
*info
;
1334 if (h
->root
.type
== bfd_link_hash_indirect
)
1337 if (h
->root
.type
== bfd_link_hash_warning
)
1338 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1340 info
= (struct bfd_link_info
*) inf
;
1342 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1343 here if it is defined and referenced in a non-shared object. */
1344 if (h
->type
== STT_GNU_IFUNC
1346 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1355 /* Allocate space in .plt, .got and associated reloc sections for
1356 local ifunc dynamic relocs. */
1359 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1361 struct elf_link_hash_entry
*h
1362 = (struct elf_link_hash_entry
*) *slot
;
1364 if (h
->type
!= STT_GNU_IFUNC
1368 || h
->root
.type
!= bfd_link_hash_defined
)
1371 return allocate_ifunc_dynrelocs (h
, inf
);
1375 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1377 struct riscv_elf_link_hash_table
*htab
;
1382 htab
= riscv_elf_hash_table (info
);
1383 BFD_ASSERT (htab
!= NULL
);
1384 dynobj
= htab
->elf
.dynobj
;
1385 BFD_ASSERT (dynobj
!= NULL
);
1387 if (elf_hash_table (info
)->dynamic_sections_created
)
1389 /* Set the contents of the .interp section to the interpreter. */
1390 if (bfd_link_executable (info
) && !info
->nointerp
)
1392 s
= bfd_get_linker_section (dynobj
, ".interp");
1393 BFD_ASSERT (s
!= NULL
);
1394 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1395 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1399 /* Set up .got offsets for local syms, and space for local dynamic
1401 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1403 bfd_signed_vma
*local_got
;
1404 bfd_signed_vma
*end_local_got
;
1405 char *local_tls_type
;
1406 bfd_size_type locsymcount
;
1407 Elf_Internal_Shdr
*symtab_hdr
;
1410 if (! is_riscv_elf (ibfd
))
1413 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1415 struct elf_dyn_relocs
*p
;
1417 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1419 if (!bfd_is_abs_section (p
->sec
)
1420 && bfd_is_abs_section (p
->sec
->output_section
))
1422 /* Input section has been discarded, either because
1423 it is a copy of a linkonce section or due to
1424 linker script /DISCARD/, so we'll be discarding
1427 else if (p
->count
!= 0)
1429 srel
= elf_section_data (p
->sec
)->sreloc
;
1430 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1431 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1432 info
->flags
|= DF_TEXTREL
;
1437 local_got
= elf_local_got_refcounts (ibfd
);
1441 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1442 locsymcount
= symtab_hdr
->sh_info
;
1443 end_local_got
= local_got
+ locsymcount
;
1444 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1446 srel
= htab
->elf
.srelgot
;
1447 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1451 *local_got
= s
->size
;
1452 s
->size
+= RISCV_ELF_WORD_BYTES
;
1453 if (*local_tls_type
& GOT_TLS_GD
)
1454 s
->size
+= RISCV_ELF_WORD_BYTES
;
1455 if (bfd_link_pic (info
)
1456 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1457 srel
->size
+= sizeof (ElfNN_External_Rela
);
1460 *local_got
= (bfd_vma
) -1;
1464 /* Allocate .plt and .got entries and space dynamic relocs for
1466 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1468 /* Allocate .plt and .got entries and space dynamic relocs for
1469 global ifunc symbols. */
1470 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1472 /* Allocate .plt and .got entries and space dynamic relocs for
1473 local ifunc symbols. */
1474 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1476 /* Used to resolve the dynamic relocs overwite problems when
1477 generating static executable. */
1478 if (htab
->elf
.irelplt
)
1479 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1481 if (htab
->elf
.sgotplt
)
1483 struct elf_link_hash_entry
*got
;
1484 got
= elf_link_hash_lookup (elf_hash_table (info
),
1485 "_GLOBAL_OFFSET_TABLE_",
1486 false, false, false);
1488 /* Don't allocate .got.plt section if there are no GOT nor PLT
1489 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1491 || !got
->ref_regular_nonweak
)
1492 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1493 && (htab
->elf
.splt
== NULL
1494 || htab
->elf
.splt
->size
== 0)
1495 && (htab
->elf
.sgot
== NULL
1496 || (htab
->elf
.sgot
->size
1497 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1498 htab
->elf
.sgotplt
->size
= 0;
1501 /* The check_relocs and adjust_dynamic_symbol entry points have
1502 determined the sizes of the various dynamic sections. Allocate
1504 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1506 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1509 if (s
== htab
->elf
.splt
1510 || s
== htab
->elf
.sgot
1511 || s
== htab
->elf
.sgotplt
1512 || s
== htab
->elf
.iplt
1513 || s
== htab
->elf
.igotplt
1514 || s
== htab
->elf
.sdynbss
1515 || s
== htab
->elf
.sdynrelro
1516 || s
== htab
->sdyntdata
)
1518 /* Strip this section if we don't need it; see the
1521 else if (startswith (s
->name
, ".rela"))
1525 /* We use the reloc_count field as a counter if we need
1526 to copy relocs into the output file. */
1532 /* It's not one of our sections. */
1538 /* If we don't need this section, strip it from the
1539 output file. This is mostly to handle .rela.bss and
1540 .rela.plt. We must create both sections in
1541 create_dynamic_sections, because they must be created
1542 before the linker maps input sections to output
1543 sections. The linker does that before
1544 adjust_dynamic_symbol is called, and it is that
1545 function which decides whether anything needs to go
1546 into these sections. */
1547 s
->flags
|= SEC_EXCLUDE
;
1551 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1554 /* Allocate memory for the section contents. Zero the memory
1555 for the benefit of .rela.plt, which has 4 unused entries
1556 at the beginning, and we don't want garbage. */
1557 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1558 if (s
->contents
== NULL
)
1562 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, true);
1566 #define DTP_OFFSET 0x800
1568 /* Return the relocation value for a TLS dtp-relative reloc. */
1571 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1573 /* If tls_sec is NULL, we should have signalled an error already. */
1574 if (elf_hash_table (info
)->tls_sec
== NULL
)
1576 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1579 /* Return the relocation value for a static TLS tp-relative relocation. */
1582 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1584 /* If tls_sec is NULL, we should have signalled an error already. */
1585 if (elf_hash_table (info
)->tls_sec
== NULL
)
1587 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1590 /* Return the global pointer's value, or 0 if it is not in use. */
1593 riscv_global_pointer_value (struct bfd_link_info
*info
)
1595 struct bfd_link_hash_entry
*h
;
1597 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1598 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1601 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1604 /* Emplace a static relocation. */
1606 static bfd_reloc_status_type
1607 perform_relocation (const reloc_howto_type
*howto
,
1608 const Elf_Internal_Rela
*rel
,
1610 asection
*input_section
,
1614 if (howto
->pc_relative
)
1615 value
-= sec_addr (input_section
) + rel
->r_offset
;
1616 value
+= rel
->r_addend
;
1618 switch (ELFNN_R_TYPE (rel
->r_info
))
1621 case R_RISCV_TPREL_HI20
:
1622 case R_RISCV_PCREL_HI20
:
1623 case R_RISCV_GOT_HI20
:
1624 case R_RISCV_TLS_GOT_HI20
:
1625 case R_RISCV_TLS_GD_HI20
:
1626 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1627 return bfd_reloc_overflow
;
1628 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1631 case R_RISCV_LO12_I
:
1632 case R_RISCV_GPREL_I
:
1633 case R_RISCV_TPREL_LO12_I
:
1634 case R_RISCV_TPREL_I
:
1635 case R_RISCV_PCREL_LO12_I
:
1636 value
= ENCODE_ITYPE_IMM (value
);
1639 case R_RISCV_LO12_S
:
1640 case R_RISCV_GPREL_S
:
1641 case R_RISCV_TPREL_LO12_S
:
1642 case R_RISCV_TPREL_S
:
1643 case R_RISCV_PCREL_LO12_S
:
1644 value
= ENCODE_STYPE_IMM (value
);
1648 case R_RISCV_CALL_PLT
:
1649 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1650 return bfd_reloc_overflow
;
1651 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1652 | (ENCODE_ITYPE_IMM (value
) << 32);
1656 if (!VALID_JTYPE_IMM (value
))
1657 return bfd_reloc_overflow
;
1658 value
= ENCODE_JTYPE_IMM (value
);
1661 case R_RISCV_BRANCH
:
1662 if (!VALID_BTYPE_IMM (value
))
1663 return bfd_reloc_overflow
;
1664 value
= ENCODE_BTYPE_IMM (value
);
1667 case R_RISCV_RVC_BRANCH
:
1668 if (!VALID_CBTYPE_IMM (value
))
1669 return bfd_reloc_overflow
;
1670 value
= ENCODE_CBTYPE_IMM (value
);
1673 case R_RISCV_RVC_JUMP
:
1674 if (!VALID_CJTYPE_IMM (value
))
1675 return bfd_reloc_overflow
;
1676 value
= ENCODE_CJTYPE_IMM (value
);
1679 case R_RISCV_RVC_LUI
:
1680 if (RISCV_CONST_HIGH_PART (value
) == 0)
1682 /* Linker relaxation can convert an address equal to or greater than
1683 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1684 valid immediate. We can fix this by converting it to a C.LI. */
1685 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1686 contents
+ rel
->r_offset
);
1687 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1688 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1689 value
= ENCODE_CITYPE_IMM (0);
1691 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1692 return bfd_reloc_overflow
;
1694 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1712 case R_RISCV_32_PCREL
:
1713 case R_RISCV_TLS_DTPREL32
:
1714 case R_RISCV_TLS_DTPREL64
:
1717 case R_RISCV_DELETE
:
1718 return bfd_reloc_ok
;
1721 return bfd_reloc_notsupported
;
1725 if (riscv_is_insn_reloc (howto
))
1726 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1728 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1729 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1730 if (riscv_is_insn_reloc (howto
))
1731 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1733 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1735 return bfd_reloc_ok
;
1738 /* Remember all PC-relative high-part relocs we've encountered to help us
1739 later resolve the corresponding low-part relocs. */
1745 /* Relocation value with addend. */
1747 /* Original reloc type. */
1749 } riscv_pcrel_hi_reloc
;
1751 typedef struct riscv_pcrel_lo_reloc
1753 /* PC value of auipc. */
1755 /* Internal relocation. */
1756 const Elf_Internal_Rela
*reloc
;
1757 /* Record the following information helps to resolve the %pcrel
1758 which cross different input section. For now we build a hash
1759 for pcrel at the start of riscv_elf_relocate_section, and then
1760 free the hash at the end. But riscv_elf_relocate_section only
1761 handles an input section at a time, so that means we can only
1762 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1763 section. Otherwise, we will report dangerous relocation errors
1764 for those %pcrel which are not in the same input section. */
1765 asection
*input_section
;
1766 struct bfd_link_info
*info
;
1767 reloc_howto_type
*howto
;
1769 /* The next riscv_pcrel_lo_reloc. */
1770 struct riscv_pcrel_lo_reloc
*next
;
1771 } riscv_pcrel_lo_reloc
;
1775 /* Hash table for riscv_pcrel_hi_reloc. */
1777 /* Linked list for riscv_pcrel_lo_reloc. */
1778 riscv_pcrel_lo_reloc
*lo_relocs
;
1779 } riscv_pcrel_relocs
;
1782 riscv_pcrel_reloc_hash (const void *entry
)
1784 const riscv_pcrel_hi_reloc
*e
= entry
;
1785 return (hashval_t
)(e
->address
>> 2);
1789 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1791 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1792 return e1
->address
== e2
->address
;
1796 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1798 p
->lo_relocs
= NULL
;
1799 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1800 riscv_pcrel_reloc_eq
, free
);
1801 return p
->hi_relocs
!= NULL
;
1805 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1807 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1811 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1816 htab_delete (p
->hi_relocs
);
1820 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1821 struct bfd_link_info
*info
,
1825 const reloc_howto_type
*howto
)
1827 /* We may need to reference low addreses in PC-relative modes even when the
1828 PC is far away from these addresses. For example, undefweak references
1829 need to produce the address 0 when linked. As 0 is far from the arbitrary
1830 addresses that we can link PC-relative programs at, the linker can't
1831 actually relocate references to those symbols. In order to allow these
1832 programs to work we simply convert the PC-relative auipc sequences to
1833 0-relative lui sequences. */
1834 if (bfd_link_pic (info
))
1837 /* If it's possible to reference the symbol using auipc we do so, as that's
1838 more in the spirit of the PC-relative relocations we're processing. */
1839 bfd_vma offset
= addr
- pc
;
1840 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1843 /* If it's impossible to reference this with a LUI-based offset then don't
1844 bother to convert it at all so users still see the PC-relative relocation
1845 in the truncation message. */
1846 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1849 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
1851 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1852 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1853 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1858 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
,
1864 bfd_vma offset
= absolute
? value
: value
- addr
;
1865 riscv_pcrel_hi_reloc entry
= {addr
, offset
, type
};
1866 riscv_pcrel_hi_reloc
**slot
=
1867 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1869 BFD_ASSERT (*slot
== NULL
);
1870 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1878 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1880 const Elf_Internal_Rela
*reloc
,
1881 asection
*input_section
,
1882 struct bfd_link_info
*info
,
1883 reloc_howto_type
*howto
,
1886 riscv_pcrel_lo_reloc
*entry
;
1887 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1890 *entry
= (riscv_pcrel_lo_reloc
) {addr
, reloc
, input_section
, info
,
1891 howto
, contents
, p
->lo_relocs
};
1892 p
->lo_relocs
= entry
;
1897 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1899 riscv_pcrel_lo_reloc
*r
;
1901 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1903 bfd
*input_bfd
= r
->input_section
->owner
;
1905 riscv_pcrel_hi_reloc search
= {r
->address
, 0, 0};
1906 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1907 /* There may be a risk if the %pcrel_lo with addend refers to
1908 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
1909 so the corresponding %pcrel_lo with addend looks wrong. */
1910 char *string
= NULL
;
1912 string
= _("%pcrel_lo missing matching %pcrel_hi");
1913 else if (entry
->type
== R_RISCV_GOT_HI20
1914 && r
->reloc
->r_addend
!= 0)
1915 string
= _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
1916 else if (RISCV_CONST_HIGH_PART (entry
->value
)
1917 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
))
1919 /* Check the overflow when adding reloc addend. */
1920 if (asprintf (&string
,
1921 _("%%pcrel_lo overflow with an addend, the "
1922 "value of %%pcrel_hi is 0x%" PRIx64
" without "
1923 "any addend, but may be 0x%" PRIx64
" after "
1924 "adding the %%pcrel_lo addend"),
1925 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
1926 (int64_t) RISCV_CONST_HIGH_PART
1927 (entry
->value
+ r
->reloc
->r_addend
)) == -1)
1928 string
= _("%pcrel_lo overflow with an addend");
1933 (*r
->info
->callbacks
->reloc_dangerous
)
1934 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1938 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1939 input_bfd
, r
->contents
);
1945 /* Relocate a RISC-V ELF section.
1947 The RELOCATE_SECTION function is called by the new ELF backend linker
1948 to handle the relocations for a section.
1950 The relocs are always passed as Rela structures.
1952 This function is responsible for adjusting the section contents as
1953 necessary, and (if generating a relocatable output file) adjusting
1954 the reloc addend as necessary.
1956 This function does not have to worry about setting the reloc
1957 address or the reloc symbol index.
1959 LOCAL_SYMS is a pointer to the swapped in local symbols.
1961 LOCAL_SECTIONS is an array giving the section in the input file
1962 corresponding to the st_shndx field of each local symbol.
1964 The global hash table entry for the global symbols can be found
1965 via elf_sym_hashes (input_bfd).
1967 When generating relocatable output, this function must handle
1968 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1969 going to be the section symbol corresponding to the output
1970 section, which means that the addend must be adjusted
1974 riscv_elf_relocate_section (bfd
*output_bfd
,
1975 struct bfd_link_info
*info
,
1977 asection
*input_section
,
1979 Elf_Internal_Rela
*relocs
,
1980 Elf_Internal_Sym
*local_syms
,
1981 asection
**local_sections
)
1983 Elf_Internal_Rela
*rel
;
1984 Elf_Internal_Rela
*relend
;
1985 riscv_pcrel_relocs pcrel_relocs
;
1987 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1988 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1989 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1990 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1993 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1996 relend
= relocs
+ input_section
->reloc_count
;
1997 for (rel
= relocs
; rel
< relend
; rel
++)
1999 unsigned long r_symndx
;
2000 struct elf_link_hash_entry
*h
;
2001 Elf_Internal_Sym
*sym
;
2004 bfd_reloc_status_type r
= bfd_reloc_ok
;
2005 const char *name
= NULL
;
2006 bfd_vma off
, ie_off
;
2007 bool unresolved_reloc
, is_ie
= false;
2008 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
2009 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
2010 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2011 const char *msg
= NULL
;
2012 char *msg_buf
= NULL
;
2013 bool resolved_to_zero
;
2016 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
2019 /* This is a final link. */
2020 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
2024 unresolved_reloc
= false;
2025 if (r_symndx
< symtab_hdr
->sh_info
)
2027 sym
= local_syms
+ r_symndx
;
2028 sec
= local_sections
[r_symndx
];
2029 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2031 /* Relocate against local STT_GNU_IFUNC symbol. */
2032 if (!bfd_link_relocatable (info
)
2033 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2035 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2039 /* Set STT_GNU_IFUNC symbol value. */
2040 h
->root
.u
.def
.value
= sym
->st_value
;
2041 h
->root
.u
.def
.section
= sec
;
2046 bool warned
, ignored
;
2048 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2049 r_symndx
, symtab_hdr
, sym_hashes
,
2051 unresolved_reloc
, warned
, ignored
);
2054 /* To avoid generating warning messages about truncated
2055 relocations, set the relocation's address to be the same as
2056 the start of this section. */
2057 if (input_section
->output_section
!= NULL
)
2058 relocation
= input_section
->output_section
->vma
;
2064 if (sec
!= NULL
&& discarded_section (sec
))
2065 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2066 rel
, 1, relend
, howto
, 0, contents
);
2068 if (bfd_link_relocatable (info
))
2071 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2072 it here if it is defined in a non-shared object. */
2074 && h
->type
== STT_GNU_IFUNC
2077 asection
*plt
, *base_got
;
2079 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2081 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2082 STT_GNU_IFUNC symbol as STT_FUNC. */
2083 if (elf_section_type (input_section
) == SHT_NOTE
)
2086 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2087 sections because such sections are not SEC_ALLOC and
2088 thus ld.so will not process them. */
2089 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2094 else if (h
->plt
.offset
== (bfd_vma
) -1
2095 /* The following relocation may not need the .plt entries
2096 when all references to a STT_GNU_IFUNC symbols are done
2097 via GOT or static function pointers. */
2098 && r_type
!= R_RISCV_32
2099 && r_type
!= R_RISCV_64
2100 && r_type
!= R_RISCV_HI20
2101 && r_type
!= R_RISCV_GOT_HI20
2102 && r_type
!= R_RISCV_LO12_I
2103 && r_type
!= R_RISCV_LO12_S
)
2104 goto bad_ifunc_reloc
;
2106 /* STT_GNU_IFUNC symbol must go through PLT. */
2107 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2108 relocation
= plt
->output_section
->vma
2109 + plt
->output_offset
2116 if (rel
->r_addend
!= 0)
2118 if (h
->root
.root
.string
)
2119 name
= h
->root
.root
.string
;
2121 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2124 /* xgettext:c-format */
2125 (_("%pB: relocation %s against STT_GNU_IFUNC "
2126 "symbol `%s' has non-zero addend: %" PRId64
),
2127 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2128 bfd_set_error (bfd_error_bad_value
);
2132 /* Generate dynamic relocation only when there is a non-GOT
2133 reference in a shared object or there is no PLT. */
2134 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2135 || h
->plt
.offset
== (bfd_vma
) -1)
2137 Elf_Internal_Rela outrel
;
2140 /* Need a dynamic relocation to get the real function
2142 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2146 if (outrel
.r_offset
== (bfd_vma
) -1
2147 || outrel
.r_offset
== (bfd_vma
) -2)
2150 outrel
.r_offset
+= input_section
->output_section
->vma
2151 + input_section
->output_offset
;
2153 if (h
->dynindx
== -1
2155 || bfd_link_executable (info
))
2157 info
->callbacks
->minfo
2158 (_("Local IFUNC function `%s' in %pB\n"),
2159 h
->root
.root
.string
,
2160 h
->root
.u
.def
.section
->owner
);
2162 /* This symbol is resolved locally. */
2163 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2164 outrel
.r_addend
= h
->root
.u
.def
.value
2165 + h
->root
.u
.def
.section
->output_section
->vma
2166 + h
->root
.u
.def
.section
->output_offset
;
2170 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2171 outrel
.r_addend
= 0;
2174 /* Dynamic relocations are stored in
2175 1. .rela.ifunc section in PIC object.
2176 2. .rela.got section in dynamic executable.
2177 3. .rela.iplt section in static executable. */
2178 if (bfd_link_pic (info
))
2179 sreloc
= htab
->elf
.irelifunc
;
2180 else if (htab
->elf
.splt
!= NULL
)
2181 sreloc
= htab
->elf
.srelgot
;
2183 sreloc
= htab
->elf
.irelplt
;
2185 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2187 /* If this reloc is against an external symbol, we
2188 do not want to fiddle with the addend. Otherwise,
2189 we need to include the symbol value so that it
2190 becomes an addend for the dynamic reloc. For an
2191 internal symbol, we have updated addend. */
2196 case R_RISCV_GOT_HI20
:
2197 base_got
= htab
->elf
.sgot
;
2198 off
= h
->got
.offset
;
2200 if (base_got
== NULL
)
2203 if (off
== (bfd_vma
) -1)
2207 /* We can't use h->got.offset here to save state, or
2208 even just remember the offset, as finish_dynamic_symbol
2209 would use that as offset into .got. */
2211 if (htab
->elf
.splt
!= NULL
)
2213 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2215 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2216 base_got
= htab
->elf
.sgotplt
;
2220 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2221 off
= plt_idx
* GOT_ENTRY_SIZE
;
2222 base_got
= htab
->elf
.igotplt
;
2225 if (h
->dynindx
== -1
2229 /* This references the local definition. We must
2230 initialize this entry in the global offset table.
2231 Since the offset must always be a multiple of 8,
2232 we use the least significant bit to record
2233 whether we have initialized it already.
2235 When doing a dynamic link, we create a .rela.got
2236 relocation entry to initialize the value. This
2237 is done in the finish_dynamic_symbol routine. */
2242 bfd_put_NN (output_bfd
, relocation
,
2243 base_got
->contents
+ off
);
2244 /* Note that this is harmless for the case,
2245 as -1 | 1 still is -1. */
2251 relocation
= base_got
->output_section
->vma
2252 + base_got
->output_offset
+ off
;
2254 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2257 r
= bfd_reloc_overflow
;
2261 case R_RISCV_CALL_PLT
:
2263 case R_RISCV_LO12_I
:
2264 case R_RISCV_LO12_S
:
2267 case R_RISCV_PCREL_HI20
:
2268 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2271 r
= bfd_reloc_overflow
;
2276 if (h
->root
.root
.string
)
2277 name
= h
->root
.root
.string
;
2279 /* The entry of local ifunc is fake in global hash table,
2280 we should find the name by the original local symbol. */
2281 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2284 /* xgettext:c-format */
2285 (_("%pB: relocation %s against STT_GNU_IFUNC "
2286 "symbol `%s' isn't supported"), input_bfd
,
2288 bfd_set_error (bfd_error_bad_value
);
2295 name
= h
->root
.root
.string
;
2298 name
= (bfd_elf_string_from_elf_section
2299 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2300 if (name
== NULL
|| *name
== '\0')
2301 name
= bfd_section_name (sec
);
2304 resolved_to_zero
= (h
!= NULL
2305 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2311 case R_RISCV_TPREL_ADD
:
2313 case R_RISCV_JUMP_SLOT
:
2314 case R_RISCV_RELATIVE
:
2315 /* These require nothing of us at all. */
2319 case R_RISCV_BRANCH
:
2320 case R_RISCV_RVC_BRANCH
:
2321 case R_RISCV_RVC_LUI
:
2322 case R_RISCV_LO12_I
:
2323 case R_RISCV_LO12_S
:
2328 case R_RISCV_32_PCREL
:
2329 case R_RISCV_DELETE
:
2330 /* These require no special handling beyond perform_relocation. */
2333 case R_RISCV_GOT_HI20
:
2338 off
= h
->got
.offset
;
2339 BFD_ASSERT (off
!= (bfd_vma
) -1);
2340 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2341 pic
= bfd_link_pic (info
);
2343 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2344 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
2346 /* This is actually a static link, or it is a
2347 -Bsymbolic link and the symbol is defined
2348 locally, or the symbol was forced to be local
2349 because of a version file. We must initialize
2350 this entry in the global offset table. Since the
2351 offset must always be a multiple of the word size,
2352 we use the least significant bit to record whether
2353 we have initialized it already.
2355 When doing a dynamic link, we create a .rela.got
2356 relocation entry to initialize the value. This
2357 is done in the finish_dynamic_symbol routine. */
2362 bfd_put_NN (output_bfd
, relocation
,
2363 htab
->elf
.sgot
->contents
+ off
);
2368 unresolved_reloc
= false;
2372 BFD_ASSERT (local_got_offsets
!= NULL
2373 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2375 off
= local_got_offsets
[r_symndx
];
2377 /* The offset must always be a multiple of the word size.
2378 So, we can use the least significant bit to record
2379 whether we have already processed this entry. */
2384 if (bfd_link_pic (info
))
2387 Elf_Internal_Rela outrel
;
2389 /* We need to generate a R_RISCV_RELATIVE reloc
2390 for the dynamic linker. */
2391 s
= htab
->elf
.srelgot
;
2392 BFD_ASSERT (s
!= NULL
);
2394 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2396 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2397 outrel
.r_addend
= relocation
;
2399 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2402 bfd_put_NN (output_bfd
, relocation
,
2403 htab
->elf
.sgot
->contents
+ off
);
2404 local_got_offsets
[r_symndx
] |= 1;
2408 if (rel
->r_addend
!= 0)
2410 msg
= _("The addend isn't allowed for R_RISCV_GOT_HI20");
2411 r
= bfd_reloc_dangerous
;
2415 /* Address of got entry. */
2416 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2417 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
,
2418 relocation
, contents
,
2420 /* Update howto if relocation is changed. */
2421 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2422 ELFNN_R_TYPE (rel
->r_info
));
2424 r
= bfd_reloc_notsupported
;
2425 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2428 r
= bfd_reloc_overflow
;
2437 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2438 contents
+ rel
->r_offset
);
2439 relocation
= old_value
+ relocation
;
2449 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2450 contents
+ rel
->r_offset
);
2451 relocation
= old_value
- relocation
;
2456 case R_RISCV_CALL_PLT
:
2457 /* Handle a call to an undefined weak function. This won't be
2458 relaxed, so we have to handle it here. */
2459 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2460 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2462 /* We can use x0 as the base register. */
2463 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2464 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2465 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2466 /* Set the relocation value so that we get 0 after the pc
2467 relative adjustment. */
2468 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2473 case R_RISCV_RVC_JUMP
:
2474 /* This line has to match the check in _bfd_riscv_relax_section. */
2475 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2477 /* Refer to the PLT entry. */
2478 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2479 unresolved_reloc
= false;
2483 case R_RISCV_TPREL_HI20
:
2484 relocation
= tpoff (info
, relocation
);
2487 case R_RISCV_TPREL_LO12_I
:
2488 case R_RISCV_TPREL_LO12_S
:
2489 relocation
= tpoff (info
, relocation
);
2492 case R_RISCV_TPREL_I
:
2493 case R_RISCV_TPREL_S
:
2494 relocation
= tpoff (info
, relocation
);
2495 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2497 /* We can use tp as the base register. */
2498 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2499 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2500 insn
|= X_TP
<< OP_SH_RS1
;
2501 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2504 r
= bfd_reloc_overflow
;
2507 case R_RISCV_GPREL_I
:
2508 case R_RISCV_GPREL_S
:
2510 bfd_vma gp
= riscv_global_pointer_value (info
);
2511 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2512 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2514 /* We can use x0 or gp as the base register. */
2515 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2516 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2519 rel
->r_addend
-= gp
;
2520 insn
|= X_GP
<< OP_SH_RS1
;
2522 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2525 r
= bfd_reloc_overflow
;
2529 case R_RISCV_PCREL_HI20
:
2530 absolute
= riscv_zero_pcrel_hi_reloc (rel
, info
, pc
, relocation
,
2532 /* Update howto if relocation is changed. */
2533 howto
= riscv_elf_rtype_to_howto (input_bfd
,
2534 ELFNN_R_TYPE (rel
->r_info
));
2536 r
= bfd_reloc_notsupported
;
2537 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2538 relocation
+ rel
->r_addend
,
2540 r
= bfd_reloc_overflow
;
2543 case R_RISCV_PCREL_LO12_I
:
2544 case R_RISCV_PCREL_LO12_S
:
2545 /* We don't allow section symbols plus addends as the auipc address,
2546 because then riscv_relax_delete_bytes would have to search through
2547 all relocs to update these addends. This is also ambiguous, as
2548 we do allow offsets to be added to the target address, which are
2549 not to be used to find the auipc address. */
2550 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2551 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2554 msg
= _("%pcrel_lo section symbol with an addend");
2555 r
= bfd_reloc_dangerous
;
2559 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, relocation
, rel
,
2560 input_section
, info
, howto
,
2563 r
= bfd_reloc_overflow
;
2566 case R_RISCV_TLS_DTPREL32
:
2567 case R_RISCV_TLS_DTPREL64
:
2568 relocation
= dtpoff (info
, relocation
);
2573 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2576 if ((bfd_link_pic (info
)
2578 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2579 && !resolved_to_zero
)
2580 || h
->root
.type
!= bfd_link_hash_undefweak
)
2581 && (!howto
->pc_relative
2582 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2583 || (!bfd_link_pic (info
)
2589 || h
->root
.type
== bfd_link_hash_undefweak
2590 || h
->root
.type
== bfd_link_hash_undefined
)))
2592 Elf_Internal_Rela outrel
;
2594 bool skip_static_relocation
, skip_dynamic_relocation
;
2596 /* When generating a shared object, these relocations
2597 are copied into the output file to be resolved at run
2601 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2603 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2604 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2605 outrel
.r_offset
+= sec_addr (input_section
);
2607 if (skip_dynamic_relocation
)
2608 memset (&outrel
, 0, sizeof outrel
);
2609 else if (h
!= NULL
&& h
->dynindx
!= -1
2610 && !(bfd_link_pic (info
)
2611 && SYMBOLIC_BIND (info
, h
)
2614 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2615 outrel
.r_addend
= rel
->r_addend
;
2619 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2620 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2623 sreloc
= elf_section_data (input_section
)->sreloc
;
2624 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2625 if (skip_static_relocation
)
2630 case R_RISCV_TLS_GOT_HI20
:
2634 case R_RISCV_TLS_GD_HI20
:
2637 off
= h
->got
.offset
;
2642 off
= local_got_offsets
[r_symndx
];
2643 local_got_offsets
[r_symndx
] |= 1;
2646 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2647 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2648 /* If this symbol is referenced by both GD and IE TLS, the IE
2649 reference's GOT slot follows the GD reference's slots. */
2651 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2652 ie_off
= 2 * GOT_ENTRY_SIZE
;
2658 Elf_Internal_Rela outrel
;
2660 bool need_relocs
= false;
2662 if (htab
->elf
.srelgot
== NULL
)
2668 dyn
= htab
->elf
.dynamic_sections_created
;
2669 pic
= bfd_link_pic (info
);
2671 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2672 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2676 /* The GOT entries have not been initialized yet. Do it
2677 now, and emit any relocations. */
2678 if ((bfd_link_pic (info
) || indx
!= 0)
2680 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2681 || h
->root
.type
!= bfd_link_hash_undefweak
))
2684 if (tls_type
& GOT_TLS_GD
)
2688 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2689 outrel
.r_addend
= 0;
2690 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2691 bfd_put_NN (output_bfd
, 0,
2692 htab
->elf
.sgot
->contents
+ off
);
2693 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2696 BFD_ASSERT (! unresolved_reloc
);
2697 bfd_put_NN (output_bfd
,
2698 dtpoff (info
, relocation
),
2699 (htab
->elf
.sgot
->contents
2700 + off
+ RISCV_ELF_WORD_BYTES
));
2704 bfd_put_NN (output_bfd
, 0,
2705 (htab
->elf
.sgot
->contents
2706 + off
+ RISCV_ELF_WORD_BYTES
));
2707 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2708 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2709 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2714 /* If we are not emitting relocations for a
2715 general dynamic reference, then we must be in a
2716 static link or an executable link with the
2717 symbol binding locally. Mark it as belonging
2718 to module 1, the executable. */
2719 bfd_put_NN (output_bfd
, 1,
2720 htab
->elf
.sgot
->contents
+ off
);
2721 bfd_put_NN (output_bfd
,
2722 dtpoff (info
, relocation
),
2723 (htab
->elf
.sgot
->contents
2724 + off
+ RISCV_ELF_WORD_BYTES
));
2728 if (tls_type
& GOT_TLS_IE
)
2732 bfd_put_NN (output_bfd
, 0,
2733 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2734 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2736 outrel
.r_addend
= 0;
2738 outrel
.r_addend
= tpoff (info
, relocation
);
2739 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2740 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2744 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2745 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2750 BFD_ASSERT (off
< (bfd_vma
) -2);
2751 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2752 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2755 r
= bfd_reloc_overflow
;
2756 unresolved_reloc
= false;
2760 r
= bfd_reloc_notsupported
;
2763 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2764 because such sections are not SEC_ALLOC and thus ld.so will
2765 not process them. */
2766 if (unresolved_reloc
2767 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2769 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2770 rel
->r_offset
) != (bfd_vma
) -1)
2775 case R_RISCV_RVC_JUMP
:
2776 if (asprintf (&msg_buf
,
2777 _("%%X%%P: relocation %s against `%s' can "
2778 "not be used when making a shared object; "
2779 "recompile with -fPIC\n"),
2781 h
->root
.root
.string
) == -1)
2786 if (asprintf (&msg_buf
,
2787 _("%%X%%P: unresolvable %s relocation against "
2790 h
->root
.root
.string
) == -1)
2796 r
= bfd_reloc_notsupported
;
2800 if (r
== bfd_reloc_ok
)
2801 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2802 input_bfd
, contents
);
2804 /* We should have already detected the error and set message before.
2805 If the error message isn't set since the linker runs out of memory
2806 or we don't set it before, then we should set the default message
2807 with the "internal error" string here. */
2813 case bfd_reloc_overflow
:
2814 info
->callbacks
->reloc_overflow
2815 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2816 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2819 case bfd_reloc_undefined
:
2820 info
->callbacks
->undefined_symbol
2821 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2825 case bfd_reloc_outofrange
:
2827 msg
= _("%X%P: internal error: out of range error\n");
2830 case bfd_reloc_notsupported
:
2832 msg
= _("%X%P: internal error: unsupported relocation error\n");
2835 case bfd_reloc_dangerous
:
2836 /* The error message should already be set. */
2838 msg
= _("dangerous relocation error");
2839 info
->callbacks
->reloc_dangerous
2840 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2844 msg
= _("%X%P: internal error: unknown error\n");
2848 /* Do not report error message for the dangerous relocation again. */
2849 if (msg
&& r
!= bfd_reloc_dangerous
)
2850 info
->callbacks
->einfo (msg
);
2852 /* Free the unused `msg_buf`. */
2855 /* We already reported the error via a callback, so don't try to report
2856 it again by returning false. That leads to spurious errors. */
2861 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2863 riscv_free_pcrel_relocs (&pcrel_relocs
);
2867 /* Finish up dynamic symbol handling. We set the contents of various
2868 dynamic sections here. */
2871 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2872 struct bfd_link_info
*info
,
2873 struct elf_link_hash_entry
*h
,
2874 Elf_Internal_Sym
*sym
)
2876 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2877 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2879 if (h
->plt
.offset
!= (bfd_vma
) -1)
2881 /* We've decided to create a PLT entry for this symbol. */
2883 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2884 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2885 Elf_Internal_Rela rela
;
2886 asection
*plt
, *gotplt
, *relplt
;
2888 /* When building a static executable, use .iplt, .igot.plt and
2889 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2890 if (htab
->elf
.splt
!= NULL
)
2892 plt
= htab
->elf
.splt
;
2893 gotplt
= htab
->elf
.sgotplt
;
2894 relplt
= htab
->elf
.srelplt
;
2898 plt
= htab
->elf
.iplt
;
2899 gotplt
= htab
->elf
.igotplt
;
2900 relplt
= htab
->elf
.irelplt
;
2903 /* This symbol has an entry in the procedure linkage table. Set
2905 if ((h
->dynindx
== -1
2906 && !((h
->forced_local
|| bfd_link_executable (info
))
2908 && h
->type
== STT_GNU_IFUNC
))
2914 /* Calculate the address of the PLT header. */
2915 header_address
= sec_addr (plt
);
2917 /* Calculate the index of the entry and the offset of .got.plt entry.
2918 For static executables, we don't reserve anything. */
2919 if (plt
== htab
->elf
.splt
)
2921 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2922 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2926 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2927 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2930 /* Calculate the address of the .got.plt entry. */
2931 got_address
= sec_addr (gotplt
) + got_offset
;
2933 /* Find out where the .plt entry should go. */
2934 loc
= plt
->contents
+ h
->plt
.offset
;
2936 /* Fill in the PLT entry itself. */
2937 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2938 header_address
+ h
->plt
.offset
,
2942 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2943 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
2945 /* Fill in the initial value of the .got.plt entry. */
2946 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2947 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2949 rela
.r_offset
= got_address
;
2951 if (h
->dynindx
== -1
2952 || ((bfd_link_executable (info
)
2953 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2955 && h
->type
== STT_GNU_IFUNC
))
2957 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2958 h
->root
.root
.string
,
2959 h
->root
.u
.def
.section
->owner
);
2961 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2962 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2963 asection
*sec
= h
->root
.u
.def
.section
;
2964 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2965 rela
.r_addend
= h
->root
.u
.def
.value
2966 + sec
->output_section
->vma
2967 + sec
->output_offset
;
2971 /* Fill in the entry in the .rela.plt section. */
2972 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2976 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2977 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2979 if (!h
->def_regular
)
2981 /* Mark the symbol as undefined, rather than as defined in
2982 the .plt section. Leave the value alone. */
2983 sym
->st_shndx
= SHN_UNDEF
;
2984 /* If the symbol is weak, we do need to clear the value.
2985 Otherwise, the PLT entry would provide a definition for
2986 the symbol even if the symbol wasn't defined anywhere,
2987 and so the symbol would never be NULL. */
2988 if (!h
->ref_regular_nonweak
)
2993 if (h
->got
.offset
!= (bfd_vma
) -1
2994 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2995 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2999 Elf_Internal_Rela rela
;
3000 bool use_elf_append_rela
= true;
3002 /* This symbol has an entry in the GOT. Set it up. */
3004 sgot
= htab
->elf
.sgot
;
3005 srela
= htab
->elf
.srelgot
;
3006 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
3008 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
3010 /* Handle the ifunc symbol in GOT entry. */
3012 && h
->type
== STT_GNU_IFUNC
)
3014 if (h
->plt
.offset
== (bfd_vma
) -1)
3016 /* STT_GNU_IFUNC is referenced without PLT. */
3018 if (htab
->elf
.splt
== NULL
)
3020 /* Use .rela.iplt section to store .got relocations
3021 in static executable. */
3022 srela
= htab
->elf
.irelplt
;
3024 /* Do not use riscv_elf_append_rela to add dynamic
3026 use_elf_append_rela
= false;
3029 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3031 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3032 h
->root
.root
.string
,
3033 h
->root
.u
.def
.section
->owner
);
3035 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3036 rela
.r_addend
= (h
->root
.u
.def
.value
3037 + h
->root
.u
.def
.section
->output_section
->vma
3038 + h
->root
.u
.def
.section
->output_offset
);
3042 /* Generate R_RISCV_NN. */
3043 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3044 BFD_ASSERT (h
->dynindx
!= -1);
3045 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3049 else if (bfd_link_pic (info
))
3051 /* Generate R_RISCV_NN. */
3052 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3053 BFD_ASSERT (h
->dynindx
!= -1);
3054 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3061 if (!h
->pointer_equality_needed
)
3064 /* For non-shared object, we can't use .got.plt, which
3065 contains the real function address if we need pointer
3066 equality. We load the GOT entry with the PLT entry. */
3067 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3068 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3069 + plt
->output_offset
3071 htab
->elf
.sgot
->contents
3072 + (h
->got
.offset
& ~(bfd_vma
) 1));
3076 else if (bfd_link_pic (info
)
3077 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3079 /* If this is a local symbol reference, we just want to emit
3080 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3081 or a pie link, or the symbol was forced to be local because
3082 of a version file. The entry in the global offset table will
3083 already have been initialized in the relocate_section function. */
3084 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3085 asection
*sec
= h
->root
.u
.def
.section
;
3086 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3087 rela
.r_addend
= (h
->root
.u
.def
.value
3088 + sec
->output_section
->vma
3089 + sec
->output_offset
);
3093 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3094 BFD_ASSERT (h
->dynindx
!= -1);
3095 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3099 bfd_put_NN (output_bfd
, 0,
3100 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3102 if (use_elf_append_rela
)
3103 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3106 /* Use riscv_elf_append_rela to add the dynamic relocs into
3107 .rela.iplt may cause the overwrite problems. Since we insert
3108 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3109 but the riscv_elf_append_rela adds the relocs to the place
3110 that are calculated from the reloc_index (in seqential).
3112 One solution is that add these dynamic relocs (GOT IFUNC)
3113 from the last of .rela.iplt section. */
3114 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3115 bfd_byte
*loc
= srela
->contents
3116 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3117 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3123 Elf_Internal_Rela rela
;
3126 /* This symbols needs a copy reloc. Set it up. */
3127 BFD_ASSERT (h
->dynindx
!= -1);
3129 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3130 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3132 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3133 s
= htab
->elf
.sreldynrelro
;
3135 s
= htab
->elf
.srelbss
;
3136 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3139 /* Mark some specially defined symbols as absolute. */
3140 if (h
== htab
->elf
.hdynamic
3141 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3142 sym
->st_shndx
= SHN_ABS
;
3147 /* Finish up local dynamic symbol handling. We set the contents of
3148 various dynamic sections here. */
3151 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3153 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3154 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3156 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3159 /* Finish up the dynamic sections. */
3162 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3163 bfd
*dynobj
, asection
*sdyn
)
3165 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3166 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3167 size_t dynsize
= bed
->s
->sizeof_dyn
;
3168 bfd_byte
*dyncon
, *dynconend
;
3170 dynconend
= sdyn
->contents
+ sdyn
->size
;
3171 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3173 Elf_Internal_Dyn dyn
;
3176 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3181 s
= htab
->elf
.sgotplt
;
3182 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3185 s
= htab
->elf
.srelplt
;
3186 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3189 s
= htab
->elf
.srelplt
;
3190 dyn
.d_un
.d_val
= s
->size
;
3196 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3202 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3203 struct bfd_link_info
*info
)
3207 struct riscv_elf_link_hash_table
*htab
;
3209 htab
= riscv_elf_hash_table (info
);
3210 BFD_ASSERT (htab
!= NULL
);
3211 dynobj
= htab
->elf
.dynobj
;
3213 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3215 if (elf_hash_table (info
)->dynamic_sections_created
)
3220 splt
= htab
->elf
.splt
;
3221 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3223 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3228 /* Fill in the head and tail entries in the procedure linkage table. */
3232 uint32_t plt_header
[PLT_HEADER_INSNS
];
3233 ret
= riscv_make_plt_header (output_bfd
,
3234 sec_addr (htab
->elf
.sgotplt
),
3235 sec_addr (splt
), plt_header
);
3239 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3240 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3242 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3247 if (htab
->elf
.sgotplt
)
3249 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3251 if (bfd_is_abs_section (output_section
))
3253 (*_bfd_error_handler
)
3254 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3258 if (htab
->elf
.sgotplt
->size
> 0)
3260 /* Write the first two entries in .got.plt, needed for the dynamic
3262 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3263 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3264 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3267 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3272 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3274 if (htab
->elf
.sgot
->size
> 0)
3276 /* Set the first entry in the global offset table to the address of
3277 the dynamic section. */
3278 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3279 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3282 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3285 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3286 htab_traverse (htab
->loc_hash_table
,
3287 riscv_elf_finish_local_dynamic_symbol
,
3293 /* Return address for Ith PLT stub in section PLT, for relocation REL
3294 or (bfd_vma) -1 if it should not be included. */
3297 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3298 const arelent
*rel ATTRIBUTE_UNUSED
)
3300 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3303 static enum elf_reloc_type_class
3304 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3305 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3306 const Elf_Internal_Rela
*rela
)
3308 switch (ELFNN_R_TYPE (rela
->r_info
))
3310 case R_RISCV_RELATIVE
:
3311 return reloc_class_relative
;
3312 case R_RISCV_JUMP_SLOT
:
3313 return reloc_class_plt
;
3315 return reloc_class_copy
;
3317 return reloc_class_normal
;
3321 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3325 riscv_float_abi_string (flagword flags
)
3327 switch (flags
& EF_RISCV_FLOAT_ABI
)
3329 case EF_RISCV_FLOAT_ABI_SOFT
:
3330 return "soft-float";
3332 case EF_RISCV_FLOAT_ABI_SINGLE
:
3333 return "single-float";
3335 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3336 return "double-float";
3338 case EF_RISCV_FLOAT_ABI_QUAD
:
3339 return "quad-float";
3346 /* The information of architecture elf attributes. */
3347 static riscv_subset_list_t in_subsets
;
3348 static riscv_subset_list_t out_subsets
;
3349 static riscv_subset_list_t merged_subsets
;
3351 /* Predicator for standard extension. */
3354 riscv_std_ext_p (const char *name
)
3356 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3359 /* Check if the versions are compatible. */
3362 riscv_version_mismatch (bfd
*ibfd
,
3363 struct riscv_subset_t
*in
,
3364 struct riscv_subset_t
*out
)
3366 if (in
== NULL
|| out
== NULL
)
3369 /* Since there are no version conflicts for now, we just report
3370 warning when the versions are mis-matched. */
3371 if (in
->major_version
!= out
->major_version
3372 || in
->minor_version
!= out
->minor_version
)
3374 if ((in
->major_version
== RISCV_UNKNOWN_VERSION
3375 && in
->minor_version
== RISCV_UNKNOWN_VERSION
)
3376 || (out
->major_version
== RISCV_UNKNOWN_VERSION
3377 && out
->minor_version
== RISCV_UNKNOWN_VERSION
))
3379 /* Do not report the warning when the version of input
3380 or output is RISCV_UNKNOWN_VERSION, since the extension
3381 is added implicitly. */
3385 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
3386 "extension, the output version is %d.%d"),
3392 out
->minor_version
);
3394 /* Update the output ISA versions to the newest ones. */
3395 if ((in
->major_version
> out
->major_version
)
3396 || (in
->major_version
== out
->major_version
3397 && in
->minor_version
> out
->minor_version
))
3399 out
->major_version
= in
->major_version
;
3400 out
->minor_version
= in
->minor_version
;
3407 /* Return true if subset is 'i' or 'e'. */
3410 riscv_i_or_e_p (bfd
*ibfd
,
3412 struct riscv_subset_t
*subset
)
3414 if ((strcasecmp (subset
->name
, "e") != 0)
3415 && (strcasecmp (subset
->name
, "i") != 0))
3418 (_("error: %pB: corrupted ISA string '%s'. "
3419 "First letter should be 'i' or 'e' but got '%s'"),
3420 ibfd
, arch
, subset
->name
);
3426 /* Merge standard extensions.
3429 Return FALSE if failed to merge.
3433 `in_arch`: Raw ISA string for input object.
3434 `out_arch`: Raw ISA string for output object.
3435 `pin`: Subset list for input object.
3436 `pout`: Subset list for output object. */
3439 riscv_merge_std_ext (bfd
*ibfd
,
3440 const char *in_arch
,
3441 const char *out_arch
,
3442 struct riscv_subset_t
**pin
,
3443 struct riscv_subset_t
**pout
)
3445 const char *standard_exts
= "mafdqlcbjtpvn";
3447 struct riscv_subset_t
*in
= *pin
;
3448 struct riscv_subset_t
*out
= *pout
;
3450 /* First letter should be 'i' or 'e'. */
3451 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3454 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3457 if (strcasecmp (in
->name
, out
->name
) != 0)
3459 /* TODO: We might allow merge 'i' with 'e'. */
3461 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3462 ibfd
, in
->name
, out
->name
);
3465 else if (!riscv_version_mismatch (ibfd
, in
, out
))
3468 riscv_add_subset (&merged_subsets
,
3469 out
->name
, out
->major_version
, out
->minor_version
);
3474 /* Handle standard extension first. */
3475 for (p
= standard_exts
; *p
; ++p
)
3477 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3478 char find_ext
[2] = {*p
, '\0'};
3479 bool find_in
, find_out
;
3481 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3482 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3484 if (!find_in
&& !find_out
)
3489 && !riscv_version_mismatch (ibfd
, ext_in
, ext_out
))
3492 ext_merged
= find_out
? ext_out
: ext_in
;
3493 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3494 ext_merged
->major_version
, ext_merged
->minor_version
);
3497 /* Skip all standard extensions. */
3498 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3499 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3507 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3508 object subset list. Likewise for POUT and the output object. Return TRUE
3509 on success and FALSE when a conflict is found. */
3512 riscv_merge_multi_letter_ext (bfd
*ibfd
,
3513 riscv_subset_t
**pin
,
3514 riscv_subset_t
**pout
)
3516 riscv_subset_t
*in
= *pin
;
3517 riscv_subset_t
*out
= *pout
;
3518 riscv_subset_t
*tail
;
3524 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3528 /* `in' comes before `out', append `in' and increment. */
3529 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3535 /* `out' comes before `in', append `out' and increment. */
3536 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3537 out
->minor_version
);
3542 /* Both present, check version and increment both. */
3543 if (!riscv_version_mismatch (ibfd
, in
, out
))
3546 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3547 out
->minor_version
);
3555 /* If we're here, either `in' or `out' is running longer than
3556 the other. So, we need to append the corresponding tail. */
3557 tail
= in
? in
: out
;
3560 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3561 tail
->minor_version
);
3569 /* Merge Tag_RISCV_arch attribute. */
3572 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3574 riscv_subset_t
*in
, *out
;
3575 char *merged_arch_str
;
3577 unsigned xlen_in
, xlen_out
;
3578 merged_subsets
.head
= NULL
;
3579 merged_subsets
.tail
= NULL
;
3581 riscv_parse_subset_t rpe_in
;
3582 riscv_parse_subset_t rpe_out
;
3584 /* Only assembler needs to check the default version of ISA, so just set
3585 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
3586 rpe_in
.subset_list
= &in_subsets
;
3587 rpe_in
.error_handler
= _bfd_error_handler
;
3588 rpe_in
.xlen
= &xlen_in
;
3589 rpe_in
.isa_spec
= ISA_SPEC_CLASS_NONE
;
3590 rpe_in
.check_unknown_prefixed_ext
= false;
3592 rpe_out
.subset_list
= &out_subsets
;
3593 rpe_out
.error_handler
= _bfd_error_handler
;
3594 rpe_out
.xlen
= &xlen_out
;
3595 rpe_out
.isa_spec
= ISA_SPEC_CLASS_NONE
;
3596 rpe_out
.check_unknown_prefixed_ext
= false;
3598 if (in_arch
== NULL
&& out_arch
== NULL
)
3601 if (in_arch
== NULL
&& out_arch
!= NULL
)
3604 if (in_arch
!= NULL
&& out_arch
== NULL
)
3607 /* Parse subset from ISA string. */
3608 if (!riscv_parse_subset (&rpe_in
, in_arch
))
3611 if (!riscv_parse_subset (&rpe_out
, out_arch
))
3614 /* Checking XLEN. */
3615 if (xlen_out
!= xlen_in
)
3618 (_("error: %pB: ISA string of input (%s) doesn't match "
3619 "output (%s)"), ibfd
, in_arch
, out_arch
);
3623 /* Merge subset list. */
3624 in
= in_subsets
.head
;
3625 out
= out_subsets
.head
;
3627 /* Merge standard extension. */
3628 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3631 /* Merge all non-single letter extensions with single call. */
3632 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3635 if (xlen_in
!= xlen_out
)
3638 (_("error: %pB: XLEN of input (%u) doesn't match "
3639 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3643 if (xlen_in
!= ARCH_SIZE
)
3646 (_("error: %pB: unsupported XLEN (%u), you might be "
3647 "using wrong emulation"), ibfd
, xlen_in
);
3651 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3653 /* Release the subset lists. */
3654 riscv_release_subset_list (&in_subsets
);
3655 riscv_release_subset_list (&out_subsets
);
3656 riscv_release_subset_list (&merged_subsets
);
3658 return merged_arch_str
;
3661 /* Merge object attributes from IBFD into output_bfd of INFO.
3662 Raise an error if there are conflicting attributes. */
3665 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3667 bfd
*obfd
= info
->output_bfd
;
3668 obj_attribute
*in_attr
;
3669 obj_attribute
*out_attr
;
3671 bool priv_attrs_merged
= false;
3672 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3675 /* Skip linker created files. */
3676 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3679 /* Skip any input that doesn't have an attribute section.
3680 This enables to link object files without attribute section with
3682 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3685 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3687 /* This is the first object. Copy the attributes. */
3688 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3690 out_attr
= elf_known_obj_attributes_proc (obfd
);
3692 /* Use the Tag_null value to indicate the attributes have been
3699 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3700 out_attr
= elf_known_obj_attributes_proc (obfd
);
3702 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3706 case Tag_RISCV_arch
:
3707 if (!out_attr
[Tag_RISCV_arch
].s
)
3708 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3709 else if (in_attr
[Tag_RISCV_arch
].s
3710 && out_attr
[Tag_RISCV_arch
].s
)
3712 /* Check compatible. */
3714 riscv_merge_arch_attr_info (ibfd
,
3715 in_attr
[Tag_RISCV_arch
].s
,
3716 out_attr
[Tag_RISCV_arch
].s
);
3717 if (merged_arch
== NULL
)
3720 out_attr
[Tag_RISCV_arch
].s
= "";
3723 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3727 case Tag_RISCV_priv_spec
:
3728 case Tag_RISCV_priv_spec_minor
:
3729 case Tag_RISCV_priv_spec_revision
:
3730 /* If we have handled the privileged elf attributes, then skip it. */
3731 if (!priv_attrs_merged
)
3733 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3734 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3735 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3736 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3737 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3739 /* Get the privileged spec class from elf attributes. */
3740 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3744 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3749 /* Allow to link the object without the privileged specs. */
3750 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3752 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3753 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3754 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3756 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3757 && in_priv_spec
!= out_priv_spec
)
3760 (_("warning: %pB use privileged spec version %u.%u.%u but "
3761 "the output use version %u.%u.%u"),
3770 /* The privileged spec v1.9.1 can not be linked with others
3771 since the conflicts, so we plan to drop it in a year or
3773 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3774 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3777 (_("warning: privileged spec version 1.9.1 can not be "
3778 "linked with other spec versions"));
3781 /* Update the output privileged spec to the newest one. */
3782 if (in_priv_spec
> out_priv_spec
)
3784 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3785 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3786 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3789 priv_attrs_merged
= true;
3793 case Tag_RISCV_unaligned_access
:
3794 out_attr
[i
].i
|= in_attr
[i
].i
;
3797 case Tag_RISCV_stack_align
:
3798 if (out_attr
[i
].i
== 0)
3799 out_attr
[i
].i
= in_attr
[i
].i
;
3800 else if (in_attr
[i
].i
!= 0
3801 && out_attr
[i
].i
!= 0
3802 && out_attr
[i
].i
!= in_attr
[i
].i
)
3805 (_("error: %pB use %u-byte stack aligned but the output "
3806 "use %u-byte stack aligned"),
3807 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3813 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3816 /* If out_attr was copied from in_attr then it won't have a type yet. */
3817 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3818 out_attr
[i
].type
= in_attr
[i
].type
;
3821 /* Merge Tag_compatibility attributes and any common GNU ones. */
3822 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3825 /* Check for any attributes not known on RISC-V. */
3826 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3831 /* Merge backend specific data from an object file to the output
3832 object file when linking. */
3835 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3837 bfd
*obfd
= info
->output_bfd
;
3838 flagword new_flags
, old_flags
;
3840 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3843 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3845 (*_bfd_error_handler
)
3846 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3847 " target emulation `%s' does not match `%s'"),
3848 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3852 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3855 if (!riscv_merge_attributes (ibfd
, info
))
3858 /* Check to see if the input BFD actually contains any sections. If not,
3859 its flags may not have been initialized either, but it cannot actually
3860 cause any incompatibility. Do not short-circuit dynamic objects; their
3861 section list may be emptied by elf_link_add_object_symbols.
3863 Also check to see if there are no code sections in the input. In this
3864 case, there is no need to check for code specific flags. */
3865 if (!(ibfd
->flags
& DYNAMIC
))
3867 bool null_input_bfd
= true;
3868 bool only_data_sections
= true;
3871 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3873 null_input_bfd
= false;
3875 if ((bfd_section_flags (sec
)
3876 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3877 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3879 only_data_sections
= false;
3884 if (null_input_bfd
|| only_data_sections
)
3888 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3889 old_flags
= elf_elfheader (obfd
)->e_flags
;
3891 if (!elf_flags_init (obfd
))
3893 elf_flags_init (obfd
) = true;
3894 elf_elfheader (obfd
)->e_flags
= new_flags
;
3898 /* Disallow linking different float ABIs. */
3899 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3901 (*_bfd_error_handler
)
3902 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3903 riscv_float_abi_string (new_flags
),
3904 riscv_float_abi_string (old_flags
));
3908 /* Disallow linking RVE and non-RVE. */
3909 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3911 (*_bfd_error_handler
)
3912 (_("%pB: can't link RVE with other target"), ibfd
);
3916 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3917 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3922 bfd_set_error (bfd_error_bad_value
);
3926 /* Delete some bytes from a section while relaxing. */
3929 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3930 struct bfd_link_info
*link_info
)
3932 unsigned int i
, symcount
;
3933 bfd_vma toaddr
= sec
->size
;
3934 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3935 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3936 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3937 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3938 bfd_byte
*contents
= data
->this_hdr
.contents
;
3940 /* Actually delete the bytes. */
3942 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3944 /* Adjust the location of all of the relocs. Note that we need not
3945 adjust the addends, since all PC-relative references must be against
3946 symbols, which we will adjust below. */
3947 for (i
= 0; i
< sec
->reloc_count
; i
++)
3948 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3949 data
->relocs
[i
].r_offset
-= count
;
3951 /* Adjust the local symbols defined in this section. */
3952 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3954 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3955 if (sym
->st_shndx
== sec_shndx
)
3957 /* If the symbol is in the range of memory we just moved, we
3958 have to adjust its value. */
3959 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3960 sym
->st_value
-= count
;
3962 /* If the symbol *spans* the bytes we just deleted (i.e. its
3963 *end* is in the moved bytes but its *start* isn't), then we
3964 must adjust its size.
3966 This test needs to use the original value of st_value, otherwise
3967 we might accidentally decrease size when deleting bytes right
3968 before the symbol. But since deleted relocs can't span across
3969 symbols, we can't have both a st_value and a st_size decrease,
3970 so it is simpler to just use an else. */
3971 else if (sym
->st_value
<= addr
3972 && sym
->st_value
+ sym
->st_size
> addr
3973 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3974 sym
->st_size
-= count
;
3978 /* Now adjust the global symbols defined in this section. */
3979 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3980 - symtab_hdr
->sh_info
);
3982 for (i
= 0; i
< symcount
; i
++)
3984 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3986 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3987 containing the definition of __wrap_SYMBOL, includes a direct
3988 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3989 the same symbol (which is __wrap_SYMBOL), but still exist as two
3990 different symbols in 'sym_hashes', we don't want to adjust
3991 the global symbol __wrap_SYMBOL twice.
3993 The same problem occurs with symbols that are versioned_hidden, as
3994 foo becomes an alias for foo@BAR, and hence they need the same
3996 if (link_info
->wrap_hash
!= NULL
3997 || sym_hash
->versioned
!= unversioned
)
3999 struct elf_link_hash_entry
**cur_sym_hashes
;
4001 /* Loop only over the symbols which have already been checked. */
4002 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
4005 /* If the current symbol is identical to 'sym_hash', that means
4006 the symbol was already adjusted (or at least checked). */
4007 if (*cur_sym_hashes
== sym_hash
)
4010 /* Don't adjust the symbol again. */
4011 if (cur_sym_hashes
< &sym_hashes
[i
])
4015 if ((sym_hash
->root
.type
== bfd_link_hash_defined
4016 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
4017 && sym_hash
->root
.u
.def
.section
== sec
)
4019 /* As above, adjust the value if needed. */
4020 if (sym_hash
->root
.u
.def
.value
> addr
4021 && sym_hash
->root
.u
.def
.value
<= toaddr
)
4022 sym_hash
->root
.u
.def
.value
-= count
;
4024 /* As above, adjust the size if needed. */
4025 else if (sym_hash
->root
.u
.def
.value
<= addr
4026 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4027 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4028 sym_hash
->size
-= count
;
4035 /* A second format for recording PC-relative hi relocations. This stores the
4036 information required to relax them to GP-relative addresses. */
4038 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
4039 struct riscv_pcgp_hi_reloc
4046 bool undefined_weak
;
4047 riscv_pcgp_hi_reloc
*next
;
4050 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
4051 struct riscv_pcgp_lo_reloc
4054 riscv_pcgp_lo_reloc
*next
;
4059 riscv_pcgp_hi_reloc
*hi
;
4060 riscv_pcgp_lo_reloc
*lo
;
4061 } riscv_pcgp_relocs
;
4063 /* Initialize the pcgp reloc info in P. */
4066 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
4073 /* Free the pcgp reloc info in P. */
4076 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
4077 bfd
*abfd ATTRIBUTE_UNUSED
,
4078 asection
*sec ATTRIBUTE_UNUSED
)
4080 riscv_pcgp_hi_reloc
*c
;
4081 riscv_pcgp_lo_reloc
*l
;
4083 for (c
= p
->hi
; c
!= NULL
; )
4085 riscv_pcgp_hi_reloc
*next
= c
->next
;
4090 for (l
= p
->lo
; l
!= NULL
; )
4092 riscv_pcgp_lo_reloc
*next
= l
->next
;
4098 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4099 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4100 relax the corresponding lo part reloc. */
4103 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4104 bfd_vma hi_addend
, bfd_vma hi_addr
,
4105 unsigned hi_sym
, asection
*sym_sec
,
4106 bool undefined_weak
)
4108 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
4111 new->hi_sec_off
= hi_sec_off
;
4112 new->hi_addend
= hi_addend
;
4113 new->hi_addr
= hi_addr
;
4114 new->hi_sym
= hi_sym
;
4115 new->sym_sec
= sym_sec
;
4116 new->undefined_weak
= undefined_weak
;
4122 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4123 This is used by a lo part reloc to find the corresponding hi part reloc. */
4125 static riscv_pcgp_hi_reloc
*
4126 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4128 riscv_pcgp_hi_reloc
*c
;
4130 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4131 if (c
->hi_sec_off
== hi_sec_off
)
4136 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4137 This is used to record relocs that can't be relaxed. */
4140 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4142 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4145 new->hi_sec_off
= hi_sec_off
;
4151 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4152 This is used by a hi part reloc to find the corresponding lo part reloc. */
4155 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4157 riscv_pcgp_lo_reloc
*c
;
4159 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4160 if (c
->hi_sec_off
== hi_sec_off
)
4165 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4166 struct bfd_link_info
*,
4167 Elf_Internal_Rela
*,
4168 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4169 riscv_pcgp_relocs
*,
4170 bool undefined_weak
);
4172 /* Relax AUIPC + JALR into JAL. */
4175 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4176 struct bfd_link_info
*link_info
,
4177 Elf_Internal_Rela
*rel
,
4179 bfd_vma max_alignment
,
4180 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4182 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4183 bool undefined_weak ATTRIBUTE_UNUSED
)
4185 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4186 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4187 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4188 bfd_vma auipc
, jalr
;
4189 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4191 /* If the call crosses section boundaries, an alignment directive could
4192 cause the PC-relative offset to later increase, so we need to add in the
4193 max alignment of any section inclusive from the call to the target.
4194 Otherwise, we only need to use the alignment of the current section. */
4195 if (VALID_JTYPE_IMM (foff
))
4197 if (sym_sec
->output_section
== sec
->output_section
4198 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4199 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4200 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4203 /* See if this function call can be shortened. */
4204 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4207 /* Shorten the function call. */
4208 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4210 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4211 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4212 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4213 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4215 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4216 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4220 /* Relax to C.J[AL] rd, addr. */
4221 r_type
= R_RISCV_RVC_JUMP
;
4222 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4225 else if (VALID_JTYPE_IMM (foff
))
4227 /* Relax to JAL rd, addr. */
4228 r_type
= R_RISCV_JAL
;
4229 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4233 /* Near zero, relax to JALR rd, x0, addr. */
4234 r_type
= R_RISCV_LO12_I
;
4235 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4238 /* Replace the R_RISCV_CALL reloc. */
4239 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4240 /* Replace the AUIPC. */
4241 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4243 /* Delete unnecessary JALR. */
4245 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4249 /* Traverse all output sections and return the max alignment. */
4252 _bfd_riscv_get_max_alignment (asection
*sec
)
4254 unsigned int max_alignment_power
= 0;
4257 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4259 if (o
->alignment_power
> max_alignment_power
)
4260 max_alignment_power
= o
->alignment_power
;
4263 return (bfd_vma
) 1 << max_alignment_power
;
4266 /* Relax non-PIC global variable references to GP-relative references. */
4269 _bfd_riscv_relax_lui (bfd
*abfd
,
4272 struct bfd_link_info
*link_info
,
4273 Elf_Internal_Rela
*rel
,
4275 bfd_vma max_alignment
,
4276 bfd_vma reserve_size
,
4278 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4279 bool undefined_weak
)
4281 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4282 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4283 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4285 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4289 /* If gp and the symbol are in the same output section, which is not the
4290 abs section, then consider only that output section's alignment. */
4291 struct bfd_link_hash_entry
*h
=
4292 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4294 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4295 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4296 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4299 /* Is the reference in range of x0 or gp?
4300 Valid gp range conservatively because of alignment issue. */
4302 || (VALID_ITYPE_IMM (symval
)
4304 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4306 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4308 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4309 switch (ELFNN_R_TYPE (rel
->r_info
))
4311 case R_RISCV_LO12_I
:
4314 /* Change the RS1 to zero. */
4315 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4316 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4317 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4320 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4323 case R_RISCV_LO12_S
:
4326 /* Change the RS1 to zero. */
4327 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4328 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4329 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4332 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4336 /* We can delete the unnecessary LUI and reloc. */
4337 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4339 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4347 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4348 account for this assuming page alignment at worst. In the presence of
4349 RELRO segment the linker aligns it by one page size, therefore sections
4350 after the segment can be moved more than one page. */
4353 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4354 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4355 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4356 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4357 : ELF_MAXPAGESIZE
)))
4359 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4360 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4361 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4362 if (rd
== 0 || rd
== X_SP
)
4365 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4366 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4368 /* Replace the R_RISCV_HI20 reloc. */
4369 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4372 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4379 /* Relax non-PIC TLS references to TP-relative references. */
4382 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4384 asection
*sym_sec ATTRIBUTE_UNUSED
,
4385 struct bfd_link_info
*link_info
,
4386 Elf_Internal_Rela
*rel
,
4388 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4389 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4391 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
4392 bool undefined_weak ATTRIBUTE_UNUSED
)
4394 /* See if this symbol is in range of tp. */
4395 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4398 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4399 switch (ELFNN_R_TYPE (rel
->r_info
))
4401 case R_RISCV_TPREL_LO12_I
:
4402 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4405 case R_RISCV_TPREL_LO12_S
:
4406 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4409 case R_RISCV_TPREL_HI20
:
4410 case R_RISCV_TPREL_ADD
:
4411 /* We can delete the unnecessary instruction and reloc. */
4412 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4414 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
4421 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4422 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4425 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4427 struct bfd_link_info
*link_info
,
4428 Elf_Internal_Rela
*rel
,
4430 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4431 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4432 bool *again ATTRIBUTE_UNUSED
,
4433 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
4434 bool undefined_weak ATTRIBUTE_UNUSED
)
4436 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4437 bfd_vma alignment
= 1, pos
;
4438 while (alignment
<= rel
->r_addend
)
4441 symval
-= rel
->r_addend
;
4442 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4443 bfd_vma nop_bytes
= aligned_addr
- symval
;
4445 /* Make sure there are enough NOPs to actually achieve the alignment. */
4446 if (rel
->r_addend
< nop_bytes
)
4449 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4450 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4451 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4452 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4453 bfd_set_error (bfd_error_bad_value
);
4457 /* Delete the reloc. */
4458 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4460 /* If the number of NOPs is already correct, there's nothing to do. */
4461 if (nop_bytes
== rel
->r_addend
)
4464 /* Write as many RISC-V NOPs as we need. */
4465 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4466 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4468 /* Write a final RVC NOP if need be. */
4469 if (nop_bytes
% 4 != 0)
4470 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4472 /* Delete the excess bytes. */
4473 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4474 rel
->r_addend
- nop_bytes
, link_info
);
4477 /* Relax PC-relative references to GP-relative references. */
4480 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4483 struct bfd_link_info
*link_info
,
4484 Elf_Internal_Rela
*rel
,
4486 bfd_vma max_alignment
,
4487 bfd_vma reserve_size
,
4488 bool *again ATTRIBUTE_UNUSED
,
4489 riscv_pcgp_relocs
*pcgp_relocs
,
4490 bool undefined_weak
)
4492 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4493 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4495 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4497 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4498 actual target address. */
4499 riscv_pcgp_hi_reloc hi_reloc
;
4500 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4501 switch (ELFNN_R_TYPE (rel
->r_info
))
4503 case R_RISCV_PCREL_LO12_I
:
4504 case R_RISCV_PCREL_LO12_S
:
4506 /* If the %lo has an addend, it isn't for the label pointing at the
4507 hi part instruction, but rather for the symbol pointed at by the
4508 hi part instruction. So we must subtract it here for the lookup.
4509 It is still used below in the final symbol address. */
4510 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4511 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4515 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4520 symval
= hi_reloc
.hi_addr
;
4521 sym_sec
= hi_reloc
.sym_sec
;
4523 /* We can not know whether the undefined weak symbol is referenced
4524 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4525 we have to record the 'undefined_weak' flag when handling the
4526 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4527 undefined_weak
= hi_reloc
.undefined_weak
;
4531 case R_RISCV_PCREL_HI20
:
4532 /* Mergeable symbols and code might later move out of range. */
4533 if (! undefined_weak
4534 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4537 /* If the cooresponding lo relocation has already been seen then it's not
4538 safe to relax this relocation. */
4539 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4550 /* If gp and the symbol are in the same output section, which is not the
4551 abs section, then consider only that output section's alignment. */
4552 struct bfd_link_hash_entry
*h
=
4553 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4555 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4556 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4557 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4560 /* Is the reference in range of x0 or gp?
4561 Valid gp range conservatively because of alignment issue. */
4563 || (VALID_ITYPE_IMM (symval
)
4565 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4567 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4569 unsigned sym
= hi_reloc
.hi_sym
;
4570 switch (ELFNN_R_TYPE (rel
->r_info
))
4572 case R_RISCV_PCREL_LO12_I
:
4575 /* Change the RS1 to zero, and then modify the relocation
4576 type to R_RISCV_LO12_I. */
4577 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4578 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4579 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4580 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4581 rel
->r_addend
= hi_reloc
.hi_addend
;
4585 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4586 rel
->r_addend
+= hi_reloc
.hi_addend
;
4590 case R_RISCV_PCREL_LO12_S
:
4593 /* Change the RS1 to zero, and then modify the relocation
4594 type to R_RISCV_LO12_S. */
4595 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4596 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4597 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4598 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4599 rel
->r_addend
= hi_reloc
.hi_addend
;
4603 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4604 rel
->r_addend
+= hi_reloc
.hi_addend
;
4608 case R_RISCV_PCREL_HI20
:
4609 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4613 ELFNN_R_SYM(rel
->r_info
),
4616 /* We can delete the unnecessary AUIPC and reloc. */
4617 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4629 /* Delete the bytes for R_RISCV_DELETE. */
4632 _bfd_riscv_relax_delete (bfd
*abfd
,
4634 asection
*sym_sec ATTRIBUTE_UNUSED
,
4635 struct bfd_link_info
*link_info
,
4636 Elf_Internal_Rela
*rel
,
4637 bfd_vma symval ATTRIBUTE_UNUSED
,
4638 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4639 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4641 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4642 bool undefined_weak ATTRIBUTE_UNUSED
)
4644 if (!riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4647 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4652 /* Called by after_allocation to set the information of data segment
4656 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
4657 int *data_segment_phase
)
4659 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4660 htab
->data_segment_phase
= data_segment_phase
;
4663 /* Called by after_allocation to check if we need to run the whole
4664 relaxations again. */
4667 bfd_elfNN_riscv_restart_relax_sections (struct bfd_link_info
*info
)
4669 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4670 bool restart
= htab
->restart_relax
;
4671 /* Reset the flag. */
4672 htab
->restart_relax
= false;
4678 Pass 0: Shortens code sequences for LUI/CALL/TPREL relocs.
4679 Pass 1: Shortens code sequences for PCREL relocs.
4680 Pass 2: Deletes the bytes that pass 1 made obsolete.
4681 Pass 3: Which cannot be disabled, handles code alignment directives.
4683 The `again` is used to determine whether the relax pass itself needs to
4684 run again. And the `restart_relax` is used to determine if we need to
4685 run the whole relax passes again from 0 to 2. Once we have deleted the
4686 code between relax pass 0 to 2, the restart_relax will be set to TRUE,
4687 and we should run the whole relaxations again to give them more chances
4688 to shorten the code.
4690 Since we can't relax anything else once we start to handle the alignments,
4691 we will only enter into the relax pass 3 when the restart_relax is FALSE. */
4694 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4695 struct bfd_link_info
*info
,
4698 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4699 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4700 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4701 Elf_Internal_Rela
*relocs
;
4704 bfd_vma max_alignment
, reserve_size
= 0;
4705 riscv_pcgp_relocs pcgp_relocs
;
4709 if (bfd_link_relocatable (info
)
4710 || (sec
->flags
& SEC_RELOC
) == 0
4711 || sec
->reloc_count
== 0
4712 || (info
->disable_target_specific_optimizations
4713 && info
->relax_pass
< 2)
4714 || (htab
->restart_relax
4715 && info
->relax_pass
== 3)
4716 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4717 and defined in ld/ldexp.h. */
4718 || *(htab
->data_segment_phase
) == 4)
4721 riscv_init_pcgp_relocs (&pcgp_relocs
);
4723 /* Read this BFD's relocs if we haven't done so already. */
4725 relocs
= data
->relocs
;
4726 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4727 info
->keep_memory
)))
4732 max_alignment
= htab
->max_alignment
;
4733 if (max_alignment
== (bfd_vma
) -1)
4735 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4736 htab
->max_alignment
= max_alignment
;
4740 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4742 /* Examine and consider relaxing each reloc. */
4743 for (i
= 0; i
< sec
->reloc_count
; i
++)
4746 Elf_Internal_Rela
*rel
= relocs
+ i
;
4747 relax_func_t relax_func
;
4748 int type
= ELFNN_R_TYPE (rel
->r_info
);
4751 bool undefined_weak
= false;
4754 if (info
->relax_pass
== 0)
4756 if (type
== R_RISCV_CALL
4757 || type
== R_RISCV_CALL_PLT
)
4758 relax_func
= _bfd_riscv_relax_call
;
4759 else if (type
== R_RISCV_HI20
4760 || type
== R_RISCV_LO12_I
4761 || type
== R_RISCV_LO12_S
)
4762 relax_func
= _bfd_riscv_relax_lui
;
4763 else if (type
== R_RISCV_TPREL_HI20
4764 || type
== R_RISCV_TPREL_ADD
4765 || type
== R_RISCV_TPREL_LO12_I
4766 || type
== R_RISCV_TPREL_LO12_S
)
4767 relax_func
= _bfd_riscv_relax_tls_le
;
4771 else if (info
->relax_pass
== 1
4772 && !bfd_link_pic (info
)
4773 && (type
== R_RISCV_PCREL_HI20
4774 || type
== R_RISCV_PCREL_LO12_I
4775 || type
== R_RISCV_PCREL_LO12_S
))
4776 relax_func
= _bfd_riscv_relax_pc
;
4777 else if (info
->relax_pass
== 2 && type
== R_RISCV_DELETE
)
4778 relax_func
= _bfd_riscv_relax_delete
;
4779 else if (info
->relax_pass
== 3 && type
== R_RISCV_ALIGN
)
4780 relax_func
= _bfd_riscv_relax_align
;
4784 if (info
->relax_pass
< 2)
4786 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4787 if (i
== sec
->reloc_count
- 1
4788 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4789 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4792 /* Skip over the R_RISCV_RELAX. */
4796 data
->relocs
= relocs
;
4798 /* Read this BFD's contents if we haven't done so already. */
4799 if (!data
->this_hdr
.contents
4800 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4803 /* Read this BFD's symbols if we haven't done so already. */
4804 if (symtab_hdr
->sh_info
!= 0
4805 && !symtab_hdr
->contents
4806 && !(symtab_hdr
->contents
=
4807 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4808 symtab_hdr
->sh_info
,
4809 0, NULL
, NULL
, NULL
)))
4812 /* Get the value of the symbol referred to by the reloc. */
4813 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4815 /* A local symbol. */
4816 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4817 + ELFNN_R_SYM (rel
->r_info
));
4818 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4819 ? 0 : isym
->st_size
- rel
->r_addend
;
4821 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4822 a fake global symbol entry for this, so deal with the local ifunc
4824 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4827 if (isym
->st_shndx
== SHN_UNDEF
)
4828 sym_sec
= sec
, symval
= rel
->r_offset
;
4831 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4832 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4834 /* The purpose of this code is unknown. It breaks linker scripts
4835 for embedded development that place sections at address zero.
4836 This code is believed to be unnecessary. Disabling it but not
4837 yet removing it, in case something breaks. */
4838 if (sec_addr (sym_sec
) == 0)
4841 symval
= isym
->st_value
;
4843 symtype
= ELF_ST_TYPE (isym
->st_info
);
4848 struct elf_link_hash_entry
*h
;
4850 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4851 h
= elf_sym_hashes (abfd
)[indx
];
4853 while (h
->root
.type
== bfd_link_hash_indirect
4854 || h
->root
.type
== bfd_link_hash_warning
)
4855 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4857 /* Disable the relaxation for ifunc. */
4858 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4861 if (h
->root
.type
== bfd_link_hash_undefweak
4862 && (relax_func
== _bfd_riscv_relax_lui
4863 || relax_func
== _bfd_riscv_relax_pc
))
4865 /* For the lui and auipc relaxations, since the symbol
4866 value of an undefined weak symbol is always be zero,
4867 we can optimize the patterns into a single LI/MV/ADDI
4870 Note that, creating shared libraries and pie output may
4871 break the rule above. Fortunately, since we do not relax
4872 pc relocs when creating shared libraries and pie output,
4873 and the absolute address access for R_RISCV_HI20 isn't
4874 allowed when "-fPIC" is set, the problem of creating shared
4875 libraries can not happen currently. Once we support the
4876 auipc relaxations when creating shared libraries, then we will
4877 need the more rigorous checking for this optimization. */
4878 undefined_weak
= true;
4881 /* This line has to match the check in riscv_elf_relocate_section
4882 in the R_RISCV_CALL[_PLT] case. */
4883 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4885 sym_sec
= htab
->elf
.splt
;
4886 symval
= h
->plt
.offset
;
4888 else if (undefined_weak
)
4891 sym_sec
= bfd_und_section_ptr
;
4893 else if ((h
->root
.type
== bfd_link_hash_defined
4894 || h
->root
.type
== bfd_link_hash_defweak
)
4895 && h
->root
.u
.def
.section
!= NULL
4896 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4898 symval
= h
->root
.u
.def
.value
;
4899 sym_sec
= h
->root
.u
.def
.section
;
4904 if (h
->type
!= STT_FUNC
)
4906 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4910 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4911 && (sym_sec
->flags
& SEC_MERGE
))
4913 /* At this stage in linking, no SEC_MERGE symbol has been
4914 adjusted, so all references to such symbols need to be
4915 passed through _bfd_merged_section_offset. (Later, in
4916 relocate_section, all SEC_MERGE symbols *except* for
4917 section symbols have been adjusted.)
4919 gas may reduce relocations against symbols in SEC_MERGE
4920 sections to a relocation against the section symbol when
4921 the original addend was zero. When the reloc is against
4922 a section symbol we should include the addend in the
4923 offset passed to _bfd_merged_section_offset, since the
4924 location of interest is the original symbol. On the
4925 other hand, an access to "sym+addend" where "sym" is not
4926 a section symbol should not include the addend; Such an
4927 access is presumed to be an offset from "sym"; The
4928 location of interest is just "sym". */
4929 if (symtype
== STT_SECTION
)
4930 symval
+= rel
->r_addend
;
4932 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4933 elf_section_data (sym_sec
)->sec_info
,
4936 if (symtype
!= STT_SECTION
)
4937 symval
+= rel
->r_addend
;
4940 symval
+= rel
->r_addend
;
4942 symval
+= sec_addr (sym_sec
);
4944 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4945 max_alignment
, reserve_size
, again
,
4946 &pcgp_relocs
, undefined_weak
))
4953 if (relocs
!= data
->relocs
)
4955 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
4958 htab
->restart_relax
= true;
4964 # define PRSTATUS_SIZE 204
4965 # define PRSTATUS_OFFSET_PR_CURSIG 12
4966 # define PRSTATUS_OFFSET_PR_PID 24
4967 # define PRSTATUS_OFFSET_PR_REG 72
4968 # define ELF_GREGSET_T_SIZE 128
4969 # define PRPSINFO_SIZE 128
4970 # define PRPSINFO_OFFSET_PR_PID 16
4971 # define PRPSINFO_OFFSET_PR_FNAME 32
4972 # define PRPSINFO_OFFSET_PR_PSARGS 48
4973 # define PRPSINFO_PR_FNAME_LENGTH 16
4974 # define PRPSINFO_PR_PSARGS_LENGTH 80
4976 # define PRSTATUS_SIZE 376
4977 # define PRSTATUS_OFFSET_PR_CURSIG 12
4978 # define PRSTATUS_OFFSET_PR_PID 32
4979 # define PRSTATUS_OFFSET_PR_REG 112
4980 # define ELF_GREGSET_T_SIZE 256
4981 # define PRPSINFO_SIZE 136
4982 # define PRPSINFO_OFFSET_PR_PID 24
4983 # define PRPSINFO_OFFSET_PR_FNAME 40
4984 # define PRPSINFO_OFFSET_PR_PSARGS 56
4985 # define PRPSINFO_PR_FNAME_LENGTH 16
4986 # define PRPSINFO_PR_PSARGS_LENGTH 80
4989 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
4990 before the generic code in elf.c. By checking the compiler defines we
4991 only perform any action here if the generic code would otherwise not be
4992 able to help us. The intention is that bare metal core dumps (where the
4993 prstatus_t and/or prpsinfo_t might not be available) will use this code,
4994 while non bare metal tools will use the generic elf code. */
4997 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
4998 char *buf ATTRIBUTE_UNUSED
,
4999 int *bufsiz ATTRIBUTE_UNUSED
,
5000 int note_type ATTRIBUTE_UNUSED
, ...)
5007 #if !defined (HAVE_PRPSINFO_T)
5010 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
5013 va_start (ap
, note_type
);
5014 memset (data
, 0, sizeof (data
));
5015 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
5016 PRPSINFO_PR_FNAME_LENGTH
);
5017 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5019 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5020 -Wstringop-truncation:
5021 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5023 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5025 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5026 PRPSINFO_PR_PSARGS_LENGTH
);
5027 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5031 return elfcore_write_note (abfd
, buf
, bufsiz
,
5032 "CORE", note_type
, data
, sizeof (data
));
5034 #endif /* !HAVE_PRPSINFO_T */
5036 #if !defined (HAVE_PRSTATUS_T)
5039 char data
[PRSTATUS_SIZE
];
5045 va_start (ap
, note_type
);
5046 memset (data
, 0, sizeof(data
));
5047 pid
= va_arg (ap
, long);
5048 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5049 cursig
= va_arg (ap
, int);
5050 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5051 greg
= va_arg (ap
, const void *);
5052 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5053 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5055 return elfcore_write_note (abfd
, buf
, bufsiz
,
5056 "CORE", note_type
, data
, sizeof (data
));
5058 #endif /* !HAVE_PRSTATUS_T */
5062 /* Support for core dump NOTE sections. */
5065 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5067 switch (note
->descsz
)
5072 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5074 elf_tdata (abfd
)->core
->signal
5075 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5078 elf_tdata (abfd
)->core
->lwpid
5079 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5083 /* Make a ".reg/999" section. */
5084 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5085 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5089 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5091 switch (note
->descsz
)
5096 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5098 elf_tdata (abfd
)->core
->pid
5099 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5102 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5103 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5104 PRPSINFO_PR_FNAME_LENGTH
);
5107 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5108 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5109 PRPSINFO_PR_PSARGS_LENGTH
);
5113 /* Note that for some reason, a spurious space is tacked
5114 onto the end of the args in some (at least one anyway)
5115 implementations, so strip it off if it exists. */
5118 char *command
= elf_tdata (abfd
)->core
->command
;
5119 int n
= strlen (command
);
5121 if (0 < n
&& command
[n
- 1] == ' ')
5122 command
[n
- 1] = '\0';
5128 /* Set the right mach type. */
5131 riscv_elf_object_p (bfd
*abfd
)
5133 /* There are only two mach types in RISCV currently. */
5134 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5135 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5136 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5138 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5143 /* Determine whether an object attribute tag takes an integer, a
5147 riscv_elf_obj_attrs_arg_type (int tag
)
5149 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5152 /* Do not choose mapping symbols as a function name. */
5154 static bfd_size_type
5155 riscv_maybe_function_sym (const asymbol
*sym
,
5159 if (sym
->flags
& BSF_LOCAL
5160 && riscv_elf_is_mapping_symbols (sym
->name
))
5163 return _bfd_elf_maybe_function_sym (sym
, sec
, code_off
);
5166 /* Treat the following cases as target special symbols, they are
5170 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5172 /* PR27584, local and empty symbols. Since they are usually
5173 generated for pcrel relocations. */
5174 return (!strcmp (sym
->name
, "")
5175 || _bfd_elf_is_local_label_name (abfd
, sym
->name
)
5176 /* PR27916, mapping symbols. */
5177 || riscv_elf_is_mapping_symbols (sym
->name
));
5181 riscv_elf_additional_program_headers (bfd
*abfd
,
5182 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5186 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5187 if (bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
))
5194 riscv_elf_modify_segment_map (bfd
*abfd
,
5195 struct bfd_link_info
*info ATTRIBUTE_UNUSED
)
5198 struct elf_segment_map
*m
, **pm
;
5201 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5203 s
= bfd_get_section_by_name (abfd
, RISCV_ATTRIBUTES_SECTION_NAME
);
5206 for (m
= elf_seg_map (abfd
); m
!= NULL
; m
= m
->next
)
5207 if (m
->p_type
== PT_RISCV_ATTRIBUTES
)
5209 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5214 m
= bfd_zalloc (abfd
, amt
);
5218 m
->p_type
= PT_RISCV_ATTRIBUTES
;
5222 /* We want to put it after the PHDR and INTERP segments. */
5223 pm
= &elf_seg_map (abfd
);
5225 && ((*pm
)->p_type
== PT_PHDR
5226 || (*pm
)->p_type
== PT_INTERP
))
5237 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5238 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5239 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5240 #define TARGET_BIG_NAME "elfNN-bigriscv"
5242 #define elf_backend_reloc_type_class riscv_reloc_type_class
5244 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5245 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5246 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5247 #define bfd_elfNN_bfd_merge_private_bfd_data \
5248 _bfd_riscv_elf_merge_private_bfd_data
5249 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5251 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5252 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5253 #define elf_backend_check_relocs riscv_elf_check_relocs
5254 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5255 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5256 #define elf_backend_relocate_section riscv_elf_relocate_section
5257 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5258 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5259 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
5260 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5261 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5262 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5263 #define elf_backend_object_p riscv_elf_object_p
5264 #define elf_backend_write_core_note riscv_write_core_note
5265 #define elf_backend_maybe_function_sym riscv_maybe_function_sym
5266 #define elf_info_to_howto_rel NULL
5267 #define elf_info_to_howto riscv_info_to_howto_rela
5268 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5269 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5270 #define elf_backend_additional_program_headers \
5271 riscv_elf_additional_program_headers
5272 #define elf_backend_modify_segment_map riscv_elf_modify_segment_map
5274 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5276 #define elf_backend_can_gc_sections 1
5277 #define elf_backend_can_refcount 1
5278 #define elf_backend_want_got_plt 1
5279 #define elf_backend_plt_readonly 1
5280 #define elf_backend_plt_alignment 4
5281 #define elf_backend_want_plt_sym 1
5282 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5283 #define elf_backend_want_dynrelro 1
5284 #define elf_backend_rela_normal 1
5285 #define elf_backend_default_execstack 0
5287 #undef elf_backend_obj_attrs_vendor
5288 #define elf_backend_obj_attrs_vendor "riscv"
5289 #undef elf_backend_obj_attrs_arg_type
5290 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5291 #undef elf_backend_obj_attrs_section_type
5292 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5293 #undef elf_backend_obj_attrs_section
5294 #define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
5296 #include "elfNN-target.h"