1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2020 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 /* Internal relocations used exclusively by the relaxation pass. */
36 #define R_RISCV_DELETE (R_RISCV_max + 1)
40 #define MINUS_ONE ((bfd_vma)0 - 1)
42 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
44 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
46 /* The name of the dynamic interpreter. This is put in the .interp
49 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
50 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
52 #define ELF_ARCH bfd_arch_riscv
53 #define ELF_TARGET_ID RISCV_ELF_DATA
54 #define ELF_MACHINE_CODE EM_RISCV
55 #define ELF_MAXPAGESIZE 0x1000
56 #define ELF_COMMONPAGESIZE 0x1000
58 /* RISC-V ELF linker hash entry. */
60 struct riscv_elf_link_hash_entry
62 struct elf_link_hash_entry elf
;
72 #define riscv_elf_hash_entry(ent) \
73 ((struct riscv_elf_link_hash_entry *)(ent))
75 struct _bfd_riscv_elf_obj_tdata
77 struct elf_obj_tdata root
;
79 /* tls_type for each local got entry. */
80 char *local_got_tls_type
;
83 #define _bfd_riscv_elf_tdata(abfd) \
84 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
86 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
87 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
89 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
90 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
91 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
93 #define is_riscv_elf(bfd) \
94 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
95 && elf_tdata (bfd) != NULL \
96 && elf_object_id (bfd) == RISCV_ELF_DATA)
99 elfNN_riscv_mkobject (bfd
*abfd
)
101 return bfd_elf_allocate_object (abfd
,
102 sizeof (struct _bfd_riscv_elf_obj_tdata
),
106 #include "elf/common.h"
107 #include "elf/internal.h"
109 struct riscv_elf_link_hash_table
111 struct elf_link_hash_table elf
;
113 /* Short-cuts to get to dynamic linker sections. */
116 /* Small local sym to section mapping cache. */
117 struct sym_cache sym_cache
;
119 /* The max alignment of output sections. */
120 bfd_vma max_alignment
;
124 /* Get the RISC-V ELF linker hash table from a link_info structure. */
125 #define riscv_elf_hash_table(p) \
126 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
127 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
130 riscv_info_to_howto_rela (bfd
*abfd
,
132 Elf_Internal_Rela
*dst
)
134 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
135 return cache_ptr
->howto
!= NULL
;
139 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
141 const struct elf_backend_data
*bed
;
144 bed
= get_elf_backend_data (abfd
);
145 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
146 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
151 #define PLT_HEADER_INSNS 8
152 #define PLT_ENTRY_INSNS 4
153 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
154 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
156 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
158 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
160 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
163 riscv_elf_got_plt_val (bfd_vma plt_index
, struct bfd_link_info
*info
)
165 return sec_addr (riscv_elf_hash_table (info
)->elf
.sgotplt
)
166 + GOTPLT_HEADER_SIZE
+ (plt_index
* GOT_ENTRY_SIZE
);
170 # define MATCH_LREG MATCH_LW
172 # define MATCH_LREG MATCH_LD
175 /* Generate a PLT header. */
178 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
181 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
182 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
184 /* RVE has no t3 register, so this won't work, and is not supported. */
185 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
187 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
192 /* auipc t2, %hi(.got.plt)
193 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
194 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
195 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
196 addi t0, t2, %lo(.got.plt) # &.got.plt
197 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
198 l[w|d] t0, PTRSIZE(t0) # link map
201 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
202 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
203 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
204 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, -(PLT_HEADER_SIZE
+ 12));
205 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
206 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
207 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
208 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
213 /* Generate a PLT entry. */
216 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
219 /* RVE has no t3 register, so this won't work, and is not supported. */
220 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
222 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
227 /* auipc t3, %hi(.got.plt entry)
228 l[w|d] t3, %lo(.got.plt entry)(t3)
232 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
233 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
234 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
235 entry
[3] = RISCV_NOP
;
240 /* Create an entry in an RISC-V ELF linker hash table. */
242 static struct bfd_hash_entry
*
243 link_hash_newfunc (struct bfd_hash_entry
*entry
,
244 struct bfd_hash_table
*table
, const char *string
)
246 /* Allocate the structure if it has not already been allocated by a
251 bfd_hash_allocate (table
,
252 sizeof (struct riscv_elf_link_hash_entry
));
257 /* Call the allocation method of the superclass. */
258 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
261 struct riscv_elf_link_hash_entry
*eh
;
263 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
264 eh
->tls_type
= GOT_UNKNOWN
;
270 /* Create a RISC-V ELF linker hash table. */
272 static struct bfd_link_hash_table
*
273 riscv_elf_link_hash_table_create (bfd
*abfd
)
275 struct riscv_elf_link_hash_table
*ret
;
276 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
278 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
282 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
283 sizeof (struct riscv_elf_link_hash_entry
),
290 ret
->max_alignment
= (bfd_vma
) -1;
291 return &ret
->elf
.root
;
294 /* Create the .got section. */
297 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
301 struct elf_link_hash_entry
*h
;
302 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
303 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
305 /* This function may be called more than once. */
306 if (htab
->sgot
!= NULL
)
309 flags
= bed
->dynamic_sec_flags
;
311 s
= bfd_make_section_anyway_with_flags (abfd
,
312 (bed
->rela_plts_and_copies_p
313 ? ".rela.got" : ".rel.got"),
314 (bed
->dynamic_sec_flags
317 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
321 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
323 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
327 /* The first bit of the global offset table is the header. */
328 s
->size
+= bed
->got_header_size
;
330 if (bed
->want_got_plt
)
332 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
334 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
338 /* Reserve room for the header. */
339 s
->size
+= GOTPLT_HEADER_SIZE
;
342 if (bed
->want_got_sym
)
344 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
345 section. We don't do this in the linker script because we don't want
346 to define the symbol if we are not creating a global offset
348 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
349 "_GLOBAL_OFFSET_TABLE_");
350 elf_hash_table (info
)->hgot
= h
;
358 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
359 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
363 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
364 struct bfd_link_info
*info
)
366 struct riscv_elf_link_hash_table
*htab
;
368 htab
= riscv_elf_hash_table (info
);
369 BFD_ASSERT (htab
!= NULL
);
371 if (!riscv_elf_create_got_section (dynobj
, info
))
374 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
377 if (!bfd_link_pic (info
))
379 /* Technically, this section doesn't have contents. It is used as the
380 target of TLS copy relocs, to copy TLS data from shared libraries into
381 the executable. However, if we don't mark it as loadable, then it
382 matches the IS_TBSS test in ldlang.c, and there is no run-time address
383 space allocated for it even though it has SEC_ALLOC. That test is
384 correct for .tbss, but not correct for this section. There is also
385 a second problem that having a section with no contents can only work
386 if it comes after all sections with contents in the same segment,
387 but the linker script does not guarantee that. This is just mixed in
388 with other .tdata.* sections. We can fix both problems by lying and
389 saying that there are contents. This section is expected to be small
390 so this should not cause a significant extra program startup cost. */
392 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
393 (SEC_ALLOC
| SEC_THREAD_LOCAL
394 | SEC_LOAD
| SEC_DATA
396 | SEC_LINKER_CREATED
));
399 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
400 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
406 /* Copy the extra info we tack onto an elf_link_hash_entry. */
409 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
410 struct elf_link_hash_entry
*dir
,
411 struct elf_link_hash_entry
*ind
)
413 struct riscv_elf_link_hash_entry
*edir
, *eind
;
415 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
416 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
418 if (ind
->dyn_relocs
!= NULL
)
420 if (dir
->dyn_relocs
!= NULL
)
422 struct elf_dyn_relocs
**pp
;
423 struct elf_dyn_relocs
*p
;
425 /* Add reloc counts against the indirect sym to the direct sym
426 list. Merge any entries against the same section. */
427 for (pp
= &ind
->dyn_relocs
; (p
= *pp
) != NULL
; )
429 struct elf_dyn_relocs
*q
;
431 for (q
= dir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
432 if (q
->sec
== p
->sec
)
434 q
->pc_count
+= p
->pc_count
;
435 q
->count
+= p
->count
;
442 *pp
= dir
->dyn_relocs
;
445 dir
->dyn_relocs
= ind
->dyn_relocs
;
446 ind
->dyn_relocs
= NULL
;
449 if (ind
->root
.type
== bfd_link_hash_indirect
450 && dir
->got
.refcount
<= 0)
452 edir
->tls_type
= eind
->tls_type
;
453 eind
->tls_type
= GOT_UNKNOWN
;
455 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
459 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
460 unsigned long symndx
, char tls_type
)
462 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
464 *new_tls_type
|= tls_type
;
465 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
467 (*_bfd_error_handler
)
468 (_("%pB: `%s' accessed both as normal and thread local symbol"),
469 abfd
, h
? h
->root
.root
.string
: "<local>");
476 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
477 struct elf_link_hash_entry
*h
, long symndx
)
479 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
480 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
482 if (htab
->elf
.sgot
== NULL
)
484 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
490 h
->got
.refcount
+= 1;
494 /* This is a global offset table entry for a local symbol. */
495 if (elf_local_got_refcounts (abfd
) == NULL
)
497 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
498 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
500 _bfd_riscv_elf_local_got_tls_type (abfd
)
501 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
503 elf_local_got_refcounts (abfd
) [symndx
] += 1;
509 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
511 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
513 (*_bfd_error_handler
)
514 (_("%pB: relocation %s against `%s' can not be used when making a shared "
515 "object; recompile with -fPIC"),
516 abfd
, r
? r
->name
: _("<unknown>"),
517 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
518 bfd_set_error (bfd_error_bad_value
);
521 /* Look through the relocs for a section during the first phase, and
522 allocate space in the global offset table or procedure linkage
526 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
527 asection
*sec
, const Elf_Internal_Rela
*relocs
)
529 struct riscv_elf_link_hash_table
*htab
;
530 Elf_Internal_Shdr
*symtab_hdr
;
531 struct elf_link_hash_entry
**sym_hashes
;
532 const Elf_Internal_Rela
*rel
;
533 asection
*sreloc
= NULL
;
535 if (bfd_link_relocatable (info
))
538 htab
= riscv_elf_hash_table (info
);
539 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
540 sym_hashes
= elf_sym_hashes (abfd
);
542 if (htab
->elf
.dynobj
== NULL
)
543 htab
->elf
.dynobj
= abfd
;
545 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
548 unsigned int r_symndx
;
549 struct elf_link_hash_entry
*h
;
551 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
552 r_type
= ELFNN_R_TYPE (rel
->r_info
);
554 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
556 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
561 if (r_symndx
< symtab_hdr
->sh_info
)
565 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
566 while (h
->root
.type
== bfd_link_hash_indirect
567 || h
->root
.type
== bfd_link_hash_warning
)
568 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
573 case R_RISCV_TLS_GD_HI20
:
574 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
575 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
579 case R_RISCV_TLS_GOT_HI20
:
580 if (bfd_link_pic (info
))
581 info
->flags
|= DF_STATIC_TLS
;
582 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
583 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
587 case R_RISCV_GOT_HI20
:
588 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
589 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
593 case R_RISCV_CALL_PLT
:
594 /* This symbol requires a procedure linkage table entry. We
595 actually build the entry in adjust_dynamic_symbol,
596 because this might be a case of linking PIC code without
597 linking in any dynamic objects, in which case we don't
598 need to generate a procedure linkage table after all. */
603 h
->plt
.refcount
+= 1;
610 case R_RISCV_RVC_BRANCH
:
611 case R_RISCV_RVC_JUMP
:
612 case R_RISCV_PCREL_HI20
:
613 /* In shared libraries, these relocs are known to bind locally. */
614 if (bfd_link_pic (info
))
618 case R_RISCV_TPREL_HI20
:
619 if (!bfd_link_executable (info
))
620 return bad_static_reloc (abfd
, r_type
, h
);
622 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
626 if (bfd_link_pic (info
))
627 return bad_static_reloc (abfd
, r_type
, h
);
631 case R_RISCV_JUMP_SLOT
:
632 case R_RISCV_RELATIVE
:
638 /* This reloc might not bind locally. */
642 if (h
!= NULL
&& !bfd_link_pic (info
))
644 /* We may need a .plt entry if the function this reloc
645 refers to is in a shared lib. */
646 h
->plt
.refcount
+= 1;
649 /* If we are creating a shared library, and this is a reloc
650 against a global symbol, or a non PC relative reloc
651 against a local symbol, then we need to copy the reloc
652 into the shared library. However, if we are linking with
653 -Bsymbolic, we do not need to copy a reloc against a
654 global symbol which is defined in an object we are
655 including in the link (i.e., DEF_REGULAR is set). At
656 this point we have not seen all the input files, so it is
657 possible that DEF_REGULAR is not set now but will be set
658 later (it is never cleared). In case of a weak definition,
659 DEF_REGULAR may be cleared later by a strong definition in
660 a shared library. We account for that possibility below by
661 storing information in the relocs_copied field of the hash
662 table entry. A similar situation occurs when creating
663 shared libraries and symbol visibility changes render the
666 If on the other hand, we are creating an executable, we
667 may need to keep relocations for symbols satisfied by a
668 dynamic library if we manage to avoid copy relocs for the
670 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
672 if ((bfd_link_pic (info
)
673 && (sec
->flags
& SEC_ALLOC
) != 0
674 && ((r
!= NULL
&& ! r
->pc_relative
)
677 || h
->root
.type
== bfd_link_hash_defweak
678 || !h
->def_regular
))))
679 || (!bfd_link_pic (info
)
680 && (sec
->flags
& SEC_ALLOC
) != 0
682 && (h
->root
.type
== bfd_link_hash_defweak
683 || !h
->def_regular
)))
685 struct elf_dyn_relocs
*p
;
686 struct elf_dyn_relocs
**head
;
688 /* When creating a shared object, we must copy these
689 relocs into the output file. We create a reloc
690 section in dynobj and make room for the reloc. */
693 sreloc
= _bfd_elf_make_dynamic_reloc_section
694 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
695 abfd
, /*rela?*/ TRUE
);
701 /* If this is a global symbol, we count the number of
702 relocations we need for this symbol. */
704 head
= &h
->dyn_relocs
;
707 /* Track dynamic relocs needed for local syms too.
708 We really need local syms available to do this
713 Elf_Internal_Sym
*isym
;
715 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
720 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
724 vpp
= &elf_section_data (s
)->local_dynrel
;
725 head
= (struct elf_dyn_relocs
**) vpp
;
729 if (p
== NULL
|| p
->sec
!= sec
)
731 size_t amt
= sizeof *p
;
732 p
= ((struct elf_dyn_relocs
*)
733 bfd_alloc (htab
->elf
.dynobj
, amt
));
744 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
749 case R_RISCV_GNU_VTINHERIT
:
750 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
754 case R_RISCV_GNU_VTENTRY
:
755 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
768 riscv_elf_gc_mark_hook (asection
*sec
,
769 struct bfd_link_info
*info
,
770 Elf_Internal_Rela
*rel
,
771 struct elf_link_hash_entry
*h
,
772 Elf_Internal_Sym
*sym
)
775 switch (ELFNN_R_TYPE (rel
->r_info
))
777 case R_RISCV_GNU_VTINHERIT
:
778 case R_RISCV_GNU_VTENTRY
:
782 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
785 /* Find dynamic relocs for H that apply to read-only sections. */
788 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
790 struct elf_dyn_relocs
*p
;
792 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
794 asection
*s
= p
->sec
->output_section
;
796 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
802 /* Adjust a symbol defined by a dynamic object and referenced by a
803 regular object. The current definition is in some section of the
804 dynamic object, but we're not including those sections. We have to
805 change the definition to something the rest of the link can
809 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
810 struct elf_link_hash_entry
*h
)
812 struct riscv_elf_link_hash_table
*htab
;
813 struct riscv_elf_link_hash_entry
* eh
;
817 htab
= riscv_elf_hash_table (info
);
818 BFD_ASSERT (htab
!= NULL
);
820 dynobj
= htab
->elf
.dynobj
;
822 /* Make sure we know what is going on here. */
823 BFD_ASSERT (dynobj
!= NULL
825 || h
->type
== STT_GNU_IFUNC
829 && !h
->def_regular
)));
831 /* If this is a function, put it in the procedure linkage table. We
832 will fill in the contents of the procedure linkage table later
833 (although we could actually do it here). */
834 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
836 if (h
->plt
.refcount
<= 0
837 || SYMBOL_CALLS_LOCAL (info
, h
)
838 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
839 && h
->root
.type
== bfd_link_hash_undefweak
))
841 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
842 input file, but the symbol was never referred to by a dynamic
843 object, or if all references were garbage collected. In such
844 a case, we don't actually need to build a PLT entry. */
845 h
->plt
.offset
= (bfd_vma
) -1;
852 h
->plt
.offset
= (bfd_vma
) -1;
854 /* If this is a weak symbol, and there is a real definition, the
855 processor independent code will have arranged for us to see the
856 real definition first, and we can just use the same value. */
859 struct elf_link_hash_entry
*def
= weakdef (h
);
860 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
861 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
862 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
866 /* This is a reference to a symbol defined by a dynamic object which
867 is not a function. */
869 /* If we are creating a shared library, we must presume that the
870 only references to the symbol are via the global offset table.
871 For such cases we need not do anything here; the relocations will
872 be handled correctly by relocate_section. */
873 if (bfd_link_pic (info
))
876 /* If there are no references to this symbol that do not use the
877 GOT, we don't need to generate a copy reloc. */
881 /* If -z nocopyreloc was given, we won't generate them either. */
882 if (info
->nocopyreloc
)
888 /* If we don't find any dynamic relocs in read-only sections, then
889 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
890 if (!readonly_dynrelocs (h
))
896 /* We must allocate the symbol in our .dynbss section, which will
897 become part of the .bss section of the executable. There will be
898 an entry for this symbol in the .dynsym section. The dynamic
899 object will contain position independent code, so all references
900 from the dynamic object to this symbol will go through the global
901 offset table. The dynamic linker will use the .dynsym entry to
902 determine the address it must put in the global offset table, so
903 both the dynamic object and the regular object will refer to the
904 same memory location for the variable. */
906 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
907 to copy the initial value out of the dynamic object and into the
908 runtime process image. We need to remember the offset into the
909 .rel.bss section we are going to use. */
910 eh
= (struct riscv_elf_link_hash_entry
*) h
;
911 if (eh
->tls_type
& ~GOT_NORMAL
)
914 srel
= htab
->elf
.srelbss
;
916 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
918 s
= htab
->elf
.sdynrelro
;
919 srel
= htab
->elf
.sreldynrelro
;
923 s
= htab
->elf
.sdynbss
;
924 srel
= htab
->elf
.srelbss
;
926 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
928 srel
->size
+= sizeof (ElfNN_External_Rela
);
932 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
935 /* Allocate space in .plt, .got and associated reloc sections for
939 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
941 struct bfd_link_info
*info
;
942 struct riscv_elf_link_hash_table
*htab
;
943 struct elf_dyn_relocs
*p
;
945 if (h
->root
.type
== bfd_link_hash_indirect
)
948 info
= (struct bfd_link_info
*) inf
;
949 htab
= riscv_elf_hash_table (info
);
950 BFD_ASSERT (htab
!= NULL
);
952 if (htab
->elf
.dynamic_sections_created
953 && h
->plt
.refcount
> 0)
955 /* Make sure this symbol is output as a dynamic symbol.
956 Undefined weak syms won't yet be marked as dynamic. */
960 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
964 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
966 asection
*s
= htab
->elf
.splt
;
969 s
->size
= PLT_HEADER_SIZE
;
971 h
->plt
.offset
= s
->size
;
973 /* Make room for this entry. */
974 s
->size
+= PLT_ENTRY_SIZE
;
976 /* We also need to make an entry in the .got.plt section. */
977 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
979 /* We also need to make an entry in the .rela.plt section. */
980 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
982 /* If this symbol is not defined in a regular file, and we are
983 not generating a shared library, then set the symbol to this
984 location in the .plt. This is required to make function
985 pointers compare as equal between the normal executable and
986 the shared library. */
987 if (! bfd_link_pic (info
)
990 h
->root
.u
.def
.section
= s
;
991 h
->root
.u
.def
.value
= h
->plt
.offset
;
996 h
->plt
.offset
= (bfd_vma
) -1;
1002 h
->plt
.offset
= (bfd_vma
) -1;
1006 if (h
->got
.refcount
> 0)
1010 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1012 /* Make sure this symbol is output as a dynamic symbol.
1013 Undefined weak syms won't yet be marked as dynamic. */
1014 if (h
->dynindx
== -1
1015 && !h
->forced_local
)
1017 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1022 h
->got
.offset
= s
->size
;
1023 dyn
= htab
->elf
.dynamic_sections_created
;
1024 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1026 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1027 if (tls_type
& GOT_TLS_GD
)
1029 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1030 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1033 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1034 if (tls_type
& GOT_TLS_IE
)
1036 s
->size
+= RISCV_ELF_WORD_BYTES
;
1037 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1042 s
->size
+= RISCV_ELF_WORD_BYTES
;
1043 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1044 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1045 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1049 h
->got
.offset
= (bfd_vma
) -1;
1051 if (h
->dyn_relocs
== NULL
)
1054 /* In the shared -Bsymbolic case, discard space allocated for
1055 dynamic pc-relative relocs against symbols which turn out to be
1056 defined in regular objects. For the normal shared case, discard
1057 space for pc-relative relocs that have become local due to symbol
1058 visibility changes. */
1060 if (bfd_link_pic (info
))
1062 if (SYMBOL_CALLS_LOCAL (info
, h
))
1064 struct elf_dyn_relocs
**pp
;
1066 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1068 p
->count
-= p
->pc_count
;
1077 /* Also discard relocs on undefined weak syms with non-default
1079 if (h
->dyn_relocs
!= NULL
1080 && h
->root
.type
== bfd_link_hash_undefweak
)
1082 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1083 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1084 h
->dyn_relocs
= NULL
;
1086 /* Make sure undefined weak symbols are output as a dynamic
1088 else if (h
->dynindx
== -1
1089 && !h
->forced_local
)
1091 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1098 /* For the non-shared case, discard space for relocs against
1099 symbols which turn out to need copy relocs or are not
1105 || (htab
->elf
.dynamic_sections_created
1106 && (h
->root
.type
== bfd_link_hash_undefweak
1107 || h
->root
.type
== bfd_link_hash_undefined
))))
1109 /* Make sure this symbol is output as a dynamic symbol.
1110 Undefined weak syms won't yet be marked as dynamic. */
1111 if (h
->dynindx
== -1
1112 && !h
->forced_local
)
1114 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1118 /* If that succeeded, we know we'll be keeping all the
1120 if (h
->dynindx
!= -1)
1124 h
->dyn_relocs
= NULL
;
1129 /* Finally, allocate space. */
1130 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1132 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1133 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1139 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1140 read-only sections. */
1143 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
1147 if (h
->root
.type
== bfd_link_hash_indirect
)
1150 sec
= readonly_dynrelocs (h
);
1153 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
1155 info
->flags
|= DF_TEXTREL
;
1156 info
->callbacks
->minfo
1157 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1158 sec
->owner
, h
->root
.root
.string
, sec
);
1160 /* Not an error, just cut short the traversal. */
1167 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1169 struct riscv_elf_link_hash_table
*htab
;
1174 htab
= riscv_elf_hash_table (info
);
1175 BFD_ASSERT (htab
!= NULL
);
1176 dynobj
= htab
->elf
.dynobj
;
1177 BFD_ASSERT (dynobj
!= NULL
);
1179 if (elf_hash_table (info
)->dynamic_sections_created
)
1181 /* Set the contents of the .interp section to the interpreter. */
1182 if (bfd_link_executable (info
) && !info
->nointerp
)
1184 s
= bfd_get_linker_section (dynobj
, ".interp");
1185 BFD_ASSERT (s
!= NULL
);
1186 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1187 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1191 /* Set up .got offsets for local syms, and space for local dynamic
1193 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1195 bfd_signed_vma
*local_got
;
1196 bfd_signed_vma
*end_local_got
;
1197 char *local_tls_type
;
1198 bfd_size_type locsymcount
;
1199 Elf_Internal_Shdr
*symtab_hdr
;
1202 if (! is_riscv_elf (ibfd
))
1205 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1207 struct elf_dyn_relocs
*p
;
1209 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1211 if (!bfd_is_abs_section (p
->sec
)
1212 && bfd_is_abs_section (p
->sec
->output_section
))
1214 /* Input section has been discarded, either because
1215 it is a copy of a linkonce section or due to
1216 linker script /DISCARD/, so we'll be discarding
1219 else if (p
->count
!= 0)
1221 srel
= elf_section_data (p
->sec
)->sreloc
;
1222 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1223 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1224 info
->flags
|= DF_TEXTREL
;
1229 local_got
= elf_local_got_refcounts (ibfd
);
1233 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1234 locsymcount
= symtab_hdr
->sh_info
;
1235 end_local_got
= local_got
+ locsymcount
;
1236 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1238 srel
= htab
->elf
.srelgot
;
1239 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1243 *local_got
= s
->size
;
1244 s
->size
+= RISCV_ELF_WORD_BYTES
;
1245 if (*local_tls_type
& GOT_TLS_GD
)
1246 s
->size
+= RISCV_ELF_WORD_BYTES
;
1247 if (bfd_link_pic (info
)
1248 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1249 srel
->size
+= sizeof (ElfNN_External_Rela
);
1252 *local_got
= (bfd_vma
) -1;
1256 /* Allocate global sym .plt and .got entries, and space for global
1257 sym dynamic relocs. */
1258 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1260 if (htab
->elf
.sgotplt
)
1262 struct elf_link_hash_entry
*got
;
1263 got
= elf_link_hash_lookup (elf_hash_table (info
),
1264 "_GLOBAL_OFFSET_TABLE_",
1265 FALSE
, FALSE
, FALSE
);
1267 /* Don't allocate .got.plt section if there are no GOT nor PLT
1268 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1270 || !got
->ref_regular_nonweak
)
1271 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1272 && (htab
->elf
.splt
== NULL
1273 || htab
->elf
.splt
->size
== 0)
1274 && (htab
->elf
.sgot
== NULL
1275 || (htab
->elf
.sgot
->size
1276 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1277 htab
->elf
.sgotplt
->size
= 0;
1280 /* The check_relocs and adjust_dynamic_symbol entry points have
1281 determined the sizes of the various dynamic sections. Allocate
1283 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1285 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1288 if (s
== htab
->elf
.splt
1289 || s
== htab
->elf
.sgot
1290 || s
== htab
->elf
.sgotplt
1291 || s
== htab
->elf
.sdynbss
1292 || s
== htab
->elf
.sdynrelro
1293 || s
== htab
->sdyntdata
)
1295 /* Strip this section if we don't need it; see the
1298 else if (strncmp (s
->name
, ".rela", 5) == 0)
1302 /* We use the reloc_count field as a counter if we need
1303 to copy relocs into the output file. */
1309 /* It's not one of our sections. */
1315 /* If we don't need this section, strip it from the
1316 output file. This is mostly to handle .rela.bss and
1317 .rela.plt. We must create both sections in
1318 create_dynamic_sections, because they must be created
1319 before the linker maps input sections to output
1320 sections. The linker does that before
1321 adjust_dynamic_symbol is called, and it is that
1322 function which decides whether anything needs to go
1323 into these sections. */
1324 s
->flags
|= SEC_EXCLUDE
;
1328 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1331 /* Allocate memory for the section contents. Zero the memory
1332 for the benefit of .rela.plt, which has 4 unused entries
1333 at the beginning, and we don't want garbage. */
1334 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1335 if (s
->contents
== NULL
)
1339 if (elf_hash_table (info
)->dynamic_sections_created
)
1341 /* Add some entries to the .dynamic section. We fill in the
1342 values later, in riscv_elf_finish_dynamic_sections, but we
1343 must add the entries now so that we get the correct size for
1344 the .dynamic section. The DT_DEBUG entry is filled in by the
1345 dynamic linker and used by the debugger. */
1346 #define add_dynamic_entry(TAG, VAL) \
1347 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1349 if (bfd_link_executable (info
))
1351 if (!add_dynamic_entry (DT_DEBUG
, 0))
1355 if (htab
->elf
.srelplt
->size
!= 0)
1357 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1358 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1359 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1360 || !add_dynamic_entry (DT_JMPREL
, 0))
1364 if (!add_dynamic_entry (DT_RELA
, 0)
1365 || !add_dynamic_entry (DT_RELASZ
, 0)
1366 || !add_dynamic_entry (DT_RELAENT
, sizeof (ElfNN_External_Rela
)))
1369 /* If any dynamic relocs apply to a read-only section,
1370 then we need a DT_TEXTREL entry. */
1371 if ((info
->flags
& DF_TEXTREL
) == 0)
1372 elf_link_hash_traverse (&htab
->elf
, maybe_set_textrel
, info
);
1374 if (info
->flags
& DF_TEXTREL
)
1376 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1380 #undef add_dynamic_entry
1386 #define DTP_OFFSET 0x800
1388 /* Return the relocation value for a TLS dtp-relative reloc. */
1391 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1393 /* If tls_sec is NULL, we should have signalled an error already. */
1394 if (elf_hash_table (info
)->tls_sec
== NULL
)
1396 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1399 /* Return the relocation value for a static TLS tp-relative relocation. */
1402 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1404 /* If tls_sec is NULL, we should have signalled an error already. */
1405 if (elf_hash_table (info
)->tls_sec
== NULL
)
1407 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1410 /* Return the global pointer's value, or 0 if it is not in use. */
1413 riscv_global_pointer_value (struct bfd_link_info
*info
)
1415 struct bfd_link_hash_entry
*h
;
1417 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1418 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1421 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1424 /* Emplace a static relocation. */
1426 static bfd_reloc_status_type
1427 perform_relocation (const reloc_howto_type
*howto
,
1428 const Elf_Internal_Rela
*rel
,
1430 asection
*input_section
,
1434 if (howto
->pc_relative
)
1435 value
-= sec_addr (input_section
) + rel
->r_offset
;
1436 value
+= rel
->r_addend
;
1438 switch (ELFNN_R_TYPE (rel
->r_info
))
1441 case R_RISCV_TPREL_HI20
:
1442 case R_RISCV_PCREL_HI20
:
1443 case R_RISCV_GOT_HI20
:
1444 case R_RISCV_TLS_GOT_HI20
:
1445 case R_RISCV_TLS_GD_HI20
:
1446 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1447 return bfd_reloc_overflow
;
1448 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1451 case R_RISCV_LO12_I
:
1452 case R_RISCV_GPREL_I
:
1453 case R_RISCV_TPREL_LO12_I
:
1454 case R_RISCV_TPREL_I
:
1455 case R_RISCV_PCREL_LO12_I
:
1456 value
= ENCODE_ITYPE_IMM (value
);
1459 case R_RISCV_LO12_S
:
1460 case R_RISCV_GPREL_S
:
1461 case R_RISCV_TPREL_LO12_S
:
1462 case R_RISCV_TPREL_S
:
1463 case R_RISCV_PCREL_LO12_S
:
1464 value
= ENCODE_STYPE_IMM (value
);
1468 case R_RISCV_CALL_PLT
:
1469 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1470 return bfd_reloc_overflow
;
1471 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1472 | (ENCODE_ITYPE_IMM (value
) << 32);
1476 if (!VALID_UJTYPE_IMM (value
))
1477 return bfd_reloc_overflow
;
1478 value
= ENCODE_UJTYPE_IMM (value
);
1481 case R_RISCV_BRANCH
:
1482 if (!VALID_SBTYPE_IMM (value
))
1483 return bfd_reloc_overflow
;
1484 value
= ENCODE_SBTYPE_IMM (value
);
1487 case R_RISCV_RVC_BRANCH
:
1488 if (!VALID_RVC_B_IMM (value
))
1489 return bfd_reloc_overflow
;
1490 value
= ENCODE_RVC_B_IMM (value
);
1493 case R_RISCV_RVC_JUMP
:
1494 if (!VALID_RVC_J_IMM (value
))
1495 return bfd_reloc_overflow
;
1496 value
= ENCODE_RVC_J_IMM (value
);
1499 case R_RISCV_RVC_LUI
:
1500 if (RISCV_CONST_HIGH_PART (value
) == 0)
1502 /* Linker relaxation can convert an address equal to or greater than
1503 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1504 valid immediate. We can fix this by converting it to a C.LI. */
1505 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1506 contents
+ rel
->r_offset
);
1507 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1508 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1509 value
= ENCODE_RVC_IMM (0);
1511 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1512 return bfd_reloc_overflow
;
1514 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1532 case R_RISCV_32_PCREL
:
1533 case R_RISCV_TLS_DTPREL32
:
1534 case R_RISCV_TLS_DTPREL64
:
1537 case R_RISCV_DELETE
:
1538 return bfd_reloc_ok
;
1541 return bfd_reloc_notsupported
;
1544 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1545 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1546 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1548 return bfd_reloc_ok
;
1551 /* Remember all PC-relative high-part relocs we've encountered to help us
1552 later resolve the corresponding low-part relocs. */
1558 } riscv_pcrel_hi_reloc
;
1560 typedef struct riscv_pcrel_lo_reloc
1562 asection
* input_section
;
1563 struct bfd_link_info
* info
;
1564 reloc_howto_type
* howto
;
1565 const Elf_Internal_Rela
* reloc
;
1568 bfd_byte
* contents
;
1569 struct riscv_pcrel_lo_reloc
* next
;
1570 } riscv_pcrel_lo_reloc
;
1575 riscv_pcrel_lo_reloc
*lo_relocs
;
1576 } riscv_pcrel_relocs
;
1579 riscv_pcrel_reloc_hash (const void *entry
)
1581 const riscv_pcrel_hi_reloc
*e
= entry
;
1582 return (hashval_t
)(e
->address
>> 2);
1586 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1588 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1589 return e1
->address
== e2
->address
;
1593 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1596 p
->lo_relocs
= NULL
;
1597 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1598 riscv_pcrel_reloc_eq
, free
);
1599 return p
->hi_relocs
!= NULL
;
1603 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1605 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1609 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1614 htab_delete (p
->hi_relocs
);
1618 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1619 struct bfd_link_info
*info
,
1623 const reloc_howto_type
*howto
,
1626 /* We may need to reference low addreses in PC-relative modes even when the
1627 * PC is far away from these addresses. For example, undefweak references
1628 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1629 * addresses that we can link PC-relative programs at, the linker can't
1630 * actually relocate references to those symbols. In order to allow these
1631 * programs to work we simply convert the PC-relative auipc sequences to
1632 * 0-relative lui sequences. */
1633 if (bfd_link_pic (info
))
1636 /* If it's possible to reference the symbol using auipc we do so, as that's
1637 * more in the spirit of the PC-relative relocations we're processing. */
1638 bfd_vma offset
= addr
- pc
;
1639 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1642 /* If it's impossible to reference this with a LUI-based offset then don't
1643 * bother to convert it at all so users still see the PC-relative relocation
1644 * in the truncation message. */
1645 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1648 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1650 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1651 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1652 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1657 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1658 bfd_vma value
, bfd_boolean absolute
)
1660 bfd_vma offset
= absolute
? value
: value
- addr
;
1661 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1662 riscv_pcrel_hi_reloc
**slot
=
1663 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1665 BFD_ASSERT (*slot
== NULL
);
1666 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1674 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1675 asection
*input_section
,
1676 struct bfd_link_info
*info
,
1677 reloc_howto_type
*howto
,
1678 const Elf_Internal_Rela
*reloc
,
1683 riscv_pcrel_lo_reloc
*entry
;
1684 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1687 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1688 name
, contents
, p
->lo_relocs
};
1689 p
->lo_relocs
= entry
;
1694 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1696 riscv_pcrel_lo_reloc
*r
;
1698 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1700 bfd
*input_bfd
= r
->input_section
->owner
;
1702 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1703 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1705 /* Check for overflow into bit 11 when adding reloc addend. */
1706 || (! (entry
->value
& 0x800)
1707 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1709 char *string
= (entry
== NULL
1710 ? "%pcrel_lo missing matching %pcrel_hi"
1711 : "%pcrel_lo overflow with an addend");
1712 (*r
->info
->callbacks
->reloc_dangerous
)
1713 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1717 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1718 input_bfd
, r
->contents
);
1724 /* Relocate a RISC-V ELF section.
1726 The RELOCATE_SECTION function is called by the new ELF backend linker
1727 to handle the relocations for a section.
1729 The relocs are always passed as Rela structures.
1731 This function is responsible for adjusting the section contents as
1732 necessary, and (if generating a relocatable output file) adjusting
1733 the reloc addend as necessary.
1735 This function does not have to worry about setting the reloc
1736 address or the reloc symbol index.
1738 LOCAL_SYMS is a pointer to the swapped in local symbols.
1740 LOCAL_SECTIONS is an array giving the section in the input file
1741 corresponding to the st_shndx field of each local symbol.
1743 The global hash table entry for the global symbols can be found
1744 via elf_sym_hashes (input_bfd).
1746 When generating relocatable output, this function must handle
1747 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1748 going to be the section symbol corresponding to the output
1749 section, which means that the addend must be adjusted
1753 riscv_elf_relocate_section (bfd
*output_bfd
,
1754 struct bfd_link_info
*info
,
1756 asection
*input_section
,
1758 Elf_Internal_Rela
*relocs
,
1759 Elf_Internal_Sym
*local_syms
,
1760 asection
**local_sections
)
1762 Elf_Internal_Rela
*rel
;
1763 Elf_Internal_Rela
*relend
;
1764 riscv_pcrel_relocs pcrel_relocs
;
1765 bfd_boolean ret
= FALSE
;
1766 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1767 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1768 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1769 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1770 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1771 bfd_boolean absolute
;
1773 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1776 relend
= relocs
+ input_section
->reloc_count
;
1777 for (rel
= relocs
; rel
< relend
; rel
++)
1779 unsigned long r_symndx
;
1780 struct elf_link_hash_entry
*h
;
1781 Elf_Internal_Sym
*sym
;
1784 bfd_reloc_status_type r
= bfd_reloc_ok
;
1786 bfd_vma off
, ie_off
;
1787 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1788 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1789 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1790 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1791 const char *msg
= NULL
;
1792 char *msg_buf
= NULL
;
1793 bfd_boolean resolved_to_zero
;
1796 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1799 /* This is a final link. */
1800 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1804 unresolved_reloc
= FALSE
;
1805 if (r_symndx
< symtab_hdr
->sh_info
)
1807 sym
= local_syms
+ r_symndx
;
1808 sec
= local_sections
[r_symndx
];
1809 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1813 bfd_boolean warned
, ignored
;
1815 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1816 r_symndx
, symtab_hdr
, sym_hashes
,
1818 unresolved_reloc
, warned
, ignored
);
1821 /* To avoid generating warning messages about truncated
1822 relocations, set the relocation's address to be the same as
1823 the start of this section. */
1824 if (input_section
->output_section
!= NULL
)
1825 relocation
= input_section
->output_section
->vma
;
1831 if (sec
!= NULL
&& discarded_section (sec
))
1832 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1833 rel
, 1, relend
, howto
, 0, contents
);
1835 if (bfd_link_relocatable (info
))
1839 name
= h
->root
.root
.string
;
1842 name
= (bfd_elf_string_from_elf_section
1843 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1844 if (name
== NULL
|| *name
== '\0')
1845 name
= bfd_section_name (sec
);
1848 resolved_to_zero
= (h
!= NULL
1849 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1855 case R_RISCV_TPREL_ADD
:
1857 case R_RISCV_JUMP_SLOT
:
1858 case R_RISCV_RELATIVE
:
1859 /* These require nothing of us at all. */
1863 case R_RISCV_BRANCH
:
1864 case R_RISCV_RVC_BRANCH
:
1865 case R_RISCV_RVC_LUI
:
1866 case R_RISCV_LO12_I
:
1867 case R_RISCV_LO12_S
:
1872 case R_RISCV_32_PCREL
:
1873 case R_RISCV_DELETE
:
1874 /* These require no special handling beyond perform_relocation. */
1877 case R_RISCV_GOT_HI20
:
1880 bfd_boolean dyn
, pic
;
1882 off
= h
->got
.offset
;
1883 BFD_ASSERT (off
!= (bfd_vma
) -1);
1884 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1885 pic
= bfd_link_pic (info
);
1887 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1888 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1890 /* This is actually a static link, or it is a
1891 -Bsymbolic link and the symbol is defined
1892 locally, or the symbol was forced to be local
1893 because of a version file. We must initialize
1894 this entry in the global offset table. Since the
1895 offset must always be a multiple of the word size,
1896 we use the least significant bit to record whether
1897 we have initialized it already.
1899 When doing a dynamic link, we create a .rela.got
1900 relocation entry to initialize the value. This
1901 is done in the finish_dynamic_symbol routine. */
1906 bfd_put_NN (output_bfd
, relocation
,
1907 htab
->elf
.sgot
->contents
+ off
);
1912 unresolved_reloc
= FALSE
;
1916 BFD_ASSERT (local_got_offsets
!= NULL
1917 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1919 off
= local_got_offsets
[r_symndx
];
1921 /* The offset must always be a multiple of the word size.
1922 So, we can use the least significant bit to record
1923 whether we have already processed this entry. */
1928 if (bfd_link_pic (info
))
1931 Elf_Internal_Rela outrel
;
1933 /* We need to generate a R_RISCV_RELATIVE reloc
1934 for the dynamic linker. */
1935 s
= htab
->elf
.srelgot
;
1936 BFD_ASSERT (s
!= NULL
);
1938 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1940 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1941 outrel
.r_addend
= relocation
;
1943 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1946 bfd_put_NN (output_bfd
, relocation
,
1947 htab
->elf
.sgot
->contents
+ off
);
1948 local_got_offsets
[r_symndx
] |= 1;
1951 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1952 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1959 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1960 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1962 r
= bfd_reloc_notsupported
;
1963 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1964 relocation
, absolute
))
1965 r
= bfd_reloc_overflow
;
1973 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1974 contents
+ rel
->r_offset
);
1975 relocation
= old_value
+ relocation
;
1985 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1986 contents
+ rel
->r_offset
);
1987 relocation
= old_value
- relocation
;
1992 case R_RISCV_CALL_PLT
:
1993 /* Handle a call to an undefined weak function. This won't be
1994 relaxed, so we have to handle it here. */
1995 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
1996 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
1998 /* We can use x0 as the base register. */
1999 bfd_vma insn
= bfd_get_32 (input_bfd
,
2000 contents
+ rel
->r_offset
+ 4);
2001 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2002 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
2003 /* Set the relocation value so that we get 0 after the pc
2004 relative adjustment. */
2005 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2010 case R_RISCV_RVC_JUMP
:
2011 /* This line has to match the check in _bfd_riscv_relax_section. */
2012 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2014 /* Refer to the PLT entry. */
2015 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2016 unresolved_reloc
= FALSE
;
2020 case R_RISCV_TPREL_HI20
:
2021 relocation
= tpoff (info
, relocation
);
2024 case R_RISCV_TPREL_LO12_I
:
2025 case R_RISCV_TPREL_LO12_S
:
2026 relocation
= tpoff (info
, relocation
);
2029 case R_RISCV_TPREL_I
:
2030 case R_RISCV_TPREL_S
:
2031 relocation
= tpoff (info
, relocation
);
2032 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2034 /* We can use tp as the base register. */
2035 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2036 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2037 insn
|= X_TP
<< OP_SH_RS1
;
2038 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2041 r
= bfd_reloc_overflow
;
2044 case R_RISCV_GPREL_I
:
2045 case R_RISCV_GPREL_S
:
2047 bfd_vma gp
= riscv_global_pointer_value (info
);
2048 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2049 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2051 /* We can use x0 or gp as the base register. */
2052 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2053 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2056 rel
->r_addend
-= gp
;
2057 insn
|= X_GP
<< OP_SH_RS1
;
2059 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2062 r
= bfd_reloc_overflow
;
2066 case R_RISCV_PCREL_HI20
:
2067 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2074 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2075 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2077 r
= bfd_reloc_notsupported
;
2078 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2079 relocation
+ rel
->r_addend
,
2081 r
= bfd_reloc_overflow
;
2084 case R_RISCV_PCREL_LO12_I
:
2085 case R_RISCV_PCREL_LO12_S
:
2086 /* We don't allow section symbols plus addends as the auipc address,
2087 because then riscv_relax_delete_bytes would have to search through
2088 all relocs to update these addends. This is also ambiguous, as
2089 we do allow offsets to be added to the target address, which are
2090 not to be used to find the auipc address. */
2091 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2092 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2095 msg
= _("%pcrel_lo section symbol with an addend");
2096 r
= bfd_reloc_dangerous
;
2100 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2101 howto
, rel
, relocation
, name
,
2104 r
= bfd_reloc_overflow
;
2107 case R_RISCV_TLS_DTPREL32
:
2108 case R_RISCV_TLS_DTPREL64
:
2109 relocation
= dtpoff (info
, relocation
);
2114 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2117 if ((bfd_link_pic (info
)
2119 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2120 && !resolved_to_zero
)
2121 || h
->root
.type
!= bfd_link_hash_undefweak
)
2122 && (! howto
->pc_relative
2123 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2124 || (!bfd_link_pic (info
)
2130 || h
->root
.type
== bfd_link_hash_undefweak
2131 || h
->root
.type
== bfd_link_hash_undefined
)))
2133 Elf_Internal_Rela outrel
;
2134 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2136 /* When generating a shared object, these relocations
2137 are copied into the output file to be resolved at run
2141 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2143 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2144 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2145 outrel
.r_offset
+= sec_addr (input_section
);
2147 if (skip_dynamic_relocation
)
2148 memset (&outrel
, 0, sizeof outrel
);
2149 else if (h
!= NULL
&& h
->dynindx
!= -1
2150 && !(bfd_link_pic (info
)
2151 && SYMBOLIC_BIND (info
, h
)
2154 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2155 outrel
.r_addend
= rel
->r_addend
;
2159 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2160 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2163 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2164 if (skip_static_relocation
)
2169 case R_RISCV_TLS_GOT_HI20
:
2173 case R_RISCV_TLS_GD_HI20
:
2176 off
= h
->got
.offset
;
2181 off
= local_got_offsets
[r_symndx
];
2182 local_got_offsets
[r_symndx
] |= 1;
2185 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2186 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2187 /* If this symbol is referenced by both GD and IE TLS, the IE
2188 reference's GOT slot follows the GD reference's slots. */
2190 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2191 ie_off
= 2 * GOT_ENTRY_SIZE
;
2197 Elf_Internal_Rela outrel
;
2199 bfd_boolean need_relocs
= FALSE
;
2201 if (htab
->elf
.srelgot
== NULL
)
2206 bfd_boolean dyn
, pic
;
2207 dyn
= htab
->elf
.dynamic_sections_created
;
2208 pic
= bfd_link_pic (info
);
2210 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2211 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2215 /* The GOT entries have not been initialized yet. Do it
2216 now, and emit any relocations. */
2217 if ((bfd_link_pic (info
) || indx
!= 0)
2219 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2220 || h
->root
.type
!= bfd_link_hash_undefweak
))
2223 if (tls_type
& GOT_TLS_GD
)
2227 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2228 outrel
.r_addend
= 0;
2229 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2230 bfd_put_NN (output_bfd
, 0,
2231 htab
->elf
.sgot
->contents
+ off
);
2232 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2235 BFD_ASSERT (! unresolved_reloc
);
2236 bfd_put_NN (output_bfd
,
2237 dtpoff (info
, relocation
),
2238 (htab
->elf
.sgot
->contents
+ off
+
2239 RISCV_ELF_WORD_BYTES
));
2243 bfd_put_NN (output_bfd
, 0,
2244 (htab
->elf
.sgot
->contents
+ off
+
2245 RISCV_ELF_WORD_BYTES
));
2246 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2247 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2248 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2253 /* If we are not emitting relocations for a
2254 general dynamic reference, then we must be in a
2255 static link or an executable link with the
2256 symbol binding locally. Mark it as belonging
2257 to module 1, the executable. */
2258 bfd_put_NN (output_bfd
, 1,
2259 htab
->elf
.sgot
->contents
+ off
);
2260 bfd_put_NN (output_bfd
,
2261 dtpoff (info
, relocation
),
2262 (htab
->elf
.sgot
->contents
+ off
+
2263 RISCV_ELF_WORD_BYTES
));
2267 if (tls_type
& GOT_TLS_IE
)
2271 bfd_put_NN (output_bfd
, 0,
2272 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2273 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2275 outrel
.r_addend
= 0;
2277 outrel
.r_addend
= tpoff (info
, relocation
);
2278 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2279 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2283 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2284 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2289 BFD_ASSERT (off
< (bfd_vma
) -2);
2290 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2291 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2293 r
= bfd_reloc_overflow
;
2294 unresolved_reloc
= FALSE
;
2298 r
= bfd_reloc_notsupported
;
2301 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2302 because such sections are not SEC_ALLOC and thus ld.so will
2303 not process them. */
2304 if (unresolved_reloc
2305 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2307 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2308 rel
->r_offset
) != (bfd_vma
) -1)
2314 case R_RISCV_RVC_JUMP
:
2315 if (asprintf (&msg_buf
,
2316 _("%%X%%P: relocation %s against `%s' can "
2317 "not be used when making a shared object; "
2318 "recompile with -fPIC\n"),
2320 h
->root
.root
.string
) == -1)
2325 if (asprintf (&msg_buf
,
2326 _("%%X%%P: unresolvable %s relocation against "
2329 h
->root
.root
.string
) == -1)
2335 r
= bfd_reloc_notsupported
;
2338 if (r
== bfd_reloc_ok
)
2339 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2340 input_bfd
, contents
);
2342 /* We should have already detected the error and set message before.
2343 If the error message isn't set since the linker runs out of memory
2344 or we don't set it before, then we should set the default message
2345 with the "internal error" string here. */
2351 case bfd_reloc_overflow
:
2352 info
->callbacks
->reloc_overflow
2353 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2354 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2357 case bfd_reloc_undefined
:
2358 info
->callbacks
->undefined_symbol
2359 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2363 case bfd_reloc_outofrange
:
2365 msg
= _("%X%P: internal error: out of range error\n");
2368 case bfd_reloc_notsupported
:
2370 msg
= _("%X%P: internal error: unsupported relocation error\n");
2373 case bfd_reloc_dangerous
:
2374 /* The error message should already be set. */
2376 msg
= _("dangerous relocation error");
2377 info
->callbacks
->reloc_dangerous
2378 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2382 msg
= _("%X%P: internal error: unknown error\n");
2386 /* Do not report error message for the dangerous relocation again. */
2387 if (msg
&& r
!= bfd_reloc_dangerous
)
2388 info
->callbacks
->einfo (msg
);
2390 /* Free the unused `msg_buf`. */
2393 /* We already reported the error via a callback, so don't try to report
2394 it again by returning false. That leads to spurious errors. */
2399 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2401 riscv_free_pcrel_relocs (&pcrel_relocs
);
2405 /* Finish up dynamic symbol handling. We set the contents of various
2406 dynamic sections here. */
2409 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2410 struct bfd_link_info
*info
,
2411 struct elf_link_hash_entry
*h
,
2412 Elf_Internal_Sym
*sym
)
2414 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2415 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2417 if (h
->plt
.offset
!= (bfd_vma
) -1)
2419 /* We've decided to create a PLT entry for this symbol. */
2421 bfd_vma i
, header_address
, plt_idx
, got_address
;
2422 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2423 Elf_Internal_Rela rela
;
2425 BFD_ASSERT (h
->dynindx
!= -1);
2427 /* Calculate the address of the PLT header. */
2428 header_address
= sec_addr (htab
->elf
.splt
);
2430 /* Calculate the index of the entry. */
2431 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2433 /* Calculate the address of the .got.plt entry. */
2434 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2436 /* Find out where the .plt entry should go. */
2437 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2439 /* Fill in the PLT entry itself. */
2440 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2441 header_address
+ h
->plt
.offset
,
2445 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2446 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2448 /* Fill in the initial value of the .got.plt entry. */
2449 loc
= htab
->elf
.sgotplt
->contents
2450 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2451 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2453 /* Fill in the entry in the .rela.plt section. */
2454 rela
.r_offset
= got_address
;
2456 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2458 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2459 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2461 if (!h
->def_regular
)
2463 /* Mark the symbol as undefined, rather than as defined in
2464 the .plt section. Leave the value alone. */
2465 sym
->st_shndx
= SHN_UNDEF
;
2466 /* If the symbol is weak, we do need to clear the value.
2467 Otherwise, the PLT entry would provide a definition for
2468 the symbol even if the symbol wasn't defined anywhere,
2469 and so the symbol would never be NULL. */
2470 if (!h
->ref_regular_nonweak
)
2475 if (h
->got
.offset
!= (bfd_vma
) -1
2476 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2477 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2481 Elf_Internal_Rela rela
;
2483 /* This symbol has an entry in the GOT. Set it up. */
2485 sgot
= htab
->elf
.sgot
;
2486 srela
= htab
->elf
.srelgot
;
2487 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2489 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2491 /* If this is a local symbol reference, we just want to emit a RELATIVE
2492 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2493 the symbol was forced to be local because of a version file.
2494 The entry in the global offset table will already have been
2495 initialized in the relocate_section function. */
2496 if (bfd_link_pic (info
)
2497 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2499 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2500 asection
*sec
= h
->root
.u
.def
.section
;
2501 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2502 rela
.r_addend
= (h
->root
.u
.def
.value
2503 + sec
->output_section
->vma
2504 + sec
->output_offset
);
2508 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2509 BFD_ASSERT (h
->dynindx
!= -1);
2510 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2514 bfd_put_NN (output_bfd
, 0,
2515 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2516 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2521 Elf_Internal_Rela rela
;
2524 /* This symbols needs a copy reloc. Set it up. */
2525 BFD_ASSERT (h
->dynindx
!= -1);
2527 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2528 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2530 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2531 s
= htab
->elf
.sreldynrelro
;
2533 s
= htab
->elf
.srelbss
;
2534 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2537 /* Mark some specially defined symbols as absolute. */
2538 if (h
== htab
->elf
.hdynamic
2539 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2540 sym
->st_shndx
= SHN_ABS
;
2545 /* Finish up the dynamic sections. */
2548 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2549 bfd
*dynobj
, asection
*sdyn
)
2551 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2552 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2553 size_t dynsize
= bed
->s
->sizeof_dyn
;
2554 bfd_byte
*dyncon
, *dynconend
;
2556 dynconend
= sdyn
->contents
+ sdyn
->size
;
2557 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2559 Elf_Internal_Dyn dyn
;
2562 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2567 s
= htab
->elf
.sgotplt
;
2568 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2571 s
= htab
->elf
.srelplt
;
2572 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2575 s
= htab
->elf
.srelplt
;
2576 dyn
.d_un
.d_val
= s
->size
;
2582 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2588 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2589 struct bfd_link_info
*info
)
2593 struct riscv_elf_link_hash_table
*htab
;
2595 htab
= riscv_elf_hash_table (info
);
2596 BFD_ASSERT (htab
!= NULL
);
2597 dynobj
= htab
->elf
.dynobj
;
2599 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2601 if (elf_hash_table (info
)->dynamic_sections_created
)
2606 splt
= htab
->elf
.splt
;
2607 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2609 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2614 /* Fill in the head and tail entries in the procedure linkage table. */
2618 uint32_t plt_header
[PLT_HEADER_INSNS
];
2619 ret
= riscv_make_plt_header (output_bfd
,
2620 sec_addr (htab
->elf
.sgotplt
),
2621 sec_addr (splt
), plt_header
);
2625 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2626 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2628 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2633 if (htab
->elf
.sgotplt
)
2635 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2637 if (bfd_is_abs_section (output_section
))
2639 (*_bfd_error_handler
)
2640 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2644 if (htab
->elf
.sgotplt
->size
> 0)
2646 /* Write the first two entries in .got.plt, needed for the dynamic
2648 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2649 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2650 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2653 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2658 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2660 if (htab
->elf
.sgot
->size
> 0)
2662 /* Set the first entry in the global offset table to the address of
2663 the dynamic section. */
2664 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2665 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2668 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2674 /* Return address for Ith PLT stub in section PLT, for relocation REL
2675 or (bfd_vma) -1 if it should not be included. */
2678 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2679 const arelent
*rel ATTRIBUTE_UNUSED
)
2681 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2684 static enum elf_reloc_type_class
2685 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2686 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2687 const Elf_Internal_Rela
*rela
)
2689 switch (ELFNN_R_TYPE (rela
->r_info
))
2691 case R_RISCV_RELATIVE
:
2692 return reloc_class_relative
;
2693 case R_RISCV_JUMP_SLOT
:
2694 return reloc_class_plt
;
2696 return reloc_class_copy
;
2698 return reloc_class_normal
;
2702 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2706 riscv_float_abi_string (flagword flags
)
2708 switch (flags
& EF_RISCV_FLOAT_ABI
)
2710 case EF_RISCV_FLOAT_ABI_SOFT
:
2711 return "soft-float";
2713 case EF_RISCV_FLOAT_ABI_SINGLE
:
2714 return "single-float";
2716 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2717 return "double-float";
2719 case EF_RISCV_FLOAT_ABI_QUAD
:
2720 return "quad-float";
2727 /* The information of architecture attribute. */
2728 static riscv_subset_list_t in_subsets
;
2729 static riscv_subset_list_t out_subsets
;
2730 static riscv_subset_list_t merged_subsets
;
2732 /* Predicator for standard extension. */
2735 riscv_std_ext_p (const char *name
)
2737 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2740 /* Error handler when version mis-match. */
2743 riscv_version_mismatch (bfd
*ibfd
,
2744 struct riscv_subset_t
*in
,
2745 struct riscv_subset_t
*out
)
2748 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2751 in
->major_version
, in
->minor_version
,
2752 out
->major_version
, out
->minor_version
);
2755 /* Return true if subset is 'i' or 'e'. */
2758 riscv_i_or_e_p (bfd
*ibfd
,
2760 struct riscv_subset_t
*subset
)
2762 if ((strcasecmp (subset
->name
, "e") != 0)
2763 && (strcasecmp (subset
->name
, "i") != 0))
2766 (_("error: %pB: corrupted ISA string '%s'. "
2767 "First letter should be 'i' or 'e' but got '%s'."),
2768 ibfd
, arch
, subset
->name
);
2774 /* Merge standard extensions.
2777 Return FALSE if failed to merge.
2781 `in_arch`: Raw arch string for input object.
2782 `out_arch`: Raw arch string for output object.
2783 `pin`: subset list for input object, and it'll skip all merged subset after
2785 `pout`: Like `pin`, but for output object. */
2788 riscv_merge_std_ext (bfd
*ibfd
,
2789 const char *in_arch
,
2790 const char *out_arch
,
2791 struct riscv_subset_t
**pin
,
2792 struct riscv_subset_t
**pout
)
2794 const char *standard_exts
= riscv_supported_std_ext ();
2796 struct riscv_subset_t
*in
= *pin
;
2797 struct riscv_subset_t
*out
= *pout
;
2799 /* First letter should be 'i' or 'e'. */
2800 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2803 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2806 if (strcasecmp (in
->name
, out
->name
) != 0)
2808 /* TODO: We might allow merge 'i' with 'e'. */
2810 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2811 ibfd
, in
->name
, out
->name
);
2814 else if ((in
->major_version
!= out
->major_version
) ||
2815 (in
->minor_version
!= out
->minor_version
))
2817 /* TODO: Allow different merge policy. */
2818 riscv_version_mismatch (ibfd
, in
, out
);
2822 riscv_add_subset (&merged_subsets
,
2823 in
->name
, in
->major_version
, in
->minor_version
);
2828 /* Handle standard extension first. */
2829 for (p
= standard_exts
; *p
; ++p
)
2831 char find_ext
[2] = {*p
, '\0'};
2832 struct riscv_subset_t
*find_in
=
2833 riscv_lookup_subset (&in_subsets
, find_ext
);
2834 struct riscv_subset_t
*find_out
=
2835 riscv_lookup_subset (&out_subsets
, find_ext
);
2837 if (find_in
== NULL
&& find_out
== NULL
)
2840 /* Check version is same or not. */
2841 /* TODO: Allow different merge policy. */
2842 if ((find_in
!= NULL
&& find_out
!= NULL
)
2843 && ((find_in
->major_version
!= find_out
->major_version
)
2844 || (find_in
->minor_version
!= find_out
->minor_version
)))
2846 riscv_version_mismatch (ibfd
, in
, out
);
2850 struct riscv_subset_t
*merged
= find_in
? find_in
: find_out
;
2851 riscv_add_subset (&merged_subsets
, merged
->name
,
2852 merged
->major_version
, merged
->minor_version
);
2855 /* Skip all standard extensions. */
2856 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2857 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2865 /* If C is a prefix class, then return the EXT string without the prefix.
2866 Otherwise return the entire EXT string. */
2869 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
2873 case RV_ISA_CLASS_X
: return &ext
[1];
2874 case RV_ISA_CLASS_S
: return &ext
[1];
2875 case RV_ISA_CLASS_Z
: return &ext
[1];
2876 default: return ext
;
2880 /* Compare prefixed extension names canonically. */
2883 riscv_prefix_cmp (const char *a
, const char *b
)
2885 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
2886 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
2888 /* Extension name without prefix */
2889 const char *anp
= riscv_skip_prefix (a
, ca
);
2890 const char *bnp
= riscv_skip_prefix (b
, cb
);
2893 return strcasecmp (anp
, bnp
);
2895 return (int)ca
- (int)cb
;
2898 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2899 object subset list. Likewise for POUT and the output object. Return TRUE
2900 on success and FALSE when a conflict is found. */
2903 riscv_merge_multi_letter_ext (bfd
*ibfd
,
2904 riscv_subset_t
**pin
,
2905 riscv_subset_t
**pout
)
2907 riscv_subset_t
*in
= *pin
;
2908 riscv_subset_t
*out
= *pout
;
2909 riscv_subset_t
*tail
;
2915 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
2919 /* `in' comes before `out', append `in' and increment. */
2920 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2926 /* `out' comes before `in', append `out' and increment. */
2927 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2928 out
->minor_version
);
2933 /* Both present, check version and increment both. */
2934 if ((in
->major_version
!= out
->major_version
)
2935 || (in
->minor_version
!= out
->minor_version
))
2937 riscv_version_mismatch (ibfd
, in
, out
);
2941 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2942 out
->minor_version
);
2949 /* If we're here, either `in' or `out' is running longer than
2950 the other. So, we need to append the corresponding tail. */
2951 tail
= in
? in
: out
;
2955 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
2956 tail
->minor_version
);
2964 /* Merge Tag_RISCV_arch attribute. */
2967 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2969 riscv_subset_t
*in
, *out
;
2970 char *merged_arch_str
;
2972 unsigned xlen_in
, xlen_out
;
2973 merged_subsets
.head
= NULL
;
2974 merged_subsets
.tail
= NULL
;
2976 riscv_parse_subset_t rpe_in
;
2977 riscv_parse_subset_t rpe_out
;
2979 /* Only assembler needs to check the default version of ISA, so just set
2980 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
2981 rpe_in
.subset_list
= &in_subsets
;
2982 rpe_in
.error_handler
= _bfd_error_handler
;
2983 rpe_in
.xlen
= &xlen_in
;
2984 rpe_in
.get_default_version
= NULL
;
2986 rpe_out
.subset_list
= &out_subsets
;
2987 rpe_out
.error_handler
= _bfd_error_handler
;
2988 rpe_out
.xlen
= &xlen_out
;
2989 rpe_out
.get_default_version
= NULL
;
2991 if (in_arch
== NULL
&& out_arch
== NULL
)
2994 if (in_arch
== NULL
&& out_arch
!= NULL
)
2997 if (in_arch
!= NULL
&& out_arch
== NULL
)
3000 /* Parse subset from arch string. */
3001 if (!riscv_parse_subset (&rpe_in
, in_arch
))
3004 if (!riscv_parse_subset (&rpe_out
, out_arch
))
3007 /* Checking XLEN. */
3008 if (xlen_out
!= xlen_in
)
3011 (_("error: %pB: ISA string of input (%s) doesn't match "
3012 "output (%s)."), ibfd
, in_arch
, out_arch
);
3016 /* Merge subset list. */
3017 in
= in_subsets
.head
;
3018 out
= out_subsets
.head
;
3020 /* Merge standard extension. */
3021 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3024 /* Merge all non-single letter extensions with single call. */
3025 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3028 if (xlen_in
!= xlen_out
)
3031 (_("error: %pB: XLEN of input (%u) doesn't match "
3032 "output (%u)."), ibfd
, xlen_in
, xlen_out
);
3036 if (xlen_in
!= ARCH_SIZE
)
3039 (_("error: %pB: Unsupported XLEN (%u), you might be "
3040 "using wrong emulation."), ibfd
, xlen_in
);
3044 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3046 /* Release the subset lists. */
3047 riscv_release_subset_list (&in_subsets
);
3048 riscv_release_subset_list (&out_subsets
);
3049 riscv_release_subset_list (&merged_subsets
);
3051 return merged_arch_str
;
3054 /* Merge object attributes from IBFD into output_bfd of INFO.
3055 Raise an error if there are conflicting attributes. */
3058 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3060 bfd
*obfd
= info
->output_bfd
;
3061 obj_attribute
*in_attr
;
3062 obj_attribute
*out_attr
;
3063 bfd_boolean result
= TRUE
;
3064 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3067 /* Skip linker created files. */
3068 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3071 /* Skip any input that doesn't have an attribute section.
3072 This enables to link object files without attribute section with
3074 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3077 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3079 /* This is the first object. Copy the attributes. */
3080 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3082 out_attr
= elf_known_obj_attributes_proc (obfd
);
3084 /* Use the Tag_null value to indicate the attributes have been
3091 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3092 out_attr
= elf_known_obj_attributes_proc (obfd
);
3094 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3098 case Tag_RISCV_arch
:
3099 if (!out_attr
[Tag_RISCV_arch
].s
)
3100 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3101 else if (in_attr
[Tag_RISCV_arch
].s
3102 && out_attr
[Tag_RISCV_arch
].s
)
3104 /* Check arch compatible. */
3106 riscv_merge_arch_attr_info (ibfd
,
3107 in_attr
[Tag_RISCV_arch
].s
,
3108 out_attr
[Tag_RISCV_arch
].s
);
3109 if (merged_arch
== NULL
)
3112 out_attr
[Tag_RISCV_arch
].s
= "";
3115 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3118 case Tag_RISCV_priv_spec
:
3119 case Tag_RISCV_priv_spec_minor
:
3120 case Tag_RISCV_priv_spec_revision
:
3121 if (out_attr
[i
].i
!= in_attr
[i
].i
)
3124 (_("error: %pB: conflicting priv spec version "
3125 "(major/minor/revision)."), ibfd
);
3129 case Tag_RISCV_unaligned_access
:
3130 out_attr
[i
].i
|= in_attr
[i
].i
;
3132 case Tag_RISCV_stack_align
:
3133 if (out_attr
[i
].i
== 0)
3134 out_attr
[i
].i
= in_attr
[i
].i
;
3135 else if (in_attr
[i
].i
!= 0
3136 && out_attr
[i
].i
!= 0
3137 && out_attr
[i
].i
!= in_attr
[i
].i
)
3140 (_("error: %pB use %u-byte stack aligned but the output "
3141 "use %u-byte stack aligned."),
3142 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3147 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3150 /* If out_attr was copied from in_attr then it won't have a type yet. */
3151 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3152 out_attr
[i
].type
= in_attr
[i
].type
;
3155 /* Merge Tag_compatibility attributes and any common GNU ones. */
3156 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3159 /* Check for any attributes not known on RISC-V. */
3160 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3165 /* Merge backend specific data from an object file to the output
3166 object file when linking. */
3169 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3171 bfd
*obfd
= info
->output_bfd
;
3172 flagword new_flags
, old_flags
;
3174 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3177 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3179 (*_bfd_error_handler
)
3180 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3181 " target emulation `%s' does not match `%s'"),
3182 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3186 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3189 if (!riscv_merge_attributes (ibfd
, info
))
3192 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3193 old_flags
= elf_elfheader (obfd
)->e_flags
;
3195 if (! elf_flags_init (obfd
))
3197 elf_flags_init (obfd
) = TRUE
;
3198 elf_elfheader (obfd
)->e_flags
= new_flags
;
3202 /* Check to see if the input BFD actually contains any sections. If not,
3203 its flags may not have been initialized either, but it cannot actually
3204 cause any incompatibility. Do not short-circuit dynamic objects; their
3205 section list may be emptied by elf_link_add_object_symbols.
3207 Also check to see if there are no code sections in the input. In this
3208 case, there is no need to check for code specific flags. */
3209 if (!(ibfd
->flags
& DYNAMIC
))
3211 bfd_boolean null_input_bfd
= TRUE
;
3212 bfd_boolean only_data_sections
= TRUE
;
3215 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3217 if ((bfd_section_flags (sec
)
3218 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3219 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3220 only_data_sections
= FALSE
;
3222 null_input_bfd
= FALSE
;
3226 if (null_input_bfd
|| only_data_sections
)
3230 /* Disallow linking different float ABIs. */
3231 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3233 (*_bfd_error_handler
)
3234 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3235 riscv_float_abi_string (new_flags
),
3236 riscv_float_abi_string (old_flags
));
3240 /* Disallow linking RVE and non-RVE. */
3241 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3243 (*_bfd_error_handler
)
3244 (_("%pB: can't link RVE with other target"), ibfd
);
3248 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3249 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3254 bfd_set_error (bfd_error_bad_value
);
3258 /* Delete some bytes from a section while relaxing. */
3261 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3262 struct bfd_link_info
*link_info
)
3264 unsigned int i
, symcount
;
3265 bfd_vma toaddr
= sec
->size
;
3266 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3267 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3268 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3269 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3270 bfd_byte
*contents
= data
->this_hdr
.contents
;
3272 /* Actually delete the bytes. */
3274 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3276 /* Adjust the location of all of the relocs. Note that we need not
3277 adjust the addends, since all PC-relative references must be against
3278 symbols, which we will adjust below. */
3279 for (i
= 0; i
< sec
->reloc_count
; i
++)
3280 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3281 data
->relocs
[i
].r_offset
-= count
;
3283 /* Adjust the local symbols defined in this section. */
3284 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3286 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3287 if (sym
->st_shndx
== sec_shndx
)
3289 /* If the symbol is in the range of memory we just moved, we
3290 have to adjust its value. */
3291 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3292 sym
->st_value
-= count
;
3294 /* If the symbol *spans* the bytes we just deleted (i.e. its
3295 *end* is in the moved bytes but its *start* isn't), then we
3296 must adjust its size.
3298 This test needs to use the original value of st_value, otherwise
3299 we might accidentally decrease size when deleting bytes right
3300 before the symbol. But since deleted relocs can't span across
3301 symbols, we can't have both a st_value and a st_size decrease,
3302 so it is simpler to just use an else. */
3303 else if (sym
->st_value
<= addr
3304 && sym
->st_value
+ sym
->st_size
> addr
3305 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3306 sym
->st_size
-= count
;
3310 /* Now adjust the global symbols defined in this section. */
3311 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3312 - symtab_hdr
->sh_info
);
3314 for (i
= 0; i
< symcount
; i
++)
3316 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3318 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3319 containing the definition of __wrap_SYMBOL, includes a direct
3320 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3321 the same symbol (which is __wrap_SYMBOL), but still exist as two
3322 different symbols in 'sym_hashes', we don't want to adjust
3323 the global symbol __wrap_SYMBOL twice. */
3324 /* The same problem occurs with symbols that are versioned_hidden, as
3325 foo becomes an alias for foo@BAR, and hence they need the same
3327 if (link_info
->wrap_hash
!= NULL
3328 || sym_hash
->versioned
== versioned_hidden
)
3330 struct elf_link_hash_entry
**cur_sym_hashes
;
3332 /* Loop only over the symbols which have already been checked. */
3333 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3336 /* If the current symbol is identical to 'sym_hash', that means
3337 the symbol was already adjusted (or at least checked). */
3338 if (*cur_sym_hashes
== sym_hash
)
3341 /* Don't adjust the symbol again. */
3342 if (cur_sym_hashes
< &sym_hashes
[i
])
3346 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3347 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3348 && sym_hash
->root
.u
.def
.section
== sec
)
3350 /* As above, adjust the value if needed. */
3351 if (sym_hash
->root
.u
.def
.value
> addr
3352 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3353 sym_hash
->root
.u
.def
.value
-= count
;
3355 /* As above, adjust the size if needed. */
3356 else if (sym_hash
->root
.u
.def
.value
<= addr
3357 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3358 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3359 sym_hash
->size
-= count
;
3366 /* A second format for recording PC-relative hi relocations. This stores the
3367 information required to relax them to GP-relative addresses. */
3369 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3370 struct riscv_pcgp_hi_reloc
3377 bfd_boolean undefined_weak
;
3378 riscv_pcgp_hi_reloc
*next
;
3381 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3382 struct riscv_pcgp_lo_reloc
3385 riscv_pcgp_lo_reloc
*next
;
3390 riscv_pcgp_hi_reloc
*hi
;
3391 riscv_pcgp_lo_reloc
*lo
;
3392 } riscv_pcgp_relocs
;
3394 /* Initialize the pcgp reloc info in P. */
3397 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3404 /* Free the pcgp reloc info in P. */
3407 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3408 bfd
*abfd ATTRIBUTE_UNUSED
,
3409 asection
*sec ATTRIBUTE_UNUSED
)
3411 riscv_pcgp_hi_reloc
*c
;
3412 riscv_pcgp_lo_reloc
*l
;
3414 for (c
= p
->hi
; c
!= NULL
;)
3416 riscv_pcgp_hi_reloc
*next
= c
->next
;
3421 for (l
= p
->lo
; l
!= NULL
;)
3423 riscv_pcgp_lo_reloc
*next
= l
->next
;
3429 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3430 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3431 relax the corresponding lo part reloc. */
3434 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3435 bfd_vma hi_addend
, bfd_vma hi_addr
,
3436 unsigned hi_sym
, asection
*sym_sec
,
3437 bfd_boolean undefined_weak
)
3439 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3442 new->hi_sec_off
= hi_sec_off
;
3443 new->hi_addend
= hi_addend
;
3444 new->hi_addr
= hi_addr
;
3445 new->hi_sym
= hi_sym
;
3446 new->sym_sec
= sym_sec
;
3447 new->undefined_weak
= undefined_weak
;
3453 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3454 This is used by a lo part reloc to find the corresponding hi part reloc. */
3456 static riscv_pcgp_hi_reloc
*
3457 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3459 riscv_pcgp_hi_reloc
*c
;
3461 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3462 if (c
->hi_sec_off
== hi_sec_off
)
3467 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3468 This is used to record relocs that can't be relaxed. */
3471 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3473 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3476 new->hi_sec_off
= hi_sec_off
;
3482 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3483 This is used by a hi part reloc to find the corresponding lo part reloc. */
3486 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3488 riscv_pcgp_lo_reloc
*c
;
3490 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3491 if (c
->hi_sec_off
== hi_sec_off
)
3496 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3497 struct bfd_link_info
*,
3498 Elf_Internal_Rela
*,
3499 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3500 riscv_pcgp_relocs
*,
3501 bfd_boolean undefined_weak
);
3503 /* Relax AUIPC + JALR into JAL. */
3506 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3507 struct bfd_link_info
*link_info
,
3508 Elf_Internal_Rela
*rel
,
3510 bfd_vma max_alignment
,
3511 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3513 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3514 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3516 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3517 bfd_signed_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3518 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3519 bfd_vma auipc
, jalr
;
3520 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3522 /* If the call crosses section boundaries, an alignment directive could
3523 cause the PC-relative offset to later increase, so we need to add in the
3524 max alignment of any section inclusive from the call to the target.
3525 Otherwise, we only need to use the alignment of the current section. */
3526 if (VALID_UJTYPE_IMM (foff
))
3528 if (sym_sec
->output_section
== sec
->output_section
3529 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3530 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3531 foff
+= (foff
< 0 ? -max_alignment
: max_alignment
);
3534 /* See if this function call can be shortened. */
3535 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3538 /* Shorten the function call. */
3539 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3541 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3542 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3543 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3544 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3546 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3547 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3551 /* Relax to C.J[AL] rd, addr. */
3552 r_type
= R_RISCV_RVC_JUMP
;
3553 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3556 else if (VALID_UJTYPE_IMM (foff
))
3558 /* Relax to JAL rd, addr. */
3559 r_type
= R_RISCV_JAL
;
3560 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3562 else /* near_zero */
3564 /* Relax to JALR rd, x0, addr. */
3565 r_type
= R_RISCV_LO12_I
;
3566 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3569 /* Replace the R_RISCV_CALL reloc. */
3570 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3571 /* Replace the AUIPC. */
3572 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3574 /* Delete unnecessary JALR. */
3576 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3580 /* Traverse all output sections and return the max alignment. */
3583 _bfd_riscv_get_max_alignment (asection
*sec
)
3585 unsigned int max_alignment_power
= 0;
3588 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3590 if (o
->alignment_power
> max_alignment_power
)
3591 max_alignment_power
= o
->alignment_power
;
3594 return (bfd_vma
) 1 << max_alignment_power
;
3597 /* Relax non-PIC global variable references. */
3600 _bfd_riscv_relax_lui (bfd
*abfd
,
3603 struct bfd_link_info
*link_info
,
3604 Elf_Internal_Rela
*rel
,
3606 bfd_vma max_alignment
,
3607 bfd_vma reserve_size
,
3609 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3610 bfd_boolean undefined_weak
)
3612 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3613 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3614 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3616 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3620 /* If gp and the symbol are in the same output section, which is not the
3621 abs section, then consider only that output section's alignment. */
3622 struct bfd_link_hash_entry
*h
=
3623 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3625 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3626 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3627 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3630 /* Is the reference in range of x0 or gp?
3631 Valid gp range conservatively because of alignment issue. */
3633 || (VALID_ITYPE_IMM (symval
)
3635 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3637 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3639 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3640 switch (ELFNN_R_TYPE (rel
->r_info
))
3642 case R_RISCV_LO12_I
:
3645 /* Change the RS1 to zero. */
3646 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3647 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3648 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3651 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3654 case R_RISCV_LO12_S
:
3657 /* Change the RS1 to zero. */
3658 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3659 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3660 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3663 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3667 /* We can delete the unnecessary LUI and reloc. */
3668 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3670 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3678 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3679 account for this assuming page alignment at worst. In the presence of
3680 RELRO segment the linker aligns it by one page size, therefore sections
3681 after the segment can be moved more than one page. */
3684 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3685 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3686 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3687 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3688 : ELF_MAXPAGESIZE
)))
3690 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3691 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3692 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3693 if (rd
== 0 || rd
== X_SP
)
3696 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3697 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3699 /* Replace the R_RISCV_HI20 reloc. */
3700 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3703 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3710 /* Relax non-PIC TLS references. */
3713 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3715 asection
*sym_sec ATTRIBUTE_UNUSED
,
3716 struct bfd_link_info
*link_info
,
3717 Elf_Internal_Rela
*rel
,
3719 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3720 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3722 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3723 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3725 /* See if this symbol is in range of tp. */
3726 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3729 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3730 switch (ELFNN_R_TYPE (rel
->r_info
))
3732 case R_RISCV_TPREL_LO12_I
:
3733 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3736 case R_RISCV_TPREL_LO12_S
:
3737 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3740 case R_RISCV_TPREL_HI20
:
3741 case R_RISCV_TPREL_ADD
:
3742 /* We can delete the unnecessary instruction and reloc. */
3743 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3745 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3752 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3755 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3757 struct bfd_link_info
*link_info
,
3758 Elf_Internal_Rela
*rel
,
3760 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3761 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3762 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3763 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3764 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3766 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3767 bfd_vma alignment
= 1, pos
;
3768 while (alignment
<= rel
->r_addend
)
3771 symval
-= rel
->r_addend
;
3772 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3773 bfd_vma nop_bytes
= aligned_addr
- symval
;
3775 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3776 sec
->sec_flg0
= TRUE
;
3778 /* Make sure there are enough NOPs to actually achieve the alignment. */
3779 if (rel
->r_addend
< nop_bytes
)
3782 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3783 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3784 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3785 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3786 bfd_set_error (bfd_error_bad_value
);
3790 /* Delete the reloc. */
3791 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3793 /* If the number of NOPs is already correct, there's nothing to do. */
3794 if (nop_bytes
== rel
->r_addend
)
3797 /* Write as many RISC-V NOPs as we need. */
3798 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3799 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3801 /* Write a final RVC NOP if need be. */
3802 if (nop_bytes
% 4 != 0)
3803 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3805 /* Delete the excess bytes. */
3806 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3807 rel
->r_addend
- nop_bytes
, link_info
);
3810 /* Relax PC-relative references to GP-relative references. */
3813 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3816 struct bfd_link_info
*link_info
,
3817 Elf_Internal_Rela
*rel
,
3819 bfd_vma max_alignment
,
3820 bfd_vma reserve_size
,
3821 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3822 riscv_pcgp_relocs
*pcgp_relocs
,
3823 bfd_boolean undefined_weak
)
3825 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3826 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3828 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3830 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3831 * actual target address. */
3832 riscv_pcgp_hi_reloc hi_reloc
;
3833 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3834 switch (ELFNN_R_TYPE (rel
->r_info
))
3836 case R_RISCV_PCREL_LO12_I
:
3837 case R_RISCV_PCREL_LO12_S
:
3839 /* If the %lo has an addend, it isn't for the label pointing at the
3840 hi part instruction, but rather for the symbol pointed at by the
3841 hi part instruction. So we must subtract it here for the lookup.
3842 It is still used below in the final symbol address. */
3843 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3844 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3848 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3853 symval
= hi_reloc
.hi_addr
;
3854 sym_sec
= hi_reloc
.sym_sec
;
3856 /* We can not know whether the undefined weak symbol is referenced
3857 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3858 we have to record the 'undefined_weak' flag when handling the
3859 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3860 undefined_weak
= hi_reloc
.undefined_weak
;
3864 case R_RISCV_PCREL_HI20
:
3865 /* Mergeable symbols and code might later move out of range. */
3866 if (! undefined_weak
3867 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3870 /* If the cooresponding lo relocation has already been seen then it's not
3871 * safe to relax this relocation. */
3872 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3883 /* If gp and the symbol are in the same output section, which is not the
3884 abs section, then consider only that output section's alignment. */
3885 struct bfd_link_hash_entry
*h
=
3886 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3888 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3889 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3890 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3893 /* Is the reference in range of x0 or gp?
3894 Valid gp range conservatively because of alignment issue. */
3896 || (VALID_ITYPE_IMM (symval
)
3898 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3900 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3902 unsigned sym
= hi_reloc
.hi_sym
;
3903 switch (ELFNN_R_TYPE (rel
->r_info
))
3905 case R_RISCV_PCREL_LO12_I
:
3908 /* Change the RS1 to zero, and then modify the relocation
3909 type to R_RISCV_LO12_I. */
3910 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3911 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3912 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3913 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3914 rel
->r_addend
= hi_reloc
.hi_addend
;
3918 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3919 rel
->r_addend
+= hi_reloc
.hi_addend
;
3923 case R_RISCV_PCREL_LO12_S
:
3926 /* Change the RS1 to zero, and then modify the relocation
3927 type to R_RISCV_LO12_S. */
3928 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3929 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3930 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3931 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3932 rel
->r_addend
= hi_reloc
.hi_addend
;
3936 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3937 rel
->r_addend
+= hi_reloc
.hi_addend
;
3941 case R_RISCV_PCREL_HI20
:
3942 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3946 ELFNN_R_SYM(rel
->r_info
),
3949 /* We can delete the unnecessary AUIPC and reloc. */
3950 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3962 /* Relax PC-relative references to GP-relative references. */
3965 _bfd_riscv_relax_delete (bfd
*abfd
,
3967 asection
*sym_sec ATTRIBUTE_UNUSED
,
3968 struct bfd_link_info
*link_info
,
3969 Elf_Internal_Rela
*rel
,
3970 bfd_vma symval ATTRIBUTE_UNUSED
,
3971 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3972 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3973 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3974 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3975 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3977 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3980 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3984 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3985 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3986 disabled, handles code alignment directives. */
3989 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3990 struct bfd_link_info
*info
,
3993 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3994 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3995 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3996 Elf_Internal_Rela
*relocs
;
3997 bfd_boolean ret
= FALSE
;
3999 bfd_vma max_alignment
, reserve_size
= 0;
4000 riscv_pcgp_relocs pcgp_relocs
;
4004 if (bfd_link_relocatable (info
)
4006 || (sec
->flags
& SEC_RELOC
) == 0
4007 || sec
->reloc_count
== 0
4008 || (info
->disable_target_specific_optimizations
4009 && info
->relax_pass
== 0))
4012 riscv_init_pcgp_relocs (&pcgp_relocs
);
4014 /* Read this BFD's relocs if we haven't done so already. */
4016 relocs
= data
->relocs
;
4017 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4018 info
->keep_memory
)))
4023 max_alignment
= htab
->max_alignment
;
4024 if (max_alignment
== (bfd_vma
) -1)
4026 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4027 htab
->max_alignment
= max_alignment
;
4031 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4033 /* Examine and consider relaxing each reloc. */
4034 for (i
= 0; i
< sec
->reloc_count
; i
++)
4037 Elf_Internal_Rela
*rel
= relocs
+ i
;
4038 relax_func_t relax_func
;
4039 int type
= ELFNN_R_TYPE (rel
->r_info
);
4042 bfd_boolean undefined_weak
= FALSE
;
4045 if (info
->relax_pass
== 0)
4047 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
4048 relax_func
= _bfd_riscv_relax_call
;
4049 else if (type
== R_RISCV_HI20
4050 || type
== R_RISCV_LO12_I
4051 || type
== R_RISCV_LO12_S
)
4052 relax_func
= _bfd_riscv_relax_lui
;
4053 else if (!bfd_link_pic(info
)
4054 && (type
== R_RISCV_PCREL_HI20
4055 || type
== R_RISCV_PCREL_LO12_I
4056 || type
== R_RISCV_PCREL_LO12_S
))
4057 relax_func
= _bfd_riscv_relax_pc
;
4058 else if (type
== R_RISCV_TPREL_HI20
4059 || type
== R_RISCV_TPREL_ADD
4060 || type
== R_RISCV_TPREL_LO12_I
4061 || type
== R_RISCV_TPREL_LO12_S
)
4062 relax_func
= _bfd_riscv_relax_tls_le
;
4066 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4067 if (i
== sec
->reloc_count
- 1
4068 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4069 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4072 /* Skip over the R_RISCV_RELAX. */
4075 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4076 relax_func
= _bfd_riscv_relax_delete
;
4077 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4078 relax_func
= _bfd_riscv_relax_align
;
4082 data
->relocs
= relocs
;
4084 /* Read this BFD's contents if we haven't done so already. */
4085 if (!data
->this_hdr
.contents
4086 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4089 /* Read this BFD's symbols if we haven't done so already. */
4090 if (symtab_hdr
->sh_info
!= 0
4091 && !symtab_hdr
->contents
4092 && !(symtab_hdr
->contents
=
4093 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4094 symtab_hdr
->sh_info
,
4095 0, NULL
, NULL
, NULL
)))
4098 /* Get the value of the symbol referred to by the reloc. */
4099 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4101 /* A local symbol. */
4102 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4103 + ELFNN_R_SYM (rel
->r_info
));
4104 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4105 ? 0 : isym
->st_size
- rel
->r_addend
;
4107 if (isym
->st_shndx
== SHN_UNDEF
)
4108 sym_sec
= sec
, symval
= rel
->r_offset
;
4111 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4112 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4114 /* The purpose of this code is unknown. It breaks linker scripts
4115 for embedded development that place sections at address zero.
4116 This code is believed to be unnecessary. Disabling it but not
4117 yet removing it, in case something breaks. */
4118 if (sec_addr (sym_sec
) == 0)
4121 symval
= isym
->st_value
;
4123 symtype
= ELF_ST_TYPE (isym
->st_info
);
4128 struct elf_link_hash_entry
*h
;
4130 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4131 h
= elf_sym_hashes (abfd
)[indx
];
4133 while (h
->root
.type
== bfd_link_hash_indirect
4134 || h
->root
.type
== bfd_link_hash_warning
)
4135 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4137 if (h
->root
.type
== bfd_link_hash_undefweak
4138 && (relax_func
== _bfd_riscv_relax_lui
4139 || relax_func
== _bfd_riscv_relax_pc
))
4141 /* For the lui and auipc relaxations, since the symbol
4142 value of an undefined weak symbol is always be zero,
4143 we can optimize the patterns into a single LI/MV/ADDI
4146 Note that, creating shared libraries and pie output may
4147 break the rule above. Fortunately, since we do not relax
4148 pc relocs when creating shared libraries and pie output,
4149 and the absolute address access for R_RISCV_HI20 isn't
4150 allowed when "-fPIC" is set, the problem of creating shared
4151 libraries can not happen currently. Once we support the
4152 auipc relaxations when creating shared libraries, then we will
4153 need the more rigorous checking for this optimization. */
4154 undefined_weak
= TRUE
;
4157 /* This line has to match the check in riscv_elf_relocate_section
4158 in the R_RISCV_CALL[_PLT] case. */
4159 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4161 sym_sec
= htab
->elf
.splt
;
4162 symval
= h
->plt
.offset
;
4164 else if (undefined_weak
)
4167 sym_sec
= bfd_und_section_ptr
;
4169 else if ((h
->root
.type
== bfd_link_hash_defined
4170 || h
->root
.type
== bfd_link_hash_defweak
)
4171 && h
->root
.u
.def
.section
!= NULL
4172 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4174 symval
= h
->root
.u
.def
.value
;
4175 sym_sec
= h
->root
.u
.def
.section
;
4180 if (h
->type
!= STT_FUNC
)
4182 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4186 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4187 && (sym_sec
->flags
& SEC_MERGE
))
4189 /* At this stage in linking, no SEC_MERGE symbol has been
4190 adjusted, so all references to such symbols need to be
4191 passed through _bfd_merged_section_offset. (Later, in
4192 relocate_section, all SEC_MERGE symbols *except* for
4193 section symbols have been adjusted.)
4195 gas may reduce relocations against symbols in SEC_MERGE
4196 sections to a relocation against the section symbol when
4197 the original addend was zero. When the reloc is against
4198 a section symbol we should include the addend in the
4199 offset passed to _bfd_merged_section_offset, since the
4200 location of interest is the original symbol. On the
4201 other hand, an access to "sym+addend" where "sym" is not
4202 a section symbol should not include the addend; Such an
4203 access is presumed to be an offset from "sym"; The
4204 location of interest is just "sym". */
4205 if (symtype
== STT_SECTION
)
4206 symval
+= rel
->r_addend
;
4208 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4209 elf_section_data (sym_sec
)->sec_info
,
4212 if (symtype
!= STT_SECTION
)
4213 symval
+= rel
->r_addend
;
4216 symval
+= rel
->r_addend
;
4218 symval
+= sec_addr (sym_sec
);
4220 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4221 max_alignment
, reserve_size
, again
,
4222 &pcgp_relocs
, undefined_weak
))
4229 if (relocs
!= data
->relocs
)
4231 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4237 # define PRSTATUS_SIZE 204
4238 # define PRSTATUS_OFFSET_PR_CURSIG 12
4239 # define PRSTATUS_OFFSET_PR_PID 24
4240 # define PRSTATUS_OFFSET_PR_REG 72
4241 # define ELF_GREGSET_T_SIZE 128
4242 # define PRPSINFO_SIZE 128
4243 # define PRPSINFO_OFFSET_PR_PID 16
4244 # define PRPSINFO_OFFSET_PR_FNAME 32
4245 # define PRPSINFO_OFFSET_PR_PSARGS 48
4247 # define PRSTATUS_SIZE 376
4248 # define PRSTATUS_OFFSET_PR_CURSIG 12
4249 # define PRSTATUS_OFFSET_PR_PID 32
4250 # define PRSTATUS_OFFSET_PR_REG 112
4251 # define ELF_GREGSET_T_SIZE 256
4252 # define PRPSINFO_SIZE 136
4253 # define PRPSINFO_OFFSET_PR_PID 24
4254 # define PRPSINFO_OFFSET_PR_FNAME 40
4255 # define PRPSINFO_OFFSET_PR_PSARGS 56
4258 /* Support for core dump NOTE sections. */
4261 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4263 switch (note
->descsz
)
4268 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4270 elf_tdata (abfd
)->core
->signal
4271 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4274 elf_tdata (abfd
)->core
->lwpid
4275 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4279 /* Make a ".reg/999" section. */
4280 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4281 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4285 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4287 switch (note
->descsz
)
4292 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4294 elf_tdata (abfd
)->core
->pid
4295 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4298 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4299 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4302 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4303 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4307 /* Note that for some reason, a spurious space is tacked
4308 onto the end of the args in some (at least one anyway)
4309 implementations, so strip it off if it exists. */
4312 char *command
= elf_tdata (abfd
)->core
->command
;
4313 int n
= strlen (command
);
4315 if (0 < n
&& command
[n
- 1] == ' ')
4316 command
[n
- 1] = '\0';
4322 /* Set the right mach type. */
4324 riscv_elf_object_p (bfd
*abfd
)
4326 /* There are only two mach types in RISCV currently. */
4327 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4328 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4330 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4335 /* Determine whether an object attribute tag takes an integer, a
4339 riscv_elf_obj_attrs_arg_type (int tag
)
4341 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4344 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4345 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4347 #define elf_backend_reloc_type_class riscv_reloc_type_class
4349 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4350 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4351 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4352 #define bfd_elfNN_bfd_merge_private_bfd_data \
4353 _bfd_riscv_elf_merge_private_bfd_data
4355 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4356 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4357 #define elf_backend_check_relocs riscv_elf_check_relocs
4358 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4359 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4360 #define elf_backend_relocate_section riscv_elf_relocate_section
4361 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4362 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4363 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4364 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4365 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4366 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4367 #define elf_backend_object_p riscv_elf_object_p
4368 #define elf_info_to_howto_rel NULL
4369 #define elf_info_to_howto riscv_info_to_howto_rela
4370 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4371 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4373 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4375 #define elf_backend_can_gc_sections 1
4376 #define elf_backend_can_refcount 1
4377 #define elf_backend_want_got_plt 1
4378 #define elf_backend_plt_readonly 1
4379 #define elf_backend_plt_alignment 4
4380 #define elf_backend_want_plt_sym 1
4381 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4382 #define elf_backend_want_dynrelro 1
4383 #define elf_backend_rela_normal 1
4384 #define elf_backend_default_execstack 0
4386 #undef elf_backend_obj_attrs_vendor
4387 #define elf_backend_obj_attrs_vendor "riscv"
4388 #undef elf_backend_obj_attrs_arg_type
4389 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4390 #undef elf_backend_obj_attrs_section_type
4391 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4392 #undef elf_backend_obj_attrs_section
4393 #define elf_backend_obj_attrs_section ".riscv.attributes"
4395 #include "elfNN-target.h"