1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2019 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
;
64 /* Track dynamic relocs copied for this symbol. */
65 struct elf_dyn_relocs
*dyn_relocs
;
75 #define riscv_elf_hash_entry(ent) \
76 ((struct riscv_elf_link_hash_entry *)(ent))
78 struct _bfd_riscv_elf_obj_tdata
80 struct elf_obj_tdata root
;
82 /* tls_type for each local got entry. */
83 char *local_got_tls_type
;
86 #define _bfd_riscv_elf_tdata(abfd) \
87 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
89 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
90 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
92 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
93 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
94 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
96 #define is_riscv_elf(bfd) \
97 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
98 && elf_tdata (bfd) != NULL \
99 && elf_object_id (bfd) == RISCV_ELF_DATA)
101 #include "elf/common.h"
102 #include "elf/internal.h"
104 struct riscv_elf_link_hash_table
106 struct elf_link_hash_table elf
;
108 /* Short-cuts to get to dynamic linker sections. */
111 /* Small local sym to section mapping cache. */
112 struct sym_cache sym_cache
;
114 /* The max alignment of output sections. */
115 bfd_vma max_alignment
;
119 /* Get the RISC-V ELF linker hash table from a link_info structure. */
120 #define riscv_elf_hash_table(p) \
121 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
122 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
125 riscv_info_to_howto_rela (bfd
*abfd
,
127 Elf_Internal_Rela
*dst
)
129 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
130 return cache_ptr
->howto
!= NULL
;
134 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
136 const struct elf_backend_data
*bed
;
139 bed
= get_elf_backend_data (abfd
);
140 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
141 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
146 #define PLT_HEADER_INSNS 8
147 #define PLT_ENTRY_INSNS 4
148 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
149 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
151 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
153 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
155 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
158 riscv_elf_got_plt_val (bfd_vma plt_index
, struct bfd_link_info
*info
)
160 return sec_addr (riscv_elf_hash_table (info
)->elf
.sgotplt
)
161 + GOTPLT_HEADER_SIZE
+ (plt_index
* GOT_ENTRY_SIZE
);
165 # define MATCH_LREG MATCH_LW
167 # define MATCH_LREG MATCH_LD
170 /* Generate a PLT header. */
173 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
176 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
177 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
179 /* RVE has no t3 register, so this won't work, and is not supported. */
180 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
182 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
187 /* auipc t2, %hi(.got.plt)
188 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
189 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
190 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
191 addi t0, t2, %lo(.got.plt) # &.got.plt
192 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
193 l[w|d] t0, PTRSIZE(t0) # link map
196 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
197 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
198 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
199 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, -(PLT_HEADER_SIZE
+ 12));
200 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
201 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
202 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
203 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
208 /* Generate a PLT entry. */
211 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
214 /* RVE has no t3 register, so this won't work, and is not supported. */
215 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
217 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
222 /* auipc t3, %hi(.got.plt entry)
223 l[w|d] t3, %lo(.got.plt entry)(t3)
227 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
228 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
229 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
230 entry
[3] = RISCV_NOP
;
235 /* Create an entry in an RISC-V ELF linker hash table. */
237 static struct bfd_hash_entry
*
238 link_hash_newfunc (struct bfd_hash_entry
*entry
,
239 struct bfd_hash_table
*table
, const char *string
)
241 /* Allocate the structure if it has not already been allocated by a
246 bfd_hash_allocate (table
,
247 sizeof (struct riscv_elf_link_hash_entry
));
252 /* Call the allocation method of the superclass. */
253 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
256 struct riscv_elf_link_hash_entry
*eh
;
258 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
259 eh
->dyn_relocs
= NULL
;
260 eh
->tls_type
= GOT_UNKNOWN
;
266 /* Create a RISC-V ELF linker hash table. */
268 static struct bfd_link_hash_table
*
269 riscv_elf_link_hash_table_create (bfd
*abfd
)
271 struct riscv_elf_link_hash_table
*ret
;
272 bfd_size_type amt
= sizeof (struct riscv_elf_link_hash_table
);
274 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
278 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
279 sizeof (struct riscv_elf_link_hash_entry
),
286 ret
->max_alignment
= (bfd_vma
) -1;
287 return &ret
->elf
.root
;
290 /* Create the .got section. */
293 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
297 struct elf_link_hash_entry
*h
;
298 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
299 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
301 /* This function may be called more than once. */
302 if (htab
->sgot
!= NULL
)
305 flags
= bed
->dynamic_sec_flags
;
307 s
= bfd_make_section_anyway_with_flags (abfd
,
308 (bed
->rela_plts_and_copies_p
309 ? ".rela.got" : ".rel.got"),
310 (bed
->dynamic_sec_flags
313 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
317 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
319 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
323 /* The first bit of the global offset table is the header. */
324 s
->size
+= bed
->got_header_size
;
326 if (bed
->want_got_plt
)
328 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
330 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
334 /* Reserve room for the header. */
335 s
->size
+= GOTPLT_HEADER_SIZE
;
338 if (bed
->want_got_sym
)
340 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
341 section. We don't do this in the linker script because we don't want
342 to define the symbol if we are not creating a global offset
344 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
345 "_GLOBAL_OFFSET_TABLE_");
346 elf_hash_table (info
)->hgot
= h
;
354 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
355 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
359 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
360 struct bfd_link_info
*info
)
362 struct riscv_elf_link_hash_table
*htab
;
364 htab
= riscv_elf_hash_table (info
);
365 BFD_ASSERT (htab
!= NULL
);
367 if (!riscv_elf_create_got_section (dynobj
, info
))
370 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
373 if (!bfd_link_pic (info
))
375 /* Technically, this section doesn't have contents. It is used as the
376 target of TLS copy relocs, to copy TLS data from shared libraries into
377 the executable. However, if we don't mark it as loadable, then it
378 matches the IS_TBSS test in ldlang.c, and there is no run-time address
379 space allocated for it even though it has SEC_ALLOC. That test is
380 correct for .tbss, but not correct for this section. There is also
381 a second problem that having a section with no contents can only work
382 if it comes after all sections with contents in the same segment,
383 but the linker script does not guarantee that. This is just mixed in
384 with other .tdata.* sections. We can fix both problems by lying and
385 saying that there are contents. This section is expected to be small
386 so this should not cause a significant extra program startup cost. */
388 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
389 (SEC_ALLOC
| SEC_THREAD_LOCAL
390 | SEC_LOAD
| SEC_DATA
392 | SEC_LINKER_CREATED
));
395 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
396 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
402 /* Copy the extra info we tack onto an elf_link_hash_entry. */
405 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
406 struct elf_link_hash_entry
*dir
,
407 struct elf_link_hash_entry
*ind
)
409 struct riscv_elf_link_hash_entry
*edir
, *eind
;
411 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
412 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
414 if (eind
->dyn_relocs
!= NULL
)
416 if (edir
->dyn_relocs
!= NULL
)
418 struct elf_dyn_relocs
**pp
;
419 struct elf_dyn_relocs
*p
;
421 /* Add reloc counts against the indirect sym to the direct sym
422 list. Merge any entries against the same section. */
423 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
425 struct elf_dyn_relocs
*q
;
427 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
428 if (q
->sec
== p
->sec
)
430 q
->pc_count
+= p
->pc_count
;
431 q
->count
+= p
->count
;
438 *pp
= edir
->dyn_relocs
;
441 edir
->dyn_relocs
= eind
->dyn_relocs
;
442 eind
->dyn_relocs
= NULL
;
445 if (ind
->root
.type
== bfd_link_hash_indirect
446 && dir
->got
.refcount
<= 0)
448 edir
->tls_type
= eind
->tls_type
;
449 eind
->tls_type
= GOT_UNKNOWN
;
451 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
455 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
456 unsigned long symndx
, char tls_type
)
458 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
460 *new_tls_type
|= tls_type
;
461 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
463 (*_bfd_error_handler
)
464 (_("%pB: `%s' accessed both as normal and thread local symbol"),
465 abfd
, h
? h
->root
.root
.string
: "<local>");
472 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
473 struct elf_link_hash_entry
*h
, long symndx
)
475 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
476 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
478 if (htab
->elf
.sgot
== NULL
)
480 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
486 h
->got
.refcount
+= 1;
490 /* This is a global offset table entry for a local symbol. */
491 if (elf_local_got_refcounts (abfd
) == NULL
)
493 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
494 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
496 _bfd_riscv_elf_local_got_tls_type (abfd
)
497 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
499 elf_local_got_refcounts (abfd
) [symndx
] += 1;
505 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
507 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
509 (*_bfd_error_handler
)
510 (_("%pB: relocation %s against `%s' can not be used when making a shared "
511 "object; recompile with -fPIC"),
512 abfd
, r
? r
->name
: _("<unknown>"),
513 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
514 bfd_set_error (bfd_error_bad_value
);
517 /* Look through the relocs for a section during the first phase, and
518 allocate space in the global offset table or procedure linkage
522 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
523 asection
*sec
, const Elf_Internal_Rela
*relocs
)
525 struct riscv_elf_link_hash_table
*htab
;
526 Elf_Internal_Shdr
*symtab_hdr
;
527 struct elf_link_hash_entry
**sym_hashes
;
528 const Elf_Internal_Rela
*rel
;
529 asection
*sreloc
= NULL
;
531 if (bfd_link_relocatable (info
))
534 htab
= riscv_elf_hash_table (info
);
535 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
536 sym_hashes
= elf_sym_hashes (abfd
);
538 if (htab
->elf
.dynobj
== NULL
)
539 htab
->elf
.dynobj
= abfd
;
541 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
544 unsigned int r_symndx
;
545 struct elf_link_hash_entry
*h
;
547 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
548 r_type
= ELFNN_R_TYPE (rel
->r_info
);
550 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
552 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
557 if (r_symndx
< symtab_hdr
->sh_info
)
561 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
562 while (h
->root
.type
== bfd_link_hash_indirect
563 || h
->root
.type
== bfd_link_hash_warning
)
564 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
569 case R_RISCV_TLS_GD_HI20
:
570 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
571 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
575 case R_RISCV_TLS_GOT_HI20
:
576 if (bfd_link_pic (info
))
577 info
->flags
|= DF_STATIC_TLS
;
578 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
579 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
583 case R_RISCV_GOT_HI20
:
584 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
585 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
589 case R_RISCV_CALL_PLT
:
590 /* This symbol requires a procedure linkage table entry. We
591 actually build the entry in adjust_dynamic_symbol,
592 because this might be a case of linking PIC code without
593 linking in any dynamic objects, in which case we don't
594 need to generate a procedure linkage table after all. */
599 h
->plt
.refcount
+= 1;
606 case R_RISCV_RVC_BRANCH
:
607 case R_RISCV_RVC_JUMP
:
608 case R_RISCV_PCREL_HI20
:
609 /* In shared libraries, these relocs are known to bind locally. */
610 if (bfd_link_pic (info
))
614 case R_RISCV_TPREL_HI20
:
615 if (!bfd_link_executable (info
))
616 return bad_static_reloc (abfd
, r_type
, h
);
618 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
622 if (bfd_link_pic (info
))
623 return bad_static_reloc (abfd
, r_type
, h
);
627 case R_RISCV_JUMP_SLOT
:
628 case R_RISCV_RELATIVE
:
634 /* This reloc might not bind locally. */
638 if (h
!= NULL
&& !bfd_link_pic (info
))
640 /* We may need a .plt entry if the function this reloc
641 refers to is in a shared lib. */
642 h
->plt
.refcount
+= 1;
645 /* If we are creating a shared library, and this is a reloc
646 against a global symbol, or a non PC relative reloc
647 against a local symbol, then we need to copy the reloc
648 into the shared library. However, if we are linking with
649 -Bsymbolic, we do not need to copy a reloc against a
650 global symbol which is defined in an object we are
651 including in the link (i.e., DEF_REGULAR is set). At
652 this point we have not seen all the input files, so it is
653 possible that DEF_REGULAR is not set now but will be set
654 later (it is never cleared). In case of a weak definition,
655 DEF_REGULAR may be cleared later by a strong definition in
656 a shared library. We account for that possibility below by
657 storing information in the relocs_copied field of the hash
658 table entry. A similar situation occurs when creating
659 shared libraries and symbol visibility changes render the
662 If on the other hand, we are creating an executable, we
663 may need to keep relocations for symbols satisfied by a
664 dynamic library if we manage to avoid copy relocs for the
666 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
668 if ((bfd_link_pic (info
)
669 && (sec
->flags
& SEC_ALLOC
) != 0
670 && ((r
!= NULL
&& ! r
->pc_relative
)
673 || h
->root
.type
== bfd_link_hash_defweak
674 || !h
->def_regular
))))
675 || (!bfd_link_pic (info
)
676 && (sec
->flags
& SEC_ALLOC
) != 0
678 && (h
->root
.type
== bfd_link_hash_defweak
679 || !h
->def_regular
)))
681 struct elf_dyn_relocs
*p
;
682 struct elf_dyn_relocs
**head
;
684 /* When creating a shared object, we must copy these
685 relocs into the output file. We create a reloc
686 section in dynobj and make room for the reloc. */
689 sreloc
= _bfd_elf_make_dynamic_reloc_section
690 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
691 abfd
, /*rela?*/ TRUE
);
697 /* If this is a global symbol, we count the number of
698 relocations we need for this symbol. */
700 head
= &((struct riscv_elf_link_hash_entry
*) h
)->dyn_relocs
;
703 /* Track dynamic relocs needed for local syms too.
704 We really need local syms available to do this
709 Elf_Internal_Sym
*isym
;
711 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
716 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
720 vpp
= &elf_section_data (s
)->local_dynrel
;
721 head
= (struct elf_dyn_relocs
**) vpp
;
725 if (p
== NULL
|| p
->sec
!= sec
)
727 bfd_size_type amt
= sizeof *p
;
728 p
= ((struct elf_dyn_relocs
*)
729 bfd_alloc (htab
->elf
.dynobj
, amt
));
740 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
745 case R_RISCV_GNU_VTINHERIT
:
746 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
750 case R_RISCV_GNU_VTENTRY
:
751 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
764 riscv_elf_gc_mark_hook (asection
*sec
,
765 struct bfd_link_info
*info
,
766 Elf_Internal_Rela
*rel
,
767 struct elf_link_hash_entry
*h
,
768 Elf_Internal_Sym
*sym
)
771 switch (ELFNN_R_TYPE (rel
->r_info
))
773 case R_RISCV_GNU_VTINHERIT
:
774 case R_RISCV_GNU_VTENTRY
:
778 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
781 /* Find dynamic relocs for H that apply to read-only sections. */
784 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
786 struct elf_dyn_relocs
*p
;
788 for (p
= riscv_elf_hash_entry (h
)->dyn_relocs
; p
!= NULL
; p
= p
->next
)
790 asection
*s
= p
->sec
->output_section
;
792 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
798 /* Adjust a symbol defined by a dynamic object and referenced by a
799 regular object. The current definition is in some section of the
800 dynamic object, but we're not including those sections. We have to
801 change the definition to something the rest of the link can
805 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
806 struct elf_link_hash_entry
*h
)
808 struct riscv_elf_link_hash_table
*htab
;
809 struct riscv_elf_link_hash_entry
* eh
;
813 htab
= riscv_elf_hash_table (info
);
814 BFD_ASSERT (htab
!= NULL
);
816 dynobj
= htab
->elf
.dynobj
;
818 /* Make sure we know what is going on here. */
819 BFD_ASSERT (dynobj
!= NULL
821 || h
->type
== STT_GNU_IFUNC
825 && !h
->def_regular
)));
827 /* If this is a function, put it in the procedure linkage table. We
828 will fill in the contents of the procedure linkage table later
829 (although we could actually do it here). */
830 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
832 if (h
->plt
.refcount
<= 0
833 || SYMBOL_CALLS_LOCAL (info
, h
)
834 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
835 && h
->root
.type
== bfd_link_hash_undefweak
))
837 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
838 input file, but the symbol was never referred to by a dynamic
839 object, or if all references were garbage collected. In such
840 a case, we don't actually need to build a PLT entry. */
841 h
->plt
.offset
= (bfd_vma
) -1;
848 h
->plt
.offset
= (bfd_vma
) -1;
850 /* If this is a weak symbol, and there is a real definition, the
851 processor independent code will have arranged for us to see the
852 real definition first, and we can just use the same value. */
855 struct elf_link_hash_entry
*def
= weakdef (h
);
856 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
857 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
858 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
862 /* This is a reference to a symbol defined by a dynamic object which
863 is not a function. */
865 /* If we are creating a shared library, we must presume that the
866 only references to the symbol are via the global offset table.
867 For such cases we need not do anything here; the relocations will
868 be handled correctly by relocate_section. */
869 if (bfd_link_pic (info
))
872 /* If there are no references to this symbol that do not use the
873 GOT, we don't need to generate a copy reloc. */
877 /* If -z nocopyreloc was given, we won't generate them either. */
878 if (info
->nocopyreloc
)
884 /* If we don't find any dynamic relocs in read-only sections, then
885 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
886 if (!readonly_dynrelocs (h
))
892 /* We must allocate the symbol in our .dynbss section, which will
893 become part of the .bss section of the executable. There will be
894 an entry for this symbol in the .dynsym section. The dynamic
895 object will contain position independent code, so all references
896 from the dynamic object to this symbol will go through the global
897 offset table. The dynamic linker will use the .dynsym entry to
898 determine the address it must put in the global offset table, so
899 both the dynamic object and the regular object will refer to the
900 same memory location for the variable. */
902 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
903 to copy the initial value out of the dynamic object and into the
904 runtime process image. We need to remember the offset into the
905 .rel.bss section we are going to use. */
906 eh
= (struct riscv_elf_link_hash_entry
*) h
;
907 if (eh
->tls_type
& ~GOT_NORMAL
)
910 srel
= htab
->elf
.srelbss
;
912 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
914 s
= htab
->elf
.sdynrelro
;
915 srel
= htab
->elf
.sreldynrelro
;
919 s
= htab
->elf
.sdynbss
;
920 srel
= htab
->elf
.srelbss
;
922 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
924 srel
->size
+= sizeof (ElfNN_External_Rela
);
928 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
931 /* Allocate space in .plt, .got and associated reloc sections for
935 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
937 struct bfd_link_info
*info
;
938 struct riscv_elf_link_hash_table
*htab
;
939 struct riscv_elf_link_hash_entry
*eh
;
940 struct elf_dyn_relocs
*p
;
942 if (h
->root
.type
== bfd_link_hash_indirect
)
945 info
= (struct bfd_link_info
*) inf
;
946 htab
= riscv_elf_hash_table (info
);
947 BFD_ASSERT (htab
!= NULL
);
949 if (htab
->elf
.dynamic_sections_created
950 && h
->plt
.refcount
> 0)
952 /* Make sure this symbol is output as a dynamic symbol.
953 Undefined weak syms won't yet be marked as dynamic. */
957 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
961 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
963 asection
*s
= htab
->elf
.splt
;
966 s
->size
= PLT_HEADER_SIZE
;
968 h
->plt
.offset
= s
->size
;
970 /* Make room for this entry. */
971 s
->size
+= PLT_ENTRY_SIZE
;
973 /* We also need to make an entry in the .got.plt section. */
974 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
976 /* We also need to make an entry in the .rela.plt section. */
977 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
979 /* If this symbol is not defined in a regular file, and we are
980 not generating a shared library, then set the symbol to this
981 location in the .plt. This is required to make function
982 pointers compare as equal between the normal executable and
983 the shared library. */
984 if (! bfd_link_pic (info
)
987 h
->root
.u
.def
.section
= s
;
988 h
->root
.u
.def
.value
= h
->plt
.offset
;
993 h
->plt
.offset
= (bfd_vma
) -1;
999 h
->plt
.offset
= (bfd_vma
) -1;
1003 if (h
->got
.refcount
> 0)
1007 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1009 /* Make sure this symbol is output as a dynamic symbol.
1010 Undefined weak syms won't yet be marked as dynamic. */
1011 if (h
->dynindx
== -1
1012 && !h
->forced_local
)
1014 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1019 h
->got
.offset
= s
->size
;
1020 dyn
= htab
->elf
.dynamic_sections_created
;
1021 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1023 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1024 if (tls_type
& GOT_TLS_GD
)
1026 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1027 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1030 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1031 if (tls_type
& GOT_TLS_IE
)
1033 s
->size
+= RISCV_ELF_WORD_BYTES
;
1034 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1039 s
->size
+= RISCV_ELF_WORD_BYTES
;
1040 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1041 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1042 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1046 h
->got
.offset
= (bfd_vma
) -1;
1048 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1049 if (eh
->dyn_relocs
== NULL
)
1052 /* In the shared -Bsymbolic case, discard space allocated for
1053 dynamic pc-relative relocs against symbols which turn out to be
1054 defined in regular objects. For the normal shared case, discard
1055 space for pc-relative relocs that have become local due to symbol
1056 visibility changes. */
1058 if (bfd_link_pic (info
))
1060 if (SYMBOL_CALLS_LOCAL (info
, h
))
1062 struct elf_dyn_relocs
**pp
;
1064 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1066 p
->count
-= p
->pc_count
;
1075 /* Also discard relocs on undefined weak syms with non-default
1077 if (eh
->dyn_relocs
!= NULL
1078 && h
->root
.type
== bfd_link_hash_undefweak
)
1080 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1081 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1082 eh
->dyn_relocs
= NULL
;
1084 /* Make sure undefined weak symbols are output as a dynamic
1086 else if (h
->dynindx
== -1
1087 && !h
->forced_local
)
1089 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1096 /* For the non-shared case, discard space for relocs against
1097 symbols which turn out to need copy relocs or are not
1103 || (htab
->elf
.dynamic_sections_created
1104 && (h
->root
.type
== bfd_link_hash_undefweak
1105 || h
->root
.type
== bfd_link_hash_undefined
))))
1107 /* Make sure this symbol is output as a dynamic symbol.
1108 Undefined weak syms won't yet be marked as dynamic. */
1109 if (h
->dynindx
== -1
1110 && !h
->forced_local
)
1112 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1116 /* If that succeeded, we know we'll be keeping all the
1118 if (h
->dynindx
!= -1)
1122 eh
->dyn_relocs
= NULL
;
1127 /* Finally, allocate space. */
1128 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1130 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1131 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1137 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1138 read-only sections. */
1141 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
1145 if (h
->root
.type
== bfd_link_hash_indirect
)
1148 sec
= readonly_dynrelocs (h
);
1151 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
1153 info
->flags
|= DF_TEXTREL
;
1154 info
->callbacks
->minfo
1155 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1156 sec
->owner
, h
->root
.root
.string
, sec
);
1158 /* Not an error, just cut short the traversal. */
1165 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1167 struct riscv_elf_link_hash_table
*htab
;
1172 htab
= riscv_elf_hash_table (info
);
1173 BFD_ASSERT (htab
!= NULL
);
1174 dynobj
= htab
->elf
.dynobj
;
1175 BFD_ASSERT (dynobj
!= NULL
);
1177 if (elf_hash_table (info
)->dynamic_sections_created
)
1179 /* Set the contents of the .interp section to the interpreter. */
1180 if (bfd_link_executable (info
) && !info
->nointerp
)
1182 s
= bfd_get_linker_section (dynobj
, ".interp");
1183 BFD_ASSERT (s
!= NULL
);
1184 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1185 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1189 /* Set up .got offsets for local syms, and space for local dynamic
1191 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1193 bfd_signed_vma
*local_got
;
1194 bfd_signed_vma
*end_local_got
;
1195 char *local_tls_type
;
1196 bfd_size_type locsymcount
;
1197 Elf_Internal_Shdr
*symtab_hdr
;
1200 if (! is_riscv_elf (ibfd
))
1203 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1205 struct elf_dyn_relocs
*p
;
1207 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1209 if (!bfd_is_abs_section (p
->sec
)
1210 && bfd_is_abs_section (p
->sec
->output_section
))
1212 /* Input section has been discarded, either because
1213 it is a copy of a linkonce section or due to
1214 linker script /DISCARD/, so we'll be discarding
1217 else if (p
->count
!= 0)
1219 srel
= elf_section_data (p
->sec
)->sreloc
;
1220 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1221 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1222 info
->flags
|= DF_TEXTREL
;
1227 local_got
= elf_local_got_refcounts (ibfd
);
1231 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1232 locsymcount
= symtab_hdr
->sh_info
;
1233 end_local_got
= local_got
+ locsymcount
;
1234 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1236 srel
= htab
->elf
.srelgot
;
1237 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1241 *local_got
= s
->size
;
1242 s
->size
+= RISCV_ELF_WORD_BYTES
;
1243 if (*local_tls_type
& GOT_TLS_GD
)
1244 s
->size
+= RISCV_ELF_WORD_BYTES
;
1245 if (bfd_link_pic (info
)
1246 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1247 srel
->size
+= sizeof (ElfNN_External_Rela
);
1250 *local_got
= (bfd_vma
) -1;
1254 /* Allocate global sym .plt and .got entries, and space for global
1255 sym dynamic relocs. */
1256 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1258 if (htab
->elf
.sgotplt
)
1260 struct elf_link_hash_entry
*got
;
1261 got
= elf_link_hash_lookup (elf_hash_table (info
),
1262 "_GLOBAL_OFFSET_TABLE_",
1263 FALSE
, FALSE
, FALSE
);
1265 /* Don't allocate .got.plt section if there are no GOT nor PLT
1266 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1268 || !got
->ref_regular_nonweak
)
1269 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1270 && (htab
->elf
.splt
== NULL
1271 || htab
->elf
.splt
->size
== 0)
1272 && (htab
->elf
.sgot
== NULL
1273 || (htab
->elf
.sgot
->size
1274 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1275 htab
->elf
.sgotplt
->size
= 0;
1278 /* The check_relocs and adjust_dynamic_symbol entry points have
1279 determined the sizes of the various dynamic sections. Allocate
1281 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1283 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1286 if (s
== htab
->elf
.splt
1287 || s
== htab
->elf
.sgot
1288 || s
== htab
->elf
.sgotplt
1289 || s
== htab
->elf
.sdynbss
1290 || s
== htab
->elf
.sdynrelro
1291 || s
== htab
->sdyntdata
)
1293 /* Strip this section if we don't need it; see the
1296 else if (strncmp (s
->name
, ".rela", 5) == 0)
1300 /* We use the reloc_count field as a counter if we need
1301 to copy relocs into the output file. */
1307 /* It's not one of our sections. */
1313 /* If we don't need this section, strip it from the
1314 output file. This is mostly to handle .rela.bss and
1315 .rela.plt. We must create both sections in
1316 create_dynamic_sections, because they must be created
1317 before the linker maps input sections to output
1318 sections. The linker does that before
1319 adjust_dynamic_symbol is called, and it is that
1320 function which decides whether anything needs to go
1321 into these sections. */
1322 s
->flags
|= SEC_EXCLUDE
;
1326 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1329 /* Allocate memory for the section contents. Zero the memory
1330 for the benefit of .rela.plt, which has 4 unused entries
1331 at the beginning, and we don't want garbage. */
1332 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1333 if (s
->contents
== NULL
)
1337 if (elf_hash_table (info
)->dynamic_sections_created
)
1339 /* Add some entries to the .dynamic section. We fill in the
1340 values later, in riscv_elf_finish_dynamic_sections, but we
1341 must add the entries now so that we get the correct size for
1342 the .dynamic section. The DT_DEBUG entry is filled in by the
1343 dynamic linker and used by the debugger. */
1344 #define add_dynamic_entry(TAG, VAL) \
1345 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1347 if (bfd_link_executable (info
))
1349 if (!add_dynamic_entry (DT_DEBUG
, 0))
1353 if (htab
->elf
.srelplt
->size
!= 0)
1355 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1356 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1357 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1358 || !add_dynamic_entry (DT_JMPREL
, 0))
1362 if (!add_dynamic_entry (DT_RELA
, 0)
1363 || !add_dynamic_entry (DT_RELASZ
, 0)
1364 || !add_dynamic_entry (DT_RELAENT
, sizeof (ElfNN_External_Rela
)))
1367 /* If any dynamic relocs apply to a read-only section,
1368 then we need a DT_TEXTREL entry. */
1369 if ((info
->flags
& DF_TEXTREL
) == 0)
1370 elf_link_hash_traverse (&htab
->elf
, maybe_set_textrel
, info
);
1372 if (info
->flags
& DF_TEXTREL
)
1374 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1378 #undef add_dynamic_entry
1384 #define DTP_OFFSET 0x800
1386 /* Return the relocation value for a TLS dtp-relative reloc. */
1389 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1391 /* If tls_sec is NULL, we should have signalled an error already. */
1392 if (elf_hash_table (info
)->tls_sec
== NULL
)
1394 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1397 /* Return the relocation value for a static TLS tp-relative relocation. */
1400 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1402 /* If tls_sec is NULL, we should have signalled an error already. */
1403 if (elf_hash_table (info
)->tls_sec
== NULL
)
1405 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1408 /* Return the global pointer's value, or 0 if it is not in use. */
1411 riscv_global_pointer_value (struct bfd_link_info
*info
)
1413 struct bfd_link_hash_entry
*h
;
1415 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1416 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1419 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1422 /* Emplace a static relocation. */
1424 static bfd_reloc_status_type
1425 perform_relocation (const reloc_howto_type
*howto
,
1426 const Elf_Internal_Rela
*rel
,
1428 asection
*input_section
,
1432 if (howto
->pc_relative
)
1433 value
-= sec_addr (input_section
) + rel
->r_offset
;
1434 value
+= rel
->r_addend
;
1436 switch (ELFNN_R_TYPE (rel
->r_info
))
1439 case R_RISCV_TPREL_HI20
:
1440 case R_RISCV_PCREL_HI20
:
1441 case R_RISCV_GOT_HI20
:
1442 case R_RISCV_TLS_GOT_HI20
:
1443 case R_RISCV_TLS_GD_HI20
:
1444 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1445 return bfd_reloc_overflow
;
1446 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1449 case R_RISCV_LO12_I
:
1450 case R_RISCV_GPREL_I
:
1451 case R_RISCV_TPREL_LO12_I
:
1452 case R_RISCV_TPREL_I
:
1453 case R_RISCV_PCREL_LO12_I
:
1454 value
= ENCODE_ITYPE_IMM (value
);
1457 case R_RISCV_LO12_S
:
1458 case R_RISCV_GPREL_S
:
1459 case R_RISCV_TPREL_LO12_S
:
1460 case R_RISCV_TPREL_S
:
1461 case R_RISCV_PCREL_LO12_S
:
1462 value
= ENCODE_STYPE_IMM (value
);
1466 case R_RISCV_CALL_PLT
:
1467 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1468 return bfd_reloc_overflow
;
1469 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1470 | (ENCODE_ITYPE_IMM (value
) << 32);
1474 if (!VALID_UJTYPE_IMM (value
))
1475 return bfd_reloc_overflow
;
1476 value
= ENCODE_UJTYPE_IMM (value
);
1479 case R_RISCV_BRANCH
:
1480 if (!VALID_SBTYPE_IMM (value
))
1481 return bfd_reloc_overflow
;
1482 value
= ENCODE_SBTYPE_IMM (value
);
1485 case R_RISCV_RVC_BRANCH
:
1486 if (!VALID_RVC_B_IMM (value
))
1487 return bfd_reloc_overflow
;
1488 value
= ENCODE_RVC_B_IMM (value
);
1491 case R_RISCV_RVC_JUMP
:
1492 if (!VALID_RVC_J_IMM (value
))
1493 return bfd_reloc_overflow
;
1494 value
= ENCODE_RVC_J_IMM (value
);
1497 case R_RISCV_RVC_LUI
:
1498 if (RISCV_CONST_HIGH_PART (value
) == 0)
1500 /* Linker relaxation can convert an address equal to or greater than
1501 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1502 valid immediate. We can fix this by converting it to a C.LI. */
1503 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1504 contents
+ rel
->r_offset
);
1505 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1506 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1507 value
= ENCODE_RVC_IMM (0);
1509 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1510 return bfd_reloc_overflow
;
1512 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1530 case R_RISCV_32_PCREL
:
1531 case R_RISCV_TLS_DTPREL32
:
1532 case R_RISCV_TLS_DTPREL64
:
1535 case R_RISCV_DELETE
:
1536 return bfd_reloc_ok
;
1539 return bfd_reloc_notsupported
;
1542 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1543 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1544 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1546 return bfd_reloc_ok
;
1549 /* Remember all PC-relative high-part relocs we've encountered to help us
1550 later resolve the corresponding low-part relocs. */
1556 } riscv_pcrel_hi_reloc
;
1558 typedef struct riscv_pcrel_lo_reloc
1560 asection
* input_section
;
1561 struct bfd_link_info
* info
;
1562 reloc_howto_type
* howto
;
1563 const Elf_Internal_Rela
* reloc
;
1566 bfd_byte
* contents
;
1567 struct riscv_pcrel_lo_reloc
* next
;
1568 } riscv_pcrel_lo_reloc
;
1573 riscv_pcrel_lo_reloc
*lo_relocs
;
1574 } riscv_pcrel_relocs
;
1577 riscv_pcrel_reloc_hash (const void *entry
)
1579 const riscv_pcrel_hi_reloc
*e
= entry
;
1580 return (hashval_t
)(e
->address
>> 2);
1584 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1586 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1587 return e1
->address
== e2
->address
;
1591 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1594 p
->lo_relocs
= NULL
;
1595 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1596 riscv_pcrel_reloc_eq
, free
);
1597 return p
->hi_relocs
!= NULL
;
1601 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1603 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1607 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1612 htab_delete (p
->hi_relocs
);
1616 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1617 struct bfd_link_info
*info
,
1621 const reloc_howto_type
*howto
,
1624 /* We may need to reference low addreses in PC-relative modes even when the
1625 * PC is far away from these addresses. For example, undefweak references
1626 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1627 * addresses that we can link PC-relative programs at, the linker can't
1628 * actually relocate references to those symbols. In order to allow these
1629 * programs to work we simply convert the PC-relative auipc sequences to
1630 * 0-relative lui sequences. */
1631 if (bfd_link_pic (info
))
1634 /* If it's possible to reference the symbol using auipc we do so, as that's
1635 * more in the spirit of the PC-relative relocations we're processing. */
1636 bfd_vma offset
= addr
- pc
;
1637 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1640 /* If it's impossible to reference this with a LUI-based offset then don't
1641 * bother to convert it at all so users still see the PC-relative relocation
1642 * in the truncation message. */
1643 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1646 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1648 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1649 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1650 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1655 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1656 bfd_vma value
, bfd_boolean absolute
)
1658 bfd_vma offset
= absolute
? value
: value
- addr
;
1659 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1660 riscv_pcrel_hi_reloc
**slot
=
1661 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1663 BFD_ASSERT (*slot
== NULL
);
1664 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1672 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1673 asection
*input_section
,
1674 struct bfd_link_info
*info
,
1675 reloc_howto_type
*howto
,
1676 const Elf_Internal_Rela
*reloc
,
1681 riscv_pcrel_lo_reloc
*entry
;
1682 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1685 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1686 name
, contents
, p
->lo_relocs
};
1687 p
->lo_relocs
= entry
;
1692 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1694 riscv_pcrel_lo_reloc
*r
;
1696 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1698 bfd
*input_bfd
= r
->input_section
->owner
;
1700 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1701 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1703 /* Check for overflow into bit 11 when adding reloc addend. */
1704 || (! (entry
->value
& 0x800)
1705 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1707 char *string
= (entry
== NULL
1708 ? "%pcrel_lo missing matching %pcrel_hi"
1709 : "%pcrel_lo overflow with an addend");
1710 (*r
->info
->callbacks
->reloc_dangerous
)
1711 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1715 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1716 input_bfd
, r
->contents
);
1722 /* Relocate a RISC-V ELF section.
1724 The RELOCATE_SECTION function is called by the new ELF backend linker
1725 to handle the relocations for a section.
1727 The relocs are always passed as Rela structures.
1729 This function is responsible for adjusting the section contents as
1730 necessary, and (if generating a relocatable output file) adjusting
1731 the reloc addend as necessary.
1733 This function does not have to worry about setting the reloc
1734 address or the reloc symbol index.
1736 LOCAL_SYMS is a pointer to the swapped in local symbols.
1738 LOCAL_SECTIONS is an array giving the section in the input file
1739 corresponding to the st_shndx field of each local symbol.
1741 The global hash table entry for the global symbols can be found
1742 via elf_sym_hashes (input_bfd).
1744 When generating relocatable output, this function must handle
1745 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1746 going to be the section symbol corresponding to the output
1747 section, which means that the addend must be adjusted
1751 riscv_elf_relocate_section (bfd
*output_bfd
,
1752 struct bfd_link_info
*info
,
1754 asection
*input_section
,
1756 Elf_Internal_Rela
*relocs
,
1757 Elf_Internal_Sym
*local_syms
,
1758 asection
**local_sections
)
1760 Elf_Internal_Rela
*rel
;
1761 Elf_Internal_Rela
*relend
;
1762 riscv_pcrel_relocs pcrel_relocs
;
1763 bfd_boolean ret
= FALSE
;
1764 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1765 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1766 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1767 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1768 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1769 bfd_boolean absolute
;
1771 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1774 relend
= relocs
+ input_section
->reloc_count
;
1775 for (rel
= relocs
; rel
< relend
; rel
++)
1777 unsigned long r_symndx
;
1778 struct elf_link_hash_entry
*h
;
1779 Elf_Internal_Sym
*sym
;
1782 bfd_reloc_status_type r
= bfd_reloc_ok
;
1784 bfd_vma off
, ie_off
;
1785 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1786 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1787 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1788 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1789 const char *msg
= NULL
;
1790 bfd_boolean resolved_to_zero
;
1793 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1796 /* This is a final link. */
1797 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1801 unresolved_reloc
= FALSE
;
1802 if (r_symndx
< symtab_hdr
->sh_info
)
1804 sym
= local_syms
+ r_symndx
;
1805 sec
= local_sections
[r_symndx
];
1806 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1810 bfd_boolean warned
, ignored
;
1812 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1813 r_symndx
, symtab_hdr
, sym_hashes
,
1815 unresolved_reloc
, warned
, ignored
);
1818 /* To avoid generating warning messages about truncated
1819 relocations, set the relocation's address to be the same as
1820 the start of this section. */
1821 if (input_section
->output_section
!= NULL
)
1822 relocation
= input_section
->output_section
->vma
;
1828 if (sec
!= NULL
&& discarded_section (sec
))
1829 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1830 rel
, 1, relend
, howto
, 0, contents
);
1832 if (bfd_link_relocatable (info
))
1836 name
= h
->root
.root
.string
;
1839 name
= (bfd_elf_string_from_elf_section
1840 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1841 if (name
== NULL
|| *name
== '\0')
1842 name
= bfd_section_name (sec
);
1845 resolved_to_zero
= (h
!= NULL
1846 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1852 case R_RISCV_TPREL_ADD
:
1854 case R_RISCV_JUMP_SLOT
:
1855 case R_RISCV_RELATIVE
:
1856 /* These require nothing of us at all. */
1860 case R_RISCV_BRANCH
:
1861 case R_RISCV_RVC_BRANCH
:
1862 case R_RISCV_RVC_LUI
:
1863 case R_RISCV_LO12_I
:
1864 case R_RISCV_LO12_S
:
1869 case R_RISCV_32_PCREL
:
1870 case R_RISCV_DELETE
:
1871 /* These require no special handling beyond perform_relocation. */
1874 case R_RISCV_GOT_HI20
:
1877 bfd_boolean dyn
, pic
;
1879 off
= h
->got
.offset
;
1880 BFD_ASSERT (off
!= (bfd_vma
) -1);
1881 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1882 pic
= bfd_link_pic (info
);
1884 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1885 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1887 /* This is actually a static link, or it is a
1888 -Bsymbolic link and the symbol is defined
1889 locally, or the symbol was forced to be local
1890 because of a version file. We must initialize
1891 this entry in the global offset table. Since the
1892 offset must always be a multiple of the word size,
1893 we use the least significant bit to record whether
1894 we have initialized it already.
1896 When doing a dynamic link, we create a .rela.got
1897 relocation entry to initialize the value. This
1898 is done in the finish_dynamic_symbol routine. */
1903 bfd_put_NN (output_bfd
, relocation
,
1904 htab
->elf
.sgot
->contents
+ off
);
1909 unresolved_reloc
= FALSE
;
1913 BFD_ASSERT (local_got_offsets
!= NULL
1914 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1916 off
= local_got_offsets
[r_symndx
];
1918 /* The offset must always be a multiple of the word size.
1919 So, we can use the least significant bit to record
1920 whether we have already processed this entry. */
1925 if (bfd_link_pic (info
))
1928 Elf_Internal_Rela outrel
;
1930 /* We need to generate a R_RISCV_RELATIVE reloc
1931 for the dynamic linker. */
1932 s
= htab
->elf
.srelgot
;
1933 BFD_ASSERT (s
!= NULL
);
1935 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1937 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1938 outrel
.r_addend
= relocation
;
1940 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1943 bfd_put_NN (output_bfd
, relocation
,
1944 htab
->elf
.sgot
->contents
+ off
);
1945 local_got_offsets
[r_symndx
] |= 1;
1948 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1949 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1956 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1957 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1959 r
= bfd_reloc_notsupported
;
1960 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1961 relocation
, absolute
))
1962 r
= bfd_reloc_overflow
;
1970 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1971 contents
+ rel
->r_offset
);
1972 relocation
= old_value
+ relocation
;
1982 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1983 contents
+ rel
->r_offset
);
1984 relocation
= old_value
- relocation
;
1989 /* Handle a call to an undefined weak function. This won't be
1990 relaxed, so we have to handle it here. */
1991 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
1992 && h
->plt
.offset
== MINUS_ONE
)
1994 /* We can use x0 as the base register. */
1995 bfd_vma insn
= bfd_get_32 (input_bfd
,
1996 contents
+ rel
->r_offset
+ 4);
1997 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1998 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
1999 /* Set the relocation value so that we get 0 after the pc
2000 relative adjustment. */
2001 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2005 case R_RISCV_CALL_PLT
:
2007 case R_RISCV_RVC_JUMP
:
2008 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2010 /* Refer to the PLT entry. */
2011 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2012 unresolved_reloc
= FALSE
;
2016 case R_RISCV_TPREL_HI20
:
2017 relocation
= tpoff (info
, relocation
);
2020 case R_RISCV_TPREL_LO12_I
:
2021 case R_RISCV_TPREL_LO12_S
:
2022 relocation
= tpoff (info
, relocation
);
2025 case R_RISCV_TPREL_I
:
2026 case R_RISCV_TPREL_S
:
2027 relocation
= tpoff (info
, relocation
);
2028 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2030 /* We can use tp as the base register. */
2031 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2032 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2033 insn
|= X_TP
<< OP_SH_RS1
;
2034 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2037 r
= bfd_reloc_overflow
;
2040 case R_RISCV_GPREL_I
:
2041 case R_RISCV_GPREL_S
:
2043 bfd_vma gp
= riscv_global_pointer_value (info
);
2044 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2045 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2047 /* We can use x0 or gp as the base register. */
2048 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2049 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2052 rel
->r_addend
-= gp
;
2053 insn
|= X_GP
<< OP_SH_RS1
;
2055 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2058 r
= bfd_reloc_overflow
;
2062 case R_RISCV_PCREL_HI20
:
2063 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2070 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2071 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2073 r
= bfd_reloc_notsupported
;
2074 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2075 relocation
+ rel
->r_addend
,
2077 r
= bfd_reloc_overflow
;
2080 case R_RISCV_PCREL_LO12_I
:
2081 case R_RISCV_PCREL_LO12_S
:
2082 /* We don't allow section symbols plus addends as the auipc address,
2083 because then riscv_relax_delete_bytes would have to search through
2084 all relocs to update these addends. This is also ambiguous, as
2085 we do allow offsets to be added to the target address, which are
2086 not to be used to find the auipc address. */
2087 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2088 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2091 r
= bfd_reloc_dangerous
;
2095 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2096 howto
, rel
, relocation
, name
,
2099 r
= bfd_reloc_overflow
;
2102 case R_RISCV_TLS_DTPREL32
:
2103 case R_RISCV_TLS_DTPREL64
:
2104 relocation
= dtpoff (info
, relocation
);
2109 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2112 if ((bfd_link_pic (info
)
2114 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2115 && !resolved_to_zero
)
2116 || h
->root
.type
!= bfd_link_hash_undefweak
)
2117 && (! howto
->pc_relative
2118 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2119 || (!bfd_link_pic (info
)
2125 || h
->root
.type
== bfd_link_hash_undefweak
2126 || h
->root
.type
== bfd_link_hash_undefined
)))
2128 Elf_Internal_Rela outrel
;
2129 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2131 /* When generating a shared object, these relocations
2132 are copied into the output file to be resolved at run
2136 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2138 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2139 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2140 outrel
.r_offset
+= sec_addr (input_section
);
2142 if (skip_dynamic_relocation
)
2143 memset (&outrel
, 0, sizeof outrel
);
2144 else if (h
!= NULL
&& h
->dynindx
!= -1
2145 && !(bfd_link_pic (info
)
2146 && SYMBOLIC_BIND (info
, h
)
2149 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2150 outrel
.r_addend
= rel
->r_addend
;
2154 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2155 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2158 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2159 if (skip_static_relocation
)
2164 case R_RISCV_TLS_GOT_HI20
:
2168 case R_RISCV_TLS_GD_HI20
:
2171 off
= h
->got
.offset
;
2176 off
= local_got_offsets
[r_symndx
];
2177 local_got_offsets
[r_symndx
] |= 1;
2180 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2181 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2182 /* If this symbol is referenced by both GD and IE TLS, the IE
2183 reference's GOT slot follows the GD reference's slots. */
2185 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2186 ie_off
= 2 * GOT_ENTRY_SIZE
;
2192 Elf_Internal_Rela outrel
;
2194 bfd_boolean need_relocs
= FALSE
;
2196 if (htab
->elf
.srelgot
== NULL
)
2201 bfd_boolean dyn
, pic
;
2202 dyn
= htab
->elf
.dynamic_sections_created
;
2203 pic
= bfd_link_pic (info
);
2205 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2206 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2210 /* The GOT entries have not been initialized yet. Do it
2211 now, and emit any relocations. */
2212 if ((bfd_link_pic (info
) || indx
!= 0)
2214 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2215 || h
->root
.type
!= bfd_link_hash_undefweak
))
2218 if (tls_type
& GOT_TLS_GD
)
2222 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2223 outrel
.r_addend
= 0;
2224 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2225 bfd_put_NN (output_bfd
, 0,
2226 htab
->elf
.sgot
->contents
+ off
);
2227 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2230 BFD_ASSERT (! unresolved_reloc
);
2231 bfd_put_NN (output_bfd
,
2232 dtpoff (info
, relocation
),
2233 (htab
->elf
.sgot
->contents
+ off
+
2234 RISCV_ELF_WORD_BYTES
));
2238 bfd_put_NN (output_bfd
, 0,
2239 (htab
->elf
.sgot
->contents
+ off
+
2240 RISCV_ELF_WORD_BYTES
));
2241 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2242 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2243 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2248 /* If we are not emitting relocations for a
2249 general dynamic reference, then we must be in a
2250 static link or an executable link with the
2251 symbol binding locally. Mark it as belonging
2252 to module 1, the executable. */
2253 bfd_put_NN (output_bfd
, 1,
2254 htab
->elf
.sgot
->contents
+ off
);
2255 bfd_put_NN (output_bfd
,
2256 dtpoff (info
, relocation
),
2257 (htab
->elf
.sgot
->contents
+ off
+
2258 RISCV_ELF_WORD_BYTES
));
2262 if (tls_type
& GOT_TLS_IE
)
2266 bfd_put_NN (output_bfd
, 0,
2267 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2268 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2270 outrel
.r_addend
= 0;
2272 outrel
.r_addend
= tpoff (info
, relocation
);
2273 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2274 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2278 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2279 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2284 BFD_ASSERT (off
< (bfd_vma
) -2);
2285 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2286 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2288 r
= bfd_reloc_overflow
;
2289 unresolved_reloc
= FALSE
;
2293 r
= bfd_reloc_notsupported
;
2296 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2297 because such sections are not SEC_ALLOC and thus ld.so will
2298 not process them. */
2299 if (unresolved_reloc
2300 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2302 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2303 rel
->r_offset
) != (bfd_vma
) -1)
2305 (*_bfd_error_handler
)
2306 (_("%pB(%pA+%#" PRIx64
"): "
2307 "unresolvable %s relocation against symbol `%s'"),
2310 (uint64_t) rel
->r_offset
,
2312 h
->root
.root
.string
);
2314 bfd_set_error (bfd_error_bad_value
);
2319 if (r
== bfd_reloc_ok
)
2320 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2321 input_bfd
, contents
);
2328 case bfd_reloc_overflow
:
2329 info
->callbacks
->reloc_overflow
2330 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2331 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2334 case bfd_reloc_undefined
:
2335 info
->callbacks
->undefined_symbol
2336 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2340 case bfd_reloc_outofrange
:
2341 msg
= _("%X%P: internal error: out of range error\n");
2344 case bfd_reloc_notsupported
:
2345 msg
= _("%X%P: internal error: unsupported relocation error\n");
2348 case bfd_reloc_dangerous
:
2349 info
->callbacks
->reloc_dangerous
2350 (info
, "%pcrel_lo section symbol with an addend", input_bfd
,
2351 input_section
, rel
->r_offset
);
2355 msg
= _("%X%P: internal error: unknown error\n");
2360 info
->callbacks
->einfo (msg
);
2362 /* We already reported the error via a callback, so don't try to report
2363 it again by returning false. That leads to spurious errors. */
2368 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2370 riscv_free_pcrel_relocs (&pcrel_relocs
);
2374 /* Finish up dynamic symbol handling. We set the contents of various
2375 dynamic sections here. */
2378 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2379 struct bfd_link_info
*info
,
2380 struct elf_link_hash_entry
*h
,
2381 Elf_Internal_Sym
*sym
)
2383 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2384 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2386 if (h
->plt
.offset
!= (bfd_vma
) -1)
2388 /* We've decided to create a PLT entry for this symbol. */
2390 bfd_vma i
, header_address
, plt_idx
, got_address
;
2391 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2392 Elf_Internal_Rela rela
;
2394 BFD_ASSERT (h
->dynindx
!= -1);
2396 /* Calculate the address of the PLT header. */
2397 header_address
= sec_addr (htab
->elf
.splt
);
2399 /* Calculate the index of the entry. */
2400 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2402 /* Calculate the address of the .got.plt entry. */
2403 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2405 /* Find out where the .plt entry should go. */
2406 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2408 /* Fill in the PLT entry itself. */
2409 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2410 header_address
+ h
->plt
.offset
,
2414 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2415 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2417 /* Fill in the initial value of the .got.plt entry. */
2418 loc
= htab
->elf
.sgotplt
->contents
2419 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2420 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2422 /* Fill in the entry in the .rela.plt section. */
2423 rela
.r_offset
= got_address
;
2425 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2427 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2428 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2430 if (!h
->def_regular
)
2432 /* Mark the symbol as undefined, rather than as defined in
2433 the .plt section. Leave the value alone. */
2434 sym
->st_shndx
= SHN_UNDEF
;
2435 /* If the symbol is weak, we do need to clear the value.
2436 Otherwise, the PLT entry would provide a definition for
2437 the symbol even if the symbol wasn't defined anywhere,
2438 and so the symbol would never be NULL. */
2439 if (!h
->ref_regular_nonweak
)
2444 if (h
->got
.offset
!= (bfd_vma
) -1
2445 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2446 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2450 Elf_Internal_Rela rela
;
2452 /* This symbol has an entry in the GOT. Set it up. */
2454 sgot
= htab
->elf
.sgot
;
2455 srela
= htab
->elf
.srelgot
;
2456 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2458 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2460 /* If this is a local symbol reference, we just want to emit a RELATIVE
2461 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2462 the symbol was forced to be local because of a version file.
2463 The entry in the global offset table will already have been
2464 initialized in the relocate_section function. */
2465 if (bfd_link_pic (info
)
2466 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2468 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2469 asection
*sec
= h
->root
.u
.def
.section
;
2470 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2471 rela
.r_addend
= (h
->root
.u
.def
.value
2472 + sec
->output_section
->vma
2473 + sec
->output_offset
);
2477 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2478 BFD_ASSERT (h
->dynindx
!= -1);
2479 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2483 bfd_put_NN (output_bfd
, 0,
2484 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2485 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2490 Elf_Internal_Rela rela
;
2493 /* This symbols needs a copy reloc. Set it up. */
2494 BFD_ASSERT (h
->dynindx
!= -1);
2496 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2497 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2499 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2500 s
= htab
->elf
.sreldynrelro
;
2502 s
= htab
->elf
.srelbss
;
2503 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2506 /* Mark some specially defined symbols as absolute. */
2507 if (h
== htab
->elf
.hdynamic
2508 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2509 sym
->st_shndx
= SHN_ABS
;
2514 /* Finish up the dynamic sections. */
2517 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2518 bfd
*dynobj
, asection
*sdyn
)
2520 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2521 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2522 size_t dynsize
= bed
->s
->sizeof_dyn
;
2523 bfd_byte
*dyncon
, *dynconend
;
2525 dynconend
= sdyn
->contents
+ sdyn
->size
;
2526 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2528 Elf_Internal_Dyn dyn
;
2531 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2536 s
= htab
->elf
.sgotplt
;
2537 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2540 s
= htab
->elf
.srelplt
;
2541 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2544 s
= htab
->elf
.srelplt
;
2545 dyn
.d_un
.d_val
= s
->size
;
2551 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2557 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2558 struct bfd_link_info
*info
)
2562 struct riscv_elf_link_hash_table
*htab
;
2564 htab
= riscv_elf_hash_table (info
);
2565 BFD_ASSERT (htab
!= NULL
);
2566 dynobj
= htab
->elf
.dynobj
;
2568 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2570 if (elf_hash_table (info
)->dynamic_sections_created
)
2575 splt
= htab
->elf
.splt
;
2576 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2578 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2583 /* Fill in the head and tail entries in the procedure linkage table. */
2587 uint32_t plt_header
[PLT_HEADER_INSNS
];
2588 ret
= riscv_make_plt_header (output_bfd
,
2589 sec_addr (htab
->elf
.sgotplt
),
2590 sec_addr (splt
), plt_header
);
2594 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2595 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2597 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2602 if (htab
->elf
.sgotplt
)
2604 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2606 if (bfd_is_abs_section (output_section
))
2608 (*_bfd_error_handler
)
2609 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2613 if (htab
->elf
.sgotplt
->size
> 0)
2615 /* Write the first two entries in .got.plt, needed for the dynamic
2617 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2618 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2619 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2622 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2627 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2629 if (htab
->elf
.sgot
->size
> 0)
2631 /* Set the first entry in the global offset table to the address of
2632 the dynamic section. */
2633 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2634 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2637 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2643 /* Return address for Ith PLT stub in section PLT, for relocation REL
2644 or (bfd_vma) -1 if it should not be included. */
2647 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2648 const arelent
*rel ATTRIBUTE_UNUSED
)
2650 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2653 static enum elf_reloc_type_class
2654 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2655 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2656 const Elf_Internal_Rela
*rela
)
2658 switch (ELFNN_R_TYPE (rela
->r_info
))
2660 case R_RISCV_RELATIVE
:
2661 return reloc_class_relative
;
2662 case R_RISCV_JUMP_SLOT
:
2663 return reloc_class_plt
;
2665 return reloc_class_copy
;
2667 return reloc_class_normal
;
2671 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2675 riscv_float_abi_string (flagword flags
)
2677 switch (flags
& EF_RISCV_FLOAT_ABI
)
2679 case EF_RISCV_FLOAT_ABI_SOFT
:
2680 return "soft-float";
2682 case EF_RISCV_FLOAT_ABI_SINGLE
:
2683 return "single-float";
2685 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2686 return "double-float";
2688 case EF_RISCV_FLOAT_ABI_QUAD
:
2689 return "quad-float";
2696 /* The information of architecture attribute. */
2697 static riscv_subset_list_t in_subsets
;
2698 static riscv_subset_list_t out_subsets
;
2699 static riscv_subset_list_t merged_subsets
;
2701 /* Predicator for standard extension. */
2704 riscv_std_ext_p (const char *name
)
2706 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2709 /* Predicator for non-standard extension. */
2712 riscv_non_std_ext_p (const char *name
)
2714 return (strlen (name
) >= 2) && (name
[0] == 'x');
2717 /* Predicator for standard supervisor extension. */
2720 riscv_std_sv_ext_p (const char *name
)
2722 return (strlen (name
) >= 2) && (name
[0] == 's') && (name
[1] != 'x');
2725 /* Predicator for non-standard supervisor extension. */
2728 riscv_non_std_sv_ext_p (const char *name
)
2730 return (strlen (name
) >= 3) && (name
[0] == 's') && (name
[1] == 'x');
2733 /* Error handler when version mis-match. */
2736 riscv_version_mismatch (bfd
*ibfd
,
2737 struct riscv_subset_t
*in
,
2738 struct riscv_subset_t
*out
)
2741 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2744 in
->major_version
, in
->minor_version
,
2745 out
->major_version
, out
->minor_version
);
2748 /* Return true if subset is 'i' or 'e'. */
2751 riscv_i_or_e_p (bfd
*ibfd
,
2753 struct riscv_subset_t
*subset
)
2755 if ((strcasecmp (subset
->name
, "e") != 0)
2756 && (strcasecmp (subset
->name
, "i") != 0))
2759 (_("error: %pB: corrupted ISA string '%s'. "
2760 "First letter should be 'i' or 'e' but got '%s'."),
2761 ibfd
, arch
, subset
->name
);
2767 /* Merge standard extensions.
2770 Return FALSE if failed to merge.
2774 `in_arch`: Raw arch string for input object.
2775 `out_arch`: Raw arch string for output object.
2776 `pin`: subset list for input object, and it'll skip all merged subset after
2778 `pout`: Like `pin`, but for output object. */
2781 riscv_merge_std_ext (bfd
*ibfd
,
2782 const char *in_arch
,
2783 const char *out_arch
,
2784 struct riscv_subset_t
**pin
,
2785 struct riscv_subset_t
**pout
)
2787 const char *standard_exts
= riscv_supported_std_ext ();
2789 struct riscv_subset_t
*in
= *pin
;
2790 struct riscv_subset_t
*out
= *pout
;
2792 /* First letter should be 'i' or 'e'. */
2793 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2796 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2799 if (in
->name
[0] != out
->name
[0])
2801 /* TODO: We might allow merge 'i' with 'e'. */
2803 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2804 ibfd
, in
->name
, out
->name
);
2807 else if ((in
->major_version
!= out
->major_version
) ||
2808 (in
->minor_version
!= out
->minor_version
))
2810 /* TODO: Allow different merge policy. */
2811 riscv_version_mismatch (ibfd
, in
, out
);
2815 riscv_add_subset (&merged_subsets
,
2816 in
->name
, in
->major_version
, in
->minor_version
);
2821 /* Handle standard extension first. */
2822 for (p
= standard_exts
; *p
; ++p
)
2824 char find_ext
[2] = {*p
, '\0'};
2825 struct riscv_subset_t
*find_in
=
2826 riscv_lookup_subset (&in_subsets
, find_ext
);
2827 struct riscv_subset_t
*find_out
=
2828 riscv_lookup_subset (&out_subsets
, find_ext
);
2830 if (find_in
== NULL
&& find_out
== NULL
)
2833 /* Check version is same or not. */
2834 /* TODO: Allow different merge policy. */
2835 if ((find_in
!= NULL
&& find_out
!= NULL
)
2836 && ((find_in
->major_version
!= find_out
->major_version
)
2837 || (find_in
->minor_version
!= find_out
->minor_version
)))
2839 riscv_version_mismatch (ibfd
, in
, out
);
2843 struct riscv_subset_t
*merged
= find_in
? find_in
: find_out
;
2844 riscv_add_subset (&merged_subsets
, merged
->name
,
2845 merged
->major_version
, merged
->minor_version
);
2848 /* Skip all standard extensions. */
2849 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2850 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2858 /* Merge non-standard and supervisor extensions.
2860 Return FALSE if failed to merge.
2864 `in_arch`: Raw arch string for input object.
2865 `out_arch`: Raw arch string for output object.
2866 `pin`: subset list for input object, and it'll skip all merged subset after
2868 `pout`: Like `pin`, but for output object. */
2871 riscv_merge_non_std_and_sv_ext (bfd
*ibfd
,
2872 riscv_subset_t
**pin
,
2873 riscv_subset_t
**pout
,
2874 bfd_boolean (*predicate_func
) (const char *))
2876 riscv_subset_t
*in
= *pin
;
2877 riscv_subset_t
*out
= *pout
;
2879 for (in
= *pin
; in
!= NULL
&& predicate_func (in
->name
); in
= in
->next
)
2880 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2883 for (out
= *pout
; out
!= NULL
&& predicate_func (out
->name
); out
= out
->next
)
2885 riscv_subset_t
*find_ext
=
2886 riscv_lookup_subset (&merged_subsets
, out
->name
);
2887 if (find_ext
!= NULL
)
2889 /* Check version is same or not. */
2890 /* TODO: Allow different merge policy. */
2891 if ((find_ext
->major_version
!= out
->major_version
)
2892 || (find_ext
->minor_version
!= out
->minor_version
))
2894 riscv_version_mismatch (ibfd
, find_ext
, out
);
2899 riscv_add_subset (&merged_subsets
, out
->name
,
2900 out
->major_version
, out
->minor_version
);
2908 /* Merge Tag_RISCV_arch attribute. */
2911 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2913 riscv_subset_t
*in
, *out
;
2914 char *merged_arch_str
;
2916 unsigned xlen_in
, xlen_out
;
2917 merged_subsets
.head
= NULL
;
2918 merged_subsets
.tail
= NULL
;
2920 riscv_parse_subset_t rpe_in
;
2921 riscv_parse_subset_t rpe_out
;
2923 rpe_in
.subset_list
= &in_subsets
;
2924 rpe_in
.error_handler
= _bfd_error_handler
;
2925 rpe_in
.xlen
= &xlen_in
;
2927 rpe_out
.subset_list
= &out_subsets
;
2928 rpe_out
.error_handler
= _bfd_error_handler
;
2929 rpe_out
.xlen
= &xlen_out
;
2931 if (in_arch
== NULL
&& out_arch
== NULL
)
2934 if (in_arch
== NULL
&& out_arch
!= NULL
)
2937 if (in_arch
!= NULL
&& out_arch
== NULL
)
2940 /* Parse subset from arch string. */
2941 if (!riscv_parse_subset (&rpe_in
, in_arch
))
2944 if (!riscv_parse_subset (&rpe_out
, out_arch
))
2947 /* Checking XLEN. */
2948 if (xlen_out
!= xlen_in
)
2951 (_("error: %pB: ISA string of input (%s) doesn't match "
2952 "output (%s)."), ibfd
, in_arch
, out_arch
);
2956 /* Merge subset list. */
2957 in
= in_subsets
.head
;
2958 out
= out_subsets
.head
;
2960 /* Merge standard extension. */
2961 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
2963 /* Merge non-standard extension. */
2964 if (!riscv_merge_non_std_and_sv_ext (ibfd
, &in
, &out
, riscv_non_std_ext_p
))
2966 /* Merge standard supervisor extension. */
2967 if (!riscv_merge_non_std_and_sv_ext (ibfd
, &in
, &out
, riscv_std_sv_ext_p
))
2969 /* Merge non-standard supervisor extension. */
2970 if (!riscv_merge_non_std_and_sv_ext (ibfd
, &in
, &out
, riscv_non_std_sv_ext_p
))
2973 if (xlen_in
!= xlen_out
)
2976 (_("error: %pB: XLEN of input (%u) doesn't match "
2977 "output (%u)."), ibfd
, xlen_in
, xlen_out
);
2981 if (xlen_in
!= ARCH_SIZE
)
2984 (_("error: %pB: Unsupported XLEN (%u), you might be "
2985 "using wrong emulation."), ibfd
, xlen_in
);
2989 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
2991 /* Release the subset lists. */
2992 riscv_release_subset_list (&in_subsets
);
2993 riscv_release_subset_list (&out_subsets
);
2994 riscv_release_subset_list (&merged_subsets
);
2996 return merged_arch_str
;
2999 /* Merge object attributes from IBFD into output_bfd of INFO.
3000 Raise an error if there are conflicting attributes. */
3003 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3005 bfd
*obfd
= info
->output_bfd
;
3006 obj_attribute
*in_attr
;
3007 obj_attribute
*out_attr
;
3008 bfd_boolean result
= TRUE
;
3009 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3012 /* Skip linker created files. */
3013 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3016 /* Skip any input that doesn't have an attribute section.
3017 This enables to link object files without attribute section with
3019 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3022 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3024 /* This is the first object. Copy the attributes. */
3025 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3027 out_attr
= elf_known_obj_attributes_proc (obfd
);
3029 /* Use the Tag_null value to indicate the attributes have been
3036 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3037 out_attr
= elf_known_obj_attributes_proc (obfd
);
3039 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3043 case Tag_RISCV_arch
:
3044 if (!out_attr
[Tag_RISCV_arch
].s
)
3045 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3046 else if (in_attr
[Tag_RISCV_arch
].s
3047 && out_attr
[Tag_RISCV_arch
].s
)
3049 /* Check arch compatible. */
3051 riscv_merge_arch_attr_info (ibfd
,
3052 in_attr
[Tag_RISCV_arch
].s
,
3053 out_attr
[Tag_RISCV_arch
].s
);
3054 if (merged_arch
== NULL
)
3057 out_attr
[Tag_RISCV_arch
].s
= "";
3060 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3063 case Tag_RISCV_priv_spec
:
3064 case Tag_RISCV_priv_spec_minor
:
3065 case Tag_RISCV_priv_spec_revision
:
3066 if (out_attr
[i
].i
!= in_attr
[i
].i
)
3069 (_("error: %pB: conflicting priv spec version "
3070 "(major/minor/revision)."), ibfd
);
3074 case Tag_RISCV_unaligned_access
:
3075 out_attr
[i
].i
|= in_attr
[i
].i
;
3077 case Tag_RISCV_stack_align
:
3078 if (out_attr
[i
].i
== 0)
3079 out_attr
[i
].i
= in_attr
[i
].i
;
3080 else if (in_attr
[i
].i
!= 0
3081 && out_attr
[i
].i
!= 0
3082 && out_attr
[i
].i
!= in_attr
[i
].i
)
3085 (_("error: %pB use %u-byte stack aligned but the output "
3086 "use %u-byte stack aligned."),
3087 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3092 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3095 /* If out_attr was copied from in_attr then it won't have a type yet. */
3096 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3097 out_attr
[i
].type
= in_attr
[i
].type
;
3100 /* Merge Tag_compatibility attributes and any common GNU ones. */
3101 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3104 /* Check for any attributes not known on RISC-V. */
3105 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3110 /* Merge backend specific data from an object file to the output
3111 object file when linking. */
3114 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3116 bfd
*obfd
= info
->output_bfd
;
3117 flagword new_flags
, old_flags
;
3119 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3122 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3124 (*_bfd_error_handler
)
3125 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3126 " target emulation `%s' does not match `%s'"),
3127 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3131 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3134 if (!riscv_merge_attributes (ibfd
, info
))
3137 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3138 old_flags
= elf_elfheader (obfd
)->e_flags
;
3140 if (! elf_flags_init (obfd
))
3142 elf_flags_init (obfd
) = TRUE
;
3143 elf_elfheader (obfd
)->e_flags
= new_flags
;
3147 /* Check to see if the input BFD actually contains any sections. If not,
3148 its flags may not have been initialized either, but it cannot actually
3149 cause any incompatibility. Do not short-circuit dynamic objects; their
3150 section list may be emptied by elf_link_add_object_symbols.
3152 Also check to see if there are no code sections in the input. In this
3153 case, there is no need to check for code specific flags. */
3154 if (!(ibfd
->flags
& DYNAMIC
))
3156 bfd_boolean null_input_bfd
= TRUE
;
3157 bfd_boolean only_data_sections
= TRUE
;
3160 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3162 if ((bfd_section_flags (sec
)
3163 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3164 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3165 only_data_sections
= FALSE
;
3167 null_input_bfd
= FALSE
;
3171 if (null_input_bfd
|| only_data_sections
)
3175 /* Disallow linking different float ABIs. */
3176 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3178 (*_bfd_error_handler
)
3179 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3180 riscv_float_abi_string (new_flags
),
3181 riscv_float_abi_string (old_flags
));
3185 /* Disallow linking RVE and non-RVE. */
3186 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3188 (*_bfd_error_handler
)
3189 (_("%pB: can't link RVE with other target"), ibfd
);
3193 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3194 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3199 bfd_set_error (bfd_error_bad_value
);
3203 /* Delete some bytes from a section while relaxing. */
3206 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3207 struct bfd_link_info
*link_info
)
3209 unsigned int i
, symcount
;
3210 bfd_vma toaddr
= sec
->size
;
3211 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3212 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3213 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3214 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3215 bfd_byte
*contents
= data
->this_hdr
.contents
;
3217 /* Actually delete the bytes. */
3219 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3221 /* Adjust the location of all of the relocs. Note that we need not
3222 adjust the addends, since all PC-relative references must be against
3223 symbols, which we will adjust below. */
3224 for (i
= 0; i
< sec
->reloc_count
; i
++)
3225 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3226 data
->relocs
[i
].r_offset
-= count
;
3228 /* Adjust the local symbols defined in this section. */
3229 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3231 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3232 if (sym
->st_shndx
== sec_shndx
)
3234 /* If the symbol is in the range of memory we just moved, we
3235 have to adjust its value. */
3236 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3237 sym
->st_value
-= count
;
3239 /* If the symbol *spans* the bytes we just deleted (i.e. its
3240 *end* is in the moved bytes but its *start* isn't), then we
3241 must adjust its size.
3243 This test needs to use the original value of st_value, otherwise
3244 we might accidentally decrease size when deleting bytes right
3245 before the symbol. But since deleted relocs can't span across
3246 symbols, we can't have both a st_value and a st_size decrease,
3247 so it is simpler to just use an else. */
3248 else if (sym
->st_value
<= addr
3249 && sym
->st_value
+ sym
->st_size
> addr
3250 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3251 sym
->st_size
-= count
;
3255 /* Now adjust the global symbols defined in this section. */
3256 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3257 - symtab_hdr
->sh_info
);
3259 for (i
= 0; i
< symcount
; i
++)
3261 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3263 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3264 containing the definition of __wrap_SYMBOL, includes a direct
3265 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3266 the same symbol (which is __wrap_SYMBOL), but still exist as two
3267 different symbols in 'sym_hashes', we don't want to adjust
3268 the global symbol __wrap_SYMBOL twice. */
3269 /* The same problem occurs with symbols that are versioned_hidden, as
3270 foo becomes an alias for foo@BAR, and hence they need the same
3272 if (link_info
->wrap_hash
!= NULL
3273 || sym_hash
->versioned
== versioned_hidden
)
3275 struct elf_link_hash_entry
**cur_sym_hashes
;
3277 /* Loop only over the symbols which have already been checked. */
3278 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3281 /* If the current symbol is identical to 'sym_hash', that means
3282 the symbol was already adjusted (or at least checked). */
3283 if (*cur_sym_hashes
== sym_hash
)
3286 /* Don't adjust the symbol again. */
3287 if (cur_sym_hashes
< &sym_hashes
[i
])
3291 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3292 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3293 && sym_hash
->root
.u
.def
.section
== sec
)
3295 /* As above, adjust the value if needed. */
3296 if (sym_hash
->root
.u
.def
.value
> addr
3297 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3298 sym_hash
->root
.u
.def
.value
-= count
;
3300 /* As above, adjust the size if needed. */
3301 else if (sym_hash
->root
.u
.def
.value
<= addr
3302 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3303 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3304 sym_hash
->size
-= count
;
3311 /* A second format for recording PC-relative hi relocations. This stores the
3312 information required to relax them to GP-relative addresses. */
3314 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3315 struct riscv_pcgp_hi_reloc
3322 bfd_boolean undefined_weak
;
3323 riscv_pcgp_hi_reloc
*next
;
3326 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3327 struct riscv_pcgp_lo_reloc
3330 riscv_pcgp_lo_reloc
*next
;
3335 riscv_pcgp_hi_reloc
*hi
;
3336 riscv_pcgp_lo_reloc
*lo
;
3337 } riscv_pcgp_relocs
;
3339 /* Initialize the pcgp reloc info in P. */
3342 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3349 /* Free the pcgp reloc info in P. */
3352 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3353 bfd
*abfd ATTRIBUTE_UNUSED
,
3354 asection
*sec ATTRIBUTE_UNUSED
)
3356 riscv_pcgp_hi_reloc
*c
;
3357 riscv_pcgp_lo_reloc
*l
;
3359 for (c
= p
->hi
; c
!= NULL
;)
3361 riscv_pcgp_hi_reloc
*next
= c
->next
;
3366 for (l
= p
->lo
; l
!= NULL
;)
3368 riscv_pcgp_lo_reloc
*next
= l
->next
;
3374 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3375 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3376 relax the corresponding lo part reloc. */
3379 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3380 bfd_vma hi_addend
, bfd_vma hi_addr
,
3381 unsigned hi_sym
, asection
*sym_sec
,
3382 bfd_boolean undefined_weak
)
3384 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3387 new->hi_sec_off
= hi_sec_off
;
3388 new->hi_addend
= hi_addend
;
3389 new->hi_addr
= hi_addr
;
3390 new->hi_sym
= hi_sym
;
3391 new->sym_sec
= sym_sec
;
3392 new->undefined_weak
= undefined_weak
;
3398 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3399 This is used by a lo part reloc to find the corresponding hi part reloc. */
3401 static riscv_pcgp_hi_reloc
*
3402 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3404 riscv_pcgp_hi_reloc
*c
;
3406 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3407 if (c
->hi_sec_off
== hi_sec_off
)
3412 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3413 This is used to record relocs that can't be relaxed. */
3416 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3418 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3421 new->hi_sec_off
= hi_sec_off
;
3427 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3428 This is used by a hi part reloc to find the corresponding lo part reloc. */
3431 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3433 riscv_pcgp_lo_reloc
*c
;
3435 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3436 if (c
->hi_sec_off
== hi_sec_off
)
3441 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3442 struct bfd_link_info
*,
3443 Elf_Internal_Rela
*,
3444 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3445 riscv_pcgp_relocs
*,
3446 bfd_boolean undefined_weak
);
3448 /* Relax AUIPC + JALR into JAL. */
3451 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3452 struct bfd_link_info
*link_info
,
3453 Elf_Internal_Rela
*rel
,
3455 bfd_vma max_alignment
,
3456 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3458 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3459 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3461 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3462 bfd_signed_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3463 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3464 bfd_vma auipc
, jalr
;
3465 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3467 /* If the call crosses section boundaries, an alignment directive could
3468 cause the PC-relative offset to later increase. */
3469 if (VALID_UJTYPE_IMM (foff
) && sym_sec
->output_section
!= sec
->output_section
)
3470 foff
+= (foff
< 0 ? -max_alignment
: max_alignment
);
3472 /* See if this function call can be shortened. */
3473 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3476 /* Shorten the function call. */
3477 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3479 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3480 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3481 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3482 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3484 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3485 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3489 /* Relax to C.J[AL] rd, addr. */
3490 r_type
= R_RISCV_RVC_JUMP
;
3491 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3494 else if (VALID_UJTYPE_IMM (foff
))
3496 /* Relax to JAL rd, addr. */
3497 r_type
= R_RISCV_JAL
;
3498 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3500 else /* near_zero */
3502 /* Relax to JALR rd, x0, addr. */
3503 r_type
= R_RISCV_LO12_I
;
3504 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3507 /* Replace the R_RISCV_CALL reloc. */
3508 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3509 /* Replace the AUIPC. */
3510 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3512 /* Delete unnecessary JALR. */
3514 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3518 /* Traverse all output sections and return the max alignment. */
3521 _bfd_riscv_get_max_alignment (asection
*sec
)
3523 unsigned int max_alignment_power
= 0;
3526 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3528 if (o
->alignment_power
> max_alignment_power
)
3529 max_alignment_power
= o
->alignment_power
;
3532 return (bfd_vma
) 1 << max_alignment_power
;
3535 /* Relax non-PIC global variable references. */
3538 _bfd_riscv_relax_lui (bfd
*abfd
,
3541 struct bfd_link_info
*link_info
,
3542 Elf_Internal_Rela
*rel
,
3544 bfd_vma max_alignment
,
3545 bfd_vma reserve_size
,
3547 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3548 bfd_boolean undefined_weak
)
3550 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3551 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3552 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3554 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3558 /* If gp and the symbol are in the same output section, which is not the
3559 abs section, then consider only that output section's alignment. */
3560 struct bfd_link_hash_entry
*h
=
3561 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3563 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3564 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3565 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3568 /* Is the reference in range of x0 or gp?
3569 Valid gp range conservatively because of alignment issue. */
3571 || (VALID_ITYPE_IMM (symval
)
3573 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3575 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3577 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3578 switch (ELFNN_R_TYPE (rel
->r_info
))
3580 case R_RISCV_LO12_I
:
3583 /* Change the RS1 to zero. */
3584 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3585 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3586 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3589 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3592 case R_RISCV_LO12_S
:
3595 /* Change the RS1 to zero. */
3596 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3597 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3598 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3601 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3605 /* We can delete the unnecessary LUI and reloc. */
3606 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3608 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3616 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3617 account for this assuming page alignment at worst. In the presence of
3618 RELRO segment the linker aligns it by one page size, therefore sections
3619 after the segment can be moved more than one page. */
3622 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3623 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3624 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3625 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3626 : ELF_MAXPAGESIZE
)))
3628 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3629 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3630 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3631 if (rd
== 0 || rd
== X_SP
)
3634 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3635 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3637 /* Replace the R_RISCV_HI20 reloc. */
3638 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3641 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3648 /* Relax non-PIC TLS references. */
3651 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3653 asection
*sym_sec ATTRIBUTE_UNUSED
,
3654 struct bfd_link_info
*link_info
,
3655 Elf_Internal_Rela
*rel
,
3657 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3658 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3660 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3661 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3663 /* See if this symbol is in range of tp. */
3664 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3667 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3668 switch (ELFNN_R_TYPE (rel
->r_info
))
3670 case R_RISCV_TPREL_LO12_I
:
3671 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3674 case R_RISCV_TPREL_LO12_S
:
3675 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3678 case R_RISCV_TPREL_HI20
:
3679 case R_RISCV_TPREL_ADD
:
3680 /* We can delete the unnecessary instruction and reloc. */
3681 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3683 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3690 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3693 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3695 struct bfd_link_info
*link_info
,
3696 Elf_Internal_Rela
*rel
,
3698 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3699 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3700 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3701 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3702 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3704 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3705 bfd_vma alignment
= 1, pos
;
3706 while (alignment
<= rel
->r_addend
)
3709 symval
-= rel
->r_addend
;
3710 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3711 bfd_vma nop_bytes
= aligned_addr
- symval
;
3713 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3714 sec
->sec_flg0
= TRUE
;
3716 /* Make sure there are enough NOPs to actually achieve the alignment. */
3717 if (rel
->r_addend
< nop_bytes
)
3720 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3721 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3722 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3723 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3724 bfd_set_error (bfd_error_bad_value
);
3728 /* Delete the reloc. */
3729 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3731 /* If the number of NOPs is already correct, there's nothing to do. */
3732 if (nop_bytes
== rel
->r_addend
)
3735 /* Write as many RISC-V NOPs as we need. */
3736 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3737 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3739 /* Write a final RVC NOP if need be. */
3740 if (nop_bytes
% 4 != 0)
3741 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3743 /* Delete the excess bytes. */
3744 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3745 rel
->r_addend
- nop_bytes
, link_info
);
3748 /* Relax PC-relative references to GP-relative references. */
3751 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3754 struct bfd_link_info
*link_info
,
3755 Elf_Internal_Rela
*rel
,
3757 bfd_vma max_alignment
,
3758 bfd_vma reserve_size
,
3759 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3760 riscv_pcgp_relocs
*pcgp_relocs
,
3761 bfd_boolean undefined_weak
)
3763 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3764 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3766 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3768 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3769 * actual target address. */
3770 riscv_pcgp_hi_reloc hi_reloc
;
3771 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3772 switch (ELFNN_R_TYPE (rel
->r_info
))
3774 case R_RISCV_PCREL_LO12_I
:
3775 case R_RISCV_PCREL_LO12_S
:
3777 /* If the %lo has an addend, it isn't for the label pointing at the
3778 hi part instruction, but rather for the symbol pointed at by the
3779 hi part instruction. So we must subtract it here for the lookup.
3780 It is still used below in the final symbol address. */
3781 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3782 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3786 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3791 symval
= hi_reloc
.hi_addr
;
3792 sym_sec
= hi_reloc
.sym_sec
;
3794 /* We can not know whether the undefined weak symbol is referenced
3795 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3796 we have to record the 'undefined_weak' flag when handling the
3797 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3798 undefined_weak
= hi_reloc
.undefined_weak
;
3802 case R_RISCV_PCREL_HI20
:
3803 /* Mergeable symbols and code might later move out of range. */
3804 if (! undefined_weak
3805 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3808 /* If the cooresponding lo relocation has already been seen then it's not
3809 * safe to relax this relocation. */
3810 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3821 /* If gp and the symbol are in the same output section, which is not the
3822 abs section, then consider only that output section's alignment. */
3823 struct bfd_link_hash_entry
*h
=
3824 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3826 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3827 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3828 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3831 /* Is the reference in range of x0 or gp?
3832 Valid gp range conservatively because of alignment issue. */
3834 || (VALID_ITYPE_IMM (symval
)
3836 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3838 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3840 unsigned sym
= hi_reloc
.hi_sym
;
3841 switch (ELFNN_R_TYPE (rel
->r_info
))
3843 case R_RISCV_PCREL_LO12_I
:
3846 /* Change the RS1 to zero, and then modify the relocation
3847 type to R_RISCV_LO12_I. */
3848 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3849 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3850 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3851 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3852 rel
->r_addend
= hi_reloc
.hi_addend
;
3856 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3857 rel
->r_addend
+= hi_reloc
.hi_addend
;
3861 case R_RISCV_PCREL_LO12_S
:
3864 /* Change the RS1 to zero, and then modify the relocation
3865 type to R_RISCV_LO12_S. */
3866 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3867 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3868 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3869 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3870 rel
->r_addend
= hi_reloc
.hi_addend
;
3874 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3875 rel
->r_addend
+= hi_reloc
.hi_addend
;
3879 case R_RISCV_PCREL_HI20
:
3880 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3884 ELFNN_R_SYM(rel
->r_info
),
3887 /* We can delete the unnecessary AUIPC and reloc. */
3888 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3900 /* Relax PC-relative references to GP-relative references. */
3903 _bfd_riscv_relax_delete (bfd
*abfd
,
3905 asection
*sym_sec ATTRIBUTE_UNUSED
,
3906 struct bfd_link_info
*link_info
,
3907 Elf_Internal_Rela
*rel
,
3908 bfd_vma symval ATTRIBUTE_UNUSED
,
3909 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3910 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3911 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3912 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3913 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3915 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3918 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3922 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3923 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3924 disabled, handles code alignment directives. */
3927 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3928 struct bfd_link_info
*info
,
3931 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3932 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3933 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3934 Elf_Internal_Rela
*relocs
;
3935 bfd_boolean ret
= FALSE
;
3937 bfd_vma max_alignment
, reserve_size
= 0;
3938 riscv_pcgp_relocs pcgp_relocs
;
3942 if (bfd_link_relocatable (info
)
3944 || (sec
->flags
& SEC_RELOC
) == 0
3945 || sec
->reloc_count
== 0
3946 || (info
->disable_target_specific_optimizations
3947 && info
->relax_pass
== 0))
3950 riscv_init_pcgp_relocs (&pcgp_relocs
);
3952 /* Read this BFD's relocs if we haven't done so already. */
3954 relocs
= data
->relocs
;
3955 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3956 info
->keep_memory
)))
3961 max_alignment
= htab
->max_alignment
;
3962 if (max_alignment
== (bfd_vma
) -1)
3964 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3965 htab
->max_alignment
= max_alignment
;
3969 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3971 /* Examine and consider relaxing each reloc. */
3972 for (i
= 0; i
< sec
->reloc_count
; i
++)
3975 Elf_Internal_Rela
*rel
= relocs
+ i
;
3976 relax_func_t relax_func
;
3977 int type
= ELFNN_R_TYPE (rel
->r_info
);
3980 bfd_boolean undefined_weak
= FALSE
;
3983 if (info
->relax_pass
== 0)
3985 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
3986 relax_func
= _bfd_riscv_relax_call
;
3987 else if (type
== R_RISCV_HI20
3988 || type
== R_RISCV_LO12_I
3989 || type
== R_RISCV_LO12_S
)
3990 relax_func
= _bfd_riscv_relax_lui
;
3991 else if (!bfd_link_pic(info
)
3992 && (type
== R_RISCV_PCREL_HI20
3993 || type
== R_RISCV_PCREL_LO12_I
3994 || type
== R_RISCV_PCREL_LO12_S
))
3995 relax_func
= _bfd_riscv_relax_pc
;
3996 else if (type
== R_RISCV_TPREL_HI20
3997 || type
== R_RISCV_TPREL_ADD
3998 || type
== R_RISCV_TPREL_LO12_I
3999 || type
== R_RISCV_TPREL_LO12_S
)
4000 relax_func
= _bfd_riscv_relax_tls_le
;
4004 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4005 if (i
== sec
->reloc_count
- 1
4006 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4007 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4010 /* Skip over the R_RISCV_RELAX. */
4013 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4014 relax_func
= _bfd_riscv_relax_delete
;
4015 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4016 relax_func
= _bfd_riscv_relax_align
;
4020 data
->relocs
= relocs
;
4022 /* Read this BFD's contents if we haven't done so already. */
4023 if (!data
->this_hdr
.contents
4024 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4027 /* Read this BFD's symbols if we haven't done so already. */
4028 if (symtab_hdr
->sh_info
!= 0
4029 && !symtab_hdr
->contents
4030 && !(symtab_hdr
->contents
=
4031 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4032 symtab_hdr
->sh_info
,
4033 0, NULL
, NULL
, NULL
)))
4036 /* Get the value of the symbol referred to by the reloc. */
4037 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4039 /* A local symbol. */
4040 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4041 + ELFNN_R_SYM (rel
->r_info
));
4042 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4043 ? 0 : isym
->st_size
- rel
->r_addend
;
4045 if (isym
->st_shndx
== SHN_UNDEF
)
4046 sym_sec
= sec
, symval
= rel
->r_offset
;
4049 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4050 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4052 /* The purpose of this code is unknown. It breaks linker scripts
4053 for embedded development that place sections at address zero.
4054 This code is believed to be unnecessary. Disabling it but not
4055 yet removing it, in case something breaks. */
4056 if (sec_addr (sym_sec
) == 0)
4059 symval
= isym
->st_value
;
4061 symtype
= ELF_ST_TYPE (isym
->st_info
);
4066 struct elf_link_hash_entry
*h
;
4068 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4069 h
= elf_sym_hashes (abfd
)[indx
];
4071 while (h
->root
.type
== bfd_link_hash_indirect
4072 || h
->root
.type
== bfd_link_hash_warning
)
4073 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4075 if (h
->root
.type
== bfd_link_hash_undefweak
4076 && (relax_func
== _bfd_riscv_relax_lui
4077 || relax_func
== _bfd_riscv_relax_pc
))
4079 /* For the lui and auipc relaxations, since the symbol
4080 value of an undefined weak symbol is always be zero,
4081 we can optimize the patterns into a single LI/MV/ADDI
4084 Note that, creating shared libraries and pie output may
4085 break the rule above. Fortunately, since we do not relax
4086 pc relocs when creating shared libraries and pie output,
4087 and the absolute address access for R_RISCV_HI20 isn't
4088 allowed when "-fPIC" is set, the problem of creating shared
4089 libraries can not happen currently. Once we support the
4090 auipc relaxations when creating shared libraries, then we will
4091 need the more rigorous checking for this optimization. */
4092 undefined_weak
= TRUE
;
4095 if (h
->plt
.offset
!= MINUS_ONE
)
4097 sym_sec
= htab
->elf
.splt
;
4098 symval
= h
->plt
.offset
;
4100 else if (undefined_weak
)
4103 sym_sec
= bfd_und_section_ptr
;
4105 else if (h
->root
.u
.def
.section
->output_section
== NULL
4106 || (h
->root
.type
!= bfd_link_hash_defined
4107 && h
->root
.type
!= bfd_link_hash_defweak
))
4111 symval
= h
->root
.u
.def
.value
;
4112 sym_sec
= h
->root
.u
.def
.section
;
4115 if (h
->type
!= STT_FUNC
)
4117 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4121 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4122 && (sym_sec
->flags
& SEC_MERGE
))
4124 /* At this stage in linking, no SEC_MERGE symbol has been
4125 adjusted, so all references to such symbols need to be
4126 passed through _bfd_merged_section_offset. (Later, in
4127 relocate_section, all SEC_MERGE symbols *except* for
4128 section symbols have been adjusted.)
4130 gas may reduce relocations against symbols in SEC_MERGE
4131 sections to a relocation against the section symbol when
4132 the original addend was zero. When the reloc is against
4133 a section symbol we should include the addend in the
4134 offset passed to _bfd_merged_section_offset, since the
4135 location of interest is the original symbol. On the
4136 other hand, an access to "sym+addend" where "sym" is not
4137 a section symbol should not include the addend; Such an
4138 access is presumed to be an offset from "sym"; The
4139 location of interest is just "sym". */
4140 if (symtype
== STT_SECTION
)
4141 symval
+= rel
->r_addend
;
4143 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4144 elf_section_data (sym_sec
)->sec_info
,
4147 if (symtype
!= STT_SECTION
)
4148 symval
+= rel
->r_addend
;
4151 symval
+= rel
->r_addend
;
4153 symval
+= sec_addr (sym_sec
);
4155 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4156 max_alignment
, reserve_size
, again
,
4157 &pcgp_relocs
, undefined_weak
))
4164 if (relocs
!= data
->relocs
)
4166 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4172 # define PRSTATUS_SIZE 204
4173 # define PRSTATUS_OFFSET_PR_CURSIG 12
4174 # define PRSTATUS_OFFSET_PR_PID 24
4175 # define PRSTATUS_OFFSET_PR_REG 72
4176 # define ELF_GREGSET_T_SIZE 128
4177 # define PRPSINFO_SIZE 128
4178 # define PRPSINFO_OFFSET_PR_PID 16
4179 # define PRPSINFO_OFFSET_PR_FNAME 32
4180 # define PRPSINFO_OFFSET_PR_PSARGS 48
4182 # define PRSTATUS_SIZE 376
4183 # define PRSTATUS_OFFSET_PR_CURSIG 12
4184 # define PRSTATUS_OFFSET_PR_PID 32
4185 # define PRSTATUS_OFFSET_PR_REG 112
4186 # define ELF_GREGSET_T_SIZE 256
4187 # define PRPSINFO_SIZE 136
4188 # define PRPSINFO_OFFSET_PR_PID 24
4189 # define PRPSINFO_OFFSET_PR_FNAME 40
4190 # define PRPSINFO_OFFSET_PR_PSARGS 56
4193 /* Support for core dump NOTE sections. */
4196 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4198 switch (note
->descsz
)
4203 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4205 elf_tdata (abfd
)->core
->signal
4206 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4209 elf_tdata (abfd
)->core
->lwpid
4210 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4214 /* Make a ".reg/999" section. */
4215 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4216 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4220 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4222 switch (note
->descsz
)
4227 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4229 elf_tdata (abfd
)->core
->pid
4230 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4233 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4234 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4237 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4238 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4242 /* Note that for some reason, a spurious space is tacked
4243 onto the end of the args in some (at least one anyway)
4244 implementations, so strip it off if it exists. */
4247 char *command
= elf_tdata (abfd
)->core
->command
;
4248 int n
= strlen (command
);
4250 if (0 < n
&& command
[n
- 1] == ' ')
4251 command
[n
- 1] = '\0';
4257 /* Set the right mach type. */
4259 riscv_elf_object_p (bfd
*abfd
)
4261 /* There are only two mach types in RISCV currently. */
4262 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4263 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4265 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4270 /* Determine whether an object attribute tag takes an integer, a
4274 riscv_elf_obj_attrs_arg_type (int tag
)
4276 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4279 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4280 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4282 #define elf_backend_reloc_type_class riscv_reloc_type_class
4284 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4285 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4286 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4287 #define bfd_elfNN_bfd_merge_private_bfd_data \
4288 _bfd_riscv_elf_merge_private_bfd_data
4290 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4291 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4292 #define elf_backend_check_relocs riscv_elf_check_relocs
4293 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4294 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4295 #define elf_backend_relocate_section riscv_elf_relocate_section
4296 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4297 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4298 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4299 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4300 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4301 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4302 #define elf_backend_object_p riscv_elf_object_p
4303 #define elf_info_to_howto_rel NULL
4304 #define elf_info_to_howto riscv_info_to_howto_rela
4305 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4307 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4309 #define elf_backend_can_gc_sections 1
4310 #define elf_backend_can_refcount 1
4311 #define elf_backend_want_got_plt 1
4312 #define elf_backend_plt_readonly 1
4313 #define elf_backend_plt_alignment 4
4314 #define elf_backend_want_plt_sym 1
4315 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4316 #define elf_backend_want_dynrelro 1
4317 #define elf_backend_rela_normal 1
4318 #define elf_backend_default_execstack 0
4320 #undef elf_backend_obj_attrs_vendor
4321 #define elf_backend_obj_attrs_vendor "riscv"
4322 #undef elf_backend_obj_attrs_arg_type
4323 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4324 #undef elf_backend_obj_attrs_section_type
4325 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4326 #undef elf_backend_obj_attrs_section
4327 #define elf_backend_obj_attrs_section ".riscv.attributes"
4329 #include "elfNN-target.h"