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 /* Adjust a symbol defined by a dynamic object and referenced by a
786 regular object. The current definition is in some section of the
787 dynamic object, but we're not including those sections. We have to
788 change the definition to something the rest of the link can
792 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
793 struct elf_link_hash_entry
*h
)
795 struct riscv_elf_link_hash_table
*htab
;
796 struct riscv_elf_link_hash_entry
* eh
;
800 htab
= riscv_elf_hash_table (info
);
801 BFD_ASSERT (htab
!= NULL
);
803 dynobj
= htab
->elf
.dynobj
;
805 /* Make sure we know what is going on here. */
806 BFD_ASSERT (dynobj
!= NULL
808 || h
->type
== STT_GNU_IFUNC
812 && !h
->def_regular
)));
814 /* If this is a function, put it in the procedure linkage table. We
815 will fill in the contents of the procedure linkage table later
816 (although we could actually do it here). */
817 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
819 if (h
->plt
.refcount
<= 0
820 || SYMBOL_CALLS_LOCAL (info
, h
)
821 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
822 && h
->root
.type
== bfd_link_hash_undefweak
))
824 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
825 input file, but the symbol was never referred to by a dynamic
826 object, or if all references were garbage collected. In such
827 a case, we don't actually need to build a PLT entry. */
828 h
->plt
.offset
= (bfd_vma
) -1;
835 h
->plt
.offset
= (bfd_vma
) -1;
837 /* If this is a weak symbol, and there is a real definition, the
838 processor independent code will have arranged for us to see the
839 real definition first, and we can just use the same value. */
842 struct elf_link_hash_entry
*def
= weakdef (h
);
843 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
844 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
845 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
849 /* This is a reference to a symbol defined by a dynamic object which
850 is not a function. */
852 /* If we are creating a shared library, we must presume that the
853 only references to the symbol are via the global offset table.
854 For such cases we need not do anything here; the relocations will
855 be handled correctly by relocate_section. */
856 if (bfd_link_pic (info
))
859 /* If there are no references to this symbol that do not use the
860 GOT, we don't need to generate a copy reloc. */
864 /* If -z nocopyreloc was given, we won't generate them either. */
865 if (info
->nocopyreloc
)
871 /* If we don't find any dynamic relocs in read-only sections, then
872 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
873 if (!_bfd_elf_readonly_dynrelocs (h
))
879 /* We must allocate the symbol in our .dynbss section, which will
880 become part of the .bss section of the executable. There will be
881 an entry for this symbol in the .dynsym section. The dynamic
882 object will contain position independent code, so all references
883 from the dynamic object to this symbol will go through the global
884 offset table. The dynamic linker will use the .dynsym entry to
885 determine the address it must put in the global offset table, so
886 both the dynamic object and the regular object will refer to the
887 same memory location for the variable. */
889 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
890 to copy the initial value out of the dynamic object and into the
891 runtime process image. We need to remember the offset into the
892 .rel.bss section we are going to use. */
893 eh
= (struct riscv_elf_link_hash_entry
*) h
;
894 if (eh
->tls_type
& ~GOT_NORMAL
)
897 srel
= htab
->elf
.srelbss
;
899 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
901 s
= htab
->elf
.sdynrelro
;
902 srel
= htab
->elf
.sreldynrelro
;
906 s
= htab
->elf
.sdynbss
;
907 srel
= htab
->elf
.srelbss
;
909 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
911 srel
->size
+= sizeof (ElfNN_External_Rela
);
915 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
918 /* Allocate space in .plt, .got and associated reloc sections for
922 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
924 struct bfd_link_info
*info
;
925 struct riscv_elf_link_hash_table
*htab
;
926 struct elf_dyn_relocs
*p
;
928 if (h
->root
.type
== bfd_link_hash_indirect
)
931 info
= (struct bfd_link_info
*) inf
;
932 htab
= riscv_elf_hash_table (info
);
933 BFD_ASSERT (htab
!= NULL
);
935 if (htab
->elf
.dynamic_sections_created
936 && h
->plt
.refcount
> 0)
938 /* Make sure this symbol is output as a dynamic symbol.
939 Undefined weak syms won't yet be marked as dynamic. */
943 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
947 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
949 asection
*s
= htab
->elf
.splt
;
952 s
->size
= PLT_HEADER_SIZE
;
954 h
->plt
.offset
= s
->size
;
956 /* Make room for this entry. */
957 s
->size
+= PLT_ENTRY_SIZE
;
959 /* We also need to make an entry in the .got.plt section. */
960 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
962 /* We also need to make an entry in the .rela.plt section. */
963 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
965 /* If this symbol is not defined in a regular file, and we are
966 not generating a shared library, then set the symbol to this
967 location in the .plt. This is required to make function
968 pointers compare as equal between the normal executable and
969 the shared library. */
970 if (! bfd_link_pic (info
)
973 h
->root
.u
.def
.section
= s
;
974 h
->root
.u
.def
.value
= h
->plt
.offset
;
979 h
->plt
.offset
= (bfd_vma
) -1;
985 h
->plt
.offset
= (bfd_vma
) -1;
989 if (h
->got
.refcount
> 0)
993 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
995 /* Make sure this symbol is output as a dynamic symbol.
996 Undefined weak syms won't yet be marked as dynamic. */
1000 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1005 h
->got
.offset
= s
->size
;
1006 dyn
= htab
->elf
.dynamic_sections_created
;
1007 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1009 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1010 if (tls_type
& GOT_TLS_GD
)
1012 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1013 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1016 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1017 if (tls_type
& GOT_TLS_IE
)
1019 s
->size
+= RISCV_ELF_WORD_BYTES
;
1020 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1025 s
->size
+= RISCV_ELF_WORD_BYTES
;
1026 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1027 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1028 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1032 h
->got
.offset
= (bfd_vma
) -1;
1034 if (h
->dyn_relocs
== NULL
)
1037 /* In the shared -Bsymbolic case, discard space allocated for
1038 dynamic pc-relative relocs against symbols which turn out to be
1039 defined in regular objects. For the normal shared case, discard
1040 space for pc-relative relocs that have become local due to symbol
1041 visibility changes. */
1043 if (bfd_link_pic (info
))
1045 if (SYMBOL_CALLS_LOCAL (info
, h
))
1047 struct elf_dyn_relocs
**pp
;
1049 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1051 p
->count
-= p
->pc_count
;
1060 /* Also discard relocs on undefined weak syms with non-default
1062 if (h
->dyn_relocs
!= NULL
1063 && h
->root
.type
== bfd_link_hash_undefweak
)
1065 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1066 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1067 h
->dyn_relocs
= NULL
;
1069 /* Make sure undefined weak symbols are output as a dynamic
1071 else if (h
->dynindx
== -1
1072 && !h
->forced_local
)
1074 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1081 /* For the non-shared case, discard space for relocs against
1082 symbols which turn out to need copy relocs or are not
1088 || (htab
->elf
.dynamic_sections_created
1089 && (h
->root
.type
== bfd_link_hash_undefweak
1090 || h
->root
.type
== bfd_link_hash_undefined
))))
1092 /* Make sure this symbol is output as a dynamic symbol.
1093 Undefined weak syms won't yet be marked as dynamic. */
1094 if (h
->dynindx
== -1
1095 && !h
->forced_local
)
1097 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1101 /* If that succeeded, we know we'll be keeping all the
1103 if (h
->dynindx
!= -1)
1107 h
->dyn_relocs
= NULL
;
1112 /* Finally, allocate space. */
1113 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1115 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1116 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1122 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1123 read-only sections. */
1126 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
1130 if (h
->root
.type
== bfd_link_hash_indirect
)
1133 sec
= _bfd_elf_readonly_dynrelocs (h
);
1136 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
1138 info
->flags
|= DF_TEXTREL
;
1139 info
->callbacks
->minfo
1140 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1141 sec
->owner
, h
->root
.root
.string
, sec
);
1143 /* Not an error, just cut short the traversal. */
1150 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1152 struct riscv_elf_link_hash_table
*htab
;
1157 htab
= riscv_elf_hash_table (info
);
1158 BFD_ASSERT (htab
!= NULL
);
1159 dynobj
= htab
->elf
.dynobj
;
1160 BFD_ASSERT (dynobj
!= NULL
);
1162 if (elf_hash_table (info
)->dynamic_sections_created
)
1164 /* Set the contents of the .interp section to the interpreter. */
1165 if (bfd_link_executable (info
) && !info
->nointerp
)
1167 s
= bfd_get_linker_section (dynobj
, ".interp");
1168 BFD_ASSERT (s
!= NULL
);
1169 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1170 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1174 /* Set up .got offsets for local syms, and space for local dynamic
1176 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1178 bfd_signed_vma
*local_got
;
1179 bfd_signed_vma
*end_local_got
;
1180 char *local_tls_type
;
1181 bfd_size_type locsymcount
;
1182 Elf_Internal_Shdr
*symtab_hdr
;
1185 if (! is_riscv_elf (ibfd
))
1188 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1190 struct elf_dyn_relocs
*p
;
1192 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1194 if (!bfd_is_abs_section (p
->sec
)
1195 && bfd_is_abs_section (p
->sec
->output_section
))
1197 /* Input section has been discarded, either because
1198 it is a copy of a linkonce section or due to
1199 linker script /DISCARD/, so we'll be discarding
1202 else if (p
->count
!= 0)
1204 srel
= elf_section_data (p
->sec
)->sreloc
;
1205 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1206 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1207 info
->flags
|= DF_TEXTREL
;
1212 local_got
= elf_local_got_refcounts (ibfd
);
1216 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1217 locsymcount
= symtab_hdr
->sh_info
;
1218 end_local_got
= local_got
+ locsymcount
;
1219 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1221 srel
= htab
->elf
.srelgot
;
1222 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1226 *local_got
= s
->size
;
1227 s
->size
+= RISCV_ELF_WORD_BYTES
;
1228 if (*local_tls_type
& GOT_TLS_GD
)
1229 s
->size
+= RISCV_ELF_WORD_BYTES
;
1230 if (bfd_link_pic (info
)
1231 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1232 srel
->size
+= sizeof (ElfNN_External_Rela
);
1235 *local_got
= (bfd_vma
) -1;
1239 /* Allocate global sym .plt and .got entries, and space for global
1240 sym dynamic relocs. */
1241 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1243 if (htab
->elf
.sgotplt
)
1245 struct elf_link_hash_entry
*got
;
1246 got
= elf_link_hash_lookup (elf_hash_table (info
),
1247 "_GLOBAL_OFFSET_TABLE_",
1248 FALSE
, FALSE
, FALSE
);
1250 /* Don't allocate .got.plt section if there are no GOT nor PLT
1251 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1253 || !got
->ref_regular_nonweak
)
1254 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1255 && (htab
->elf
.splt
== NULL
1256 || htab
->elf
.splt
->size
== 0)
1257 && (htab
->elf
.sgot
== NULL
1258 || (htab
->elf
.sgot
->size
1259 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1260 htab
->elf
.sgotplt
->size
= 0;
1263 /* The check_relocs and adjust_dynamic_symbol entry points have
1264 determined the sizes of the various dynamic sections. Allocate
1266 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1268 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1271 if (s
== htab
->elf
.splt
1272 || s
== htab
->elf
.sgot
1273 || s
== htab
->elf
.sgotplt
1274 || s
== htab
->elf
.sdynbss
1275 || s
== htab
->elf
.sdynrelro
1276 || s
== htab
->sdyntdata
)
1278 /* Strip this section if we don't need it; see the
1281 else if (strncmp (s
->name
, ".rela", 5) == 0)
1285 /* We use the reloc_count field as a counter if we need
1286 to copy relocs into the output file. */
1292 /* It's not one of our sections. */
1298 /* If we don't need this section, strip it from the
1299 output file. This is mostly to handle .rela.bss and
1300 .rela.plt. We must create both sections in
1301 create_dynamic_sections, because they must be created
1302 before the linker maps input sections to output
1303 sections. The linker does that before
1304 adjust_dynamic_symbol is called, and it is that
1305 function which decides whether anything needs to go
1306 into these sections. */
1307 s
->flags
|= SEC_EXCLUDE
;
1311 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1314 /* Allocate memory for the section contents. Zero the memory
1315 for the benefit of .rela.plt, which has 4 unused entries
1316 at the beginning, and we don't want garbage. */
1317 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1318 if (s
->contents
== NULL
)
1322 if (elf_hash_table (info
)->dynamic_sections_created
)
1324 /* Add some entries to the .dynamic section. We fill in the
1325 values later, in riscv_elf_finish_dynamic_sections, but we
1326 must add the entries now so that we get the correct size for
1327 the .dynamic section. The DT_DEBUG entry is filled in by the
1328 dynamic linker and used by the debugger. */
1329 #define add_dynamic_entry(TAG, VAL) \
1330 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1332 if (bfd_link_executable (info
))
1334 if (!add_dynamic_entry (DT_DEBUG
, 0))
1338 if (htab
->elf
.srelplt
->size
!= 0)
1340 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1341 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1342 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1343 || !add_dynamic_entry (DT_JMPREL
, 0))
1347 if (!add_dynamic_entry (DT_RELA
, 0)
1348 || !add_dynamic_entry (DT_RELASZ
, 0)
1349 || !add_dynamic_entry (DT_RELAENT
, sizeof (ElfNN_External_Rela
)))
1352 /* If any dynamic relocs apply to a read-only section,
1353 then we need a DT_TEXTREL entry. */
1354 if ((info
->flags
& DF_TEXTREL
) == 0)
1355 elf_link_hash_traverse (&htab
->elf
, maybe_set_textrel
, info
);
1357 if (info
->flags
& DF_TEXTREL
)
1359 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1363 #undef add_dynamic_entry
1369 #define DTP_OFFSET 0x800
1371 /* Return the relocation value for a TLS dtp-relative reloc. */
1374 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1376 /* If tls_sec is NULL, we should have signalled an error already. */
1377 if (elf_hash_table (info
)->tls_sec
== NULL
)
1379 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1382 /* Return the relocation value for a static TLS tp-relative relocation. */
1385 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1387 /* If tls_sec is NULL, we should have signalled an error already. */
1388 if (elf_hash_table (info
)->tls_sec
== NULL
)
1390 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1393 /* Return the global pointer's value, or 0 if it is not in use. */
1396 riscv_global_pointer_value (struct bfd_link_info
*info
)
1398 struct bfd_link_hash_entry
*h
;
1400 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1401 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1404 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1407 /* Emplace a static relocation. */
1409 static bfd_reloc_status_type
1410 perform_relocation (const reloc_howto_type
*howto
,
1411 const Elf_Internal_Rela
*rel
,
1413 asection
*input_section
,
1417 if (howto
->pc_relative
)
1418 value
-= sec_addr (input_section
) + rel
->r_offset
;
1419 value
+= rel
->r_addend
;
1421 switch (ELFNN_R_TYPE (rel
->r_info
))
1424 case R_RISCV_TPREL_HI20
:
1425 case R_RISCV_PCREL_HI20
:
1426 case R_RISCV_GOT_HI20
:
1427 case R_RISCV_TLS_GOT_HI20
:
1428 case R_RISCV_TLS_GD_HI20
:
1429 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1430 return bfd_reloc_overflow
;
1431 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1434 case R_RISCV_LO12_I
:
1435 case R_RISCV_GPREL_I
:
1436 case R_RISCV_TPREL_LO12_I
:
1437 case R_RISCV_TPREL_I
:
1438 case R_RISCV_PCREL_LO12_I
:
1439 value
= ENCODE_ITYPE_IMM (value
);
1442 case R_RISCV_LO12_S
:
1443 case R_RISCV_GPREL_S
:
1444 case R_RISCV_TPREL_LO12_S
:
1445 case R_RISCV_TPREL_S
:
1446 case R_RISCV_PCREL_LO12_S
:
1447 value
= ENCODE_STYPE_IMM (value
);
1451 case R_RISCV_CALL_PLT
:
1452 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1453 return bfd_reloc_overflow
;
1454 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1455 | (ENCODE_ITYPE_IMM (value
) << 32);
1459 if (!VALID_UJTYPE_IMM (value
))
1460 return bfd_reloc_overflow
;
1461 value
= ENCODE_UJTYPE_IMM (value
);
1464 case R_RISCV_BRANCH
:
1465 if (!VALID_SBTYPE_IMM (value
))
1466 return bfd_reloc_overflow
;
1467 value
= ENCODE_SBTYPE_IMM (value
);
1470 case R_RISCV_RVC_BRANCH
:
1471 if (!VALID_RVC_B_IMM (value
))
1472 return bfd_reloc_overflow
;
1473 value
= ENCODE_RVC_B_IMM (value
);
1476 case R_RISCV_RVC_JUMP
:
1477 if (!VALID_RVC_J_IMM (value
))
1478 return bfd_reloc_overflow
;
1479 value
= ENCODE_RVC_J_IMM (value
);
1482 case R_RISCV_RVC_LUI
:
1483 if (RISCV_CONST_HIGH_PART (value
) == 0)
1485 /* Linker relaxation can convert an address equal to or greater than
1486 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1487 valid immediate. We can fix this by converting it to a C.LI. */
1488 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1489 contents
+ rel
->r_offset
);
1490 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1491 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1492 value
= ENCODE_RVC_IMM (0);
1494 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1495 return bfd_reloc_overflow
;
1497 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1515 case R_RISCV_32_PCREL
:
1516 case R_RISCV_TLS_DTPREL32
:
1517 case R_RISCV_TLS_DTPREL64
:
1520 case R_RISCV_DELETE
:
1521 return bfd_reloc_ok
;
1524 return bfd_reloc_notsupported
;
1527 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1528 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1529 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1531 return bfd_reloc_ok
;
1534 /* Remember all PC-relative high-part relocs we've encountered to help us
1535 later resolve the corresponding low-part relocs. */
1541 } riscv_pcrel_hi_reloc
;
1543 typedef struct riscv_pcrel_lo_reloc
1545 asection
* input_section
;
1546 struct bfd_link_info
* info
;
1547 reloc_howto_type
* howto
;
1548 const Elf_Internal_Rela
* reloc
;
1551 bfd_byte
* contents
;
1552 struct riscv_pcrel_lo_reloc
* next
;
1553 } riscv_pcrel_lo_reloc
;
1558 riscv_pcrel_lo_reloc
*lo_relocs
;
1559 } riscv_pcrel_relocs
;
1562 riscv_pcrel_reloc_hash (const void *entry
)
1564 const riscv_pcrel_hi_reloc
*e
= entry
;
1565 return (hashval_t
)(e
->address
>> 2);
1569 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1571 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1572 return e1
->address
== e2
->address
;
1576 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1579 p
->lo_relocs
= NULL
;
1580 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1581 riscv_pcrel_reloc_eq
, free
);
1582 return p
->hi_relocs
!= NULL
;
1586 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1588 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1592 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1597 htab_delete (p
->hi_relocs
);
1601 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1602 struct bfd_link_info
*info
,
1606 const reloc_howto_type
*howto
,
1609 /* We may need to reference low addreses in PC-relative modes even when the
1610 * PC is far away from these addresses. For example, undefweak references
1611 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1612 * addresses that we can link PC-relative programs at, the linker can't
1613 * actually relocate references to those symbols. In order to allow these
1614 * programs to work we simply convert the PC-relative auipc sequences to
1615 * 0-relative lui sequences. */
1616 if (bfd_link_pic (info
))
1619 /* If it's possible to reference the symbol using auipc we do so, as that's
1620 * more in the spirit of the PC-relative relocations we're processing. */
1621 bfd_vma offset
= addr
- pc
;
1622 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1625 /* If it's impossible to reference this with a LUI-based offset then don't
1626 * bother to convert it at all so users still see the PC-relative relocation
1627 * in the truncation message. */
1628 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1631 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1633 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1634 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1635 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1640 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1641 bfd_vma value
, bfd_boolean absolute
)
1643 bfd_vma offset
= absolute
? value
: value
- addr
;
1644 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1645 riscv_pcrel_hi_reloc
**slot
=
1646 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1648 BFD_ASSERT (*slot
== NULL
);
1649 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1657 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1658 asection
*input_section
,
1659 struct bfd_link_info
*info
,
1660 reloc_howto_type
*howto
,
1661 const Elf_Internal_Rela
*reloc
,
1666 riscv_pcrel_lo_reloc
*entry
;
1667 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1670 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1671 name
, contents
, p
->lo_relocs
};
1672 p
->lo_relocs
= entry
;
1677 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1679 riscv_pcrel_lo_reloc
*r
;
1681 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1683 bfd
*input_bfd
= r
->input_section
->owner
;
1685 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1686 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1688 /* Check for overflow into bit 11 when adding reloc addend. */
1689 || (! (entry
->value
& 0x800)
1690 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1692 char *string
= (entry
== NULL
1693 ? "%pcrel_lo missing matching %pcrel_hi"
1694 : "%pcrel_lo overflow with an addend");
1695 (*r
->info
->callbacks
->reloc_dangerous
)
1696 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1700 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1701 input_bfd
, r
->contents
);
1707 /* Relocate a RISC-V ELF section.
1709 The RELOCATE_SECTION function is called by the new ELF backend linker
1710 to handle the relocations for a section.
1712 The relocs are always passed as Rela structures.
1714 This function is responsible for adjusting the section contents as
1715 necessary, and (if generating a relocatable output file) adjusting
1716 the reloc addend as necessary.
1718 This function does not have to worry about setting the reloc
1719 address or the reloc symbol index.
1721 LOCAL_SYMS is a pointer to the swapped in local symbols.
1723 LOCAL_SECTIONS is an array giving the section in the input file
1724 corresponding to the st_shndx field of each local symbol.
1726 The global hash table entry for the global symbols can be found
1727 via elf_sym_hashes (input_bfd).
1729 When generating relocatable output, this function must handle
1730 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1731 going to be the section symbol corresponding to the output
1732 section, which means that the addend must be adjusted
1736 riscv_elf_relocate_section (bfd
*output_bfd
,
1737 struct bfd_link_info
*info
,
1739 asection
*input_section
,
1741 Elf_Internal_Rela
*relocs
,
1742 Elf_Internal_Sym
*local_syms
,
1743 asection
**local_sections
)
1745 Elf_Internal_Rela
*rel
;
1746 Elf_Internal_Rela
*relend
;
1747 riscv_pcrel_relocs pcrel_relocs
;
1748 bfd_boolean ret
= FALSE
;
1749 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1750 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1751 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1752 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1753 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1754 bfd_boolean absolute
;
1756 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1759 relend
= relocs
+ input_section
->reloc_count
;
1760 for (rel
= relocs
; rel
< relend
; rel
++)
1762 unsigned long r_symndx
;
1763 struct elf_link_hash_entry
*h
;
1764 Elf_Internal_Sym
*sym
;
1767 bfd_reloc_status_type r
= bfd_reloc_ok
;
1769 bfd_vma off
, ie_off
;
1770 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1771 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1772 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1773 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1774 const char *msg
= NULL
;
1775 char *msg_buf
= NULL
;
1776 bfd_boolean resolved_to_zero
;
1779 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1782 /* This is a final link. */
1783 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1787 unresolved_reloc
= FALSE
;
1788 if (r_symndx
< symtab_hdr
->sh_info
)
1790 sym
= local_syms
+ r_symndx
;
1791 sec
= local_sections
[r_symndx
];
1792 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1796 bfd_boolean warned
, ignored
;
1798 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1799 r_symndx
, symtab_hdr
, sym_hashes
,
1801 unresolved_reloc
, warned
, ignored
);
1804 /* To avoid generating warning messages about truncated
1805 relocations, set the relocation's address to be the same as
1806 the start of this section. */
1807 if (input_section
->output_section
!= NULL
)
1808 relocation
= input_section
->output_section
->vma
;
1814 if (sec
!= NULL
&& discarded_section (sec
))
1815 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1816 rel
, 1, relend
, howto
, 0, contents
);
1818 if (bfd_link_relocatable (info
))
1822 name
= h
->root
.root
.string
;
1825 name
= (bfd_elf_string_from_elf_section
1826 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1827 if (name
== NULL
|| *name
== '\0')
1828 name
= bfd_section_name (sec
);
1831 resolved_to_zero
= (h
!= NULL
1832 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1838 case R_RISCV_TPREL_ADD
:
1840 case R_RISCV_JUMP_SLOT
:
1841 case R_RISCV_RELATIVE
:
1842 /* These require nothing of us at all. */
1846 case R_RISCV_BRANCH
:
1847 case R_RISCV_RVC_BRANCH
:
1848 case R_RISCV_RVC_LUI
:
1849 case R_RISCV_LO12_I
:
1850 case R_RISCV_LO12_S
:
1855 case R_RISCV_32_PCREL
:
1856 case R_RISCV_DELETE
:
1857 /* These require no special handling beyond perform_relocation. */
1860 case R_RISCV_GOT_HI20
:
1863 bfd_boolean dyn
, pic
;
1865 off
= h
->got
.offset
;
1866 BFD_ASSERT (off
!= (bfd_vma
) -1);
1867 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1868 pic
= bfd_link_pic (info
);
1870 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1871 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1873 /* This is actually a static link, or it is a
1874 -Bsymbolic link and the symbol is defined
1875 locally, or the symbol was forced to be local
1876 because of a version file. We must initialize
1877 this entry in the global offset table. Since the
1878 offset must always be a multiple of the word size,
1879 we use the least significant bit to record whether
1880 we have initialized it already.
1882 When doing a dynamic link, we create a .rela.got
1883 relocation entry to initialize the value. This
1884 is done in the finish_dynamic_symbol routine. */
1889 bfd_put_NN (output_bfd
, relocation
,
1890 htab
->elf
.sgot
->contents
+ off
);
1895 unresolved_reloc
= FALSE
;
1899 BFD_ASSERT (local_got_offsets
!= NULL
1900 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1902 off
= local_got_offsets
[r_symndx
];
1904 /* The offset must always be a multiple of the word size.
1905 So, we can use the least significant bit to record
1906 whether we have already processed this entry. */
1911 if (bfd_link_pic (info
))
1914 Elf_Internal_Rela outrel
;
1916 /* We need to generate a R_RISCV_RELATIVE reloc
1917 for the dynamic linker. */
1918 s
= htab
->elf
.srelgot
;
1919 BFD_ASSERT (s
!= NULL
);
1921 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1923 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1924 outrel
.r_addend
= relocation
;
1926 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1929 bfd_put_NN (output_bfd
, relocation
,
1930 htab
->elf
.sgot
->contents
+ off
);
1931 local_got_offsets
[r_symndx
] |= 1;
1934 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1935 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1942 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1943 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1945 r
= bfd_reloc_notsupported
;
1946 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1947 relocation
, absolute
))
1948 r
= bfd_reloc_overflow
;
1956 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1957 contents
+ rel
->r_offset
);
1958 relocation
= old_value
+ relocation
;
1968 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1969 contents
+ rel
->r_offset
);
1970 relocation
= old_value
- relocation
;
1975 case R_RISCV_CALL_PLT
:
1976 /* Handle a call to an undefined weak function. This won't be
1977 relaxed, so we have to handle it here. */
1978 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
1979 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
1981 /* We can use x0 as the base register. */
1982 bfd_vma insn
= bfd_get_32 (input_bfd
,
1983 contents
+ rel
->r_offset
+ 4);
1984 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1985 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
1986 /* Set the relocation value so that we get 0 after the pc
1987 relative adjustment. */
1988 relocation
= sec_addr (input_section
) + rel
->r_offset
;
1993 case R_RISCV_RVC_JUMP
:
1994 /* This line has to match the check in _bfd_riscv_relax_section. */
1995 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
1997 /* Refer to the PLT entry. */
1998 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
1999 unresolved_reloc
= FALSE
;
2003 case R_RISCV_TPREL_HI20
:
2004 relocation
= tpoff (info
, relocation
);
2007 case R_RISCV_TPREL_LO12_I
:
2008 case R_RISCV_TPREL_LO12_S
:
2009 relocation
= tpoff (info
, relocation
);
2012 case R_RISCV_TPREL_I
:
2013 case R_RISCV_TPREL_S
:
2014 relocation
= tpoff (info
, relocation
);
2015 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2017 /* We can use tp as the base register. */
2018 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2019 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2020 insn
|= X_TP
<< OP_SH_RS1
;
2021 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2024 r
= bfd_reloc_overflow
;
2027 case R_RISCV_GPREL_I
:
2028 case R_RISCV_GPREL_S
:
2030 bfd_vma gp
= riscv_global_pointer_value (info
);
2031 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2032 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2034 /* We can use x0 or gp 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
);
2039 rel
->r_addend
-= gp
;
2040 insn
|= X_GP
<< OP_SH_RS1
;
2042 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2045 r
= bfd_reloc_overflow
;
2049 case R_RISCV_PCREL_HI20
:
2050 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2057 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2058 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2060 r
= bfd_reloc_notsupported
;
2061 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2062 relocation
+ rel
->r_addend
,
2064 r
= bfd_reloc_overflow
;
2067 case R_RISCV_PCREL_LO12_I
:
2068 case R_RISCV_PCREL_LO12_S
:
2069 /* We don't allow section symbols plus addends as the auipc address,
2070 because then riscv_relax_delete_bytes would have to search through
2071 all relocs to update these addends. This is also ambiguous, as
2072 we do allow offsets to be added to the target address, which are
2073 not to be used to find the auipc address. */
2074 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2075 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2078 msg
= _("%pcrel_lo section symbol with an addend");
2079 r
= bfd_reloc_dangerous
;
2083 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2084 howto
, rel
, relocation
, name
,
2087 r
= bfd_reloc_overflow
;
2090 case R_RISCV_TLS_DTPREL32
:
2091 case R_RISCV_TLS_DTPREL64
:
2092 relocation
= dtpoff (info
, relocation
);
2097 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2100 if ((bfd_link_pic (info
)
2102 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2103 && !resolved_to_zero
)
2104 || h
->root
.type
!= bfd_link_hash_undefweak
)
2105 && (! howto
->pc_relative
2106 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2107 || (!bfd_link_pic (info
)
2113 || h
->root
.type
== bfd_link_hash_undefweak
2114 || h
->root
.type
== bfd_link_hash_undefined
)))
2116 Elf_Internal_Rela outrel
;
2117 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2119 /* When generating a shared object, these relocations
2120 are copied into the output file to be resolved at run
2124 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2126 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2127 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2128 outrel
.r_offset
+= sec_addr (input_section
);
2130 if (skip_dynamic_relocation
)
2131 memset (&outrel
, 0, sizeof outrel
);
2132 else if (h
!= NULL
&& h
->dynindx
!= -1
2133 && !(bfd_link_pic (info
)
2134 && SYMBOLIC_BIND (info
, h
)
2137 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2138 outrel
.r_addend
= rel
->r_addend
;
2142 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2143 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2146 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2147 if (skip_static_relocation
)
2152 case R_RISCV_TLS_GOT_HI20
:
2156 case R_RISCV_TLS_GD_HI20
:
2159 off
= h
->got
.offset
;
2164 off
= local_got_offsets
[r_symndx
];
2165 local_got_offsets
[r_symndx
] |= 1;
2168 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2169 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2170 /* If this symbol is referenced by both GD and IE TLS, the IE
2171 reference's GOT slot follows the GD reference's slots. */
2173 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2174 ie_off
= 2 * GOT_ENTRY_SIZE
;
2180 Elf_Internal_Rela outrel
;
2182 bfd_boolean need_relocs
= FALSE
;
2184 if (htab
->elf
.srelgot
== NULL
)
2189 bfd_boolean dyn
, pic
;
2190 dyn
= htab
->elf
.dynamic_sections_created
;
2191 pic
= bfd_link_pic (info
);
2193 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2194 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2198 /* The GOT entries have not been initialized yet. Do it
2199 now, and emit any relocations. */
2200 if ((bfd_link_pic (info
) || indx
!= 0)
2202 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2203 || h
->root
.type
!= bfd_link_hash_undefweak
))
2206 if (tls_type
& GOT_TLS_GD
)
2210 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2211 outrel
.r_addend
= 0;
2212 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2213 bfd_put_NN (output_bfd
, 0,
2214 htab
->elf
.sgot
->contents
+ off
);
2215 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2218 BFD_ASSERT (! unresolved_reloc
);
2219 bfd_put_NN (output_bfd
,
2220 dtpoff (info
, relocation
),
2221 (htab
->elf
.sgot
->contents
+ off
+
2222 RISCV_ELF_WORD_BYTES
));
2226 bfd_put_NN (output_bfd
, 0,
2227 (htab
->elf
.sgot
->contents
+ off
+
2228 RISCV_ELF_WORD_BYTES
));
2229 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2230 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2231 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2236 /* If we are not emitting relocations for a
2237 general dynamic reference, then we must be in a
2238 static link or an executable link with the
2239 symbol binding locally. Mark it as belonging
2240 to module 1, the executable. */
2241 bfd_put_NN (output_bfd
, 1,
2242 htab
->elf
.sgot
->contents
+ off
);
2243 bfd_put_NN (output_bfd
,
2244 dtpoff (info
, relocation
),
2245 (htab
->elf
.sgot
->contents
+ off
+
2246 RISCV_ELF_WORD_BYTES
));
2250 if (tls_type
& GOT_TLS_IE
)
2254 bfd_put_NN (output_bfd
, 0,
2255 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2256 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2258 outrel
.r_addend
= 0;
2260 outrel
.r_addend
= tpoff (info
, relocation
);
2261 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2262 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2266 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2267 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2272 BFD_ASSERT (off
< (bfd_vma
) -2);
2273 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2274 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2276 r
= bfd_reloc_overflow
;
2277 unresolved_reloc
= FALSE
;
2281 r
= bfd_reloc_notsupported
;
2284 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2285 because such sections are not SEC_ALLOC and thus ld.so will
2286 not process them. */
2287 if (unresolved_reloc
2288 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2290 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2291 rel
->r_offset
) != (bfd_vma
) -1)
2297 case R_RISCV_RVC_JUMP
:
2298 if (asprintf (&msg_buf
,
2299 _("%%X%%P: relocation %s against `%s' can "
2300 "not be used when making a shared object; "
2301 "recompile with -fPIC\n"),
2303 h
->root
.root
.string
) == -1)
2308 if (asprintf (&msg_buf
,
2309 _("%%X%%P: unresolvable %s relocation against "
2312 h
->root
.root
.string
) == -1)
2318 r
= bfd_reloc_notsupported
;
2321 if (r
== bfd_reloc_ok
)
2322 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2323 input_bfd
, contents
);
2325 /* We should have already detected the error and set message before.
2326 If the error message isn't set since the linker runs out of memory
2327 or we don't set it before, then we should set the default message
2328 with the "internal error" string here. */
2334 case bfd_reloc_overflow
:
2335 info
->callbacks
->reloc_overflow
2336 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2337 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2340 case bfd_reloc_undefined
:
2341 info
->callbacks
->undefined_symbol
2342 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2346 case bfd_reloc_outofrange
:
2348 msg
= _("%X%P: internal error: out of range error\n");
2351 case bfd_reloc_notsupported
:
2353 msg
= _("%X%P: internal error: unsupported relocation error\n");
2356 case bfd_reloc_dangerous
:
2357 /* The error message should already be set. */
2359 msg
= _("dangerous relocation error");
2360 info
->callbacks
->reloc_dangerous
2361 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2365 msg
= _("%X%P: internal error: unknown error\n");
2369 /* Do not report error message for the dangerous relocation again. */
2370 if (msg
&& r
!= bfd_reloc_dangerous
)
2371 info
->callbacks
->einfo (msg
);
2373 /* Free the unused `msg_buf`. */
2376 /* We already reported the error via a callback, so don't try to report
2377 it again by returning false. That leads to spurious errors. */
2382 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2384 riscv_free_pcrel_relocs (&pcrel_relocs
);
2388 /* Finish up dynamic symbol handling. We set the contents of various
2389 dynamic sections here. */
2392 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2393 struct bfd_link_info
*info
,
2394 struct elf_link_hash_entry
*h
,
2395 Elf_Internal_Sym
*sym
)
2397 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2398 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2400 if (h
->plt
.offset
!= (bfd_vma
) -1)
2402 /* We've decided to create a PLT entry for this symbol. */
2404 bfd_vma i
, header_address
, plt_idx
, got_address
;
2405 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2406 Elf_Internal_Rela rela
;
2408 BFD_ASSERT (h
->dynindx
!= -1);
2410 /* Calculate the address of the PLT header. */
2411 header_address
= sec_addr (htab
->elf
.splt
);
2413 /* Calculate the index of the entry. */
2414 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2416 /* Calculate the address of the .got.plt entry. */
2417 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2419 /* Find out where the .plt entry should go. */
2420 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2422 /* Fill in the PLT entry itself. */
2423 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2424 header_address
+ h
->plt
.offset
,
2428 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2429 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2431 /* Fill in the initial value of the .got.plt entry. */
2432 loc
= htab
->elf
.sgotplt
->contents
2433 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2434 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2436 /* Fill in the entry in the .rela.plt section. */
2437 rela
.r_offset
= got_address
;
2439 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2441 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2442 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2444 if (!h
->def_regular
)
2446 /* Mark the symbol as undefined, rather than as defined in
2447 the .plt section. Leave the value alone. */
2448 sym
->st_shndx
= SHN_UNDEF
;
2449 /* If the symbol is weak, we do need to clear the value.
2450 Otherwise, the PLT entry would provide a definition for
2451 the symbol even if the symbol wasn't defined anywhere,
2452 and so the symbol would never be NULL. */
2453 if (!h
->ref_regular_nonweak
)
2458 if (h
->got
.offset
!= (bfd_vma
) -1
2459 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2460 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2464 Elf_Internal_Rela rela
;
2466 /* This symbol has an entry in the GOT. Set it up. */
2468 sgot
= htab
->elf
.sgot
;
2469 srela
= htab
->elf
.srelgot
;
2470 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2472 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2474 /* If this is a local symbol reference, we just want to emit a RELATIVE
2475 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2476 the symbol was forced to be local because of a version file.
2477 The entry in the global offset table will already have been
2478 initialized in the relocate_section function. */
2479 if (bfd_link_pic (info
)
2480 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2482 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2483 asection
*sec
= h
->root
.u
.def
.section
;
2484 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2485 rela
.r_addend
= (h
->root
.u
.def
.value
2486 + sec
->output_section
->vma
2487 + sec
->output_offset
);
2491 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2492 BFD_ASSERT (h
->dynindx
!= -1);
2493 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2497 bfd_put_NN (output_bfd
, 0,
2498 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2499 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2504 Elf_Internal_Rela rela
;
2507 /* This symbols needs a copy reloc. Set it up. */
2508 BFD_ASSERT (h
->dynindx
!= -1);
2510 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2511 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2513 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2514 s
= htab
->elf
.sreldynrelro
;
2516 s
= htab
->elf
.srelbss
;
2517 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2520 /* Mark some specially defined symbols as absolute. */
2521 if (h
== htab
->elf
.hdynamic
2522 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2523 sym
->st_shndx
= SHN_ABS
;
2528 /* Finish up the dynamic sections. */
2531 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2532 bfd
*dynobj
, asection
*sdyn
)
2534 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2535 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2536 size_t dynsize
= bed
->s
->sizeof_dyn
;
2537 bfd_byte
*dyncon
, *dynconend
;
2539 dynconend
= sdyn
->contents
+ sdyn
->size
;
2540 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2542 Elf_Internal_Dyn dyn
;
2545 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2550 s
= htab
->elf
.sgotplt
;
2551 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2554 s
= htab
->elf
.srelplt
;
2555 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2558 s
= htab
->elf
.srelplt
;
2559 dyn
.d_un
.d_val
= s
->size
;
2565 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2571 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2572 struct bfd_link_info
*info
)
2576 struct riscv_elf_link_hash_table
*htab
;
2578 htab
= riscv_elf_hash_table (info
);
2579 BFD_ASSERT (htab
!= NULL
);
2580 dynobj
= htab
->elf
.dynobj
;
2582 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2584 if (elf_hash_table (info
)->dynamic_sections_created
)
2589 splt
= htab
->elf
.splt
;
2590 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2592 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2597 /* Fill in the head and tail entries in the procedure linkage table. */
2601 uint32_t plt_header
[PLT_HEADER_INSNS
];
2602 ret
= riscv_make_plt_header (output_bfd
,
2603 sec_addr (htab
->elf
.sgotplt
),
2604 sec_addr (splt
), plt_header
);
2608 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2609 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2611 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2616 if (htab
->elf
.sgotplt
)
2618 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2620 if (bfd_is_abs_section (output_section
))
2622 (*_bfd_error_handler
)
2623 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2627 if (htab
->elf
.sgotplt
->size
> 0)
2629 /* Write the first two entries in .got.plt, needed for the dynamic
2631 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2632 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2633 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2636 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2641 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2643 if (htab
->elf
.sgot
->size
> 0)
2645 /* Set the first entry in the global offset table to the address of
2646 the dynamic section. */
2647 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2648 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2651 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2657 /* Return address for Ith PLT stub in section PLT, for relocation REL
2658 or (bfd_vma) -1 if it should not be included. */
2661 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2662 const arelent
*rel ATTRIBUTE_UNUSED
)
2664 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2667 static enum elf_reloc_type_class
2668 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2669 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2670 const Elf_Internal_Rela
*rela
)
2672 switch (ELFNN_R_TYPE (rela
->r_info
))
2674 case R_RISCV_RELATIVE
:
2675 return reloc_class_relative
;
2676 case R_RISCV_JUMP_SLOT
:
2677 return reloc_class_plt
;
2679 return reloc_class_copy
;
2681 return reloc_class_normal
;
2685 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2689 riscv_float_abi_string (flagword flags
)
2691 switch (flags
& EF_RISCV_FLOAT_ABI
)
2693 case EF_RISCV_FLOAT_ABI_SOFT
:
2694 return "soft-float";
2696 case EF_RISCV_FLOAT_ABI_SINGLE
:
2697 return "single-float";
2699 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2700 return "double-float";
2702 case EF_RISCV_FLOAT_ABI_QUAD
:
2703 return "quad-float";
2710 /* The information of architecture attribute. */
2711 static riscv_subset_list_t in_subsets
;
2712 static riscv_subset_list_t out_subsets
;
2713 static riscv_subset_list_t merged_subsets
;
2715 /* Predicator for standard extension. */
2718 riscv_std_ext_p (const char *name
)
2720 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2723 /* Error handler when version mis-match. */
2726 riscv_version_mismatch (bfd
*ibfd
,
2727 struct riscv_subset_t
*in
,
2728 struct riscv_subset_t
*out
)
2731 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2734 in
->major_version
, in
->minor_version
,
2735 out
->major_version
, out
->minor_version
);
2738 /* Return true if subset is 'i' or 'e'. */
2741 riscv_i_or_e_p (bfd
*ibfd
,
2743 struct riscv_subset_t
*subset
)
2745 if ((strcasecmp (subset
->name
, "e") != 0)
2746 && (strcasecmp (subset
->name
, "i") != 0))
2749 (_("error: %pB: corrupted ISA string '%s'. "
2750 "First letter should be 'i' or 'e' but got '%s'."),
2751 ibfd
, arch
, subset
->name
);
2757 /* Merge standard extensions.
2760 Return FALSE if failed to merge.
2764 `in_arch`: Raw arch string for input object.
2765 `out_arch`: Raw arch string for output object.
2766 `pin`: subset list for input object, and it'll skip all merged subset after
2768 `pout`: Like `pin`, but for output object. */
2771 riscv_merge_std_ext (bfd
*ibfd
,
2772 const char *in_arch
,
2773 const char *out_arch
,
2774 struct riscv_subset_t
**pin
,
2775 struct riscv_subset_t
**pout
)
2777 const char *standard_exts
= riscv_supported_std_ext ();
2779 struct riscv_subset_t
*in
= *pin
;
2780 struct riscv_subset_t
*out
= *pout
;
2782 /* First letter should be 'i' or 'e'. */
2783 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2786 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2789 if (strcasecmp (in
->name
, out
->name
) != 0)
2791 /* TODO: We might allow merge 'i' with 'e'. */
2793 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2794 ibfd
, in
->name
, out
->name
);
2797 else if ((in
->major_version
!= out
->major_version
) ||
2798 (in
->minor_version
!= out
->minor_version
))
2800 /* TODO: Allow different merge policy. */
2801 riscv_version_mismatch (ibfd
, in
, out
);
2805 riscv_add_subset (&merged_subsets
,
2806 in
->name
, in
->major_version
, in
->minor_version
);
2811 /* Handle standard extension first. */
2812 for (p
= standard_exts
; *p
; ++p
)
2814 char find_ext
[2] = {*p
, '\0'};
2815 struct riscv_subset_t
*find_in
=
2816 riscv_lookup_subset (&in_subsets
, find_ext
);
2817 struct riscv_subset_t
*find_out
=
2818 riscv_lookup_subset (&out_subsets
, find_ext
);
2820 if (find_in
== NULL
&& find_out
== NULL
)
2823 /* Check version is same or not. */
2824 /* TODO: Allow different merge policy. */
2825 if ((find_in
!= NULL
&& find_out
!= NULL
)
2826 && ((find_in
->major_version
!= find_out
->major_version
)
2827 || (find_in
->minor_version
!= find_out
->minor_version
)))
2829 riscv_version_mismatch (ibfd
, in
, out
);
2833 struct riscv_subset_t
*merged
= find_in
? find_in
: find_out
;
2834 riscv_add_subset (&merged_subsets
, merged
->name
,
2835 merged
->major_version
, merged
->minor_version
);
2838 /* Skip all standard extensions. */
2839 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2840 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2848 /* If C is a prefix class, then return the EXT string without the prefix.
2849 Otherwise return the entire EXT string. */
2852 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
2856 case RV_ISA_CLASS_X
: return &ext
[1];
2857 case RV_ISA_CLASS_S
: return &ext
[1];
2858 case RV_ISA_CLASS_Z
: return &ext
[1];
2859 default: return ext
;
2863 /* Compare prefixed extension names canonically. */
2866 riscv_prefix_cmp (const char *a
, const char *b
)
2868 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
2869 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
2871 /* Extension name without prefix */
2872 const char *anp
= riscv_skip_prefix (a
, ca
);
2873 const char *bnp
= riscv_skip_prefix (b
, cb
);
2876 return strcasecmp (anp
, bnp
);
2878 return (int)ca
- (int)cb
;
2881 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2882 object subset list. Likewise for POUT and the output object. Return TRUE
2883 on success and FALSE when a conflict is found. */
2886 riscv_merge_multi_letter_ext (bfd
*ibfd
,
2887 riscv_subset_t
**pin
,
2888 riscv_subset_t
**pout
)
2890 riscv_subset_t
*in
= *pin
;
2891 riscv_subset_t
*out
= *pout
;
2892 riscv_subset_t
*tail
;
2898 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
2902 /* `in' comes before `out', append `in' and increment. */
2903 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2909 /* `out' comes before `in', append `out' and increment. */
2910 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2911 out
->minor_version
);
2916 /* Both present, check version and increment both. */
2917 if ((in
->major_version
!= out
->major_version
)
2918 || (in
->minor_version
!= out
->minor_version
))
2920 riscv_version_mismatch (ibfd
, in
, out
);
2924 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2925 out
->minor_version
);
2932 /* If we're here, either `in' or `out' is running longer than
2933 the other. So, we need to append the corresponding tail. */
2934 tail
= in
? in
: out
;
2938 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
2939 tail
->minor_version
);
2947 /* Merge Tag_RISCV_arch attribute. */
2950 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2952 riscv_subset_t
*in
, *out
;
2953 char *merged_arch_str
;
2955 unsigned xlen_in
, xlen_out
;
2956 merged_subsets
.head
= NULL
;
2957 merged_subsets
.tail
= NULL
;
2959 riscv_parse_subset_t rpe_in
;
2960 riscv_parse_subset_t rpe_out
;
2962 /* Only assembler needs to check the default version of ISA, so just set
2963 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
2964 rpe_in
.subset_list
= &in_subsets
;
2965 rpe_in
.error_handler
= _bfd_error_handler
;
2966 rpe_in
.xlen
= &xlen_in
;
2967 rpe_in
.get_default_version
= NULL
;
2969 rpe_out
.subset_list
= &out_subsets
;
2970 rpe_out
.error_handler
= _bfd_error_handler
;
2971 rpe_out
.xlen
= &xlen_out
;
2972 rpe_out
.get_default_version
= NULL
;
2974 if (in_arch
== NULL
&& out_arch
== NULL
)
2977 if (in_arch
== NULL
&& out_arch
!= NULL
)
2980 if (in_arch
!= NULL
&& out_arch
== NULL
)
2983 /* Parse subset from arch string. */
2984 if (!riscv_parse_subset (&rpe_in
, in_arch
))
2987 if (!riscv_parse_subset (&rpe_out
, out_arch
))
2990 /* Checking XLEN. */
2991 if (xlen_out
!= xlen_in
)
2994 (_("error: %pB: ISA string of input (%s) doesn't match "
2995 "output (%s)."), ibfd
, in_arch
, out_arch
);
2999 /* Merge subset list. */
3000 in
= in_subsets
.head
;
3001 out
= out_subsets
.head
;
3003 /* Merge standard extension. */
3004 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3007 /* Merge all non-single letter extensions with single call. */
3008 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3011 if (xlen_in
!= xlen_out
)
3014 (_("error: %pB: XLEN of input (%u) doesn't match "
3015 "output (%u)."), ibfd
, xlen_in
, xlen_out
);
3019 if (xlen_in
!= ARCH_SIZE
)
3022 (_("error: %pB: Unsupported XLEN (%u), you might be "
3023 "using wrong emulation."), ibfd
, xlen_in
);
3027 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3029 /* Release the subset lists. */
3030 riscv_release_subset_list (&in_subsets
);
3031 riscv_release_subset_list (&out_subsets
);
3032 riscv_release_subset_list (&merged_subsets
);
3034 return merged_arch_str
;
3037 /* Merge object attributes from IBFD into output_bfd of INFO.
3038 Raise an error if there are conflicting attributes. */
3041 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3043 bfd
*obfd
= info
->output_bfd
;
3044 obj_attribute
*in_attr
;
3045 obj_attribute
*out_attr
;
3046 bfd_boolean result
= TRUE
;
3047 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3050 /* Skip linker created files. */
3051 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3054 /* Skip any input that doesn't have an attribute section.
3055 This enables to link object files without attribute section with
3057 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3060 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3062 /* This is the first object. Copy the attributes. */
3063 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3065 out_attr
= elf_known_obj_attributes_proc (obfd
);
3067 /* Use the Tag_null value to indicate the attributes have been
3074 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3075 out_attr
= elf_known_obj_attributes_proc (obfd
);
3077 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3081 case Tag_RISCV_arch
:
3082 if (!out_attr
[Tag_RISCV_arch
].s
)
3083 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3084 else if (in_attr
[Tag_RISCV_arch
].s
3085 && out_attr
[Tag_RISCV_arch
].s
)
3087 /* Check arch compatible. */
3089 riscv_merge_arch_attr_info (ibfd
,
3090 in_attr
[Tag_RISCV_arch
].s
,
3091 out_attr
[Tag_RISCV_arch
].s
);
3092 if (merged_arch
== NULL
)
3095 out_attr
[Tag_RISCV_arch
].s
= "";
3098 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3101 case Tag_RISCV_priv_spec
:
3102 case Tag_RISCV_priv_spec_minor
:
3103 case Tag_RISCV_priv_spec_revision
:
3104 if (out_attr
[i
].i
!= in_attr
[i
].i
)
3107 (_("error: %pB: conflicting priv spec version "
3108 "(major/minor/revision)."), ibfd
);
3112 case Tag_RISCV_unaligned_access
:
3113 out_attr
[i
].i
|= in_attr
[i
].i
;
3115 case Tag_RISCV_stack_align
:
3116 if (out_attr
[i
].i
== 0)
3117 out_attr
[i
].i
= in_attr
[i
].i
;
3118 else if (in_attr
[i
].i
!= 0
3119 && out_attr
[i
].i
!= 0
3120 && out_attr
[i
].i
!= in_attr
[i
].i
)
3123 (_("error: %pB use %u-byte stack aligned but the output "
3124 "use %u-byte stack aligned."),
3125 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3130 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3133 /* If out_attr was copied from in_attr then it won't have a type yet. */
3134 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3135 out_attr
[i
].type
= in_attr
[i
].type
;
3138 /* Merge Tag_compatibility attributes and any common GNU ones. */
3139 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3142 /* Check for any attributes not known on RISC-V. */
3143 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3148 /* Merge backend specific data from an object file to the output
3149 object file when linking. */
3152 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3154 bfd
*obfd
= info
->output_bfd
;
3155 flagword new_flags
, old_flags
;
3157 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3160 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3162 (*_bfd_error_handler
)
3163 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3164 " target emulation `%s' does not match `%s'"),
3165 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3169 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3172 if (!riscv_merge_attributes (ibfd
, info
))
3175 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3176 old_flags
= elf_elfheader (obfd
)->e_flags
;
3178 if (! elf_flags_init (obfd
))
3180 elf_flags_init (obfd
) = TRUE
;
3181 elf_elfheader (obfd
)->e_flags
= new_flags
;
3185 /* Check to see if the input BFD actually contains any sections. If not,
3186 its flags may not have been initialized either, but it cannot actually
3187 cause any incompatibility. Do not short-circuit dynamic objects; their
3188 section list may be emptied by elf_link_add_object_symbols.
3190 Also check to see if there are no code sections in the input. In this
3191 case, there is no need to check for code specific flags. */
3192 if (!(ibfd
->flags
& DYNAMIC
))
3194 bfd_boolean null_input_bfd
= TRUE
;
3195 bfd_boolean only_data_sections
= TRUE
;
3198 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3200 if ((bfd_section_flags (sec
)
3201 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3202 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3203 only_data_sections
= FALSE
;
3205 null_input_bfd
= FALSE
;
3209 if (null_input_bfd
|| only_data_sections
)
3213 /* Disallow linking different float ABIs. */
3214 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3216 (*_bfd_error_handler
)
3217 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3218 riscv_float_abi_string (new_flags
),
3219 riscv_float_abi_string (old_flags
));
3223 /* Disallow linking RVE and non-RVE. */
3224 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3226 (*_bfd_error_handler
)
3227 (_("%pB: can't link RVE with other target"), ibfd
);
3231 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3232 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3237 bfd_set_error (bfd_error_bad_value
);
3241 /* Delete some bytes from a section while relaxing. */
3244 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3245 struct bfd_link_info
*link_info
)
3247 unsigned int i
, symcount
;
3248 bfd_vma toaddr
= sec
->size
;
3249 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3250 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3251 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3252 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3253 bfd_byte
*contents
= data
->this_hdr
.contents
;
3255 /* Actually delete the bytes. */
3257 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3259 /* Adjust the location of all of the relocs. Note that we need not
3260 adjust the addends, since all PC-relative references must be against
3261 symbols, which we will adjust below. */
3262 for (i
= 0; i
< sec
->reloc_count
; i
++)
3263 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3264 data
->relocs
[i
].r_offset
-= count
;
3266 /* Adjust the local symbols defined in this section. */
3267 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3269 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3270 if (sym
->st_shndx
== sec_shndx
)
3272 /* If the symbol is in the range of memory we just moved, we
3273 have to adjust its value. */
3274 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3275 sym
->st_value
-= count
;
3277 /* If the symbol *spans* the bytes we just deleted (i.e. its
3278 *end* is in the moved bytes but its *start* isn't), then we
3279 must adjust its size.
3281 This test needs to use the original value of st_value, otherwise
3282 we might accidentally decrease size when deleting bytes right
3283 before the symbol. But since deleted relocs can't span across
3284 symbols, we can't have both a st_value and a st_size decrease,
3285 so it is simpler to just use an else. */
3286 else if (sym
->st_value
<= addr
3287 && sym
->st_value
+ sym
->st_size
> addr
3288 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3289 sym
->st_size
-= count
;
3293 /* Now adjust the global symbols defined in this section. */
3294 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3295 - symtab_hdr
->sh_info
);
3297 for (i
= 0; i
< symcount
; i
++)
3299 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3301 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3302 containing the definition of __wrap_SYMBOL, includes a direct
3303 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3304 the same symbol (which is __wrap_SYMBOL), but still exist as two
3305 different symbols in 'sym_hashes', we don't want to adjust
3306 the global symbol __wrap_SYMBOL twice. */
3307 /* The same problem occurs with symbols that are versioned_hidden, as
3308 foo becomes an alias for foo@BAR, and hence they need the same
3310 if (link_info
->wrap_hash
!= NULL
3311 || sym_hash
->versioned
== versioned_hidden
)
3313 struct elf_link_hash_entry
**cur_sym_hashes
;
3315 /* Loop only over the symbols which have already been checked. */
3316 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3319 /* If the current symbol is identical to 'sym_hash', that means
3320 the symbol was already adjusted (or at least checked). */
3321 if (*cur_sym_hashes
== sym_hash
)
3324 /* Don't adjust the symbol again. */
3325 if (cur_sym_hashes
< &sym_hashes
[i
])
3329 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3330 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3331 && sym_hash
->root
.u
.def
.section
== sec
)
3333 /* As above, adjust the value if needed. */
3334 if (sym_hash
->root
.u
.def
.value
> addr
3335 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3336 sym_hash
->root
.u
.def
.value
-= count
;
3338 /* As above, adjust the size if needed. */
3339 else if (sym_hash
->root
.u
.def
.value
<= addr
3340 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3341 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3342 sym_hash
->size
-= count
;
3349 /* A second format for recording PC-relative hi relocations. This stores the
3350 information required to relax them to GP-relative addresses. */
3352 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3353 struct riscv_pcgp_hi_reloc
3360 bfd_boolean undefined_weak
;
3361 riscv_pcgp_hi_reloc
*next
;
3364 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3365 struct riscv_pcgp_lo_reloc
3368 riscv_pcgp_lo_reloc
*next
;
3373 riscv_pcgp_hi_reloc
*hi
;
3374 riscv_pcgp_lo_reloc
*lo
;
3375 } riscv_pcgp_relocs
;
3377 /* Initialize the pcgp reloc info in P. */
3380 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3387 /* Free the pcgp reloc info in P. */
3390 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3391 bfd
*abfd ATTRIBUTE_UNUSED
,
3392 asection
*sec ATTRIBUTE_UNUSED
)
3394 riscv_pcgp_hi_reloc
*c
;
3395 riscv_pcgp_lo_reloc
*l
;
3397 for (c
= p
->hi
; c
!= NULL
;)
3399 riscv_pcgp_hi_reloc
*next
= c
->next
;
3404 for (l
= p
->lo
; l
!= NULL
;)
3406 riscv_pcgp_lo_reloc
*next
= l
->next
;
3412 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3413 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3414 relax the corresponding lo part reloc. */
3417 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3418 bfd_vma hi_addend
, bfd_vma hi_addr
,
3419 unsigned hi_sym
, asection
*sym_sec
,
3420 bfd_boolean undefined_weak
)
3422 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3425 new->hi_sec_off
= hi_sec_off
;
3426 new->hi_addend
= hi_addend
;
3427 new->hi_addr
= hi_addr
;
3428 new->hi_sym
= hi_sym
;
3429 new->sym_sec
= sym_sec
;
3430 new->undefined_weak
= undefined_weak
;
3436 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3437 This is used by a lo part reloc to find the corresponding hi part reloc. */
3439 static riscv_pcgp_hi_reloc
*
3440 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3442 riscv_pcgp_hi_reloc
*c
;
3444 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3445 if (c
->hi_sec_off
== hi_sec_off
)
3450 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3451 This is used to record relocs that can't be relaxed. */
3454 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3456 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3459 new->hi_sec_off
= hi_sec_off
;
3465 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3466 This is used by a hi part reloc to find the corresponding lo part reloc. */
3469 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3471 riscv_pcgp_lo_reloc
*c
;
3473 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3474 if (c
->hi_sec_off
== hi_sec_off
)
3479 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3480 struct bfd_link_info
*,
3481 Elf_Internal_Rela
*,
3482 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3483 riscv_pcgp_relocs
*,
3484 bfd_boolean undefined_weak
);
3486 /* Relax AUIPC + JALR into JAL. */
3489 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3490 struct bfd_link_info
*link_info
,
3491 Elf_Internal_Rela
*rel
,
3493 bfd_vma max_alignment
,
3494 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3496 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3497 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3499 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3500 bfd_signed_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3501 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3502 bfd_vma auipc
, jalr
;
3503 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3505 /* If the call crosses section boundaries, an alignment directive could
3506 cause the PC-relative offset to later increase, so we need to add in the
3507 max alignment of any section inclusive from the call to the target.
3508 Otherwise, we only need to use the alignment of the current section. */
3509 if (VALID_UJTYPE_IMM (foff
))
3511 if (sym_sec
->output_section
== sec
->output_section
3512 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3513 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3514 foff
+= (foff
< 0 ? -max_alignment
: max_alignment
);
3517 /* See if this function call can be shortened. */
3518 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3521 /* Shorten the function call. */
3522 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3524 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3525 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3526 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3527 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3529 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3530 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3534 /* Relax to C.J[AL] rd, addr. */
3535 r_type
= R_RISCV_RVC_JUMP
;
3536 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3539 else if (VALID_UJTYPE_IMM (foff
))
3541 /* Relax to JAL rd, addr. */
3542 r_type
= R_RISCV_JAL
;
3543 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3545 else /* near_zero */
3547 /* Relax to JALR rd, x0, addr. */
3548 r_type
= R_RISCV_LO12_I
;
3549 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3552 /* Replace the R_RISCV_CALL reloc. */
3553 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3554 /* Replace the AUIPC. */
3555 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3557 /* Delete unnecessary JALR. */
3559 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3563 /* Traverse all output sections and return the max alignment. */
3566 _bfd_riscv_get_max_alignment (asection
*sec
)
3568 unsigned int max_alignment_power
= 0;
3571 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3573 if (o
->alignment_power
> max_alignment_power
)
3574 max_alignment_power
= o
->alignment_power
;
3577 return (bfd_vma
) 1 << max_alignment_power
;
3580 /* Relax non-PIC global variable references. */
3583 _bfd_riscv_relax_lui (bfd
*abfd
,
3586 struct bfd_link_info
*link_info
,
3587 Elf_Internal_Rela
*rel
,
3589 bfd_vma max_alignment
,
3590 bfd_vma reserve_size
,
3592 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3593 bfd_boolean undefined_weak
)
3595 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3596 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3597 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3599 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3603 /* If gp and the symbol are in the same output section, which is not the
3604 abs section, then consider only that output section's alignment. */
3605 struct bfd_link_hash_entry
*h
=
3606 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3608 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3609 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3610 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3613 /* Is the reference in range of x0 or gp?
3614 Valid gp range conservatively because of alignment issue. */
3616 || (VALID_ITYPE_IMM (symval
)
3618 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3620 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3622 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3623 switch (ELFNN_R_TYPE (rel
->r_info
))
3625 case R_RISCV_LO12_I
:
3628 /* Change the RS1 to zero. */
3629 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3630 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3631 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3634 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3637 case R_RISCV_LO12_S
:
3640 /* Change the RS1 to zero. */
3641 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3642 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3643 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3646 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3650 /* We can delete the unnecessary LUI and reloc. */
3651 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3653 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3661 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3662 account for this assuming page alignment at worst. In the presence of
3663 RELRO segment the linker aligns it by one page size, therefore sections
3664 after the segment can be moved more than one page. */
3667 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3668 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3669 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3670 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3671 : ELF_MAXPAGESIZE
)))
3673 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3674 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3675 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3676 if (rd
== 0 || rd
== X_SP
)
3679 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3680 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3682 /* Replace the R_RISCV_HI20 reloc. */
3683 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3686 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3693 /* Relax non-PIC TLS references. */
3696 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3698 asection
*sym_sec ATTRIBUTE_UNUSED
,
3699 struct bfd_link_info
*link_info
,
3700 Elf_Internal_Rela
*rel
,
3702 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3703 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3705 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3706 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3708 /* See if this symbol is in range of tp. */
3709 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3712 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3713 switch (ELFNN_R_TYPE (rel
->r_info
))
3715 case R_RISCV_TPREL_LO12_I
:
3716 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3719 case R_RISCV_TPREL_LO12_S
:
3720 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3723 case R_RISCV_TPREL_HI20
:
3724 case R_RISCV_TPREL_ADD
:
3725 /* We can delete the unnecessary instruction and reloc. */
3726 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3728 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3735 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3738 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3740 struct bfd_link_info
*link_info
,
3741 Elf_Internal_Rela
*rel
,
3743 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3744 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3745 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3746 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3747 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3749 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3750 bfd_vma alignment
= 1, pos
;
3751 while (alignment
<= rel
->r_addend
)
3754 symval
-= rel
->r_addend
;
3755 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3756 bfd_vma nop_bytes
= aligned_addr
- symval
;
3758 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3759 sec
->sec_flg0
= TRUE
;
3761 /* Make sure there are enough NOPs to actually achieve the alignment. */
3762 if (rel
->r_addend
< nop_bytes
)
3765 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3766 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3767 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3768 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3769 bfd_set_error (bfd_error_bad_value
);
3773 /* Delete the reloc. */
3774 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3776 /* If the number of NOPs is already correct, there's nothing to do. */
3777 if (nop_bytes
== rel
->r_addend
)
3780 /* Write as many RISC-V NOPs as we need. */
3781 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3782 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3784 /* Write a final RVC NOP if need be. */
3785 if (nop_bytes
% 4 != 0)
3786 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3788 /* Delete the excess bytes. */
3789 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3790 rel
->r_addend
- nop_bytes
, link_info
);
3793 /* Relax PC-relative references to GP-relative references. */
3796 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3799 struct bfd_link_info
*link_info
,
3800 Elf_Internal_Rela
*rel
,
3802 bfd_vma max_alignment
,
3803 bfd_vma reserve_size
,
3804 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3805 riscv_pcgp_relocs
*pcgp_relocs
,
3806 bfd_boolean undefined_weak
)
3808 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3809 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3811 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3813 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3814 * actual target address. */
3815 riscv_pcgp_hi_reloc hi_reloc
;
3816 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3817 switch (ELFNN_R_TYPE (rel
->r_info
))
3819 case R_RISCV_PCREL_LO12_I
:
3820 case R_RISCV_PCREL_LO12_S
:
3822 /* If the %lo has an addend, it isn't for the label pointing at the
3823 hi part instruction, but rather for the symbol pointed at by the
3824 hi part instruction. So we must subtract it here for the lookup.
3825 It is still used below in the final symbol address. */
3826 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3827 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3831 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3836 symval
= hi_reloc
.hi_addr
;
3837 sym_sec
= hi_reloc
.sym_sec
;
3839 /* We can not know whether the undefined weak symbol is referenced
3840 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3841 we have to record the 'undefined_weak' flag when handling the
3842 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3843 undefined_weak
= hi_reloc
.undefined_weak
;
3847 case R_RISCV_PCREL_HI20
:
3848 /* Mergeable symbols and code might later move out of range. */
3849 if (! undefined_weak
3850 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3853 /* If the cooresponding lo relocation has already been seen then it's not
3854 * safe to relax this relocation. */
3855 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3866 /* If gp and the symbol are in the same output section, which is not the
3867 abs section, then consider only that output section's alignment. */
3868 struct bfd_link_hash_entry
*h
=
3869 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3871 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3872 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3873 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3876 /* Is the reference in range of x0 or gp?
3877 Valid gp range conservatively because of alignment issue. */
3879 || (VALID_ITYPE_IMM (symval
)
3881 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3883 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3885 unsigned sym
= hi_reloc
.hi_sym
;
3886 switch (ELFNN_R_TYPE (rel
->r_info
))
3888 case R_RISCV_PCREL_LO12_I
:
3891 /* Change the RS1 to zero, and then modify the relocation
3892 type to R_RISCV_LO12_I. */
3893 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3894 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3895 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3896 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3897 rel
->r_addend
= hi_reloc
.hi_addend
;
3901 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3902 rel
->r_addend
+= hi_reloc
.hi_addend
;
3906 case R_RISCV_PCREL_LO12_S
:
3909 /* Change the RS1 to zero, and then modify the relocation
3910 type to R_RISCV_LO12_S. */
3911 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3912 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3913 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3914 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3915 rel
->r_addend
= hi_reloc
.hi_addend
;
3919 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3920 rel
->r_addend
+= hi_reloc
.hi_addend
;
3924 case R_RISCV_PCREL_HI20
:
3925 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3929 ELFNN_R_SYM(rel
->r_info
),
3932 /* We can delete the unnecessary AUIPC and reloc. */
3933 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3945 /* Relax PC-relative references to GP-relative references. */
3948 _bfd_riscv_relax_delete (bfd
*abfd
,
3950 asection
*sym_sec ATTRIBUTE_UNUSED
,
3951 struct bfd_link_info
*link_info
,
3952 Elf_Internal_Rela
*rel
,
3953 bfd_vma symval ATTRIBUTE_UNUSED
,
3954 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3955 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3956 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3957 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3958 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3960 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3963 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3967 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3968 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3969 disabled, handles code alignment directives. */
3972 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3973 struct bfd_link_info
*info
,
3976 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3977 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3978 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3979 Elf_Internal_Rela
*relocs
;
3980 bfd_boolean ret
= FALSE
;
3982 bfd_vma max_alignment
, reserve_size
= 0;
3983 riscv_pcgp_relocs pcgp_relocs
;
3987 if (bfd_link_relocatable (info
)
3989 || (sec
->flags
& SEC_RELOC
) == 0
3990 || sec
->reloc_count
== 0
3991 || (info
->disable_target_specific_optimizations
3992 && info
->relax_pass
== 0))
3995 riscv_init_pcgp_relocs (&pcgp_relocs
);
3997 /* Read this BFD's relocs if we haven't done so already. */
3999 relocs
= data
->relocs
;
4000 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4001 info
->keep_memory
)))
4006 max_alignment
= htab
->max_alignment
;
4007 if (max_alignment
== (bfd_vma
) -1)
4009 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4010 htab
->max_alignment
= max_alignment
;
4014 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4016 /* Examine and consider relaxing each reloc. */
4017 for (i
= 0; i
< sec
->reloc_count
; i
++)
4020 Elf_Internal_Rela
*rel
= relocs
+ i
;
4021 relax_func_t relax_func
;
4022 int type
= ELFNN_R_TYPE (rel
->r_info
);
4025 bfd_boolean undefined_weak
= FALSE
;
4028 if (info
->relax_pass
== 0)
4030 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
4031 relax_func
= _bfd_riscv_relax_call
;
4032 else if (type
== R_RISCV_HI20
4033 || type
== R_RISCV_LO12_I
4034 || type
== R_RISCV_LO12_S
)
4035 relax_func
= _bfd_riscv_relax_lui
;
4036 else if (!bfd_link_pic(info
)
4037 && (type
== R_RISCV_PCREL_HI20
4038 || type
== R_RISCV_PCREL_LO12_I
4039 || type
== R_RISCV_PCREL_LO12_S
))
4040 relax_func
= _bfd_riscv_relax_pc
;
4041 else if (type
== R_RISCV_TPREL_HI20
4042 || type
== R_RISCV_TPREL_ADD
4043 || type
== R_RISCV_TPREL_LO12_I
4044 || type
== R_RISCV_TPREL_LO12_S
)
4045 relax_func
= _bfd_riscv_relax_tls_le
;
4049 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4050 if (i
== sec
->reloc_count
- 1
4051 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4052 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4055 /* Skip over the R_RISCV_RELAX. */
4058 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4059 relax_func
= _bfd_riscv_relax_delete
;
4060 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4061 relax_func
= _bfd_riscv_relax_align
;
4065 data
->relocs
= relocs
;
4067 /* Read this BFD's contents if we haven't done so already. */
4068 if (!data
->this_hdr
.contents
4069 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4072 /* Read this BFD's symbols if we haven't done so already. */
4073 if (symtab_hdr
->sh_info
!= 0
4074 && !symtab_hdr
->contents
4075 && !(symtab_hdr
->contents
=
4076 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4077 symtab_hdr
->sh_info
,
4078 0, NULL
, NULL
, NULL
)))
4081 /* Get the value of the symbol referred to by the reloc. */
4082 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4084 /* A local symbol. */
4085 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4086 + ELFNN_R_SYM (rel
->r_info
));
4087 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4088 ? 0 : isym
->st_size
- rel
->r_addend
;
4090 if (isym
->st_shndx
== SHN_UNDEF
)
4091 sym_sec
= sec
, symval
= rel
->r_offset
;
4094 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4095 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4097 /* The purpose of this code is unknown. It breaks linker scripts
4098 for embedded development that place sections at address zero.
4099 This code is believed to be unnecessary. Disabling it but not
4100 yet removing it, in case something breaks. */
4101 if (sec_addr (sym_sec
) == 0)
4104 symval
= isym
->st_value
;
4106 symtype
= ELF_ST_TYPE (isym
->st_info
);
4111 struct elf_link_hash_entry
*h
;
4113 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4114 h
= elf_sym_hashes (abfd
)[indx
];
4116 while (h
->root
.type
== bfd_link_hash_indirect
4117 || h
->root
.type
== bfd_link_hash_warning
)
4118 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4120 if (h
->root
.type
== bfd_link_hash_undefweak
4121 && (relax_func
== _bfd_riscv_relax_lui
4122 || relax_func
== _bfd_riscv_relax_pc
))
4124 /* For the lui and auipc relaxations, since the symbol
4125 value of an undefined weak symbol is always be zero,
4126 we can optimize the patterns into a single LI/MV/ADDI
4129 Note that, creating shared libraries and pie output may
4130 break the rule above. Fortunately, since we do not relax
4131 pc relocs when creating shared libraries and pie output,
4132 and the absolute address access for R_RISCV_HI20 isn't
4133 allowed when "-fPIC" is set, the problem of creating shared
4134 libraries can not happen currently. Once we support the
4135 auipc relaxations when creating shared libraries, then we will
4136 need the more rigorous checking for this optimization. */
4137 undefined_weak
= TRUE
;
4140 /* This line has to match the check in riscv_elf_relocate_section
4141 in the R_RISCV_CALL[_PLT] case. */
4142 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4144 sym_sec
= htab
->elf
.splt
;
4145 symval
= h
->plt
.offset
;
4147 else if (undefined_weak
)
4150 sym_sec
= bfd_und_section_ptr
;
4152 else if ((h
->root
.type
== bfd_link_hash_defined
4153 || h
->root
.type
== bfd_link_hash_defweak
)
4154 && h
->root
.u
.def
.section
!= NULL
4155 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4157 symval
= h
->root
.u
.def
.value
;
4158 sym_sec
= h
->root
.u
.def
.section
;
4163 if (h
->type
!= STT_FUNC
)
4165 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4169 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4170 && (sym_sec
->flags
& SEC_MERGE
))
4172 /* At this stage in linking, no SEC_MERGE symbol has been
4173 adjusted, so all references to such symbols need to be
4174 passed through _bfd_merged_section_offset. (Later, in
4175 relocate_section, all SEC_MERGE symbols *except* for
4176 section symbols have been adjusted.)
4178 gas may reduce relocations against symbols in SEC_MERGE
4179 sections to a relocation against the section symbol when
4180 the original addend was zero. When the reloc is against
4181 a section symbol we should include the addend in the
4182 offset passed to _bfd_merged_section_offset, since the
4183 location of interest is the original symbol. On the
4184 other hand, an access to "sym+addend" where "sym" is not
4185 a section symbol should not include the addend; Such an
4186 access is presumed to be an offset from "sym"; The
4187 location of interest is just "sym". */
4188 if (symtype
== STT_SECTION
)
4189 symval
+= rel
->r_addend
;
4191 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4192 elf_section_data (sym_sec
)->sec_info
,
4195 if (symtype
!= STT_SECTION
)
4196 symval
+= rel
->r_addend
;
4199 symval
+= rel
->r_addend
;
4201 symval
+= sec_addr (sym_sec
);
4203 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4204 max_alignment
, reserve_size
, again
,
4205 &pcgp_relocs
, undefined_weak
))
4212 if (relocs
!= data
->relocs
)
4214 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4220 # define PRSTATUS_SIZE 204
4221 # define PRSTATUS_OFFSET_PR_CURSIG 12
4222 # define PRSTATUS_OFFSET_PR_PID 24
4223 # define PRSTATUS_OFFSET_PR_REG 72
4224 # define ELF_GREGSET_T_SIZE 128
4225 # define PRPSINFO_SIZE 128
4226 # define PRPSINFO_OFFSET_PR_PID 16
4227 # define PRPSINFO_OFFSET_PR_FNAME 32
4228 # define PRPSINFO_OFFSET_PR_PSARGS 48
4230 # define PRSTATUS_SIZE 376
4231 # define PRSTATUS_OFFSET_PR_CURSIG 12
4232 # define PRSTATUS_OFFSET_PR_PID 32
4233 # define PRSTATUS_OFFSET_PR_REG 112
4234 # define ELF_GREGSET_T_SIZE 256
4235 # define PRPSINFO_SIZE 136
4236 # define PRPSINFO_OFFSET_PR_PID 24
4237 # define PRPSINFO_OFFSET_PR_FNAME 40
4238 # define PRPSINFO_OFFSET_PR_PSARGS 56
4241 /* Support for core dump NOTE sections. */
4244 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4246 switch (note
->descsz
)
4251 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4253 elf_tdata (abfd
)->core
->signal
4254 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4257 elf_tdata (abfd
)->core
->lwpid
4258 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4262 /* Make a ".reg/999" section. */
4263 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4264 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4268 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4270 switch (note
->descsz
)
4275 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4277 elf_tdata (abfd
)->core
->pid
4278 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4281 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4282 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4285 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4286 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4290 /* Note that for some reason, a spurious space is tacked
4291 onto the end of the args in some (at least one anyway)
4292 implementations, so strip it off if it exists. */
4295 char *command
= elf_tdata (abfd
)->core
->command
;
4296 int n
= strlen (command
);
4298 if (0 < n
&& command
[n
- 1] == ' ')
4299 command
[n
- 1] = '\0';
4305 /* Set the right mach type. */
4307 riscv_elf_object_p (bfd
*abfd
)
4309 /* There are only two mach types in RISCV currently. */
4310 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4311 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4313 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4318 /* Determine whether an object attribute tag takes an integer, a
4322 riscv_elf_obj_attrs_arg_type (int tag
)
4324 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4327 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4328 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4330 #define elf_backend_reloc_type_class riscv_reloc_type_class
4332 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4333 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4334 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4335 #define bfd_elfNN_bfd_merge_private_bfd_data \
4336 _bfd_riscv_elf_merge_private_bfd_data
4338 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4339 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4340 #define elf_backend_check_relocs riscv_elf_check_relocs
4341 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4342 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4343 #define elf_backend_relocate_section riscv_elf_relocate_section
4344 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4345 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4346 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4347 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4348 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4349 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4350 #define elf_backend_object_p riscv_elf_object_p
4351 #define elf_info_to_howto_rel NULL
4352 #define elf_info_to_howto riscv_info_to_howto_rela
4353 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4354 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4356 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4358 #define elf_backend_can_gc_sections 1
4359 #define elf_backend_can_refcount 1
4360 #define elf_backend_want_got_plt 1
4361 #define elf_backend_plt_readonly 1
4362 #define elf_backend_plt_alignment 4
4363 #define elf_backend_want_plt_sym 1
4364 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4365 #define elf_backend_want_dynrelro 1
4366 #define elf_backend_rela_normal 1
4367 #define elf_backend_default_execstack 0
4369 #undef elf_backend_obj_attrs_vendor
4370 #define elf_backend_obj_attrs_vendor "riscv"
4371 #undef elf_backend_obj_attrs_arg_type
4372 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4373 #undef elf_backend_obj_attrs_section_type
4374 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4375 #undef elf_backend_obj_attrs_section
4376 #define elf_backend_obj_attrs_section ".riscv.attributes"
4378 #include "elfNN-target.h"