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"
35 #include "cpu-riscv.h"
42 /* Internal relocations used exclusively by the relaxation pass. */
43 #define R_RISCV_DELETE (R_RISCV_max + 1)
47 #define MINUS_ONE ((bfd_vma)0 - 1)
49 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
51 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
53 /* The name of the dynamic interpreter. This is put in the .interp
56 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
57 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
59 #define ELF_ARCH bfd_arch_riscv
60 #define ELF_TARGET_ID RISCV_ELF_DATA
61 #define ELF_MACHINE_CODE EM_RISCV
62 #define ELF_MAXPAGESIZE 0x1000
63 #define ELF_COMMONPAGESIZE 0x1000
65 /* RISC-V ELF linker hash entry. */
67 struct riscv_elf_link_hash_entry
69 struct elf_link_hash_entry elf
;
79 #define riscv_elf_hash_entry(ent) \
80 ((struct riscv_elf_link_hash_entry *) (ent))
82 struct _bfd_riscv_elf_obj_tdata
84 struct elf_obj_tdata root
;
86 /* tls_type for each local got entry. */
87 char *local_got_tls_type
;
90 #define _bfd_riscv_elf_tdata(abfd) \
91 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
93 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
94 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
96 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
97 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
98 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
100 #define is_riscv_elf(bfd) \
101 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
102 && elf_tdata (bfd) != NULL \
103 && elf_object_id (bfd) == RISCV_ELF_DATA)
106 elfNN_riscv_mkobject (bfd
*abfd
)
108 return bfd_elf_allocate_object (abfd
,
109 sizeof (struct _bfd_riscv_elf_obj_tdata
),
113 #include "elf/common.h"
114 #include "elf/internal.h"
116 struct riscv_elf_link_hash_table
118 struct elf_link_hash_table elf
;
120 /* Short-cuts to get to dynamic linker sections. */
123 /* The max alignment of output sections. */
124 bfd_vma max_alignment
;
126 /* Used by local STT_GNU_IFUNC symbols. */
127 htab_t loc_hash_table
;
128 void * loc_hash_memory
;
130 /* The index of the last unused .rel.iplt slot. */
131 bfd_vma last_iplt_index
;
133 /* Re-run the relaxations from relax pass 0 if TRUE. */
136 /* The data segment phase, don't relax the section
137 when it is exp_seg_relro_adjust. */
138 int *data_segment_phase
;
141 /* Instruction access functions. */
142 #define riscv_get_insn(bits, ptr) \
143 ((bits) == 16 ? bfd_getl16 (ptr) \
144 : (bits) == 32 ? bfd_getl32 (ptr) \
145 : (bits) == 64 ? bfd_getl64 (ptr) \
146 : (abort (), (bfd_vma) - 1))
147 #define riscv_put_insn(bits, val, ptr) \
148 ((bits) == 16 ? bfd_putl16 (val, ptr) \
149 : (bits) == 32 ? bfd_putl32 (val, ptr) \
150 : (bits) == 64 ? bfd_putl64 (val, ptr) \
151 : (abort (), (void) 0))
153 /* Get the RISC-V ELF linker hash table from a link_info structure. */
154 #define riscv_elf_hash_table(p) \
155 ((is_elf_hash_table ((p)->hash) \
156 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
157 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
160 riscv_info_to_howto_rela (bfd
*abfd
,
162 Elf_Internal_Rela
*dst
)
164 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
165 return cache_ptr
->howto
!= NULL
;
169 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
171 const struct elf_backend_data
*bed
;
174 bed
= get_elf_backend_data (abfd
);
175 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
176 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
179 /* Return true if a relocation is modifying an instruction. */
182 riscv_is_insn_reloc (const reloc_howto_type
*howto
)
184 /* Heuristic: A multibyte destination with a nontrivial mask
186 return (howto
->bitsize
> 8
187 && howto
->dst_mask
!= 0
188 && ~(howto
->dst_mask
| (howto
->bitsize
< sizeof(bfd_vma
) * CHAR_BIT
189 ? (MINUS_ONE
<< howto
->bitsize
) : (bfd_vma
)0)) != 0);
193 #define PLT_HEADER_INSNS 8
194 #define PLT_ENTRY_INSNS 4
195 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
196 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
197 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
198 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
199 the other is used for link map. Other targets also reserve one more
200 entry used for runtime profile? */
201 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
203 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
206 # define MATCH_LREG MATCH_LW
208 # define MATCH_LREG MATCH_LD
211 /* Generate a PLT header. */
214 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
217 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
218 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
220 /* RVE has no t3 register, so this won't work, and is not supported. */
221 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
223 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
228 /* auipc t2, %hi(.got.plt)
229 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
230 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
231 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
232 addi t0, t2, %lo(.got.plt) # &.got.plt
233 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
234 l[w|d] t0, PTRSIZE(t0) # link map
237 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
238 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
239 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
240 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
241 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
242 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
243 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
244 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
249 /* Generate a PLT entry. */
252 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
255 /* RVE has no t3 register, so this won't work, and is not supported. */
256 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
258 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
263 /* auipc t3, %hi(.got.plt entry)
264 l[w|d] t3, %lo(.got.plt entry)(t3)
268 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
269 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
270 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
271 entry
[3] = RISCV_NOP
;
276 /* Create an entry in an RISC-V ELF linker hash table. */
278 static struct bfd_hash_entry
*
279 link_hash_newfunc (struct bfd_hash_entry
*entry
,
280 struct bfd_hash_table
*table
, const char *string
)
282 /* Allocate the structure if it has not already been allocated by a
287 bfd_hash_allocate (table
,
288 sizeof (struct riscv_elf_link_hash_entry
));
293 /* Call the allocation method of the superclass. */
294 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
297 struct riscv_elf_link_hash_entry
*eh
;
299 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
300 eh
->tls_type
= GOT_UNKNOWN
;
306 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
307 for local symbol so that we can handle local STT_GNU_IFUNC symbols
308 as global symbol. We reuse indx and dynstr_index for local symbol
309 hash since they aren't used by global symbols in this backend. */
312 riscv_elf_local_htab_hash (const void *ptr
)
314 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
315 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
318 /* Compare local hash entries. */
321 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
323 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
324 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
326 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
329 /* Find and/or create a hash entry for local symbol. */
331 static struct elf_link_hash_entry
*
332 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
333 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
336 struct riscv_elf_link_hash_entry eh
, *ret
;
337 asection
*sec
= abfd
->sections
;
338 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
339 ELFNN_R_SYM (rel
->r_info
));
342 eh
.elf
.indx
= sec
->id
;
343 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
344 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
345 create
? INSERT
: NO_INSERT
);
352 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
356 ret
= (struct riscv_elf_link_hash_entry
*)
357 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
358 sizeof (struct riscv_elf_link_hash_entry
));
361 memset (ret
, 0, sizeof (*ret
));
362 ret
->elf
.indx
= sec
->id
;
363 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
364 ret
->elf
.dynindx
= -1;
370 /* Destroy a RISC-V elf linker hash table. */
373 riscv_elf_link_hash_table_free (bfd
*obfd
)
375 struct riscv_elf_link_hash_table
*ret
376 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
378 if (ret
->loc_hash_table
)
379 htab_delete (ret
->loc_hash_table
);
380 if (ret
->loc_hash_memory
)
381 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
383 _bfd_elf_link_hash_table_free (obfd
);
386 /* Create a RISC-V ELF linker hash table. */
388 static struct bfd_link_hash_table
*
389 riscv_elf_link_hash_table_create (bfd
*abfd
)
391 struct riscv_elf_link_hash_table
*ret
;
392 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
394 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
398 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
399 sizeof (struct riscv_elf_link_hash_entry
),
406 ret
->max_alignment
= (bfd_vma
) -1;
407 ret
->restart_relax
= false;
409 /* Create hash table for local ifunc. */
410 ret
->loc_hash_table
= htab_try_create (1024,
411 riscv_elf_local_htab_hash
,
412 riscv_elf_local_htab_eq
,
414 ret
->loc_hash_memory
= objalloc_create ();
415 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
417 riscv_elf_link_hash_table_free (abfd
);
420 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
422 return &ret
->elf
.root
;
425 /* Create the .got section. */
428 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
432 struct elf_link_hash_entry
*h
;
433 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
434 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
436 /* This function may be called more than once. */
437 if (htab
->sgot
!= NULL
)
440 flags
= bed
->dynamic_sec_flags
;
442 s
= bfd_make_section_anyway_with_flags (abfd
,
443 (bed
->rela_plts_and_copies_p
444 ? ".rela.got" : ".rel.got"),
445 (bed
->dynamic_sec_flags
448 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
452 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
454 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
458 /* The first bit of the global offset table is the header. */
459 s
->size
+= bed
->got_header_size
;
461 if (bed
->want_got_plt
)
463 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
465 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
469 /* Reserve room for the header. */
470 s
->size
+= GOTPLT_HEADER_SIZE
;
473 if (bed
->want_got_sym
)
475 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
476 section. We don't do this in the linker script because we don't want
477 to define the symbol if we are not creating a global offset
479 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
480 "_GLOBAL_OFFSET_TABLE_");
481 elf_hash_table (info
)->hgot
= h
;
489 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
490 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
494 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
495 struct bfd_link_info
*info
)
497 struct riscv_elf_link_hash_table
*htab
;
499 htab
= riscv_elf_hash_table (info
);
500 BFD_ASSERT (htab
!= NULL
);
502 if (!riscv_elf_create_got_section (dynobj
, info
))
505 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
508 if (!bfd_link_pic (info
))
510 /* Technically, this section doesn't have contents. It is used as the
511 target of TLS copy relocs, to copy TLS data from shared libraries into
512 the executable. However, if we don't mark it as loadable, then it
513 matches the IS_TBSS test in ldlang.c, and there is no run-time address
514 space allocated for it even though it has SEC_ALLOC. That test is
515 correct for .tbss, but not correct for this section. There is also
516 a second problem that having a section with no contents can only work
517 if it comes after all sections with contents in the same segment,
518 but the linker script does not guarantee that. This is just mixed in
519 with other .tdata.* sections. We can fix both problems by lying and
520 saying that there are contents. This section is expected to be small
521 so this should not cause a significant extra program startup cost. */
523 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
524 (SEC_ALLOC
| SEC_THREAD_LOCAL
525 | SEC_LOAD
| SEC_DATA
527 | SEC_LINKER_CREATED
));
530 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
531 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
537 /* Copy the extra info we tack onto an elf_link_hash_entry. */
540 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
541 struct elf_link_hash_entry
*dir
,
542 struct elf_link_hash_entry
*ind
)
544 struct riscv_elf_link_hash_entry
*edir
, *eind
;
546 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
547 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
549 if (ind
->root
.type
== bfd_link_hash_indirect
550 && dir
->got
.refcount
<= 0)
552 edir
->tls_type
= eind
->tls_type
;
553 eind
->tls_type
= GOT_UNKNOWN
;
555 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
559 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
560 unsigned long symndx
, char tls_type
)
562 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
564 *new_tls_type
|= tls_type
;
565 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
567 (*_bfd_error_handler
)
568 (_("%pB: `%s' accessed both as normal and thread local symbol"),
569 abfd
, h
? h
->root
.root
.string
: "<local>");
576 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
577 struct elf_link_hash_entry
*h
, long symndx
)
579 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
580 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
582 if (htab
->elf
.sgot
== NULL
)
584 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
590 h
->got
.refcount
+= 1;
594 /* This is a global offset table entry for a local symbol. */
595 if (elf_local_got_refcounts (abfd
) == NULL
)
597 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
598 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
600 _bfd_riscv_elf_local_got_tls_type (abfd
)
601 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
603 elf_local_got_refcounts (abfd
) [symndx
] += 1;
609 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
611 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
613 /* We propably can improve the information to tell users that they
614 should be recompile the code with -fPIC or -fPIE, just like what
616 (*_bfd_error_handler
)
617 (_("%pB: relocation %s against `%s' can not be used when making a shared "
618 "object; recompile with -fPIC"),
619 abfd
, r
? r
->name
: _("<unknown>"),
620 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
621 bfd_set_error (bfd_error_bad_value
);
625 /* Look through the relocs for a section during the first phase, and
626 allocate space in the global offset table or procedure linkage
630 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
631 asection
*sec
, const Elf_Internal_Rela
*relocs
)
633 struct riscv_elf_link_hash_table
*htab
;
634 Elf_Internal_Shdr
*symtab_hdr
;
635 struct elf_link_hash_entry
**sym_hashes
;
636 const Elf_Internal_Rela
*rel
;
637 asection
*sreloc
= NULL
;
639 if (bfd_link_relocatable (info
))
642 htab
= riscv_elf_hash_table (info
);
643 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
644 sym_hashes
= elf_sym_hashes (abfd
);
646 if (htab
->elf
.dynobj
== NULL
)
647 htab
->elf
.dynobj
= abfd
;
649 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
652 unsigned int r_symndx
;
653 struct elf_link_hash_entry
*h
;
655 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
656 r_type
= ELFNN_R_TYPE (rel
->r_info
);
658 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
660 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
665 if (r_symndx
< symtab_hdr
->sh_info
)
667 /* A local symbol. */
668 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
673 /* Check relocation against local STT_GNU_IFUNC symbol. */
674 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
676 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, true);
680 /* Fake STT_GNU_IFUNC global symbol. */
681 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
683 h
->type
= STT_GNU_IFUNC
;
687 h
->root
.type
= bfd_link_hash_defined
;
694 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
695 while (h
->root
.type
== bfd_link_hash_indirect
696 || h
->root
.type
== bfd_link_hash_warning
)
697 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
707 case R_RISCV_CALL_PLT
:
709 case R_RISCV_GOT_HI20
:
710 case R_RISCV_PCREL_HI20
:
711 /* Create the ifunc sections, iplt and ipltgot, for static
713 if (h
->type
== STT_GNU_IFUNC
714 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
722 /* It is referenced by a non-shared object. */
728 case R_RISCV_TLS_GD_HI20
:
729 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
730 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
734 case R_RISCV_TLS_GOT_HI20
:
735 if (bfd_link_pic (info
))
736 info
->flags
|= DF_STATIC_TLS
;
737 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
738 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
742 case R_RISCV_GOT_HI20
:
743 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
744 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
749 case R_RISCV_CALL_PLT
:
750 /* These symbol requires a procedure linkage table entry.
751 We actually build the entry in adjust_dynamic_symbol,
752 because these might be a case of linking PIC code without
753 linking in any dynamic objects, in which case we don't
754 need to generate a procedure linkage table after all. */
756 /* If it is a local symbol, then we resolve it directly
757 without creating a PLT entry. */
762 h
->plt
.refcount
+= 1;
765 case R_RISCV_PCREL_HI20
:
767 && h
->type
== STT_GNU_IFUNC
)
770 h
->pointer_equality_needed
= 1;
772 /* We don't use the PCREL_HI20 in the data section,
773 so we always need the plt when it refers to
775 h
->plt
.refcount
+= 1;
781 case R_RISCV_RVC_BRANCH
:
782 case R_RISCV_RVC_JUMP
:
783 /* In shared libraries and pie, these relocs are known
785 if (bfd_link_pic (info
))
789 case R_RISCV_TPREL_HI20
:
790 if (!bfd_link_executable (info
))
791 return bad_static_reloc (abfd
, r_type
, h
);
793 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
797 if (bfd_link_pic (info
))
798 return bad_static_reloc (abfd
, r_type
, h
);
802 case R_RISCV_JUMP_SLOT
:
803 case R_RISCV_RELATIVE
:
811 && (!bfd_link_pic (info
)
812 || h
->type
== STT_GNU_IFUNC
))
814 /* This reloc might not bind locally. */
816 h
->pointer_equality_needed
= 1;
819 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
821 /* We may need a .plt entry if the symbol is a function
822 defined in a shared lib or is a function referenced
823 from the code or read-only section. */
824 h
->plt
.refcount
+= 1;
828 /* If we are creating a shared library, and this is a reloc
829 against a global symbol, or a non PC relative reloc
830 against a local symbol, then we need to copy the reloc
831 into the shared library. However, if we are linking with
832 -Bsymbolic, we do not need to copy a reloc against a
833 global symbol which is defined in an object we are
834 including in the link (i.e., DEF_REGULAR is set). At
835 this point we have not seen all the input files, so it is
836 possible that DEF_REGULAR is not set now but will be set
837 later (it is never cleared). In case of a weak definition,
838 DEF_REGULAR may be cleared later by a strong definition in
839 a shared library. We account for that possibility below by
840 storing information in the relocs_copied field of the hash
841 table entry. A similar situation occurs when creating
842 shared libraries and symbol visibility changes render the
845 If on the other hand, we are creating an executable, we
846 may need to keep relocations for symbols satisfied by a
847 dynamic library if we manage to avoid copy relocs for the
850 Generate dynamic pointer relocation against STT_GNU_IFUNC
851 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
852 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
854 if ((bfd_link_pic (info
)
855 && (sec
->flags
& SEC_ALLOC
) != 0
856 && ((r
!= NULL
&& !r
->pc_relative
)
859 || h
->root
.type
== bfd_link_hash_defweak
860 || !h
->def_regular
))))
861 || (!bfd_link_pic (info
)
862 && (sec
->flags
& SEC_ALLOC
) != 0
864 && (h
->root
.type
== bfd_link_hash_defweak
866 || (!bfd_link_pic (info
)
868 && h
->type
== STT_GNU_IFUNC
869 && (sec
->flags
& SEC_CODE
) == 0))
871 struct elf_dyn_relocs
*p
;
872 struct elf_dyn_relocs
**head
;
874 /* When creating a shared object, we must copy these
875 relocs into the output file. We create a reloc
876 section in dynobj and make room for the reloc. */
879 sreloc
= _bfd_elf_make_dynamic_reloc_section
880 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
881 abfd
, /*rela?*/ true);
887 /* If this is a global symbol, we count the number of
888 relocations we need for this symbol. */
890 head
= &h
->dyn_relocs
;
893 /* Track dynamic relocs needed for local syms too.
894 We really need local syms available to do this
899 Elf_Internal_Sym
*isym
;
901 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
906 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
910 vpp
= &elf_section_data (s
)->local_dynrel
;
911 head
= (struct elf_dyn_relocs
**) vpp
;
915 if (p
== NULL
|| p
->sec
!= sec
)
917 size_t amt
= sizeof *p
;
918 p
= ((struct elf_dyn_relocs
*)
919 bfd_alloc (htab
->elf
.dynobj
, amt
));
930 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
935 case R_RISCV_GNU_VTINHERIT
:
936 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
940 case R_RISCV_GNU_VTENTRY
:
941 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
954 riscv_elf_gc_mark_hook (asection
*sec
,
955 struct bfd_link_info
*info
,
956 Elf_Internal_Rela
*rel
,
957 struct elf_link_hash_entry
*h
,
958 Elf_Internal_Sym
*sym
)
961 switch (ELFNN_R_TYPE (rel
->r_info
))
963 case R_RISCV_GNU_VTINHERIT
:
964 case R_RISCV_GNU_VTENTRY
:
968 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
971 /* Adjust a symbol defined by a dynamic object and referenced by a
972 regular object. The current definition is in some section of the
973 dynamic object, but we're not including those sections. We have to
974 change the definition to something the rest of the link can
978 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
979 struct elf_link_hash_entry
*h
)
981 struct riscv_elf_link_hash_table
*htab
;
982 struct riscv_elf_link_hash_entry
* eh
;
986 htab
= riscv_elf_hash_table (info
);
987 BFD_ASSERT (htab
!= NULL
);
989 dynobj
= htab
->elf
.dynobj
;
991 /* Make sure we know what is going on here. */
992 BFD_ASSERT (dynobj
!= NULL
994 || h
->type
== STT_GNU_IFUNC
998 && !h
->def_regular
)));
1000 /* If this is a function, put it in the procedure linkage table. We
1001 will fill in the contents of the procedure linkage table later
1002 (although we could actually do it here). */
1003 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
1005 if (h
->plt
.refcount
<= 0
1006 || (h
->type
!= STT_GNU_IFUNC
1007 && (SYMBOL_CALLS_LOCAL (info
, h
)
1008 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1009 && h
->root
.type
== bfd_link_hash_undefweak
))))
1011 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1012 input file, but the symbol was never referred to by a dynamic
1013 object, or if all references were garbage collected. In such
1014 a case, we don't actually need to build a PLT entry. */
1015 h
->plt
.offset
= (bfd_vma
) -1;
1022 h
->plt
.offset
= (bfd_vma
) -1;
1024 /* If this is a weak symbol, and there is a real definition, the
1025 processor independent code will have arranged for us to see the
1026 real definition first, and we can just use the same value. */
1027 if (h
->is_weakalias
)
1029 struct elf_link_hash_entry
*def
= weakdef (h
);
1030 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
1031 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
1032 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
1036 /* This is a reference to a symbol defined by a dynamic object which
1037 is not a function. */
1039 /* If we are creating a shared library, we must presume that the
1040 only references to the symbol are via the global offset table.
1041 For such cases we need not do anything here; the relocations will
1042 be handled correctly by relocate_section. */
1043 if (bfd_link_pic (info
))
1046 /* If there are no references to this symbol that do not use the
1047 GOT, we don't need to generate a copy reloc. */
1048 if (!h
->non_got_ref
)
1051 /* If -z nocopyreloc was given, we won't generate them either. */
1052 if (info
->nocopyreloc
)
1058 /* If we don't find any dynamic relocs in read-only sections, then
1059 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1060 if (!_bfd_elf_readonly_dynrelocs (h
))
1066 /* We must allocate the symbol in our .dynbss section, which will
1067 become part of the .bss section of the executable. There will be
1068 an entry for this symbol in the .dynsym section. The dynamic
1069 object will contain position independent code, so all references
1070 from the dynamic object to this symbol will go through the global
1071 offset table. The dynamic linker will use the .dynsym entry to
1072 determine the address it must put in the global offset table, so
1073 both the dynamic object and the regular object will refer to the
1074 same memory location for the variable. */
1076 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1077 to copy the initial value out of the dynamic object and into the
1078 runtime process image. We need to remember the offset into the
1079 .rel.bss section we are going to use. */
1080 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1081 if (eh
->tls_type
& ~GOT_NORMAL
)
1083 s
= htab
->sdyntdata
;
1084 srel
= htab
->elf
.srelbss
;
1086 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1088 s
= htab
->elf
.sdynrelro
;
1089 srel
= htab
->elf
.sreldynrelro
;
1093 s
= htab
->elf
.sdynbss
;
1094 srel
= htab
->elf
.srelbss
;
1096 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1098 srel
->size
+= sizeof (ElfNN_External_Rela
);
1102 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1105 /* Allocate space in .plt, .got and associated reloc sections for
1109 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1111 struct bfd_link_info
*info
;
1112 struct riscv_elf_link_hash_table
*htab
;
1113 struct elf_dyn_relocs
*p
;
1115 if (h
->root
.type
== bfd_link_hash_indirect
)
1118 info
= (struct bfd_link_info
*) inf
;
1119 htab
= riscv_elf_hash_table (info
);
1120 BFD_ASSERT (htab
!= NULL
);
1122 /* When we are generating pde, make sure gp symbol is output as a
1123 dynamic symbol. Then ld.so can set the gp register earlier, before
1124 resolving the ifunc. */
1125 if (!bfd_link_pic (info
)
1126 && htab
->elf
.dynamic_sections_created
1127 && strcmp (h
->root
.root
.string
, RISCV_GP_SYMBOL
) == 0
1128 && !bfd_elf_link_record_dynamic_symbol (info
, h
))
1131 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1132 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1133 if they are defined and referenced in a non-shared object. */
1134 if (h
->type
== STT_GNU_IFUNC
1137 else if (htab
->elf
.dynamic_sections_created
1138 && h
->plt
.refcount
> 0)
1140 /* Make sure this symbol is output as a dynamic symbol.
1141 Undefined weak syms won't yet be marked as dynamic. */
1142 if (h
->dynindx
== -1
1143 && !h
->forced_local
)
1145 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1149 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1151 asection
*s
= htab
->elf
.splt
;
1154 s
->size
= PLT_HEADER_SIZE
;
1156 h
->plt
.offset
= s
->size
;
1158 /* Make room for this entry. */
1159 s
->size
+= PLT_ENTRY_SIZE
;
1161 /* We also need to make an entry in the .got.plt section. */
1162 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1164 /* We also need to make an entry in the .rela.plt section. */
1165 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1167 /* If this symbol is not defined in a regular file, and we are
1168 not generating a shared library, then set the symbol to this
1169 location in the .plt. This is required to make function
1170 pointers compare as equal between the normal executable and
1171 the shared library. */
1172 if (! bfd_link_pic (info
)
1175 h
->root
.u
.def
.section
= s
;
1176 h
->root
.u
.def
.value
= h
->plt
.offset
;
1181 h
->plt
.offset
= (bfd_vma
) -1;
1187 h
->plt
.offset
= (bfd_vma
) -1;
1191 if (h
->got
.refcount
> 0)
1195 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1197 /* Make sure this symbol is output as a dynamic symbol.
1198 Undefined weak syms won't yet be marked as dynamic. */
1199 if (h
->dynindx
== -1
1200 && !h
->forced_local
)
1202 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1207 h
->got
.offset
= s
->size
;
1208 dyn
= htab
->elf
.dynamic_sections_created
;
1209 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1211 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1212 if (tls_type
& GOT_TLS_GD
)
1214 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1215 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1218 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1219 if (tls_type
& GOT_TLS_IE
)
1221 s
->size
+= RISCV_ELF_WORD_BYTES
;
1222 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1227 s
->size
+= RISCV_ELF_WORD_BYTES
;
1228 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1229 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1230 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1234 h
->got
.offset
= (bfd_vma
) -1;
1236 if (h
->dyn_relocs
== NULL
)
1239 /* In the shared -Bsymbolic case, discard space allocated for
1240 dynamic pc-relative relocs against symbols which turn out to be
1241 defined in regular objects. For the normal shared case, discard
1242 space for pc-relative relocs that have become local due to symbol
1243 visibility changes. */
1245 if (bfd_link_pic (info
))
1247 if (SYMBOL_CALLS_LOCAL (info
, h
))
1249 struct elf_dyn_relocs
**pp
;
1251 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1253 p
->count
-= p
->pc_count
;
1262 /* Also discard relocs on undefined weak syms with non-default
1264 if (h
->dyn_relocs
!= NULL
1265 && h
->root
.type
== bfd_link_hash_undefweak
)
1267 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1268 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1269 h
->dyn_relocs
= NULL
;
1271 /* Make sure undefined weak symbols are output as a dynamic
1273 else if (h
->dynindx
== -1
1274 && !h
->forced_local
)
1276 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1283 /* For the non-shared case, discard space for relocs against
1284 symbols which turn out to need copy relocs or are not
1290 || (htab
->elf
.dynamic_sections_created
1291 && (h
->root
.type
== bfd_link_hash_undefweak
1292 || h
->root
.type
== bfd_link_hash_undefined
))))
1294 /* Make sure this symbol is output as a dynamic symbol.
1295 Undefined weak syms won't yet be marked as dynamic. */
1296 if (h
->dynindx
== -1
1297 && !h
->forced_local
)
1299 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1303 /* If that succeeded, we know we'll be keeping all the
1305 if (h
->dynindx
!= -1)
1309 h
->dyn_relocs
= NULL
;
1314 /* Finally, allocate space. */
1315 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1317 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1318 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1324 /* Allocate space in .plt, .got and associated reloc sections for
1325 ifunc dynamic relocs. */
1328 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1331 struct bfd_link_info
*info
;
1333 if (h
->root
.type
== bfd_link_hash_indirect
)
1336 if (h
->root
.type
== bfd_link_hash_warning
)
1337 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1339 info
= (struct bfd_link_info
*) inf
;
1341 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1342 here if it is defined and referenced in a non-shared object. */
1343 if (h
->type
== STT_GNU_IFUNC
1345 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1354 /* Allocate space in .plt, .got and associated reloc sections for
1355 local ifunc dynamic relocs. */
1358 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1360 struct elf_link_hash_entry
*h
1361 = (struct elf_link_hash_entry
*) *slot
;
1363 if (h
->type
!= STT_GNU_IFUNC
1367 || h
->root
.type
!= bfd_link_hash_defined
)
1370 return allocate_ifunc_dynrelocs (h
, inf
);
1374 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1376 struct riscv_elf_link_hash_table
*htab
;
1381 htab
= riscv_elf_hash_table (info
);
1382 BFD_ASSERT (htab
!= NULL
);
1383 dynobj
= htab
->elf
.dynobj
;
1384 BFD_ASSERT (dynobj
!= NULL
);
1386 if (elf_hash_table (info
)->dynamic_sections_created
)
1388 /* Set the contents of the .interp section to the interpreter. */
1389 if (bfd_link_executable (info
) && !info
->nointerp
)
1391 s
= bfd_get_linker_section (dynobj
, ".interp");
1392 BFD_ASSERT (s
!= NULL
);
1393 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1394 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1398 /* Set up .got offsets for local syms, and space for local dynamic
1400 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1402 bfd_signed_vma
*local_got
;
1403 bfd_signed_vma
*end_local_got
;
1404 char *local_tls_type
;
1405 bfd_size_type locsymcount
;
1406 Elf_Internal_Shdr
*symtab_hdr
;
1409 if (! is_riscv_elf (ibfd
))
1412 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1414 struct elf_dyn_relocs
*p
;
1416 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1418 if (!bfd_is_abs_section (p
->sec
)
1419 && bfd_is_abs_section (p
->sec
->output_section
))
1421 /* Input section has been discarded, either because
1422 it is a copy of a linkonce section or due to
1423 linker script /DISCARD/, so we'll be discarding
1426 else if (p
->count
!= 0)
1428 srel
= elf_section_data (p
->sec
)->sreloc
;
1429 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1430 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1431 info
->flags
|= DF_TEXTREL
;
1436 local_got
= elf_local_got_refcounts (ibfd
);
1440 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1441 locsymcount
= symtab_hdr
->sh_info
;
1442 end_local_got
= local_got
+ locsymcount
;
1443 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1445 srel
= htab
->elf
.srelgot
;
1446 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1450 *local_got
= s
->size
;
1451 s
->size
+= RISCV_ELF_WORD_BYTES
;
1452 if (*local_tls_type
& GOT_TLS_GD
)
1453 s
->size
+= RISCV_ELF_WORD_BYTES
;
1454 if (bfd_link_pic (info
)
1455 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1456 srel
->size
+= sizeof (ElfNN_External_Rela
);
1459 *local_got
= (bfd_vma
) -1;
1463 /* Allocate .plt and .got entries and space dynamic relocs for
1465 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1467 /* Allocate .plt and .got entries and space dynamic relocs for
1468 global ifunc symbols. */
1469 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1471 /* Allocate .plt and .got entries and space dynamic relocs for
1472 local ifunc symbols. */
1473 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1475 /* Used to resolve the dynamic relocs overwite problems when
1476 generating static executable. */
1477 if (htab
->elf
.irelplt
)
1478 htab
->last_iplt_index
= htab
->elf
.irelplt
->reloc_count
- 1;
1480 if (htab
->elf
.sgotplt
)
1482 struct elf_link_hash_entry
*got
;
1483 got
= elf_link_hash_lookup (elf_hash_table (info
),
1484 "_GLOBAL_OFFSET_TABLE_",
1485 false, false, false);
1487 /* Don't allocate .got.plt section if there are no GOT nor PLT
1488 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1490 || !got
->ref_regular_nonweak
)
1491 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1492 && (htab
->elf
.splt
== NULL
1493 || htab
->elf
.splt
->size
== 0)
1494 && (htab
->elf
.sgot
== NULL
1495 || (htab
->elf
.sgot
->size
1496 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1497 htab
->elf
.sgotplt
->size
= 0;
1500 /* The check_relocs and adjust_dynamic_symbol entry points have
1501 determined the sizes of the various dynamic sections. Allocate
1503 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1505 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1508 if (s
== htab
->elf
.splt
1509 || s
== htab
->elf
.sgot
1510 || s
== htab
->elf
.sgotplt
1511 || s
== htab
->elf
.iplt
1512 || s
== htab
->elf
.igotplt
1513 || s
== htab
->elf
.sdynbss
1514 || s
== htab
->elf
.sdynrelro
1515 || s
== htab
->sdyntdata
)
1517 /* Strip this section if we don't need it; see the
1520 else if (startswith (s
->name
, ".rela"))
1524 /* We use the reloc_count field as a counter if we need
1525 to copy relocs into the output file. */
1531 /* It's not one of our sections. */
1537 /* If we don't need this section, strip it from the
1538 output file. This is mostly to handle .rela.bss and
1539 .rela.plt. We must create both sections in
1540 create_dynamic_sections, because they must be created
1541 before the linker maps input sections to output
1542 sections. The linker does that before
1543 adjust_dynamic_symbol is called, and it is that
1544 function which decides whether anything needs to go
1545 into these sections. */
1546 s
->flags
|= SEC_EXCLUDE
;
1550 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1553 /* Allocate memory for the section contents. Zero the memory
1554 for the benefit of .rela.plt, which has 4 unused entries
1555 at the beginning, and we don't want garbage. */
1556 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1557 if (s
->contents
== NULL
)
1561 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, true);
1565 #define DTP_OFFSET 0x800
1567 /* Return the relocation value for a TLS dtp-relative reloc. */
1570 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1572 /* If tls_sec is NULL, we should have signalled an error already. */
1573 if (elf_hash_table (info
)->tls_sec
== NULL
)
1575 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1578 /* Return the relocation value for a static TLS tp-relative relocation. */
1581 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1583 /* If tls_sec is NULL, we should have signalled an error already. */
1584 if (elf_hash_table (info
)->tls_sec
== NULL
)
1586 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1589 /* Return the global pointer's value, or 0 if it is not in use. */
1592 riscv_global_pointer_value (struct bfd_link_info
*info
)
1594 struct bfd_link_hash_entry
*h
;
1596 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, false, false, true);
1597 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1600 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1603 /* Emplace a static relocation. */
1605 static bfd_reloc_status_type
1606 perform_relocation (const reloc_howto_type
*howto
,
1607 const Elf_Internal_Rela
*rel
,
1609 asection
*input_section
,
1613 if (howto
->pc_relative
)
1614 value
-= sec_addr (input_section
) + rel
->r_offset
;
1615 value
+= rel
->r_addend
;
1617 switch (ELFNN_R_TYPE (rel
->r_info
))
1620 case R_RISCV_TPREL_HI20
:
1621 case R_RISCV_PCREL_HI20
:
1622 case R_RISCV_GOT_HI20
:
1623 case R_RISCV_TLS_GOT_HI20
:
1624 case R_RISCV_TLS_GD_HI20
:
1625 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1626 return bfd_reloc_overflow
;
1627 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1630 case R_RISCV_LO12_I
:
1631 case R_RISCV_GPREL_I
:
1632 case R_RISCV_TPREL_LO12_I
:
1633 case R_RISCV_TPREL_I
:
1634 case R_RISCV_PCREL_LO12_I
:
1635 value
= ENCODE_ITYPE_IMM (value
);
1638 case R_RISCV_LO12_S
:
1639 case R_RISCV_GPREL_S
:
1640 case R_RISCV_TPREL_LO12_S
:
1641 case R_RISCV_TPREL_S
:
1642 case R_RISCV_PCREL_LO12_S
:
1643 value
= ENCODE_STYPE_IMM (value
);
1647 case R_RISCV_CALL_PLT
:
1648 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1649 return bfd_reloc_overflow
;
1650 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1651 | (ENCODE_ITYPE_IMM (value
) << 32);
1655 if (!VALID_JTYPE_IMM (value
))
1656 return bfd_reloc_overflow
;
1657 value
= ENCODE_JTYPE_IMM (value
);
1660 case R_RISCV_BRANCH
:
1661 if (!VALID_BTYPE_IMM (value
))
1662 return bfd_reloc_overflow
;
1663 value
= ENCODE_BTYPE_IMM (value
);
1666 case R_RISCV_RVC_BRANCH
:
1667 if (!VALID_CBTYPE_IMM (value
))
1668 return bfd_reloc_overflow
;
1669 value
= ENCODE_CBTYPE_IMM (value
);
1672 case R_RISCV_RVC_JUMP
:
1673 if (!VALID_CJTYPE_IMM (value
))
1674 return bfd_reloc_overflow
;
1675 value
= ENCODE_CJTYPE_IMM (value
);
1678 case R_RISCV_RVC_LUI
:
1679 if (RISCV_CONST_HIGH_PART (value
) == 0)
1681 /* Linker relaxation can convert an address equal to or greater than
1682 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1683 valid immediate. We can fix this by converting it to a C.LI. */
1684 bfd_vma insn
= riscv_get_insn (howto
->bitsize
,
1685 contents
+ rel
->r_offset
);
1686 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1687 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1688 value
= ENCODE_CITYPE_IMM (0);
1690 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1691 return bfd_reloc_overflow
;
1693 value
= ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1711 case R_RISCV_32_PCREL
:
1712 case R_RISCV_TLS_DTPREL32
:
1713 case R_RISCV_TLS_DTPREL64
:
1716 case R_RISCV_DELETE
:
1717 return bfd_reloc_ok
;
1720 return bfd_reloc_notsupported
;
1724 if (riscv_is_insn_reloc (howto
))
1725 word
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1727 word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1728 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1729 if (riscv_is_insn_reloc (howto
))
1730 riscv_put_insn (howto
->bitsize
, word
, contents
+ rel
->r_offset
);
1732 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1734 return bfd_reloc_ok
;
1737 /* Remember all PC-relative high-part relocs we've encountered to help us
1738 later resolve the corresponding low-part relocs. */
1744 } riscv_pcrel_hi_reloc
;
1746 typedef struct riscv_pcrel_lo_reloc
1748 asection
*input_section
;
1749 struct bfd_link_info
*info
;
1750 reloc_howto_type
*howto
;
1751 const Elf_Internal_Rela
*reloc
;
1755 struct riscv_pcrel_lo_reloc
*next
;
1756 } riscv_pcrel_lo_reloc
;
1761 riscv_pcrel_lo_reloc
*lo_relocs
;
1762 } riscv_pcrel_relocs
;
1765 riscv_pcrel_reloc_hash (const void *entry
)
1767 const riscv_pcrel_hi_reloc
*e
= entry
;
1768 return (hashval_t
)(e
->address
>> 2);
1772 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1774 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1775 return e1
->address
== e2
->address
;
1779 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1781 p
->lo_relocs
= NULL
;
1782 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1783 riscv_pcrel_reloc_eq
, free
);
1784 return p
->hi_relocs
!= NULL
;
1788 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1790 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1794 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1799 htab_delete (p
->hi_relocs
);
1803 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1804 struct bfd_link_info
*info
,
1808 const reloc_howto_type
*howto
,
1809 bfd
*input_bfd ATTRIBUTE_UNUSED
)
1811 /* We may need to reference low addreses in PC-relative modes even when the
1812 PC is far away from these addresses. For example, undefweak references
1813 need to produce the address 0 when linked. As 0 is far from the arbitrary
1814 addresses that we can link PC-relative programs at, the linker can't
1815 actually relocate references to those symbols. In order to allow these
1816 programs to work we simply convert the PC-relative auipc sequences to
1817 0-relative lui sequences. */
1818 if (bfd_link_pic (info
))
1821 /* If it's possible to reference the symbol using auipc we do so, as that's
1822 more in the spirit of the PC-relative relocations we're processing. */
1823 bfd_vma offset
= addr
- pc
;
1824 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1827 /* If it's impossible to reference this with a LUI-based offset then don't
1828 bother to convert it at all so users still see the PC-relative relocation
1829 in the truncation message. */
1830 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1833 rel
->r_info
= ELFNN_R_INFO (addr
, R_RISCV_HI20
);
1835 bfd_vma insn
= riscv_get_insn (howto
->bitsize
, contents
+ rel
->r_offset
);
1836 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1837 riscv_put_insn (howto
->bitsize
, insn
, contents
+ rel
->r_offset
);
1842 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1843 bfd_vma value
, bool absolute
)
1845 bfd_vma offset
= absolute
? value
: value
- addr
;
1846 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1847 riscv_pcrel_hi_reloc
**slot
=
1848 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1850 BFD_ASSERT (*slot
== NULL
);
1851 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1859 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1860 asection
*input_section
,
1861 struct bfd_link_info
*info
,
1862 reloc_howto_type
*howto
,
1863 const Elf_Internal_Rela
*reloc
,
1868 riscv_pcrel_lo_reloc
*entry
;
1869 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1872 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1873 name
, contents
, p
->lo_relocs
};
1874 p
->lo_relocs
= entry
;
1879 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1881 riscv_pcrel_lo_reloc
*r
;
1883 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1885 bfd
*input_bfd
= r
->input_section
->owner
;
1887 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1888 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1890 /* Check the overflow when adding reloc addend. */
1891 || (RISCV_CONST_HIGH_PART (entry
->value
)
1892 != RISCV_CONST_HIGH_PART (entry
->value
+ r
->reloc
->r_addend
)))
1896 string
= _("%pcrel_lo missing matching %pcrel_hi");
1897 else if (asprintf (&string
,
1898 _("%%pcrel_lo overflow with an addend, the "
1899 "value of %%pcrel_hi is 0x%" PRIx64
" without "
1900 "any addend, but may be 0x%" PRIx64
" after "
1901 "adding the %%pcrel_lo addend"),
1902 (int64_t) RISCV_CONST_HIGH_PART (entry
->value
),
1903 (int64_t) RISCV_CONST_HIGH_PART
1904 (entry
->value
+ r
->reloc
->r_addend
)) == -1)
1905 string
= _("%pcrel_lo overflow with an addend");
1907 (*r
->info
->callbacks
->reloc_dangerous
)
1908 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1912 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1913 input_bfd
, r
->contents
);
1919 /* Relocate a RISC-V ELF section.
1921 The RELOCATE_SECTION function is called by the new ELF backend linker
1922 to handle the relocations for a section.
1924 The relocs are always passed as Rela structures.
1926 This function is responsible for adjusting the section contents as
1927 necessary, and (if generating a relocatable output file) adjusting
1928 the reloc addend as necessary.
1930 This function does not have to worry about setting the reloc
1931 address or the reloc symbol index.
1933 LOCAL_SYMS is a pointer to the swapped in local symbols.
1935 LOCAL_SECTIONS is an array giving the section in the input file
1936 corresponding to the st_shndx field of each local symbol.
1938 The global hash table entry for the global symbols can be found
1939 via elf_sym_hashes (input_bfd).
1941 When generating relocatable output, this function must handle
1942 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1943 going to be the section symbol corresponding to the output
1944 section, which means that the addend must be adjusted
1948 riscv_elf_relocate_section (bfd
*output_bfd
,
1949 struct bfd_link_info
*info
,
1951 asection
*input_section
,
1953 Elf_Internal_Rela
*relocs
,
1954 Elf_Internal_Sym
*local_syms
,
1955 asection
**local_sections
)
1957 Elf_Internal_Rela
*rel
;
1958 Elf_Internal_Rela
*relend
;
1959 riscv_pcrel_relocs pcrel_relocs
;
1961 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1962 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1963 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1964 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1967 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1970 relend
= relocs
+ input_section
->reloc_count
;
1971 for (rel
= relocs
; rel
< relend
; rel
++)
1973 unsigned long r_symndx
;
1974 struct elf_link_hash_entry
*h
;
1975 Elf_Internal_Sym
*sym
;
1978 bfd_reloc_status_type r
= bfd_reloc_ok
;
1979 const char *name
= NULL
;
1980 bfd_vma off
, ie_off
;
1981 bool unresolved_reloc
, is_ie
= false;
1982 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1983 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1984 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1985 const char *msg
= NULL
;
1986 char *msg_buf
= NULL
;
1987 bool resolved_to_zero
;
1990 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1993 /* This is a final link. */
1994 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1998 unresolved_reloc
= false;
1999 if (r_symndx
< symtab_hdr
->sh_info
)
2001 sym
= local_syms
+ r_symndx
;
2002 sec
= local_sections
[r_symndx
];
2003 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
2005 /* Relocate against local STT_GNU_IFUNC symbol. */
2006 if (!bfd_link_relocatable (info
)
2007 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
2009 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, false);
2013 /* Set STT_GNU_IFUNC symbol value. */
2014 h
->root
.u
.def
.value
= sym
->st_value
;
2015 h
->root
.u
.def
.section
= sec
;
2020 bool warned
, ignored
;
2022 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
2023 r_symndx
, symtab_hdr
, sym_hashes
,
2025 unresolved_reloc
, warned
, ignored
);
2028 /* To avoid generating warning messages about truncated
2029 relocations, set the relocation's address to be the same as
2030 the start of this section. */
2031 if (input_section
->output_section
!= NULL
)
2032 relocation
= input_section
->output_section
->vma
;
2038 if (sec
!= NULL
&& discarded_section (sec
))
2039 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
2040 rel
, 1, relend
, howto
, 0, contents
);
2042 if (bfd_link_relocatable (info
))
2045 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2046 it here if it is defined in a non-shared object. */
2048 && h
->type
== STT_GNU_IFUNC
2051 asection
*plt
, *base_got
;
2053 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2055 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2056 STT_GNU_IFUNC symbol as STT_FUNC. */
2057 if (elf_section_type (input_section
) == SHT_NOTE
)
2060 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2061 sections because such sections are not SEC_ALLOC and
2062 thus ld.so will not process them. */
2063 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
2068 else if (h
->plt
.offset
== (bfd_vma
) -1
2069 /* The following relocation may not need the .plt entries
2070 when all references to a STT_GNU_IFUNC symbols are done
2071 via GOT or static function pointers. */
2072 && r_type
!= R_RISCV_32
2073 && r_type
!= R_RISCV_64
2074 && r_type
!= R_RISCV_HI20
2075 && r_type
!= R_RISCV_GOT_HI20
2076 && r_type
!= R_RISCV_LO12_I
2077 && r_type
!= R_RISCV_LO12_S
)
2078 goto bad_ifunc_reloc
;
2080 /* STT_GNU_IFUNC symbol must go through PLT. */
2081 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2082 relocation
= plt
->output_section
->vma
2083 + plt
->output_offset
2090 if (rel
->r_addend
!= 0)
2092 if (h
->root
.root
.string
)
2093 name
= h
->root
.root
.string
;
2095 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2098 /* xgettext:c-format */
2099 (_("%pB: relocation %s against STT_GNU_IFUNC "
2100 "symbol `%s' has non-zero addend: %" PRId64
),
2101 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2102 bfd_set_error (bfd_error_bad_value
);
2106 /* Generate dynamic relocation only when there is a non-GOT
2107 reference in a shared object or there is no PLT. */
2108 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2109 || h
->plt
.offset
== (bfd_vma
) -1)
2111 Elf_Internal_Rela outrel
;
2114 /* Need a dynamic relocation to get the real function
2116 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2120 if (outrel
.r_offset
== (bfd_vma
) -1
2121 || outrel
.r_offset
== (bfd_vma
) -2)
2124 outrel
.r_offset
+= input_section
->output_section
->vma
2125 + input_section
->output_offset
;
2127 if (h
->dynindx
== -1
2129 || bfd_link_executable (info
))
2131 info
->callbacks
->minfo
2132 (_("Local IFUNC function `%s' in %pB\n"),
2133 h
->root
.root
.string
,
2134 h
->root
.u
.def
.section
->owner
);
2136 /* This symbol is resolved locally. */
2137 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2138 outrel
.r_addend
= h
->root
.u
.def
.value
2139 + h
->root
.u
.def
.section
->output_section
->vma
2140 + h
->root
.u
.def
.section
->output_offset
;
2144 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2145 outrel
.r_addend
= 0;
2148 /* Dynamic relocations are stored in
2149 1. .rela.ifunc section in PIC object.
2150 2. .rela.got section in dynamic executable.
2151 3. .rela.iplt section in static executable. */
2152 if (bfd_link_pic (info
))
2153 sreloc
= htab
->elf
.irelifunc
;
2154 else if (htab
->elf
.splt
!= NULL
)
2155 sreloc
= htab
->elf
.srelgot
;
2157 sreloc
= htab
->elf
.irelplt
;
2159 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2161 /* If this reloc is against an external symbol, we
2162 do not want to fiddle with the addend. Otherwise,
2163 we need to include the symbol value so that it
2164 becomes an addend for the dynamic reloc. For an
2165 internal symbol, we have updated addend. */
2170 case R_RISCV_GOT_HI20
:
2171 base_got
= htab
->elf
.sgot
;
2172 off
= h
->got
.offset
;
2174 if (base_got
== NULL
)
2177 if (off
== (bfd_vma
) -1)
2181 /* We can't use h->got.offset here to save state, or
2182 even just remember the offset, as finish_dynamic_symbol
2183 would use that as offset into .got. */
2185 if (htab
->elf
.splt
!= NULL
)
2187 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2189 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2190 base_got
= htab
->elf
.sgotplt
;
2194 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2195 off
= plt_idx
* GOT_ENTRY_SIZE
;
2196 base_got
= htab
->elf
.igotplt
;
2199 if (h
->dynindx
== -1
2203 /* This references the local definition. We must
2204 initialize this entry in the global offset table.
2205 Since the offset must always be a multiple of 8,
2206 we use the least significant bit to record
2207 whether we have initialized it already.
2209 When doing a dynamic link, we create a .rela.got
2210 relocation entry to initialize the value. This
2211 is done in the finish_dynamic_symbol routine. */
2216 bfd_put_NN (output_bfd
, relocation
,
2217 base_got
->contents
+ off
);
2218 /* Note that this is harmless for the case,
2219 as -1 | 1 still is -1. */
2225 relocation
= base_got
->output_section
->vma
2226 + base_got
->output_offset
+ off
;
2228 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2229 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2231 r
= bfd_reloc_notsupported
;
2232 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2234 r
= bfd_reloc_overflow
;
2238 case R_RISCV_CALL_PLT
:
2240 case R_RISCV_LO12_I
:
2241 case R_RISCV_LO12_S
:
2244 case R_RISCV_PCREL_HI20
:
2245 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2246 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2248 r
= bfd_reloc_notsupported
;
2249 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2251 r
= bfd_reloc_overflow
;
2256 if (h
->root
.root
.string
)
2257 name
= h
->root
.root
.string
;
2259 /* The entry of local ifunc is fake in global hash table,
2260 we should find the name by the original local symbol. */
2261 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2264 /* xgettext:c-format */
2265 (_("%pB: relocation %s against STT_GNU_IFUNC "
2266 "symbol `%s' isn't supported"), input_bfd
,
2268 bfd_set_error (bfd_error_bad_value
);
2275 name
= h
->root
.root
.string
;
2278 name
= (bfd_elf_string_from_elf_section
2279 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2280 if (name
== NULL
|| *name
== '\0')
2281 name
= bfd_section_name (sec
);
2284 resolved_to_zero
= (h
!= NULL
2285 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2291 case R_RISCV_TPREL_ADD
:
2293 case R_RISCV_JUMP_SLOT
:
2294 case R_RISCV_RELATIVE
:
2295 /* These require nothing of us at all. */
2299 case R_RISCV_BRANCH
:
2300 case R_RISCV_RVC_BRANCH
:
2301 case R_RISCV_RVC_LUI
:
2302 case R_RISCV_LO12_I
:
2303 case R_RISCV_LO12_S
:
2308 case R_RISCV_32_PCREL
:
2309 case R_RISCV_DELETE
:
2310 /* These require no special handling beyond perform_relocation. */
2313 case R_RISCV_GOT_HI20
:
2318 off
= h
->got
.offset
;
2319 BFD_ASSERT (off
!= (bfd_vma
) -1);
2320 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2321 pic
= bfd_link_pic (info
);
2323 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2324 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
2326 /* This is actually a static link, or it is a
2327 -Bsymbolic link and the symbol is defined
2328 locally, or the symbol was forced to be local
2329 because of a version file. We must initialize
2330 this entry in the global offset table. Since the
2331 offset must always be a multiple of the word size,
2332 we use the least significant bit to record whether
2333 we have initialized it already.
2335 When doing a dynamic link, we create a .rela.got
2336 relocation entry to initialize the value. This
2337 is done in the finish_dynamic_symbol routine. */
2342 bfd_put_NN (output_bfd
, relocation
,
2343 htab
->elf
.sgot
->contents
+ off
);
2348 unresolved_reloc
= false;
2352 BFD_ASSERT (local_got_offsets
!= NULL
2353 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2355 off
= local_got_offsets
[r_symndx
];
2357 /* The offset must always be a multiple of the word size.
2358 So, we can use the least significant bit to record
2359 whether we have already processed this entry. */
2364 if (bfd_link_pic (info
))
2367 Elf_Internal_Rela outrel
;
2369 /* We need to generate a R_RISCV_RELATIVE reloc
2370 for the dynamic linker. */
2371 s
= htab
->elf
.srelgot
;
2372 BFD_ASSERT (s
!= NULL
);
2374 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2376 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2377 outrel
.r_addend
= relocation
;
2379 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2382 bfd_put_NN (output_bfd
, relocation
,
2383 htab
->elf
.sgot
->contents
+ off
);
2384 local_got_offsets
[r_symndx
] |= 1;
2387 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2388 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2395 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2396 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2398 r
= bfd_reloc_notsupported
;
2399 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2400 relocation
, absolute
))
2401 r
= bfd_reloc_overflow
;
2409 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2410 contents
+ rel
->r_offset
);
2411 relocation
= old_value
+ relocation
;
2421 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2422 contents
+ rel
->r_offset
);
2423 relocation
= old_value
- relocation
;
2428 case R_RISCV_CALL_PLT
:
2429 /* Handle a call to an undefined weak function. This won't be
2430 relaxed, so we have to handle it here. */
2431 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2432 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2434 /* We can use x0 as the base register. */
2435 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
2436 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2437 bfd_putl32 (insn
, contents
+ rel
->r_offset
+ 4);
2438 /* Set the relocation value so that we get 0 after the pc
2439 relative adjustment. */
2440 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2445 case R_RISCV_RVC_JUMP
:
2446 /* This line has to match the check in _bfd_riscv_relax_section. */
2447 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2449 /* Refer to the PLT entry. */
2450 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2451 unresolved_reloc
= false;
2455 case R_RISCV_TPREL_HI20
:
2456 relocation
= tpoff (info
, relocation
);
2459 case R_RISCV_TPREL_LO12_I
:
2460 case R_RISCV_TPREL_LO12_S
:
2461 relocation
= tpoff (info
, relocation
);
2464 case R_RISCV_TPREL_I
:
2465 case R_RISCV_TPREL_S
:
2466 relocation
= tpoff (info
, relocation
);
2467 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2469 /* We can use tp as the base register. */
2470 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2471 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2472 insn
|= X_TP
<< OP_SH_RS1
;
2473 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2476 r
= bfd_reloc_overflow
;
2479 case R_RISCV_GPREL_I
:
2480 case R_RISCV_GPREL_S
:
2482 bfd_vma gp
= riscv_global_pointer_value (info
);
2483 bool x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2484 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2486 /* We can use x0 or gp as the base register. */
2487 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
2488 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2491 rel
->r_addend
-= gp
;
2492 insn
|= X_GP
<< OP_SH_RS1
;
2494 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
2497 r
= bfd_reloc_overflow
;
2501 case R_RISCV_PCREL_HI20
:
2502 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2509 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2510 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2512 r
= bfd_reloc_notsupported
;
2513 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2514 relocation
+ rel
->r_addend
,
2516 r
= bfd_reloc_overflow
;
2519 case R_RISCV_PCREL_LO12_I
:
2520 case R_RISCV_PCREL_LO12_S
:
2521 /* We don't allow section symbols plus addends as the auipc address,
2522 because then riscv_relax_delete_bytes would have to search through
2523 all relocs to update these addends. This is also ambiguous, as
2524 we do allow offsets to be added to the target address, which are
2525 not to be used to find the auipc address. */
2526 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2527 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2530 msg
= _("%pcrel_lo section symbol with an addend");
2531 r
= bfd_reloc_dangerous
;
2535 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2536 howto
, rel
, relocation
, name
,
2539 r
= bfd_reloc_overflow
;
2542 case R_RISCV_TLS_DTPREL32
:
2543 case R_RISCV_TLS_DTPREL64
:
2544 relocation
= dtpoff (info
, relocation
);
2549 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2552 if ((bfd_link_pic (info
)
2554 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2555 && !resolved_to_zero
)
2556 || h
->root
.type
!= bfd_link_hash_undefweak
)
2557 && (!howto
->pc_relative
2558 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2559 || (!bfd_link_pic (info
)
2565 || h
->root
.type
== bfd_link_hash_undefweak
2566 || h
->root
.type
== bfd_link_hash_undefined
)))
2568 Elf_Internal_Rela outrel
;
2570 bool skip_static_relocation
, skip_dynamic_relocation
;
2572 /* When generating a shared object, these relocations
2573 are copied into the output file to be resolved at run
2577 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2579 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2580 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2581 outrel
.r_offset
+= sec_addr (input_section
);
2583 if (skip_dynamic_relocation
)
2584 memset (&outrel
, 0, sizeof outrel
);
2585 else if (h
!= NULL
&& h
->dynindx
!= -1
2586 && !(bfd_link_pic (info
)
2587 && SYMBOLIC_BIND (info
, h
)
2590 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2591 outrel
.r_addend
= rel
->r_addend
;
2595 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2596 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2599 sreloc
= elf_section_data (input_section
)->sreloc
;
2600 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2601 if (skip_static_relocation
)
2606 case R_RISCV_TLS_GOT_HI20
:
2610 case R_RISCV_TLS_GD_HI20
:
2613 off
= h
->got
.offset
;
2618 off
= local_got_offsets
[r_symndx
];
2619 local_got_offsets
[r_symndx
] |= 1;
2622 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2623 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2624 /* If this symbol is referenced by both GD and IE TLS, the IE
2625 reference's GOT slot follows the GD reference's slots. */
2627 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2628 ie_off
= 2 * GOT_ENTRY_SIZE
;
2634 Elf_Internal_Rela outrel
;
2636 bool need_relocs
= false;
2638 if (htab
->elf
.srelgot
== NULL
)
2644 dyn
= htab
->elf
.dynamic_sections_created
;
2645 pic
= bfd_link_pic (info
);
2647 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2648 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2652 /* The GOT entries have not been initialized yet. Do it
2653 now, and emit any relocations. */
2654 if ((bfd_link_pic (info
) || indx
!= 0)
2656 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2657 || h
->root
.type
!= bfd_link_hash_undefweak
))
2660 if (tls_type
& GOT_TLS_GD
)
2664 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2665 outrel
.r_addend
= 0;
2666 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2667 bfd_put_NN (output_bfd
, 0,
2668 htab
->elf
.sgot
->contents
+ off
);
2669 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2672 BFD_ASSERT (! unresolved_reloc
);
2673 bfd_put_NN (output_bfd
,
2674 dtpoff (info
, relocation
),
2675 (htab
->elf
.sgot
->contents
2676 + off
+ RISCV_ELF_WORD_BYTES
));
2680 bfd_put_NN (output_bfd
, 0,
2681 (htab
->elf
.sgot
->contents
2682 + off
+ RISCV_ELF_WORD_BYTES
));
2683 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2684 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2685 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2690 /* If we are not emitting relocations for a
2691 general dynamic reference, then we must be in a
2692 static link or an executable link with the
2693 symbol binding locally. Mark it as belonging
2694 to module 1, the executable. */
2695 bfd_put_NN (output_bfd
, 1,
2696 htab
->elf
.sgot
->contents
+ off
);
2697 bfd_put_NN (output_bfd
,
2698 dtpoff (info
, relocation
),
2699 (htab
->elf
.sgot
->contents
2700 + off
+ RISCV_ELF_WORD_BYTES
));
2704 if (tls_type
& GOT_TLS_IE
)
2708 bfd_put_NN (output_bfd
, 0,
2709 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2710 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2712 outrel
.r_addend
= 0;
2714 outrel
.r_addend
= tpoff (info
, relocation
);
2715 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2716 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2720 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2721 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2726 BFD_ASSERT (off
< (bfd_vma
) -2);
2727 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2728 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2730 r
= bfd_reloc_overflow
;
2731 unresolved_reloc
= false;
2735 r
= bfd_reloc_notsupported
;
2738 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2739 because such sections are not SEC_ALLOC and thus ld.so will
2740 not process them. */
2741 if (unresolved_reloc
2742 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2744 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2745 rel
->r_offset
) != (bfd_vma
) -1)
2750 case R_RISCV_RVC_JUMP
:
2751 if (asprintf (&msg_buf
,
2752 _("%%X%%P: relocation %s against `%s' can "
2753 "not be used when making a shared object; "
2754 "recompile with -fPIC\n"),
2756 h
->root
.root
.string
) == -1)
2761 if (asprintf (&msg_buf
,
2762 _("%%X%%P: unresolvable %s relocation against "
2765 h
->root
.root
.string
) == -1)
2771 r
= bfd_reloc_notsupported
;
2775 if (r
== bfd_reloc_ok
)
2776 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2777 input_bfd
, contents
);
2779 /* We should have already detected the error and set message before.
2780 If the error message isn't set since the linker runs out of memory
2781 or we don't set it before, then we should set the default message
2782 with the "internal error" string here. */
2788 case bfd_reloc_overflow
:
2789 info
->callbacks
->reloc_overflow
2790 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2791 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2794 case bfd_reloc_undefined
:
2795 info
->callbacks
->undefined_symbol
2796 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2800 case bfd_reloc_outofrange
:
2802 msg
= _("%X%P: internal error: out of range error\n");
2805 case bfd_reloc_notsupported
:
2807 msg
= _("%X%P: internal error: unsupported relocation error\n");
2810 case bfd_reloc_dangerous
:
2811 /* The error message should already be set. */
2813 msg
= _("dangerous relocation error");
2814 info
->callbacks
->reloc_dangerous
2815 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2819 msg
= _("%X%P: internal error: unknown error\n");
2823 /* Do not report error message for the dangerous relocation again. */
2824 if (msg
&& r
!= bfd_reloc_dangerous
)
2825 info
->callbacks
->einfo (msg
);
2827 /* Free the unused `msg_buf`. */
2830 /* We already reported the error via a callback, so don't try to report
2831 it again by returning false. That leads to spurious errors. */
2836 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2838 riscv_free_pcrel_relocs (&pcrel_relocs
);
2842 /* Finish up dynamic symbol handling. We set the contents of various
2843 dynamic sections here. */
2846 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2847 struct bfd_link_info
*info
,
2848 struct elf_link_hash_entry
*h
,
2849 Elf_Internal_Sym
*sym
)
2851 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2852 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2854 if (h
->plt
.offset
!= (bfd_vma
) -1)
2856 /* We've decided to create a PLT entry for this symbol. */
2858 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2859 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2860 Elf_Internal_Rela rela
;
2861 asection
*plt
, *gotplt
, *relplt
;
2863 /* When building a static executable, use .iplt, .igot.plt and
2864 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2865 if (htab
->elf
.splt
!= NULL
)
2867 plt
= htab
->elf
.splt
;
2868 gotplt
= htab
->elf
.sgotplt
;
2869 relplt
= htab
->elf
.srelplt
;
2873 plt
= htab
->elf
.iplt
;
2874 gotplt
= htab
->elf
.igotplt
;
2875 relplt
= htab
->elf
.irelplt
;
2878 /* This symbol has an entry in the procedure linkage table. Set
2880 if ((h
->dynindx
== -1
2881 && !((h
->forced_local
|| bfd_link_executable (info
))
2883 && h
->type
== STT_GNU_IFUNC
))
2889 /* Calculate the address of the PLT header. */
2890 header_address
= sec_addr (plt
);
2892 /* Calculate the index of the entry and the offset of .got.plt entry.
2893 For static executables, we don't reserve anything. */
2894 if (plt
== htab
->elf
.splt
)
2896 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2897 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2901 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2902 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2905 /* Calculate the address of the .got.plt entry. */
2906 got_address
= sec_addr (gotplt
) + got_offset
;
2908 /* Find out where the .plt entry should go. */
2909 loc
= plt
->contents
+ h
->plt
.offset
;
2911 /* Fill in the PLT entry itself. */
2912 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2913 header_address
+ h
->plt
.offset
,
2917 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2918 bfd_putl32 (plt_entry
[i
], loc
+ 4*i
);
2920 /* Fill in the initial value of the .got.plt entry. */
2921 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2922 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2924 rela
.r_offset
= got_address
;
2926 if (h
->dynindx
== -1
2927 || ((bfd_link_executable (info
)
2928 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2930 && h
->type
== STT_GNU_IFUNC
))
2932 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2933 h
->root
.root
.string
,
2934 h
->root
.u
.def
.section
->owner
);
2936 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2937 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2938 asection
*sec
= h
->root
.u
.def
.section
;
2939 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2940 rela
.r_addend
= h
->root
.u
.def
.value
2941 + sec
->output_section
->vma
2942 + sec
->output_offset
;
2946 /* Fill in the entry in the .rela.plt section. */
2947 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2951 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2952 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2954 if (!h
->def_regular
)
2956 /* Mark the symbol as undefined, rather than as defined in
2957 the .plt section. Leave the value alone. */
2958 sym
->st_shndx
= SHN_UNDEF
;
2959 /* If the symbol is weak, we do need to clear the value.
2960 Otherwise, the PLT entry would provide a definition for
2961 the symbol even if the symbol wasn't defined anywhere,
2962 and so the symbol would never be NULL. */
2963 if (!h
->ref_regular_nonweak
)
2968 if (h
->got
.offset
!= (bfd_vma
) -1
2969 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2970 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2974 Elf_Internal_Rela rela
;
2975 bool use_elf_append_rela
= true;
2977 /* This symbol has an entry in the GOT. Set it up. */
2979 sgot
= htab
->elf
.sgot
;
2980 srela
= htab
->elf
.srelgot
;
2981 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2983 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2985 /* Handle the ifunc symbol in GOT entry. */
2987 && h
->type
== STT_GNU_IFUNC
)
2989 if (h
->plt
.offset
== (bfd_vma
) -1)
2991 /* STT_GNU_IFUNC is referenced without PLT. */
2993 if (htab
->elf
.splt
== NULL
)
2995 /* Use .rela.iplt section to store .got relocations
2996 in static executable. */
2997 srela
= htab
->elf
.irelplt
;
2999 /* Do not use riscv_elf_append_rela to add dynamic
3001 use_elf_append_rela
= false;
3004 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
3006 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
3007 h
->root
.root
.string
,
3008 h
->root
.u
.def
.section
->owner
);
3010 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
3011 rela
.r_addend
= (h
->root
.u
.def
.value
3012 + h
->root
.u
.def
.section
->output_section
->vma
3013 + h
->root
.u
.def
.section
->output_offset
);
3017 /* Generate R_RISCV_NN. */
3018 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3019 BFD_ASSERT (h
->dynindx
!= -1);
3020 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3024 else if (bfd_link_pic (info
))
3026 /* Generate R_RISCV_NN. */
3027 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3028 BFD_ASSERT (h
->dynindx
!= -1);
3029 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3036 if (!h
->pointer_equality_needed
)
3039 /* For non-shared object, we can't use .got.plt, which
3040 contains the real function address if we need pointer
3041 equality. We load the GOT entry with the PLT entry. */
3042 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
3043 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
3044 + plt
->output_offset
3046 htab
->elf
.sgot
->contents
3047 + (h
->got
.offset
& ~(bfd_vma
) 1));
3051 else if (bfd_link_pic (info
)
3052 && SYMBOL_REFERENCES_LOCAL (info
, h
))
3054 /* If this is a local symbol reference, we just want to emit
3055 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3056 or a pie link, or the symbol was forced to be local because
3057 of a version file. The entry in the global offset table will
3058 already have been initialized in the relocate_section function. */
3059 BFD_ASSERT ((h
->got
.offset
& 1) != 0);
3060 asection
*sec
= h
->root
.u
.def
.section
;
3061 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
3062 rela
.r_addend
= (h
->root
.u
.def
.value
3063 + sec
->output_section
->vma
3064 + sec
->output_offset
);
3068 BFD_ASSERT ((h
->got
.offset
& 1) == 0);
3069 BFD_ASSERT (h
->dynindx
!= -1);
3070 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
3074 bfd_put_NN (output_bfd
, 0,
3075 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3077 if (use_elf_append_rela
)
3078 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3081 /* Use riscv_elf_append_rela to add the dynamic relocs into
3082 .rela.iplt may cause the overwrite problems. Since we insert
3083 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3084 but the riscv_elf_append_rela adds the relocs to the place
3085 that are calculated from the reloc_index (in seqential).
3087 One solution is that add these dynamic relocs (GOT IFUNC)
3088 from the last of .rela.iplt section. */
3089 bfd_vma iplt_idx
= htab
->last_iplt_index
--;
3090 bfd_byte
*loc
= srela
->contents
3091 + iplt_idx
* sizeof (ElfNN_External_Rela
);
3092 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
3098 Elf_Internal_Rela rela
;
3101 /* This symbols needs a copy reloc. Set it up. */
3102 BFD_ASSERT (h
->dynindx
!= -1);
3104 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3105 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3107 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3108 s
= htab
->elf
.sreldynrelro
;
3110 s
= htab
->elf
.srelbss
;
3111 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3114 /* Mark some specially defined symbols as absolute. */
3115 if (h
== htab
->elf
.hdynamic
3116 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3117 sym
->st_shndx
= SHN_ABS
;
3122 /* Finish up local dynamic symbol handling. We set the contents of
3123 various dynamic sections here. */
3126 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3128 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3129 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3131 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3134 /* Finish up the dynamic sections. */
3137 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3138 bfd
*dynobj
, asection
*sdyn
)
3140 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3141 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3142 size_t dynsize
= bed
->s
->sizeof_dyn
;
3143 bfd_byte
*dyncon
, *dynconend
;
3145 dynconend
= sdyn
->contents
+ sdyn
->size
;
3146 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3148 Elf_Internal_Dyn dyn
;
3151 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3156 s
= htab
->elf
.sgotplt
;
3157 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3160 s
= htab
->elf
.srelplt
;
3161 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3164 s
= htab
->elf
.srelplt
;
3165 dyn
.d_un
.d_val
= s
->size
;
3171 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3177 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3178 struct bfd_link_info
*info
)
3182 struct riscv_elf_link_hash_table
*htab
;
3184 htab
= riscv_elf_hash_table (info
);
3185 BFD_ASSERT (htab
!= NULL
);
3186 dynobj
= htab
->elf
.dynobj
;
3188 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3190 if (elf_hash_table (info
)->dynamic_sections_created
)
3195 splt
= htab
->elf
.splt
;
3196 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3198 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3203 /* Fill in the head and tail entries in the procedure linkage table. */
3207 uint32_t plt_header
[PLT_HEADER_INSNS
];
3208 ret
= riscv_make_plt_header (output_bfd
,
3209 sec_addr (htab
->elf
.sgotplt
),
3210 sec_addr (splt
), plt_header
);
3214 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3215 bfd_putl32 (plt_header
[i
], splt
->contents
+ 4*i
);
3217 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3222 if (htab
->elf
.sgotplt
)
3224 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3226 if (bfd_is_abs_section (output_section
))
3228 (*_bfd_error_handler
)
3229 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3233 if (htab
->elf
.sgotplt
->size
> 0)
3235 /* Write the first two entries in .got.plt, needed for the dynamic
3237 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3238 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3239 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3242 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3247 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3249 if (htab
->elf
.sgot
->size
> 0)
3251 /* Set the first entry in the global offset table to the address of
3252 the dynamic section. */
3253 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3254 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3257 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3260 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3261 htab_traverse (htab
->loc_hash_table
,
3262 riscv_elf_finish_local_dynamic_symbol
,
3268 /* Return address for Ith PLT stub in section PLT, for relocation REL
3269 or (bfd_vma) -1 if it should not be included. */
3272 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3273 const arelent
*rel ATTRIBUTE_UNUSED
)
3275 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3278 static enum elf_reloc_type_class
3279 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3280 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3281 const Elf_Internal_Rela
*rela
)
3283 switch (ELFNN_R_TYPE (rela
->r_info
))
3285 case R_RISCV_RELATIVE
:
3286 return reloc_class_relative
;
3287 case R_RISCV_JUMP_SLOT
:
3288 return reloc_class_plt
;
3290 return reloc_class_copy
;
3292 return reloc_class_normal
;
3296 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3300 riscv_float_abi_string (flagword flags
)
3302 switch (flags
& EF_RISCV_FLOAT_ABI
)
3304 case EF_RISCV_FLOAT_ABI_SOFT
:
3305 return "soft-float";
3307 case EF_RISCV_FLOAT_ABI_SINGLE
:
3308 return "single-float";
3310 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3311 return "double-float";
3313 case EF_RISCV_FLOAT_ABI_QUAD
:
3314 return "quad-float";
3321 /* The information of architecture elf attributes. */
3322 static riscv_subset_list_t in_subsets
;
3323 static riscv_subset_list_t out_subsets
;
3324 static riscv_subset_list_t merged_subsets
;
3326 /* Predicator for standard extension. */
3329 riscv_std_ext_p (const char *name
)
3331 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3334 /* Check if the versions are compatible. */
3337 riscv_version_mismatch (bfd
*ibfd
,
3338 struct riscv_subset_t
*in
,
3339 struct riscv_subset_t
*out
)
3341 if (in
== NULL
|| out
== NULL
)
3344 /* Since there are no version conflicts for now, we just report
3345 warning when the versions are mis-matched. */
3346 if (in
->major_version
!= out
->major_version
3347 || in
->minor_version
!= out
->minor_version
)
3349 if ((in
->major_version
== RISCV_UNKNOWN_VERSION
3350 && in
->minor_version
== RISCV_UNKNOWN_VERSION
)
3351 || (out
->major_version
== RISCV_UNKNOWN_VERSION
3352 && out
->minor_version
== RISCV_UNKNOWN_VERSION
))
3354 /* Do not report the warning when the version of input
3355 or output is RISCV_UNKNOWN_VERSION, since the extension
3356 is added implicitly. */
3360 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
3361 "extension, the output version is %d.%d"),
3367 out
->minor_version
);
3369 /* Update the output ISA versions to the newest ones. */
3370 if ((in
->major_version
> out
->major_version
)
3371 || (in
->major_version
== out
->major_version
3372 && in
->minor_version
> out
->minor_version
))
3374 out
->major_version
= in
->major_version
;
3375 out
->minor_version
= in
->minor_version
;
3382 /* Return true if subset is 'i' or 'e'. */
3385 riscv_i_or_e_p (bfd
*ibfd
,
3387 struct riscv_subset_t
*subset
)
3389 if ((strcasecmp (subset
->name
, "e") != 0)
3390 && (strcasecmp (subset
->name
, "i") != 0))
3393 (_("error: %pB: corrupted ISA string '%s'. "
3394 "First letter should be 'i' or 'e' but got '%s'"),
3395 ibfd
, arch
, subset
->name
);
3401 /* Merge standard extensions.
3404 Return FALSE if failed to merge.
3408 `in_arch`: Raw ISA string for input object.
3409 `out_arch`: Raw ISA string for output object.
3410 `pin`: Subset list for input object.
3411 `pout`: Subset list for output object. */
3414 riscv_merge_std_ext (bfd
*ibfd
,
3415 const char *in_arch
,
3416 const char *out_arch
,
3417 struct riscv_subset_t
**pin
,
3418 struct riscv_subset_t
**pout
)
3420 const char *standard_exts
= riscv_supported_std_ext ();
3422 struct riscv_subset_t
*in
= *pin
;
3423 struct riscv_subset_t
*out
= *pout
;
3425 /* First letter should be 'i' or 'e'. */
3426 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3429 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3432 if (strcasecmp (in
->name
, out
->name
) != 0)
3434 /* TODO: We might allow merge 'i' with 'e'. */
3436 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3437 ibfd
, in
->name
, out
->name
);
3440 else if (!riscv_version_mismatch (ibfd
, in
, out
))
3443 riscv_add_subset (&merged_subsets
,
3444 out
->name
, out
->major_version
, out
->minor_version
);
3449 /* Handle standard extension first. */
3450 for (p
= standard_exts
; *p
; ++p
)
3452 struct riscv_subset_t
*ext_in
, *ext_out
, *ext_merged
;
3453 char find_ext
[2] = {*p
, '\0'};
3454 bool find_in
, find_out
;
3456 find_in
= riscv_lookup_subset (&in_subsets
, find_ext
, &ext_in
);
3457 find_out
= riscv_lookup_subset (&out_subsets
, find_ext
, &ext_out
);
3459 if (!find_in
&& !find_out
)
3464 && !riscv_version_mismatch (ibfd
, ext_in
, ext_out
))
3467 ext_merged
= find_out
? ext_out
: ext_in
;
3468 riscv_add_subset (&merged_subsets
, ext_merged
->name
,
3469 ext_merged
->major_version
, ext_merged
->minor_version
);
3472 /* Skip all standard extensions. */
3473 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3474 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3482 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3483 object subset list. Likewise for POUT and the output object. Return TRUE
3484 on success and FALSE when a conflict is found. */
3487 riscv_merge_multi_letter_ext (bfd
*ibfd
,
3488 riscv_subset_t
**pin
,
3489 riscv_subset_t
**pout
)
3491 riscv_subset_t
*in
= *pin
;
3492 riscv_subset_t
*out
= *pout
;
3493 riscv_subset_t
*tail
;
3499 cmp
= riscv_compare_subsets (in
->name
, out
->name
);
3503 /* `in' comes before `out', append `in' and increment. */
3504 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3510 /* `out' comes before `in', append `out' and increment. */
3511 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3512 out
->minor_version
);
3517 /* Both present, check version and increment both. */
3518 if (!riscv_version_mismatch (ibfd
, in
, out
))
3521 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3522 out
->minor_version
);
3530 /* If we're here, either `in' or `out' is running longer than
3531 the other. So, we need to append the corresponding tail. */
3532 tail
= in
? in
: out
;
3535 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3536 tail
->minor_version
);
3544 /* Merge Tag_RISCV_arch attribute. */
3547 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3549 riscv_subset_t
*in
, *out
;
3550 char *merged_arch_str
;
3552 unsigned xlen_in
, xlen_out
;
3553 merged_subsets
.head
= NULL
;
3554 merged_subsets
.tail
= NULL
;
3556 riscv_parse_subset_t rpe_in
;
3557 riscv_parse_subset_t rpe_out
;
3559 /* Only assembler needs to check the default version of ISA, so just set
3560 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
3561 rpe_in
.subset_list
= &in_subsets
;
3562 rpe_in
.error_handler
= _bfd_error_handler
;
3563 rpe_in
.xlen
= &xlen_in
;
3564 rpe_in
.get_default_version
= NULL
;
3565 rpe_in
.check_unknown_prefixed_ext
= false;
3567 rpe_out
.subset_list
= &out_subsets
;
3568 rpe_out
.error_handler
= _bfd_error_handler
;
3569 rpe_out
.xlen
= &xlen_out
;
3570 rpe_out
.get_default_version
= NULL
;
3571 rpe_out
.check_unknown_prefixed_ext
= false;
3573 if (in_arch
== NULL
&& out_arch
== NULL
)
3576 if (in_arch
== NULL
&& out_arch
!= NULL
)
3579 if (in_arch
!= NULL
&& out_arch
== NULL
)
3582 /* Parse subset from ISA string. */
3583 if (!riscv_parse_subset (&rpe_in
, in_arch
))
3586 if (!riscv_parse_subset (&rpe_out
, out_arch
))
3589 /* Checking XLEN. */
3590 if (xlen_out
!= xlen_in
)
3593 (_("error: %pB: ISA string of input (%s) doesn't match "
3594 "output (%s)"), ibfd
, in_arch
, out_arch
);
3598 /* Merge subset list. */
3599 in
= in_subsets
.head
;
3600 out
= out_subsets
.head
;
3602 /* Merge standard extension. */
3603 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3606 /* Merge all non-single letter extensions with single call. */
3607 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3610 if (xlen_in
!= xlen_out
)
3613 (_("error: %pB: XLEN of input (%u) doesn't match "
3614 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3618 if (xlen_in
!= ARCH_SIZE
)
3621 (_("error: %pB: unsupported XLEN (%u), you might be "
3622 "using wrong emulation"), ibfd
, xlen_in
);
3626 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3628 /* Release the subset lists. */
3629 riscv_release_subset_list (&in_subsets
);
3630 riscv_release_subset_list (&out_subsets
);
3631 riscv_release_subset_list (&merged_subsets
);
3633 return merged_arch_str
;
3636 /* Merge object attributes from IBFD into output_bfd of INFO.
3637 Raise an error if there are conflicting attributes. */
3640 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3642 bfd
*obfd
= info
->output_bfd
;
3643 obj_attribute
*in_attr
;
3644 obj_attribute
*out_attr
;
3646 bool priv_attrs_merged
= false;
3647 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3650 /* Skip linker created files. */
3651 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3654 /* Skip any input that doesn't have an attribute section.
3655 This enables to link object files without attribute section with
3657 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3660 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3662 /* This is the first object. Copy the attributes. */
3663 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3665 out_attr
= elf_known_obj_attributes_proc (obfd
);
3667 /* Use the Tag_null value to indicate the attributes have been
3674 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3675 out_attr
= elf_known_obj_attributes_proc (obfd
);
3677 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3681 case Tag_RISCV_arch
:
3682 if (!out_attr
[Tag_RISCV_arch
].s
)
3683 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3684 else if (in_attr
[Tag_RISCV_arch
].s
3685 && out_attr
[Tag_RISCV_arch
].s
)
3687 /* Check compatible. */
3689 riscv_merge_arch_attr_info (ibfd
,
3690 in_attr
[Tag_RISCV_arch
].s
,
3691 out_attr
[Tag_RISCV_arch
].s
);
3692 if (merged_arch
== NULL
)
3695 out_attr
[Tag_RISCV_arch
].s
= "";
3698 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3702 case Tag_RISCV_priv_spec
:
3703 case Tag_RISCV_priv_spec_minor
:
3704 case Tag_RISCV_priv_spec_revision
:
3705 /* If we have handled the privileged elf attributes, then skip it. */
3706 if (!priv_attrs_merged
)
3708 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3709 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3710 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3711 enum riscv_spec_class in_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3712 enum riscv_spec_class out_priv_spec
= PRIV_SPEC_CLASS_NONE
;
3714 /* Get the privileged spec class from elf attributes. */
3715 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3719 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3724 /* Allow to link the object without the privileged specs. */
3725 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3727 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3728 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3729 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3731 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3732 && in_priv_spec
!= out_priv_spec
)
3735 (_("warning: %pB use privileged spec version %u.%u.%u but "
3736 "the output use version %u.%u.%u"),
3745 /* The privileged spec v1.9.1 can not be linked with others
3746 since the conflicts, so we plan to drop it in a year or
3748 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3749 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3752 (_("warning: privileged spec version 1.9.1 can not be "
3753 "linked with other spec versions"));
3756 /* Update the output privileged spec to the newest one. */
3757 if (in_priv_spec
> out_priv_spec
)
3759 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3760 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3761 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3764 priv_attrs_merged
= true;
3768 case Tag_RISCV_unaligned_access
:
3769 out_attr
[i
].i
|= in_attr
[i
].i
;
3772 case Tag_RISCV_stack_align
:
3773 if (out_attr
[i
].i
== 0)
3774 out_attr
[i
].i
= in_attr
[i
].i
;
3775 else if (in_attr
[i
].i
!= 0
3776 && out_attr
[i
].i
!= 0
3777 && out_attr
[i
].i
!= in_attr
[i
].i
)
3780 (_("error: %pB use %u-byte stack aligned but the output "
3781 "use %u-byte stack aligned"),
3782 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3788 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3791 /* If out_attr was copied from in_attr then it won't have a type yet. */
3792 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3793 out_attr
[i
].type
= in_attr
[i
].type
;
3796 /* Merge Tag_compatibility attributes and any common GNU ones. */
3797 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3800 /* Check for any attributes not known on RISC-V. */
3801 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3806 /* Merge backend specific data from an object file to the output
3807 object file when linking. */
3810 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3812 bfd
*obfd
= info
->output_bfd
;
3813 flagword new_flags
, old_flags
;
3815 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3818 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3820 (*_bfd_error_handler
)
3821 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3822 " target emulation `%s' does not match `%s'"),
3823 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3827 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3830 if (!riscv_merge_attributes (ibfd
, info
))
3833 /* Check to see if the input BFD actually contains any sections. If not,
3834 its flags may not have been initialized either, but it cannot actually
3835 cause any incompatibility. Do not short-circuit dynamic objects; their
3836 section list may be emptied by elf_link_add_object_symbols.
3838 Also check to see if there are no code sections in the input. In this
3839 case, there is no need to check for code specific flags. */
3840 if (!(ibfd
->flags
& DYNAMIC
))
3842 bool null_input_bfd
= true;
3843 bool only_data_sections
= true;
3846 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3848 null_input_bfd
= false;
3850 if ((bfd_section_flags (sec
)
3851 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3852 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3854 only_data_sections
= false;
3859 if (null_input_bfd
|| only_data_sections
)
3863 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3864 old_flags
= elf_elfheader (obfd
)->e_flags
;
3866 if (!elf_flags_init (obfd
))
3868 elf_flags_init (obfd
) = true;
3869 elf_elfheader (obfd
)->e_flags
= new_flags
;
3873 /* Disallow linking different float ABIs. */
3874 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3876 (*_bfd_error_handler
)
3877 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3878 riscv_float_abi_string (new_flags
),
3879 riscv_float_abi_string (old_flags
));
3883 /* Disallow linking RVE and non-RVE. */
3884 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3886 (*_bfd_error_handler
)
3887 (_("%pB: can't link RVE with other target"), ibfd
);
3891 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3892 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3897 bfd_set_error (bfd_error_bad_value
);
3901 /* Delete some bytes from a section while relaxing. */
3904 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3905 struct bfd_link_info
*link_info
)
3907 unsigned int i
, symcount
;
3908 bfd_vma toaddr
= sec
->size
;
3909 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3910 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3911 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3912 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3913 bfd_byte
*contents
= data
->this_hdr
.contents
;
3915 /* Actually delete the bytes. */
3917 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3919 /* Adjust the location of all of the relocs. Note that we need not
3920 adjust the addends, since all PC-relative references must be against
3921 symbols, which we will adjust below. */
3922 for (i
= 0; i
< sec
->reloc_count
; i
++)
3923 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3924 data
->relocs
[i
].r_offset
-= count
;
3926 /* Adjust the local symbols defined in this section. */
3927 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3929 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3930 if (sym
->st_shndx
== sec_shndx
)
3932 /* If the symbol is in the range of memory we just moved, we
3933 have to adjust its value. */
3934 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3935 sym
->st_value
-= count
;
3937 /* If the symbol *spans* the bytes we just deleted (i.e. its
3938 *end* is in the moved bytes but its *start* isn't), then we
3939 must adjust its size.
3941 This test needs to use the original value of st_value, otherwise
3942 we might accidentally decrease size when deleting bytes right
3943 before the symbol. But since deleted relocs can't span across
3944 symbols, we can't have both a st_value and a st_size decrease,
3945 so it is simpler to just use an else. */
3946 else if (sym
->st_value
<= addr
3947 && sym
->st_value
+ sym
->st_size
> addr
3948 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3949 sym
->st_size
-= count
;
3953 /* Now adjust the global symbols defined in this section. */
3954 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3955 - symtab_hdr
->sh_info
);
3957 for (i
= 0; i
< symcount
; i
++)
3959 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3961 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3962 containing the definition of __wrap_SYMBOL, includes a direct
3963 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3964 the same symbol (which is __wrap_SYMBOL), but still exist as two
3965 different symbols in 'sym_hashes', we don't want to adjust
3966 the global symbol __wrap_SYMBOL twice.
3968 The same problem occurs with symbols that are versioned_hidden, as
3969 foo becomes an alias for foo@BAR, and hence they need the same
3971 if (link_info
->wrap_hash
!= NULL
3972 || sym_hash
->versioned
== versioned_hidden
)
3974 struct elf_link_hash_entry
**cur_sym_hashes
;
3976 /* Loop only over the symbols which have already been checked. */
3977 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3980 /* If the current symbol is identical to 'sym_hash', that means
3981 the symbol was already adjusted (or at least checked). */
3982 if (*cur_sym_hashes
== sym_hash
)
3985 /* Don't adjust the symbol again. */
3986 if (cur_sym_hashes
< &sym_hashes
[i
])
3990 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3991 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3992 && sym_hash
->root
.u
.def
.section
== sec
)
3994 /* As above, adjust the value if needed. */
3995 if (sym_hash
->root
.u
.def
.value
> addr
3996 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3997 sym_hash
->root
.u
.def
.value
-= count
;
3999 /* As above, adjust the size if needed. */
4000 else if (sym_hash
->root
.u
.def
.value
<= addr
4001 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
4002 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
4003 sym_hash
->size
-= count
;
4010 /* A second format for recording PC-relative hi relocations. This stores the
4011 information required to relax them to GP-relative addresses. */
4013 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
4014 struct riscv_pcgp_hi_reloc
4021 bool undefined_weak
;
4022 riscv_pcgp_hi_reloc
*next
;
4025 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
4026 struct riscv_pcgp_lo_reloc
4029 riscv_pcgp_lo_reloc
*next
;
4034 riscv_pcgp_hi_reloc
*hi
;
4035 riscv_pcgp_lo_reloc
*lo
;
4036 } riscv_pcgp_relocs
;
4038 /* Initialize the pcgp reloc info in P. */
4041 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
4048 /* Free the pcgp reloc info in P. */
4051 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
4052 bfd
*abfd ATTRIBUTE_UNUSED
,
4053 asection
*sec ATTRIBUTE_UNUSED
)
4055 riscv_pcgp_hi_reloc
*c
;
4056 riscv_pcgp_lo_reloc
*l
;
4058 for (c
= p
->hi
; c
!= NULL
; )
4060 riscv_pcgp_hi_reloc
*next
= c
->next
;
4065 for (l
= p
->lo
; l
!= NULL
; )
4067 riscv_pcgp_lo_reloc
*next
= l
->next
;
4073 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4074 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4075 relax the corresponding lo part reloc. */
4078 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4079 bfd_vma hi_addend
, bfd_vma hi_addr
,
4080 unsigned hi_sym
, asection
*sym_sec
,
4081 bool undefined_weak
)
4083 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof (*new));
4086 new->hi_sec_off
= hi_sec_off
;
4087 new->hi_addend
= hi_addend
;
4088 new->hi_addr
= hi_addr
;
4089 new->hi_sym
= hi_sym
;
4090 new->sym_sec
= sym_sec
;
4091 new->undefined_weak
= undefined_weak
;
4097 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4098 This is used by a lo part reloc to find the corresponding hi part reloc. */
4100 static riscv_pcgp_hi_reloc
*
4101 riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4103 riscv_pcgp_hi_reloc
*c
;
4105 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4106 if (c
->hi_sec_off
== hi_sec_off
)
4111 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4112 This is used to record relocs that can't be relaxed. */
4115 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4117 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof (*new));
4120 new->hi_sec_off
= hi_sec_off
;
4126 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4127 This is used by a hi part reloc to find the corresponding lo part reloc. */
4130 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4132 riscv_pcgp_lo_reloc
*c
;
4134 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4135 if (c
->hi_sec_off
== hi_sec_off
)
4140 typedef bool (*relax_func_t
) (bfd
*, asection
*, asection
*,
4141 struct bfd_link_info
*,
4142 Elf_Internal_Rela
*,
4143 bfd_vma
, bfd_vma
, bfd_vma
, bool *,
4144 riscv_pcgp_relocs
*,
4145 bool undefined_weak
);
4147 /* Relax AUIPC + JALR into JAL. */
4150 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4151 struct bfd_link_info
*link_info
,
4152 Elf_Internal_Rela
*rel
,
4154 bfd_vma max_alignment
,
4155 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4157 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4158 bool undefined_weak ATTRIBUTE_UNUSED
)
4160 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4161 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4162 bool near_zero
= (symval
+ RISCV_IMM_REACH
/ 2) < RISCV_IMM_REACH
;
4163 bfd_vma auipc
, jalr
;
4164 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4166 /* If the call crosses section boundaries, an alignment directive could
4167 cause the PC-relative offset to later increase, so we need to add in the
4168 max alignment of any section inclusive from the call to the target.
4169 Otherwise, we only need to use the alignment of the current section. */
4170 if (VALID_JTYPE_IMM (foff
))
4172 if (sym_sec
->output_section
== sec
->output_section
4173 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4174 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4175 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4178 /* See if this function call can be shortened. */
4179 if (!VALID_JTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4182 /* Shorten the function call. */
4183 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4185 auipc
= bfd_getl32 (contents
+ rel
->r_offset
);
4186 jalr
= bfd_getl32 (contents
+ rel
->r_offset
+ 4);
4187 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4188 rvc
= rvc
&& VALID_CJTYPE_IMM (foff
);
4190 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4191 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4195 /* Relax to C.J[AL] rd, addr. */
4196 r_type
= R_RISCV_RVC_JUMP
;
4197 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4200 else if (VALID_JTYPE_IMM (foff
))
4202 /* Relax to JAL rd, addr. */
4203 r_type
= R_RISCV_JAL
;
4204 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4208 /* Near zero, relax to JALR rd, x0, addr. */
4209 r_type
= R_RISCV_LO12_I
;
4210 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4213 /* Replace the R_RISCV_CALL reloc. */
4214 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4215 /* Replace the AUIPC. */
4216 riscv_put_insn (8 * len
, auipc
, contents
+ rel
->r_offset
);
4218 /* Delete unnecessary JALR. */
4220 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4224 /* Traverse all output sections and return the max alignment. */
4227 _bfd_riscv_get_max_alignment (asection
*sec
)
4229 unsigned int max_alignment_power
= 0;
4232 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4234 if (o
->alignment_power
> max_alignment_power
)
4235 max_alignment_power
= o
->alignment_power
;
4238 return (bfd_vma
) 1 << max_alignment_power
;
4241 /* Relax non-PIC global variable references to GP-relative references. */
4244 _bfd_riscv_relax_lui (bfd
*abfd
,
4247 struct bfd_link_info
*link_info
,
4248 Elf_Internal_Rela
*rel
,
4250 bfd_vma max_alignment
,
4251 bfd_vma reserve_size
,
4253 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4254 bool undefined_weak
)
4256 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4257 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4258 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4260 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4264 /* If gp and the symbol are in the same output section, which is not the
4265 abs section, then consider only that output section's alignment. */
4266 struct bfd_link_hash_entry
*h
=
4267 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4269 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4270 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4271 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4274 /* Is the reference in range of x0 or gp?
4275 Valid gp range conservatively because of alignment issue. */
4277 || (VALID_ITYPE_IMM (symval
)
4279 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4281 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4283 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4284 switch (ELFNN_R_TYPE (rel
->r_info
))
4286 case R_RISCV_LO12_I
:
4289 /* Change the RS1 to zero. */
4290 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4291 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4292 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4295 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4298 case R_RISCV_LO12_S
:
4301 /* Change the RS1 to zero. */
4302 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4303 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4304 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4307 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4311 /* We can delete the unnecessary LUI and reloc. */
4312 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4314 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4322 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4323 account for this assuming page alignment at worst. In the presence of
4324 RELRO segment the linker aligns it by one page size, therefore sections
4325 after the segment can be moved more than one page. */
4328 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4329 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4330 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4331 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4332 : ELF_MAXPAGESIZE
)))
4334 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4335 bfd_vma lui
= bfd_getl32 (contents
+ rel
->r_offset
);
4336 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4337 if (rd
== 0 || rd
== X_SP
)
4340 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4341 bfd_putl32 (lui
, contents
+ rel
->r_offset
);
4343 /* Replace the R_RISCV_HI20 reloc. */
4344 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4347 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4354 /* Relax non-PIC TLS references to TP-relative references. */
4357 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4359 asection
*sym_sec ATTRIBUTE_UNUSED
,
4360 struct bfd_link_info
*link_info
,
4361 Elf_Internal_Rela
*rel
,
4363 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4364 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4366 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
4367 bool undefined_weak ATTRIBUTE_UNUSED
)
4369 /* See if this symbol is in range of tp. */
4370 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4373 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4374 switch (ELFNN_R_TYPE (rel
->r_info
))
4376 case R_RISCV_TPREL_LO12_I
:
4377 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4380 case R_RISCV_TPREL_LO12_S
:
4381 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4384 case R_RISCV_TPREL_HI20
:
4385 case R_RISCV_TPREL_ADD
:
4386 /* We can delete the unnecessary instruction and reloc. */
4387 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4389 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
4396 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4397 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4400 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4402 struct bfd_link_info
*link_info
,
4403 Elf_Internal_Rela
*rel
,
4405 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4406 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4407 bool *again ATTRIBUTE_UNUSED
,
4408 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
4409 bool undefined_weak ATTRIBUTE_UNUSED
)
4411 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4412 bfd_vma alignment
= 1, pos
;
4413 while (alignment
<= rel
->r_addend
)
4416 symval
-= rel
->r_addend
;
4417 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4418 bfd_vma nop_bytes
= aligned_addr
- symval
;
4420 /* Make sure there are enough NOPs to actually achieve the alignment. */
4421 if (rel
->r_addend
< nop_bytes
)
4424 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4425 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4426 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4427 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4428 bfd_set_error (bfd_error_bad_value
);
4432 /* Delete the reloc. */
4433 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4435 /* If the number of NOPs is already correct, there's nothing to do. */
4436 if (nop_bytes
== rel
->r_addend
)
4439 /* Write as many RISC-V NOPs as we need. */
4440 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4441 bfd_putl32 (RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4443 /* Write a final RVC NOP if need be. */
4444 if (nop_bytes
% 4 != 0)
4445 bfd_putl16 (RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4447 /* Delete the excess bytes. */
4448 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4449 rel
->r_addend
- nop_bytes
, link_info
);
4452 /* Relax PC-relative references to GP-relative references. */
4455 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4458 struct bfd_link_info
*link_info
,
4459 Elf_Internal_Rela
*rel
,
4461 bfd_vma max_alignment
,
4462 bfd_vma reserve_size
,
4463 bool *again ATTRIBUTE_UNUSED
,
4464 riscv_pcgp_relocs
*pcgp_relocs
,
4465 bool undefined_weak
)
4467 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4468 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4470 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4472 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4473 actual target address. */
4474 riscv_pcgp_hi_reloc hi_reloc
;
4475 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4476 switch (ELFNN_R_TYPE (rel
->r_info
))
4478 case R_RISCV_PCREL_LO12_I
:
4479 case R_RISCV_PCREL_LO12_S
:
4481 /* If the %lo has an addend, it isn't for the label pointing at the
4482 hi part instruction, but rather for the symbol pointed at by the
4483 hi part instruction. So we must subtract it here for the lookup.
4484 It is still used below in the final symbol address. */
4485 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4486 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4490 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4495 symval
= hi_reloc
.hi_addr
;
4496 sym_sec
= hi_reloc
.sym_sec
;
4498 /* We can not know whether the undefined weak symbol is referenced
4499 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4500 we have to record the 'undefined_weak' flag when handling the
4501 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4502 undefined_weak
= hi_reloc
.undefined_weak
;
4506 case R_RISCV_PCREL_HI20
:
4507 /* Mergeable symbols and code might later move out of range. */
4508 if (! undefined_weak
4509 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4512 /* If the cooresponding lo relocation has already been seen then it's not
4513 safe to relax this relocation. */
4514 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4525 /* If gp and the symbol are in the same output section, which is not the
4526 abs section, then consider only that output section's alignment. */
4527 struct bfd_link_hash_entry
*h
=
4528 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, false, false,
4530 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4531 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4532 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4535 /* Is the reference in range of x0 or gp?
4536 Valid gp range conservatively because of alignment issue. */
4538 || (VALID_ITYPE_IMM (symval
)
4540 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4542 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4544 unsigned sym
= hi_reloc
.hi_sym
;
4545 switch (ELFNN_R_TYPE (rel
->r_info
))
4547 case R_RISCV_PCREL_LO12_I
:
4550 /* Change the RS1 to zero, and then modify the relocation
4551 type to R_RISCV_LO12_I. */
4552 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4553 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4554 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4555 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4556 rel
->r_addend
= hi_reloc
.hi_addend
;
4560 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4561 rel
->r_addend
+= hi_reloc
.hi_addend
;
4565 case R_RISCV_PCREL_LO12_S
:
4568 /* Change the RS1 to zero, and then modify the relocation
4569 type to R_RISCV_LO12_S. */
4570 bfd_vma insn
= bfd_getl32 (contents
+ rel
->r_offset
);
4571 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4572 bfd_putl32 (insn
, contents
+ rel
->r_offset
);
4573 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4574 rel
->r_addend
= hi_reloc
.hi_addend
;
4578 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4579 rel
->r_addend
+= hi_reloc
.hi_addend
;
4583 case R_RISCV_PCREL_HI20
:
4584 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4588 ELFNN_R_SYM(rel
->r_info
),
4591 /* We can delete the unnecessary AUIPC and reloc. */
4592 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4604 /* Delete the bytes for R_RISCV_DELETE. */
4607 _bfd_riscv_relax_delete (bfd
*abfd
,
4609 asection
*sym_sec ATTRIBUTE_UNUSED
,
4610 struct bfd_link_info
*link_info
,
4611 Elf_Internal_Rela
*rel
,
4612 bfd_vma symval ATTRIBUTE_UNUSED
,
4613 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4614 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4616 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4617 bool undefined_weak ATTRIBUTE_UNUSED
)
4619 if (!riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4622 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4627 /* Called by after_allocation to set the information of data segment
4631 bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info
*info
,
4632 int *data_segment_phase
)
4634 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4635 htab
->data_segment_phase
= data_segment_phase
;
4638 /* Called by after_allocation to check if we need to run the whole
4639 relaxations again. */
4642 bfd_elfNN_riscv_restart_relax_sections (struct bfd_link_info
*info
)
4644 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4645 bool restart
= htab
->restart_relax
;
4646 /* Reset the flag. */
4647 htab
->restart_relax
= false;
4653 Pass 0: Shortens code sequences for LUI/CALL/TPREL relocs.
4654 Pass 1: Shortens code sequences for PCREL relocs.
4655 Pass 2: Deletes the bytes that pass 1 made obsolete.
4656 Pass 3: Which cannot be disabled, handles code alignment directives.
4658 The `again` is used to determine whether the relax pass itself needs to
4659 run again. And the `restart_relax` is used to determine if we need to
4660 run the whole relax passes again from 0 to 2. Once we have deleted the
4661 code between relax pass 0 to 2, the restart_relax will be set to TRUE,
4662 and we should run the whole relaxations again to give them more chances
4663 to shorten the code.
4665 Since we can't relax anything else once we start to handle the alignments,
4666 we will only enter into the relax pass 3 when the restart_relax is FALSE. */
4669 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4670 struct bfd_link_info
*info
,
4673 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4674 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4675 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4676 Elf_Internal_Rela
*relocs
;
4679 bfd_vma max_alignment
, reserve_size
= 0;
4680 riscv_pcgp_relocs pcgp_relocs
;
4684 if (bfd_link_relocatable (info
)
4685 || (sec
->flags
& SEC_RELOC
) == 0
4686 || sec
->reloc_count
== 0
4687 || (info
->disable_target_specific_optimizations
4688 && info
->relax_pass
< 2)
4689 || (htab
->restart_relax
4690 && info
->relax_pass
== 3)
4691 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4692 and defined in ld/ldexp.h. */
4693 || *(htab
->data_segment_phase
) == 4)
4696 riscv_init_pcgp_relocs (&pcgp_relocs
);
4698 /* Read this BFD's relocs if we haven't done so already. */
4700 relocs
= data
->relocs
;
4701 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4702 info
->keep_memory
)))
4707 max_alignment
= htab
->max_alignment
;
4708 if (max_alignment
== (bfd_vma
) -1)
4710 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4711 htab
->max_alignment
= max_alignment
;
4715 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4717 /* Examine and consider relaxing each reloc. */
4718 for (i
= 0; i
< sec
->reloc_count
; i
++)
4721 Elf_Internal_Rela
*rel
= relocs
+ i
;
4722 relax_func_t relax_func
;
4723 int type
= ELFNN_R_TYPE (rel
->r_info
);
4726 bool undefined_weak
= false;
4729 if (info
->relax_pass
== 0)
4731 if (type
== R_RISCV_CALL
4732 || type
== R_RISCV_CALL_PLT
)
4733 relax_func
= _bfd_riscv_relax_call
;
4734 else if (type
== R_RISCV_HI20
4735 || type
== R_RISCV_LO12_I
4736 || type
== R_RISCV_LO12_S
)
4737 relax_func
= _bfd_riscv_relax_lui
;
4738 else if (type
== R_RISCV_TPREL_HI20
4739 || type
== R_RISCV_TPREL_ADD
4740 || type
== R_RISCV_TPREL_LO12_I
4741 || type
== R_RISCV_TPREL_LO12_S
)
4742 relax_func
= _bfd_riscv_relax_tls_le
;
4746 else if (info
->relax_pass
== 1
4747 && !bfd_link_pic (info
)
4748 && (type
== R_RISCV_PCREL_HI20
4749 || type
== R_RISCV_PCREL_LO12_I
4750 || type
== R_RISCV_PCREL_LO12_S
))
4751 relax_func
= _bfd_riscv_relax_pc
;
4752 else if (info
->relax_pass
== 2 && type
== R_RISCV_DELETE
)
4753 relax_func
= _bfd_riscv_relax_delete
;
4754 else if (info
->relax_pass
== 3 && type
== R_RISCV_ALIGN
)
4755 relax_func
= _bfd_riscv_relax_align
;
4759 if (info
->relax_pass
< 2)
4761 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4762 if (i
== sec
->reloc_count
- 1
4763 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4764 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4767 /* Skip over the R_RISCV_RELAX. */
4771 data
->relocs
= relocs
;
4773 /* Read this BFD's contents if we haven't done so already. */
4774 if (!data
->this_hdr
.contents
4775 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4778 /* Read this BFD's symbols if we haven't done so already. */
4779 if (symtab_hdr
->sh_info
!= 0
4780 && !symtab_hdr
->contents
4781 && !(symtab_hdr
->contents
=
4782 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4783 symtab_hdr
->sh_info
,
4784 0, NULL
, NULL
, NULL
)))
4787 /* Get the value of the symbol referred to by the reloc. */
4788 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4790 /* A local symbol. */
4791 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4792 + ELFNN_R_SYM (rel
->r_info
));
4793 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4794 ? 0 : isym
->st_size
- rel
->r_addend
;
4796 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4797 a fake global symbol entry for this, so deal with the local ifunc
4799 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4802 if (isym
->st_shndx
== SHN_UNDEF
)
4803 sym_sec
= sec
, symval
= rel
->r_offset
;
4806 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4807 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4809 /* The purpose of this code is unknown. It breaks linker scripts
4810 for embedded development that place sections at address zero.
4811 This code is believed to be unnecessary. Disabling it but not
4812 yet removing it, in case something breaks. */
4813 if (sec_addr (sym_sec
) == 0)
4816 symval
= isym
->st_value
;
4818 symtype
= ELF_ST_TYPE (isym
->st_info
);
4823 struct elf_link_hash_entry
*h
;
4825 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4826 h
= elf_sym_hashes (abfd
)[indx
];
4828 while (h
->root
.type
== bfd_link_hash_indirect
4829 || h
->root
.type
== bfd_link_hash_warning
)
4830 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4832 /* Disable the relaxation for ifunc. */
4833 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4836 if (h
->root
.type
== bfd_link_hash_undefweak
4837 && (relax_func
== _bfd_riscv_relax_lui
4838 || relax_func
== _bfd_riscv_relax_pc
))
4840 /* For the lui and auipc relaxations, since the symbol
4841 value of an undefined weak symbol is always be zero,
4842 we can optimize the patterns into a single LI/MV/ADDI
4845 Note that, creating shared libraries and pie output may
4846 break the rule above. Fortunately, since we do not relax
4847 pc relocs when creating shared libraries and pie output,
4848 and the absolute address access for R_RISCV_HI20 isn't
4849 allowed when "-fPIC" is set, the problem of creating shared
4850 libraries can not happen currently. Once we support the
4851 auipc relaxations when creating shared libraries, then we will
4852 need the more rigorous checking for this optimization. */
4853 undefined_weak
= true;
4856 /* This line has to match the check in riscv_elf_relocate_section
4857 in the R_RISCV_CALL[_PLT] case. */
4858 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4860 sym_sec
= htab
->elf
.splt
;
4861 symval
= h
->plt
.offset
;
4863 else if (undefined_weak
)
4866 sym_sec
= bfd_und_section_ptr
;
4868 else if ((h
->root
.type
== bfd_link_hash_defined
4869 || h
->root
.type
== bfd_link_hash_defweak
)
4870 && h
->root
.u
.def
.section
!= NULL
4871 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4873 symval
= h
->root
.u
.def
.value
;
4874 sym_sec
= h
->root
.u
.def
.section
;
4879 if (h
->type
!= STT_FUNC
)
4881 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4885 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4886 && (sym_sec
->flags
& SEC_MERGE
))
4888 /* At this stage in linking, no SEC_MERGE symbol has been
4889 adjusted, so all references to such symbols need to be
4890 passed through _bfd_merged_section_offset. (Later, in
4891 relocate_section, all SEC_MERGE symbols *except* for
4892 section symbols have been adjusted.)
4894 gas may reduce relocations against symbols in SEC_MERGE
4895 sections to a relocation against the section symbol when
4896 the original addend was zero. When the reloc is against
4897 a section symbol we should include the addend in the
4898 offset passed to _bfd_merged_section_offset, since the
4899 location of interest is the original symbol. On the
4900 other hand, an access to "sym+addend" where "sym" is not
4901 a section symbol should not include the addend; Such an
4902 access is presumed to be an offset from "sym"; The
4903 location of interest is just "sym". */
4904 if (symtype
== STT_SECTION
)
4905 symval
+= rel
->r_addend
;
4907 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4908 elf_section_data (sym_sec
)->sec_info
,
4911 if (symtype
!= STT_SECTION
)
4912 symval
+= rel
->r_addend
;
4915 symval
+= rel
->r_addend
;
4917 symval
+= sec_addr (sym_sec
);
4919 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4920 max_alignment
, reserve_size
, again
,
4921 &pcgp_relocs
, undefined_weak
))
4928 if (relocs
!= data
->relocs
)
4930 riscv_free_pcgp_relocs (&pcgp_relocs
, abfd
, sec
);
4933 htab
->restart_relax
= true;
4939 # define PRSTATUS_SIZE 204
4940 # define PRSTATUS_OFFSET_PR_CURSIG 12
4941 # define PRSTATUS_OFFSET_PR_PID 24
4942 # define PRSTATUS_OFFSET_PR_REG 72
4943 # define ELF_GREGSET_T_SIZE 128
4944 # define PRPSINFO_SIZE 128
4945 # define PRPSINFO_OFFSET_PR_PID 16
4946 # define PRPSINFO_OFFSET_PR_FNAME 32
4947 # define PRPSINFO_OFFSET_PR_PSARGS 48
4948 # define PRPSINFO_PR_FNAME_LENGTH 16
4949 # define PRPSINFO_PR_PSARGS_LENGTH 80
4951 # define PRSTATUS_SIZE 376
4952 # define PRSTATUS_OFFSET_PR_CURSIG 12
4953 # define PRSTATUS_OFFSET_PR_PID 32
4954 # define PRSTATUS_OFFSET_PR_REG 112
4955 # define ELF_GREGSET_T_SIZE 256
4956 # define PRPSINFO_SIZE 136
4957 # define PRPSINFO_OFFSET_PR_PID 24
4958 # define PRPSINFO_OFFSET_PR_FNAME 40
4959 # define PRPSINFO_OFFSET_PR_PSARGS 56
4960 # define PRPSINFO_PR_FNAME_LENGTH 16
4961 # define PRPSINFO_PR_PSARGS_LENGTH 80
4964 /* Write PRSTATUS and PRPSINFO note into core file. This will be called
4965 before the generic code in elf.c. By checking the compiler defines we
4966 only perform any action here if the generic code would otherwise not be
4967 able to help us. The intention is that bare metal core dumps (where the
4968 prstatus_t and/or prpsinfo_t might not be available) will use this code,
4969 while non bare metal tools will use the generic elf code. */
4972 riscv_write_core_note (bfd
*abfd ATTRIBUTE_UNUSED
,
4973 char *buf ATTRIBUTE_UNUSED
,
4974 int *bufsiz ATTRIBUTE_UNUSED
,
4975 int note_type ATTRIBUTE_UNUSED
, ...)
4982 #if !defined (HAVE_PRPSINFO_T)
4985 char data
[PRPSINFO_SIZE
] ATTRIBUTE_NONSTRING
;
4988 va_start (ap
, note_type
);
4989 memset (data
, 0, sizeof (data
));
4990 strncpy (data
+ PRPSINFO_OFFSET_PR_FNAME
, va_arg (ap
, const char *),
4991 PRPSINFO_PR_FNAME_LENGTH
);
4992 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
4994 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
4995 -Wstringop-truncation:
4996 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
4998 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION
;
5000 strncpy (data
+ PRPSINFO_OFFSET_PR_PSARGS
, va_arg (ap
, const char *),
5001 PRPSINFO_PR_PSARGS_LENGTH
);
5002 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
5006 return elfcore_write_note (abfd
, buf
, bufsiz
,
5007 "CORE", note_type
, data
, sizeof (data
));
5009 #endif /* !HAVE_PRPSINFO_T */
5011 #if !defined (HAVE_PRSTATUS_T)
5014 char data
[PRSTATUS_SIZE
];
5020 va_start (ap
, note_type
);
5021 memset (data
, 0, sizeof(data
));
5022 pid
= va_arg (ap
, long);
5023 bfd_put_32 (abfd
, pid
, data
+ PRSTATUS_OFFSET_PR_PID
);
5024 cursig
= va_arg (ap
, int);
5025 bfd_put_16 (abfd
, cursig
, data
+ PRSTATUS_OFFSET_PR_CURSIG
);
5026 greg
= va_arg (ap
, const void *);
5027 memcpy (data
+ PRSTATUS_OFFSET_PR_REG
, greg
,
5028 PRSTATUS_SIZE
- PRSTATUS_OFFSET_PR_REG
- ARCH_SIZE
/ 8);
5030 return elfcore_write_note (abfd
, buf
, bufsiz
,
5031 "CORE", note_type
, data
, sizeof (data
));
5033 #endif /* !HAVE_PRSTATUS_T */
5037 /* Support for core dump NOTE sections. */
5040 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
5042 switch (note
->descsz
)
5047 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
5049 elf_tdata (abfd
)->core
->signal
5050 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
5053 elf_tdata (abfd
)->core
->lwpid
5054 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
5058 /* Make a ".reg/999" section. */
5059 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
5060 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
5064 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
5066 switch (note
->descsz
)
5071 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5073 elf_tdata (abfd
)->core
->pid
5074 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
5077 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
5078 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
,
5079 PRPSINFO_PR_FNAME_LENGTH
);
5082 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
5083 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
,
5084 PRPSINFO_PR_PSARGS_LENGTH
);
5088 /* Note that for some reason, a spurious space is tacked
5089 onto the end of the args in some (at least one anyway)
5090 implementations, so strip it off if it exists. */
5093 char *command
= elf_tdata (abfd
)->core
->command
;
5094 int n
= strlen (command
);
5096 if (0 < n
&& command
[n
- 1] == ' ')
5097 command
[n
- 1] = '\0';
5103 /* Set the right mach type. */
5106 riscv_elf_object_p (bfd
*abfd
)
5108 /* There are only two mach types in RISCV currently. */
5109 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0
5110 || strcmp (abfd
->xvec
->name
, "elf32-bigriscv") == 0)
5111 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
5113 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
5118 /* Determine whether an object attribute tag takes an integer, a
5122 riscv_elf_obj_attrs_arg_type (int tag
)
5124 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
5127 /* PR27584, Omit local and empty symbols since they usually generated
5128 for pcrel relocations. */
5131 riscv_elf_is_target_special_symbol (bfd
*abfd
, asymbol
*sym
)
5133 return (!strcmp (sym
->name
, "")
5134 || _bfd_elf_is_local_label_name (abfd
, sym
->name
));
5137 #define TARGET_LITTLE_SYM riscv_elfNN_vec
5138 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
5139 #define TARGET_BIG_SYM riscv_elfNN_be_vec
5140 #define TARGET_BIG_NAME "elfNN-bigriscv"
5142 #define elf_backend_reloc_type_class riscv_reloc_type_class
5144 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5145 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5146 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
5147 #define bfd_elfNN_bfd_merge_private_bfd_data \
5148 _bfd_riscv_elf_merge_private_bfd_data
5149 #define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
5151 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5152 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5153 #define elf_backend_check_relocs riscv_elf_check_relocs
5154 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5155 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5156 #define elf_backend_relocate_section riscv_elf_relocate_section
5157 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5158 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
5159 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
5160 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5161 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5162 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5163 #define elf_backend_object_p riscv_elf_object_p
5164 #define elf_backend_write_core_note riscv_write_core_note
5165 #define elf_info_to_howto_rel NULL
5166 #define elf_info_to_howto riscv_info_to_howto_rela
5167 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5168 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
5170 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5172 #define elf_backend_can_gc_sections 1
5173 #define elf_backend_can_refcount 1
5174 #define elf_backend_want_got_plt 1
5175 #define elf_backend_plt_readonly 1
5176 #define elf_backend_plt_alignment 4
5177 #define elf_backend_want_plt_sym 1
5178 #define elf_backend_got_header_size (ARCH_SIZE / 8)
5179 #define elf_backend_want_dynrelro 1
5180 #define elf_backend_rela_normal 1
5181 #define elf_backend_default_execstack 0
5183 #undef elf_backend_obj_attrs_vendor
5184 #define elf_backend_obj_attrs_vendor "riscv"
5185 #undef elf_backend_obj_attrs_arg_type
5186 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5187 #undef elf_backend_obj_attrs_section_type
5188 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5189 #undef elf_backend_obj_attrs_section
5190 #define elf_backend_obj_attrs_section ".riscv.attributes"
5192 #include "elfNN-target.h"