1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
35 /* Internal relocations used exclusively by the relaxation pass. */
36 #define R_RISCV_DELETE (R_RISCV_max + 1)
40 #define MINUS_ONE ((bfd_vma)0 - 1)
42 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
44 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
46 /* The name of the dynamic interpreter. This is put in the .interp
49 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
50 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
52 #define ELF_ARCH bfd_arch_riscv
53 #define ELF_TARGET_ID RISCV_ELF_DATA
54 #define ELF_MACHINE_CODE EM_RISCV
55 #define ELF_MAXPAGESIZE 0x1000
56 #define ELF_COMMONPAGESIZE 0x1000
58 /* RISC-V ELF linker hash entry. */
60 struct riscv_elf_link_hash_entry
62 struct elf_link_hash_entry elf
;
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)
102 elfNN_riscv_mkobject (bfd
*abfd
)
104 return bfd_elf_allocate_object (abfd
,
105 sizeof (struct _bfd_riscv_elf_obj_tdata
),
109 #include "elf/common.h"
110 #include "elf/internal.h"
112 struct riscv_elf_link_hash_table
114 struct elf_link_hash_table elf
;
116 /* Short-cuts to get to dynamic linker sections. */
119 /* Small local sym to section mapping cache. */
120 struct sym_cache sym_cache
;
122 /* The max alignment of output sections. */
123 bfd_vma max_alignment
;
127 /* Get the RISC-V ELF linker hash table from a link_info structure. */
128 #define riscv_elf_hash_table(p) \
129 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
130 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
133 riscv_info_to_howto_rela (bfd
*abfd
,
135 Elf_Internal_Rela
*dst
)
137 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
138 return cache_ptr
->howto
!= NULL
;
142 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
144 const struct elf_backend_data
*bed
;
147 bed
= get_elf_backend_data (abfd
);
148 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
149 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
154 #define PLT_HEADER_INSNS 8
155 #define PLT_ENTRY_INSNS 4
156 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
157 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
159 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
161 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
163 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
166 riscv_elf_got_plt_val (bfd_vma plt_index
, struct bfd_link_info
*info
)
168 return sec_addr (riscv_elf_hash_table (info
)->elf
.sgotplt
)
169 + GOTPLT_HEADER_SIZE
+ (plt_index
* GOT_ENTRY_SIZE
);
173 # define MATCH_LREG MATCH_LW
175 # define MATCH_LREG MATCH_LD
178 /* Generate a PLT header. */
181 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
184 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
185 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
187 /* RVE has no t3 register, so this won't work, and is not supported. */
188 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
190 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
195 /* auipc t2, %hi(.got.plt)
196 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
197 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
198 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
199 addi t0, t2, %lo(.got.plt) # &.got.plt
200 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
201 l[w|d] t0, PTRSIZE(t0) # link map
204 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
205 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
206 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
207 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, -(PLT_HEADER_SIZE
+ 12));
208 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
209 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
210 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
211 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
216 /* Generate a PLT entry. */
219 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
222 /* RVE has no t3 register, so this won't work, and is not supported. */
223 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
225 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
230 /* auipc t3, %hi(.got.plt entry)
231 l[w|d] t3, %lo(.got.plt entry)(t3)
235 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
236 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
237 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
238 entry
[3] = RISCV_NOP
;
243 /* Create an entry in an RISC-V ELF linker hash table. */
245 static struct bfd_hash_entry
*
246 link_hash_newfunc (struct bfd_hash_entry
*entry
,
247 struct bfd_hash_table
*table
, const char *string
)
249 /* Allocate the structure if it has not already been allocated by a
254 bfd_hash_allocate (table
,
255 sizeof (struct riscv_elf_link_hash_entry
));
260 /* Call the allocation method of the superclass. */
261 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
264 struct riscv_elf_link_hash_entry
*eh
;
266 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
267 eh
->dyn_relocs
= NULL
;
268 eh
->tls_type
= GOT_UNKNOWN
;
274 /* Create a RISC-V ELF linker hash table. */
276 static struct bfd_link_hash_table
*
277 riscv_elf_link_hash_table_create (bfd
*abfd
)
279 struct riscv_elf_link_hash_table
*ret
;
280 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
282 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
286 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
287 sizeof (struct riscv_elf_link_hash_entry
),
294 ret
->max_alignment
= (bfd_vma
) -1;
295 return &ret
->elf
.root
;
298 /* Create the .got section. */
301 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
305 struct elf_link_hash_entry
*h
;
306 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
307 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
309 /* This function may be called more than once. */
310 if (htab
->sgot
!= NULL
)
313 flags
= bed
->dynamic_sec_flags
;
315 s
= bfd_make_section_anyway_with_flags (abfd
,
316 (bed
->rela_plts_and_copies_p
317 ? ".rela.got" : ".rel.got"),
318 (bed
->dynamic_sec_flags
321 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
327 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
331 /* The first bit of the global offset table is the header. */
332 s
->size
+= bed
->got_header_size
;
334 if (bed
->want_got_plt
)
336 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
338 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
342 /* Reserve room for the header. */
343 s
->size
+= GOTPLT_HEADER_SIZE
;
346 if (bed
->want_got_sym
)
348 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
349 section. We don't do this in the linker script because we don't want
350 to define the symbol if we are not creating a global offset
352 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
353 "_GLOBAL_OFFSET_TABLE_");
354 elf_hash_table (info
)->hgot
= h
;
362 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
363 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
367 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
368 struct bfd_link_info
*info
)
370 struct riscv_elf_link_hash_table
*htab
;
372 htab
= riscv_elf_hash_table (info
);
373 BFD_ASSERT (htab
!= NULL
);
375 if (!riscv_elf_create_got_section (dynobj
, info
))
378 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
381 if (!bfd_link_pic (info
))
383 /* Technically, this section doesn't have contents. It is used as the
384 target of TLS copy relocs, to copy TLS data from shared libraries into
385 the executable. However, if we don't mark it as loadable, then it
386 matches the IS_TBSS test in ldlang.c, and there is no run-time address
387 space allocated for it even though it has SEC_ALLOC. That test is
388 correct for .tbss, but not correct for this section. There is also
389 a second problem that having a section with no contents can only work
390 if it comes after all sections with contents in the same segment,
391 but the linker script does not guarantee that. This is just mixed in
392 with other .tdata.* sections. We can fix both problems by lying and
393 saying that there are contents. This section is expected to be small
394 so this should not cause a significant extra program startup cost. */
396 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
397 (SEC_ALLOC
| SEC_THREAD_LOCAL
398 | SEC_LOAD
| SEC_DATA
400 | SEC_LINKER_CREATED
));
403 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
404 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
410 /* Copy the extra info we tack onto an elf_link_hash_entry. */
413 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
414 struct elf_link_hash_entry
*dir
,
415 struct elf_link_hash_entry
*ind
)
417 struct riscv_elf_link_hash_entry
*edir
, *eind
;
419 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
420 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
422 if (eind
->dyn_relocs
!= NULL
)
424 if (edir
->dyn_relocs
!= NULL
)
426 struct elf_dyn_relocs
**pp
;
427 struct elf_dyn_relocs
*p
;
429 /* Add reloc counts against the indirect sym to the direct sym
430 list. Merge any entries against the same section. */
431 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
433 struct elf_dyn_relocs
*q
;
435 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
436 if (q
->sec
== p
->sec
)
438 q
->pc_count
+= p
->pc_count
;
439 q
->count
+= p
->count
;
446 *pp
= edir
->dyn_relocs
;
449 edir
->dyn_relocs
= eind
->dyn_relocs
;
450 eind
->dyn_relocs
= NULL
;
453 if (ind
->root
.type
== bfd_link_hash_indirect
454 && dir
->got
.refcount
<= 0)
456 edir
->tls_type
= eind
->tls_type
;
457 eind
->tls_type
= GOT_UNKNOWN
;
459 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
463 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
464 unsigned long symndx
, char tls_type
)
466 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
468 *new_tls_type
|= tls_type
;
469 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
471 (*_bfd_error_handler
)
472 (_("%pB: `%s' accessed both as normal and thread local symbol"),
473 abfd
, h
? h
->root
.root
.string
: "<local>");
480 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
481 struct elf_link_hash_entry
*h
, long symndx
)
483 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
484 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
486 if (htab
->elf
.sgot
== NULL
)
488 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
494 h
->got
.refcount
+= 1;
498 /* This is a global offset table entry for a local symbol. */
499 if (elf_local_got_refcounts (abfd
) == NULL
)
501 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
502 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
504 _bfd_riscv_elf_local_got_tls_type (abfd
)
505 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
507 elf_local_got_refcounts (abfd
) [symndx
] += 1;
513 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
515 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
517 (*_bfd_error_handler
)
518 (_("%pB: relocation %s against `%s' can not be used when making a shared "
519 "object; recompile with -fPIC"),
520 abfd
, r
? r
->name
: _("<unknown>"),
521 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
522 bfd_set_error (bfd_error_bad_value
);
525 /* Look through the relocs for a section during the first phase, and
526 allocate space in the global offset table or procedure linkage
530 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
531 asection
*sec
, const Elf_Internal_Rela
*relocs
)
533 struct riscv_elf_link_hash_table
*htab
;
534 Elf_Internal_Shdr
*symtab_hdr
;
535 struct elf_link_hash_entry
**sym_hashes
;
536 const Elf_Internal_Rela
*rel
;
537 asection
*sreloc
= NULL
;
539 if (bfd_link_relocatable (info
))
542 htab
= riscv_elf_hash_table (info
);
543 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
544 sym_hashes
= elf_sym_hashes (abfd
);
546 if (htab
->elf
.dynobj
== NULL
)
547 htab
->elf
.dynobj
= abfd
;
549 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
552 unsigned int r_symndx
;
553 struct elf_link_hash_entry
*h
;
555 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
556 r_type
= ELFNN_R_TYPE (rel
->r_info
);
558 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
560 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
565 if (r_symndx
< symtab_hdr
->sh_info
)
569 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
570 while (h
->root
.type
== bfd_link_hash_indirect
571 || h
->root
.type
== bfd_link_hash_warning
)
572 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
577 case R_RISCV_TLS_GD_HI20
:
578 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
579 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
583 case R_RISCV_TLS_GOT_HI20
:
584 if (bfd_link_pic (info
))
585 info
->flags
|= DF_STATIC_TLS
;
586 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
587 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
591 case R_RISCV_GOT_HI20
:
592 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
593 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
597 case R_RISCV_CALL_PLT
:
598 /* This symbol requires a procedure linkage table entry. We
599 actually build the entry in adjust_dynamic_symbol,
600 because this might be a case of linking PIC code without
601 linking in any dynamic objects, in which case we don't
602 need to generate a procedure linkage table after all. */
607 h
->plt
.refcount
+= 1;
614 case R_RISCV_RVC_BRANCH
:
615 case R_RISCV_RVC_JUMP
:
616 case R_RISCV_PCREL_HI20
:
617 /* In shared libraries, these relocs are known to bind locally. */
618 if (bfd_link_pic (info
))
622 case R_RISCV_TPREL_HI20
:
623 if (!bfd_link_executable (info
))
624 return bad_static_reloc (abfd
, r_type
, h
);
626 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
630 if (bfd_link_pic (info
))
631 return bad_static_reloc (abfd
, r_type
, h
);
635 case R_RISCV_JUMP_SLOT
:
636 case R_RISCV_RELATIVE
:
642 /* This reloc might not bind locally. */
646 if (h
!= NULL
&& !bfd_link_pic (info
))
648 /* We may need a .plt entry if the function this reloc
649 refers to is in a shared lib. */
650 h
->plt
.refcount
+= 1;
653 /* If we are creating a shared library, and this is a reloc
654 against a global symbol, or a non PC relative reloc
655 against a local symbol, then we need to copy the reloc
656 into the shared library. However, if we are linking with
657 -Bsymbolic, we do not need to copy a reloc against a
658 global symbol which is defined in an object we are
659 including in the link (i.e., DEF_REGULAR is set). At
660 this point we have not seen all the input files, so it is
661 possible that DEF_REGULAR is not set now but will be set
662 later (it is never cleared). In case of a weak definition,
663 DEF_REGULAR may be cleared later by a strong definition in
664 a shared library. We account for that possibility below by
665 storing information in the relocs_copied field of the hash
666 table entry. A similar situation occurs when creating
667 shared libraries and symbol visibility changes render the
670 If on the other hand, we are creating an executable, we
671 may need to keep relocations for symbols satisfied by a
672 dynamic library if we manage to avoid copy relocs for the
674 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
676 if ((bfd_link_pic (info
)
677 && (sec
->flags
& SEC_ALLOC
) != 0
678 && ((r
!= NULL
&& ! r
->pc_relative
)
681 || h
->root
.type
== bfd_link_hash_defweak
682 || !h
->def_regular
))))
683 || (!bfd_link_pic (info
)
684 && (sec
->flags
& SEC_ALLOC
) != 0
686 && (h
->root
.type
== bfd_link_hash_defweak
687 || !h
->def_regular
)))
689 struct elf_dyn_relocs
*p
;
690 struct elf_dyn_relocs
**head
;
692 /* When creating a shared object, we must copy these
693 relocs into the output file. We create a reloc
694 section in dynobj and make room for the reloc. */
697 sreloc
= _bfd_elf_make_dynamic_reloc_section
698 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
699 abfd
, /*rela?*/ TRUE
);
705 /* If this is a global symbol, we count the number of
706 relocations we need for this symbol. */
708 head
= &((struct riscv_elf_link_hash_entry
*) h
)->dyn_relocs
;
711 /* Track dynamic relocs needed for local syms too.
712 We really need local syms available to do this
717 Elf_Internal_Sym
*isym
;
719 isym
= bfd_sym_from_r_symndx (&htab
->sym_cache
,
724 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
728 vpp
= &elf_section_data (s
)->local_dynrel
;
729 head
= (struct elf_dyn_relocs
**) vpp
;
733 if (p
== NULL
|| p
->sec
!= sec
)
735 size_t amt
= sizeof *p
;
736 p
= ((struct elf_dyn_relocs
*)
737 bfd_alloc (htab
->elf
.dynobj
, amt
));
748 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
753 case R_RISCV_GNU_VTINHERIT
:
754 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
758 case R_RISCV_GNU_VTENTRY
:
759 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
772 riscv_elf_gc_mark_hook (asection
*sec
,
773 struct bfd_link_info
*info
,
774 Elf_Internal_Rela
*rel
,
775 struct elf_link_hash_entry
*h
,
776 Elf_Internal_Sym
*sym
)
779 switch (ELFNN_R_TYPE (rel
->r_info
))
781 case R_RISCV_GNU_VTINHERIT
:
782 case R_RISCV_GNU_VTENTRY
:
786 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
789 /* Find dynamic relocs for H that apply to read-only sections. */
792 readonly_dynrelocs (struct elf_link_hash_entry
*h
)
794 struct elf_dyn_relocs
*p
;
796 for (p
= riscv_elf_hash_entry (h
)->dyn_relocs
; p
!= NULL
; p
= p
->next
)
798 asection
*s
= p
->sec
->output_section
;
800 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
806 /* Adjust a symbol defined by a dynamic object and referenced by a
807 regular object. The current definition is in some section of the
808 dynamic object, but we're not including those sections. We have to
809 change the definition to something the rest of the link can
813 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
814 struct elf_link_hash_entry
*h
)
816 struct riscv_elf_link_hash_table
*htab
;
817 struct riscv_elf_link_hash_entry
* eh
;
821 htab
= riscv_elf_hash_table (info
);
822 BFD_ASSERT (htab
!= NULL
);
824 dynobj
= htab
->elf
.dynobj
;
826 /* Make sure we know what is going on here. */
827 BFD_ASSERT (dynobj
!= NULL
829 || h
->type
== STT_GNU_IFUNC
833 && !h
->def_regular
)));
835 /* If this is a function, put it in the procedure linkage table. We
836 will fill in the contents of the procedure linkage table later
837 (although we could actually do it here). */
838 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
840 if (h
->plt
.refcount
<= 0
841 || SYMBOL_CALLS_LOCAL (info
, h
)
842 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
843 && h
->root
.type
== bfd_link_hash_undefweak
))
845 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
846 input file, but the symbol was never referred to by a dynamic
847 object, or if all references were garbage collected. In such
848 a case, we don't actually need to build a PLT entry. */
849 h
->plt
.offset
= (bfd_vma
) -1;
856 h
->plt
.offset
= (bfd_vma
) -1;
858 /* If this is a weak symbol, and there is a real definition, the
859 processor independent code will have arranged for us to see the
860 real definition first, and we can just use the same value. */
863 struct elf_link_hash_entry
*def
= weakdef (h
);
864 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
865 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
866 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
870 /* This is a reference to a symbol defined by a dynamic object which
871 is not a function. */
873 /* If we are creating a shared library, we must presume that the
874 only references to the symbol are via the global offset table.
875 For such cases we need not do anything here; the relocations will
876 be handled correctly by relocate_section. */
877 if (bfd_link_pic (info
))
880 /* If there are no references to this symbol that do not use the
881 GOT, we don't need to generate a copy reloc. */
885 /* If -z nocopyreloc was given, we won't generate them either. */
886 if (info
->nocopyreloc
)
892 /* If we don't find any dynamic relocs in read-only sections, then
893 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
894 if (!readonly_dynrelocs (h
))
900 /* We must allocate the symbol in our .dynbss section, which will
901 become part of the .bss section of the executable. There will be
902 an entry for this symbol in the .dynsym section. The dynamic
903 object will contain position independent code, so all references
904 from the dynamic object to this symbol will go through the global
905 offset table. The dynamic linker will use the .dynsym entry to
906 determine the address it must put in the global offset table, so
907 both the dynamic object and the regular object will refer to the
908 same memory location for the variable. */
910 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
911 to copy the initial value out of the dynamic object and into the
912 runtime process image. We need to remember the offset into the
913 .rel.bss section we are going to use. */
914 eh
= (struct riscv_elf_link_hash_entry
*) h
;
915 if (eh
->tls_type
& ~GOT_NORMAL
)
918 srel
= htab
->elf
.srelbss
;
920 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
922 s
= htab
->elf
.sdynrelro
;
923 srel
= htab
->elf
.sreldynrelro
;
927 s
= htab
->elf
.sdynbss
;
928 srel
= htab
->elf
.srelbss
;
930 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
932 srel
->size
+= sizeof (ElfNN_External_Rela
);
936 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
939 /* Allocate space in .plt, .got and associated reloc sections for
943 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
945 struct bfd_link_info
*info
;
946 struct riscv_elf_link_hash_table
*htab
;
947 struct riscv_elf_link_hash_entry
*eh
;
948 struct elf_dyn_relocs
*p
;
950 if (h
->root
.type
== bfd_link_hash_indirect
)
953 info
= (struct bfd_link_info
*) inf
;
954 htab
= riscv_elf_hash_table (info
);
955 BFD_ASSERT (htab
!= NULL
);
957 if (htab
->elf
.dynamic_sections_created
958 && h
->plt
.refcount
> 0)
960 /* Make sure this symbol is output as a dynamic symbol.
961 Undefined weak syms won't yet be marked as dynamic. */
965 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
969 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
971 asection
*s
= htab
->elf
.splt
;
974 s
->size
= PLT_HEADER_SIZE
;
976 h
->plt
.offset
= s
->size
;
978 /* Make room for this entry. */
979 s
->size
+= PLT_ENTRY_SIZE
;
981 /* We also need to make an entry in the .got.plt section. */
982 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
984 /* We also need to make an entry in the .rela.plt section. */
985 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
987 /* If this symbol is not defined in a regular file, and we are
988 not generating a shared library, then set the symbol to this
989 location in the .plt. This is required to make function
990 pointers compare as equal between the normal executable and
991 the shared library. */
992 if (! bfd_link_pic (info
)
995 h
->root
.u
.def
.section
= s
;
996 h
->root
.u
.def
.value
= h
->plt
.offset
;
1001 h
->plt
.offset
= (bfd_vma
) -1;
1007 h
->plt
.offset
= (bfd_vma
) -1;
1011 if (h
->got
.refcount
> 0)
1015 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1017 /* Make sure this symbol is output as a dynamic symbol.
1018 Undefined weak syms won't yet be marked as dynamic. */
1019 if (h
->dynindx
== -1
1020 && !h
->forced_local
)
1022 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1027 h
->got
.offset
= s
->size
;
1028 dyn
= htab
->elf
.dynamic_sections_created
;
1029 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1031 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1032 if (tls_type
& GOT_TLS_GD
)
1034 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1035 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1038 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1039 if (tls_type
& GOT_TLS_IE
)
1041 s
->size
+= RISCV_ELF_WORD_BYTES
;
1042 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1047 s
->size
+= RISCV_ELF_WORD_BYTES
;
1048 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1049 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1050 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1054 h
->got
.offset
= (bfd_vma
) -1;
1056 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1057 if (eh
->dyn_relocs
== NULL
)
1060 /* In the shared -Bsymbolic case, discard space allocated for
1061 dynamic pc-relative relocs against symbols which turn out to be
1062 defined in regular objects. For the normal shared case, discard
1063 space for pc-relative relocs that have become local due to symbol
1064 visibility changes. */
1066 if (bfd_link_pic (info
))
1068 if (SYMBOL_CALLS_LOCAL (info
, h
))
1070 struct elf_dyn_relocs
**pp
;
1072 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1074 p
->count
-= p
->pc_count
;
1083 /* Also discard relocs on undefined weak syms with non-default
1085 if (eh
->dyn_relocs
!= NULL
1086 && h
->root
.type
== bfd_link_hash_undefweak
)
1088 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1089 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1090 eh
->dyn_relocs
= NULL
;
1092 /* Make sure undefined weak symbols are output as a dynamic
1094 else if (h
->dynindx
== -1
1095 && !h
->forced_local
)
1097 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1104 /* For the non-shared case, discard space for relocs against
1105 symbols which turn out to need copy relocs or are not
1111 || (htab
->elf
.dynamic_sections_created
1112 && (h
->root
.type
== bfd_link_hash_undefweak
1113 || h
->root
.type
== bfd_link_hash_undefined
))))
1115 /* Make sure this symbol is output as a dynamic symbol.
1116 Undefined weak syms won't yet be marked as dynamic. */
1117 if (h
->dynindx
== -1
1118 && !h
->forced_local
)
1120 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1124 /* If that succeeded, we know we'll be keeping all the
1126 if (h
->dynindx
!= -1)
1130 eh
->dyn_relocs
= NULL
;
1135 /* Finally, allocate space. */
1136 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1138 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1139 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1145 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1146 read-only sections. */
1149 maybe_set_textrel (struct elf_link_hash_entry
*h
, void *info_p
)
1153 if (h
->root
.type
== bfd_link_hash_indirect
)
1156 sec
= readonly_dynrelocs (h
);
1159 struct bfd_link_info
*info
= (struct bfd_link_info
*) info_p
;
1161 info
->flags
|= DF_TEXTREL
;
1162 info
->callbacks
->minfo
1163 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1164 sec
->owner
, h
->root
.root
.string
, sec
);
1166 /* Not an error, just cut short the traversal. */
1173 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1175 struct riscv_elf_link_hash_table
*htab
;
1180 htab
= riscv_elf_hash_table (info
);
1181 BFD_ASSERT (htab
!= NULL
);
1182 dynobj
= htab
->elf
.dynobj
;
1183 BFD_ASSERT (dynobj
!= NULL
);
1185 if (elf_hash_table (info
)->dynamic_sections_created
)
1187 /* Set the contents of the .interp section to the interpreter. */
1188 if (bfd_link_executable (info
) && !info
->nointerp
)
1190 s
= bfd_get_linker_section (dynobj
, ".interp");
1191 BFD_ASSERT (s
!= NULL
);
1192 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1193 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1197 /* Set up .got offsets for local syms, and space for local dynamic
1199 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1201 bfd_signed_vma
*local_got
;
1202 bfd_signed_vma
*end_local_got
;
1203 char *local_tls_type
;
1204 bfd_size_type locsymcount
;
1205 Elf_Internal_Shdr
*symtab_hdr
;
1208 if (! is_riscv_elf (ibfd
))
1211 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1213 struct elf_dyn_relocs
*p
;
1215 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1217 if (!bfd_is_abs_section (p
->sec
)
1218 && bfd_is_abs_section (p
->sec
->output_section
))
1220 /* Input section has been discarded, either because
1221 it is a copy of a linkonce section or due to
1222 linker script /DISCARD/, so we'll be discarding
1225 else if (p
->count
!= 0)
1227 srel
= elf_section_data (p
->sec
)->sreloc
;
1228 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1229 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1230 info
->flags
|= DF_TEXTREL
;
1235 local_got
= elf_local_got_refcounts (ibfd
);
1239 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1240 locsymcount
= symtab_hdr
->sh_info
;
1241 end_local_got
= local_got
+ locsymcount
;
1242 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1244 srel
= htab
->elf
.srelgot
;
1245 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1249 *local_got
= s
->size
;
1250 s
->size
+= RISCV_ELF_WORD_BYTES
;
1251 if (*local_tls_type
& GOT_TLS_GD
)
1252 s
->size
+= RISCV_ELF_WORD_BYTES
;
1253 if (bfd_link_pic (info
)
1254 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1255 srel
->size
+= sizeof (ElfNN_External_Rela
);
1258 *local_got
= (bfd_vma
) -1;
1262 /* Allocate global sym .plt and .got entries, and space for global
1263 sym dynamic relocs. */
1264 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1266 if (htab
->elf
.sgotplt
)
1268 struct elf_link_hash_entry
*got
;
1269 got
= elf_link_hash_lookup (elf_hash_table (info
),
1270 "_GLOBAL_OFFSET_TABLE_",
1271 FALSE
, FALSE
, FALSE
);
1273 /* Don't allocate .got.plt section if there are no GOT nor PLT
1274 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1276 || !got
->ref_regular_nonweak
)
1277 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1278 && (htab
->elf
.splt
== NULL
1279 || htab
->elf
.splt
->size
== 0)
1280 && (htab
->elf
.sgot
== NULL
1281 || (htab
->elf
.sgot
->size
1282 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1283 htab
->elf
.sgotplt
->size
= 0;
1286 /* The check_relocs and adjust_dynamic_symbol entry points have
1287 determined the sizes of the various dynamic sections. Allocate
1289 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1291 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1294 if (s
== htab
->elf
.splt
1295 || s
== htab
->elf
.sgot
1296 || s
== htab
->elf
.sgotplt
1297 || s
== htab
->elf
.sdynbss
1298 || s
== htab
->elf
.sdynrelro
1299 || s
== htab
->sdyntdata
)
1301 /* Strip this section if we don't need it; see the
1304 else if (strncmp (s
->name
, ".rela", 5) == 0)
1308 /* We use the reloc_count field as a counter if we need
1309 to copy relocs into the output file. */
1315 /* It's not one of our sections. */
1321 /* If we don't need this section, strip it from the
1322 output file. This is mostly to handle .rela.bss and
1323 .rela.plt. We must create both sections in
1324 create_dynamic_sections, because they must be created
1325 before the linker maps input sections to output
1326 sections. The linker does that before
1327 adjust_dynamic_symbol is called, and it is that
1328 function which decides whether anything needs to go
1329 into these sections. */
1330 s
->flags
|= SEC_EXCLUDE
;
1334 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1337 /* Allocate memory for the section contents. Zero the memory
1338 for the benefit of .rela.plt, which has 4 unused entries
1339 at the beginning, and we don't want garbage. */
1340 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1341 if (s
->contents
== NULL
)
1345 if (elf_hash_table (info
)->dynamic_sections_created
)
1347 /* Add some entries to the .dynamic section. We fill in the
1348 values later, in riscv_elf_finish_dynamic_sections, but we
1349 must add the entries now so that we get the correct size for
1350 the .dynamic section. The DT_DEBUG entry is filled in by the
1351 dynamic linker and used by the debugger. */
1352 #define add_dynamic_entry(TAG, VAL) \
1353 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1355 if (bfd_link_executable (info
))
1357 if (!add_dynamic_entry (DT_DEBUG
, 0))
1361 if (htab
->elf
.srelplt
->size
!= 0)
1363 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1364 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1365 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1366 || !add_dynamic_entry (DT_JMPREL
, 0))
1370 if (!add_dynamic_entry (DT_RELA
, 0)
1371 || !add_dynamic_entry (DT_RELASZ
, 0)
1372 || !add_dynamic_entry (DT_RELAENT
, sizeof (ElfNN_External_Rela
)))
1375 /* If any dynamic relocs apply to a read-only section,
1376 then we need a DT_TEXTREL entry. */
1377 if ((info
->flags
& DF_TEXTREL
) == 0)
1378 elf_link_hash_traverse (&htab
->elf
, maybe_set_textrel
, info
);
1380 if (info
->flags
& DF_TEXTREL
)
1382 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1386 #undef add_dynamic_entry
1392 #define DTP_OFFSET 0x800
1394 /* Return the relocation value for a TLS dtp-relative reloc. */
1397 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1399 /* If tls_sec is NULL, we should have signalled an error already. */
1400 if (elf_hash_table (info
)->tls_sec
== NULL
)
1402 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1405 /* Return the relocation value for a static TLS tp-relative relocation. */
1408 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1410 /* If tls_sec is NULL, we should have signalled an error already. */
1411 if (elf_hash_table (info
)->tls_sec
== NULL
)
1413 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1416 /* Return the global pointer's value, or 0 if it is not in use. */
1419 riscv_global_pointer_value (struct bfd_link_info
*info
)
1421 struct bfd_link_hash_entry
*h
;
1423 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1424 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1427 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1430 /* Emplace a static relocation. */
1432 static bfd_reloc_status_type
1433 perform_relocation (const reloc_howto_type
*howto
,
1434 const Elf_Internal_Rela
*rel
,
1436 asection
*input_section
,
1440 if (howto
->pc_relative
)
1441 value
-= sec_addr (input_section
) + rel
->r_offset
;
1442 value
+= rel
->r_addend
;
1444 switch (ELFNN_R_TYPE (rel
->r_info
))
1447 case R_RISCV_TPREL_HI20
:
1448 case R_RISCV_PCREL_HI20
:
1449 case R_RISCV_GOT_HI20
:
1450 case R_RISCV_TLS_GOT_HI20
:
1451 case R_RISCV_TLS_GD_HI20
:
1452 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1453 return bfd_reloc_overflow
;
1454 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1457 case R_RISCV_LO12_I
:
1458 case R_RISCV_GPREL_I
:
1459 case R_RISCV_TPREL_LO12_I
:
1460 case R_RISCV_TPREL_I
:
1461 case R_RISCV_PCREL_LO12_I
:
1462 value
= ENCODE_ITYPE_IMM (value
);
1465 case R_RISCV_LO12_S
:
1466 case R_RISCV_GPREL_S
:
1467 case R_RISCV_TPREL_LO12_S
:
1468 case R_RISCV_TPREL_S
:
1469 case R_RISCV_PCREL_LO12_S
:
1470 value
= ENCODE_STYPE_IMM (value
);
1474 case R_RISCV_CALL_PLT
:
1475 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1476 return bfd_reloc_overflow
;
1477 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1478 | (ENCODE_ITYPE_IMM (value
) << 32);
1482 if (!VALID_UJTYPE_IMM (value
))
1483 return bfd_reloc_overflow
;
1484 value
= ENCODE_UJTYPE_IMM (value
);
1487 case R_RISCV_BRANCH
:
1488 if (!VALID_SBTYPE_IMM (value
))
1489 return bfd_reloc_overflow
;
1490 value
= ENCODE_SBTYPE_IMM (value
);
1493 case R_RISCV_RVC_BRANCH
:
1494 if (!VALID_RVC_B_IMM (value
))
1495 return bfd_reloc_overflow
;
1496 value
= ENCODE_RVC_B_IMM (value
);
1499 case R_RISCV_RVC_JUMP
:
1500 if (!VALID_RVC_J_IMM (value
))
1501 return bfd_reloc_overflow
;
1502 value
= ENCODE_RVC_J_IMM (value
);
1505 case R_RISCV_RVC_LUI
:
1506 if (RISCV_CONST_HIGH_PART (value
) == 0)
1508 /* Linker relaxation can convert an address equal to or greater than
1509 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1510 valid immediate. We can fix this by converting it to a C.LI. */
1511 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1512 contents
+ rel
->r_offset
);
1513 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1514 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1515 value
= ENCODE_RVC_IMM (0);
1517 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1518 return bfd_reloc_overflow
;
1520 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1538 case R_RISCV_32_PCREL
:
1539 case R_RISCV_TLS_DTPREL32
:
1540 case R_RISCV_TLS_DTPREL64
:
1543 case R_RISCV_DELETE
:
1544 return bfd_reloc_ok
;
1547 return bfd_reloc_notsupported
;
1550 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1551 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1552 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1554 return bfd_reloc_ok
;
1557 /* Remember all PC-relative high-part relocs we've encountered to help us
1558 later resolve the corresponding low-part relocs. */
1564 } riscv_pcrel_hi_reloc
;
1566 typedef struct riscv_pcrel_lo_reloc
1568 asection
* input_section
;
1569 struct bfd_link_info
* info
;
1570 reloc_howto_type
* howto
;
1571 const Elf_Internal_Rela
* reloc
;
1574 bfd_byte
* contents
;
1575 struct riscv_pcrel_lo_reloc
* next
;
1576 } riscv_pcrel_lo_reloc
;
1581 riscv_pcrel_lo_reloc
*lo_relocs
;
1582 } riscv_pcrel_relocs
;
1585 riscv_pcrel_reloc_hash (const void *entry
)
1587 const riscv_pcrel_hi_reloc
*e
= entry
;
1588 return (hashval_t
)(e
->address
>> 2);
1592 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1594 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1595 return e1
->address
== e2
->address
;
1599 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1602 p
->lo_relocs
= NULL
;
1603 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1604 riscv_pcrel_reloc_eq
, free
);
1605 return p
->hi_relocs
!= NULL
;
1609 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1611 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1615 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1620 htab_delete (p
->hi_relocs
);
1624 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1625 struct bfd_link_info
*info
,
1629 const reloc_howto_type
*howto
,
1632 /* We may need to reference low addreses in PC-relative modes even when the
1633 * PC is far away from these addresses. For example, undefweak references
1634 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1635 * addresses that we can link PC-relative programs at, the linker can't
1636 * actually relocate references to those symbols. In order to allow these
1637 * programs to work we simply convert the PC-relative auipc sequences to
1638 * 0-relative lui sequences. */
1639 if (bfd_link_pic (info
))
1642 /* If it's possible to reference the symbol using auipc we do so, as that's
1643 * more in the spirit of the PC-relative relocations we're processing. */
1644 bfd_vma offset
= addr
- pc
;
1645 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1648 /* If it's impossible to reference this with a LUI-based offset then don't
1649 * bother to convert it at all so users still see the PC-relative relocation
1650 * in the truncation message. */
1651 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1654 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1656 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1657 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1658 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1663 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1664 bfd_vma value
, bfd_boolean absolute
)
1666 bfd_vma offset
= absolute
? value
: value
- addr
;
1667 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1668 riscv_pcrel_hi_reloc
**slot
=
1669 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1671 BFD_ASSERT (*slot
== NULL
);
1672 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1680 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1681 asection
*input_section
,
1682 struct bfd_link_info
*info
,
1683 reloc_howto_type
*howto
,
1684 const Elf_Internal_Rela
*reloc
,
1689 riscv_pcrel_lo_reloc
*entry
;
1690 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1693 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1694 name
, contents
, p
->lo_relocs
};
1695 p
->lo_relocs
= entry
;
1700 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1702 riscv_pcrel_lo_reloc
*r
;
1704 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1706 bfd
*input_bfd
= r
->input_section
->owner
;
1708 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1709 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1711 /* Check for overflow into bit 11 when adding reloc addend. */
1712 || (! (entry
->value
& 0x800)
1713 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1715 char *string
= (entry
== NULL
1716 ? "%pcrel_lo missing matching %pcrel_hi"
1717 : "%pcrel_lo overflow with an addend");
1718 (*r
->info
->callbacks
->reloc_dangerous
)
1719 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1723 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1724 input_bfd
, r
->contents
);
1730 /* Relocate a RISC-V ELF section.
1732 The RELOCATE_SECTION function is called by the new ELF backend linker
1733 to handle the relocations for a section.
1735 The relocs are always passed as Rela structures.
1737 This function is responsible for adjusting the section contents as
1738 necessary, and (if generating a relocatable output file) adjusting
1739 the reloc addend as necessary.
1741 This function does not have to worry about setting the reloc
1742 address or the reloc symbol index.
1744 LOCAL_SYMS is a pointer to the swapped in local symbols.
1746 LOCAL_SECTIONS is an array giving the section in the input file
1747 corresponding to the st_shndx field of each local symbol.
1749 The global hash table entry for the global symbols can be found
1750 via elf_sym_hashes (input_bfd).
1752 When generating relocatable output, this function must handle
1753 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1754 going to be the section symbol corresponding to the output
1755 section, which means that the addend must be adjusted
1759 riscv_elf_relocate_section (bfd
*output_bfd
,
1760 struct bfd_link_info
*info
,
1762 asection
*input_section
,
1764 Elf_Internal_Rela
*relocs
,
1765 Elf_Internal_Sym
*local_syms
,
1766 asection
**local_sections
)
1768 Elf_Internal_Rela
*rel
;
1769 Elf_Internal_Rela
*relend
;
1770 riscv_pcrel_relocs pcrel_relocs
;
1771 bfd_boolean ret
= FALSE
;
1772 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1773 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1774 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1775 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1776 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1777 bfd_boolean absolute
;
1779 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1782 relend
= relocs
+ input_section
->reloc_count
;
1783 for (rel
= relocs
; rel
< relend
; rel
++)
1785 unsigned long r_symndx
;
1786 struct elf_link_hash_entry
*h
;
1787 Elf_Internal_Sym
*sym
;
1790 bfd_reloc_status_type r
= bfd_reloc_ok
;
1792 bfd_vma off
, ie_off
;
1793 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1794 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1795 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1796 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1797 const char *msg
= NULL
;
1798 char *msg_buf
= NULL
;
1799 bfd_boolean resolved_to_zero
;
1802 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1805 /* This is a final link. */
1806 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1810 unresolved_reloc
= FALSE
;
1811 if (r_symndx
< symtab_hdr
->sh_info
)
1813 sym
= local_syms
+ r_symndx
;
1814 sec
= local_sections
[r_symndx
];
1815 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1819 bfd_boolean warned
, ignored
;
1821 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1822 r_symndx
, symtab_hdr
, sym_hashes
,
1824 unresolved_reloc
, warned
, ignored
);
1827 /* To avoid generating warning messages about truncated
1828 relocations, set the relocation's address to be the same as
1829 the start of this section. */
1830 if (input_section
->output_section
!= NULL
)
1831 relocation
= input_section
->output_section
->vma
;
1837 if (sec
!= NULL
&& discarded_section (sec
))
1838 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1839 rel
, 1, relend
, howto
, 0, contents
);
1841 if (bfd_link_relocatable (info
))
1845 name
= h
->root
.root
.string
;
1848 name
= (bfd_elf_string_from_elf_section
1849 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1850 if (name
== NULL
|| *name
== '\0')
1851 name
= bfd_section_name (sec
);
1854 resolved_to_zero
= (h
!= NULL
1855 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1861 case R_RISCV_TPREL_ADD
:
1863 case R_RISCV_JUMP_SLOT
:
1864 case R_RISCV_RELATIVE
:
1865 /* These require nothing of us at all. */
1869 case R_RISCV_BRANCH
:
1870 case R_RISCV_RVC_BRANCH
:
1871 case R_RISCV_RVC_LUI
:
1872 case R_RISCV_LO12_I
:
1873 case R_RISCV_LO12_S
:
1878 case R_RISCV_32_PCREL
:
1879 case R_RISCV_DELETE
:
1880 /* These require no special handling beyond perform_relocation. */
1883 case R_RISCV_GOT_HI20
:
1886 bfd_boolean dyn
, pic
;
1888 off
= h
->got
.offset
;
1889 BFD_ASSERT (off
!= (bfd_vma
) -1);
1890 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1891 pic
= bfd_link_pic (info
);
1893 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1894 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1896 /* This is actually a static link, or it is a
1897 -Bsymbolic link and the symbol is defined
1898 locally, or the symbol was forced to be local
1899 because of a version file. We must initialize
1900 this entry in the global offset table. Since the
1901 offset must always be a multiple of the word size,
1902 we use the least significant bit to record whether
1903 we have initialized it already.
1905 When doing a dynamic link, we create a .rela.got
1906 relocation entry to initialize the value. This
1907 is done in the finish_dynamic_symbol routine. */
1912 bfd_put_NN (output_bfd
, relocation
,
1913 htab
->elf
.sgot
->contents
+ off
);
1918 unresolved_reloc
= FALSE
;
1922 BFD_ASSERT (local_got_offsets
!= NULL
1923 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1925 off
= local_got_offsets
[r_symndx
];
1927 /* The offset must always be a multiple of the word size.
1928 So, we can use the least significant bit to record
1929 whether we have already processed this entry. */
1934 if (bfd_link_pic (info
))
1937 Elf_Internal_Rela outrel
;
1939 /* We need to generate a R_RISCV_RELATIVE reloc
1940 for the dynamic linker. */
1941 s
= htab
->elf
.srelgot
;
1942 BFD_ASSERT (s
!= NULL
);
1944 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1946 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1947 outrel
.r_addend
= relocation
;
1949 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1952 bfd_put_NN (output_bfd
, relocation
,
1953 htab
->elf
.sgot
->contents
+ off
);
1954 local_got_offsets
[r_symndx
] |= 1;
1957 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1958 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1965 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1966 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1968 r
= bfd_reloc_notsupported
;
1969 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1970 relocation
, absolute
))
1971 r
= bfd_reloc_overflow
;
1979 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1980 contents
+ rel
->r_offset
);
1981 relocation
= old_value
+ relocation
;
1991 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1992 contents
+ rel
->r_offset
);
1993 relocation
= old_value
- relocation
;
1998 case R_RISCV_CALL_PLT
:
1999 /* Handle a call to an undefined weak function. This won't be
2000 relaxed, so we have to handle it here. */
2001 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2002 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2004 /* We can use x0 as the base register. */
2005 bfd_vma insn
= bfd_get_32 (input_bfd
,
2006 contents
+ rel
->r_offset
+ 4);
2007 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2008 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
2009 /* Set the relocation value so that we get 0 after the pc
2010 relative adjustment. */
2011 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2016 case R_RISCV_RVC_JUMP
:
2017 /* This line has to match the check in _bfd_riscv_relax_section. */
2018 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2020 /* Refer to the PLT entry. */
2021 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2022 unresolved_reloc
= FALSE
;
2026 case R_RISCV_TPREL_HI20
:
2027 relocation
= tpoff (info
, relocation
);
2030 case R_RISCV_TPREL_LO12_I
:
2031 case R_RISCV_TPREL_LO12_S
:
2032 relocation
= tpoff (info
, relocation
);
2035 case R_RISCV_TPREL_I
:
2036 case R_RISCV_TPREL_S
:
2037 relocation
= tpoff (info
, relocation
);
2038 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2040 /* We can use tp as the base register. */
2041 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2042 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2043 insn
|= X_TP
<< OP_SH_RS1
;
2044 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2047 r
= bfd_reloc_overflow
;
2050 case R_RISCV_GPREL_I
:
2051 case R_RISCV_GPREL_S
:
2053 bfd_vma gp
= riscv_global_pointer_value (info
);
2054 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2055 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2057 /* We can use x0 or gp as the base register. */
2058 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2059 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2062 rel
->r_addend
-= gp
;
2063 insn
|= X_GP
<< OP_SH_RS1
;
2065 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2068 r
= bfd_reloc_overflow
;
2072 case R_RISCV_PCREL_HI20
:
2073 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2080 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2081 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2083 r
= bfd_reloc_notsupported
;
2084 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2085 relocation
+ rel
->r_addend
,
2087 r
= bfd_reloc_overflow
;
2090 case R_RISCV_PCREL_LO12_I
:
2091 case R_RISCV_PCREL_LO12_S
:
2092 /* We don't allow section symbols plus addends as the auipc address,
2093 because then riscv_relax_delete_bytes would have to search through
2094 all relocs to update these addends. This is also ambiguous, as
2095 we do allow offsets to be added to the target address, which are
2096 not to be used to find the auipc address. */
2097 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2098 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2101 msg
= _("%pcrel_lo section symbol with an addend");
2102 r
= bfd_reloc_dangerous
;
2106 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2107 howto
, rel
, relocation
, name
,
2110 r
= bfd_reloc_overflow
;
2113 case R_RISCV_TLS_DTPREL32
:
2114 case R_RISCV_TLS_DTPREL64
:
2115 relocation
= dtpoff (info
, relocation
);
2120 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2123 if ((bfd_link_pic (info
)
2125 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2126 && !resolved_to_zero
)
2127 || h
->root
.type
!= bfd_link_hash_undefweak
)
2128 && (! howto
->pc_relative
2129 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2130 || (!bfd_link_pic (info
)
2136 || h
->root
.type
== bfd_link_hash_undefweak
2137 || h
->root
.type
== bfd_link_hash_undefined
)))
2139 Elf_Internal_Rela outrel
;
2140 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2142 /* When generating a shared object, these relocations
2143 are copied into the output file to be resolved at run
2147 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2149 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2150 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2151 outrel
.r_offset
+= sec_addr (input_section
);
2153 if (skip_dynamic_relocation
)
2154 memset (&outrel
, 0, sizeof outrel
);
2155 else if (h
!= NULL
&& h
->dynindx
!= -1
2156 && !(bfd_link_pic (info
)
2157 && SYMBOLIC_BIND (info
, h
)
2160 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2161 outrel
.r_addend
= rel
->r_addend
;
2165 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2166 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2169 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2170 if (skip_static_relocation
)
2175 case R_RISCV_TLS_GOT_HI20
:
2179 case R_RISCV_TLS_GD_HI20
:
2182 off
= h
->got
.offset
;
2187 off
= local_got_offsets
[r_symndx
];
2188 local_got_offsets
[r_symndx
] |= 1;
2191 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2192 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2193 /* If this symbol is referenced by both GD and IE TLS, the IE
2194 reference's GOT slot follows the GD reference's slots. */
2196 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2197 ie_off
= 2 * GOT_ENTRY_SIZE
;
2203 Elf_Internal_Rela outrel
;
2205 bfd_boolean need_relocs
= FALSE
;
2207 if (htab
->elf
.srelgot
== NULL
)
2212 bfd_boolean dyn
, pic
;
2213 dyn
= htab
->elf
.dynamic_sections_created
;
2214 pic
= bfd_link_pic (info
);
2216 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2217 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2221 /* The GOT entries have not been initialized yet. Do it
2222 now, and emit any relocations. */
2223 if ((bfd_link_pic (info
) || indx
!= 0)
2225 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2226 || h
->root
.type
!= bfd_link_hash_undefweak
))
2229 if (tls_type
& GOT_TLS_GD
)
2233 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2234 outrel
.r_addend
= 0;
2235 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2236 bfd_put_NN (output_bfd
, 0,
2237 htab
->elf
.sgot
->contents
+ off
);
2238 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2241 BFD_ASSERT (! unresolved_reloc
);
2242 bfd_put_NN (output_bfd
,
2243 dtpoff (info
, relocation
),
2244 (htab
->elf
.sgot
->contents
+ off
+
2245 RISCV_ELF_WORD_BYTES
));
2249 bfd_put_NN (output_bfd
, 0,
2250 (htab
->elf
.sgot
->contents
+ off
+
2251 RISCV_ELF_WORD_BYTES
));
2252 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2253 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2254 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2259 /* If we are not emitting relocations for a
2260 general dynamic reference, then we must be in a
2261 static link or an executable link with the
2262 symbol binding locally. Mark it as belonging
2263 to module 1, the executable. */
2264 bfd_put_NN (output_bfd
, 1,
2265 htab
->elf
.sgot
->contents
+ off
);
2266 bfd_put_NN (output_bfd
,
2267 dtpoff (info
, relocation
),
2268 (htab
->elf
.sgot
->contents
+ off
+
2269 RISCV_ELF_WORD_BYTES
));
2273 if (tls_type
& GOT_TLS_IE
)
2277 bfd_put_NN (output_bfd
, 0,
2278 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2279 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2281 outrel
.r_addend
= 0;
2283 outrel
.r_addend
= tpoff (info
, relocation
);
2284 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2285 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2289 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2290 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2295 BFD_ASSERT (off
< (bfd_vma
) -2);
2296 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2297 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2299 r
= bfd_reloc_overflow
;
2300 unresolved_reloc
= FALSE
;
2304 r
= bfd_reloc_notsupported
;
2307 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2308 because such sections are not SEC_ALLOC and thus ld.so will
2309 not process them. */
2310 if (unresolved_reloc
2311 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2313 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2314 rel
->r_offset
) != (bfd_vma
) -1)
2320 case R_RISCV_RVC_JUMP
:
2321 if (asprintf (&msg_buf
,
2322 _("%%X%%P: relocation %s against `%s' can "
2323 "not be used when making a shared object; "
2324 "recompile with -fPIC\n"),
2326 h
->root
.root
.string
) == -1)
2331 if (asprintf (&msg_buf
,
2332 _("%%X%%P: unresolvable %s relocation against "
2335 h
->root
.root
.string
) == -1)
2341 r
= bfd_reloc_notsupported
;
2344 if (r
== bfd_reloc_ok
)
2345 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2346 input_bfd
, contents
);
2348 /* We should have already detected the error and set message before.
2349 If the error message isn't set since the linker runs out of memory
2350 or we don't set it before, then we should set the default message
2351 with the "internal error" string here. */
2357 case bfd_reloc_overflow
:
2358 info
->callbacks
->reloc_overflow
2359 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2360 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2363 case bfd_reloc_undefined
:
2364 info
->callbacks
->undefined_symbol
2365 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2369 case bfd_reloc_outofrange
:
2371 msg
= _("%X%P: internal error: out of range error\n");
2374 case bfd_reloc_notsupported
:
2376 msg
= _("%X%P: internal error: unsupported relocation error\n");
2379 case bfd_reloc_dangerous
:
2380 /* The error message should already be set. */
2382 msg
= _("dangerous relocation error");
2383 info
->callbacks
->reloc_dangerous
2384 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2388 msg
= _("%X%P: internal error: unknown error\n");
2392 /* Do not report error message for the dangerous relocation again. */
2393 if (msg
&& r
!= bfd_reloc_dangerous
)
2394 info
->callbacks
->einfo (msg
);
2396 /* Free the unused `msg_buf` if needed. */
2400 /* We already reported the error via a callback, so don't try to report
2401 it again by returning false. That leads to spurious errors. */
2406 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2408 riscv_free_pcrel_relocs (&pcrel_relocs
);
2412 /* Finish up dynamic symbol handling. We set the contents of various
2413 dynamic sections here. */
2416 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2417 struct bfd_link_info
*info
,
2418 struct elf_link_hash_entry
*h
,
2419 Elf_Internal_Sym
*sym
)
2421 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2422 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2424 if (h
->plt
.offset
!= (bfd_vma
) -1)
2426 /* We've decided to create a PLT entry for this symbol. */
2428 bfd_vma i
, header_address
, plt_idx
, got_address
;
2429 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2430 Elf_Internal_Rela rela
;
2432 BFD_ASSERT (h
->dynindx
!= -1);
2434 /* Calculate the address of the PLT header. */
2435 header_address
= sec_addr (htab
->elf
.splt
);
2437 /* Calculate the index of the entry. */
2438 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2440 /* Calculate the address of the .got.plt entry. */
2441 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2443 /* Find out where the .plt entry should go. */
2444 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2446 /* Fill in the PLT entry itself. */
2447 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2448 header_address
+ h
->plt
.offset
,
2452 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2453 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2455 /* Fill in the initial value of the .got.plt entry. */
2456 loc
= htab
->elf
.sgotplt
->contents
2457 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2458 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2460 /* Fill in the entry in the .rela.plt section. */
2461 rela
.r_offset
= got_address
;
2463 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2465 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2466 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2468 if (!h
->def_regular
)
2470 /* Mark the symbol as undefined, rather than as defined in
2471 the .plt section. Leave the value alone. */
2472 sym
->st_shndx
= SHN_UNDEF
;
2473 /* If the symbol is weak, we do need to clear the value.
2474 Otherwise, the PLT entry would provide a definition for
2475 the symbol even if the symbol wasn't defined anywhere,
2476 and so the symbol would never be NULL. */
2477 if (!h
->ref_regular_nonweak
)
2482 if (h
->got
.offset
!= (bfd_vma
) -1
2483 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2484 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2488 Elf_Internal_Rela rela
;
2490 /* This symbol has an entry in the GOT. Set it up. */
2492 sgot
= htab
->elf
.sgot
;
2493 srela
= htab
->elf
.srelgot
;
2494 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2496 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2498 /* If this is a local symbol reference, we just want to emit a RELATIVE
2499 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2500 the symbol was forced to be local because of a version file.
2501 The entry in the global offset table will already have been
2502 initialized in the relocate_section function. */
2503 if (bfd_link_pic (info
)
2504 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2506 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2507 asection
*sec
= h
->root
.u
.def
.section
;
2508 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2509 rela
.r_addend
= (h
->root
.u
.def
.value
2510 + sec
->output_section
->vma
2511 + sec
->output_offset
);
2515 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2516 BFD_ASSERT (h
->dynindx
!= -1);
2517 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2521 bfd_put_NN (output_bfd
, 0,
2522 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2523 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2528 Elf_Internal_Rela rela
;
2531 /* This symbols needs a copy reloc. Set it up. */
2532 BFD_ASSERT (h
->dynindx
!= -1);
2534 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2535 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2537 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2538 s
= htab
->elf
.sreldynrelro
;
2540 s
= htab
->elf
.srelbss
;
2541 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2544 /* Mark some specially defined symbols as absolute. */
2545 if (h
== htab
->elf
.hdynamic
2546 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2547 sym
->st_shndx
= SHN_ABS
;
2552 /* Finish up the dynamic sections. */
2555 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2556 bfd
*dynobj
, asection
*sdyn
)
2558 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2559 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2560 size_t dynsize
= bed
->s
->sizeof_dyn
;
2561 bfd_byte
*dyncon
, *dynconend
;
2563 dynconend
= sdyn
->contents
+ sdyn
->size
;
2564 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2566 Elf_Internal_Dyn dyn
;
2569 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2574 s
= htab
->elf
.sgotplt
;
2575 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2578 s
= htab
->elf
.srelplt
;
2579 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2582 s
= htab
->elf
.srelplt
;
2583 dyn
.d_un
.d_val
= s
->size
;
2589 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2595 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2596 struct bfd_link_info
*info
)
2600 struct riscv_elf_link_hash_table
*htab
;
2602 htab
= riscv_elf_hash_table (info
);
2603 BFD_ASSERT (htab
!= NULL
);
2604 dynobj
= htab
->elf
.dynobj
;
2606 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2608 if (elf_hash_table (info
)->dynamic_sections_created
)
2613 splt
= htab
->elf
.splt
;
2614 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2616 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2621 /* Fill in the head and tail entries in the procedure linkage table. */
2625 uint32_t plt_header
[PLT_HEADER_INSNS
];
2626 ret
= riscv_make_plt_header (output_bfd
,
2627 sec_addr (htab
->elf
.sgotplt
),
2628 sec_addr (splt
), plt_header
);
2632 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2633 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2635 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2640 if (htab
->elf
.sgotplt
)
2642 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2644 if (bfd_is_abs_section (output_section
))
2646 (*_bfd_error_handler
)
2647 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2651 if (htab
->elf
.sgotplt
->size
> 0)
2653 /* Write the first two entries in .got.plt, needed for the dynamic
2655 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2656 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2657 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2660 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2665 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2667 if (htab
->elf
.sgot
->size
> 0)
2669 /* Set the first entry in the global offset table to the address of
2670 the dynamic section. */
2671 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2672 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2675 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2681 /* Return address for Ith PLT stub in section PLT, for relocation REL
2682 or (bfd_vma) -1 if it should not be included. */
2685 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2686 const arelent
*rel ATTRIBUTE_UNUSED
)
2688 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2691 static enum elf_reloc_type_class
2692 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2693 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2694 const Elf_Internal_Rela
*rela
)
2696 switch (ELFNN_R_TYPE (rela
->r_info
))
2698 case R_RISCV_RELATIVE
:
2699 return reloc_class_relative
;
2700 case R_RISCV_JUMP_SLOT
:
2701 return reloc_class_plt
;
2703 return reloc_class_copy
;
2705 return reloc_class_normal
;
2709 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2713 riscv_float_abi_string (flagword flags
)
2715 switch (flags
& EF_RISCV_FLOAT_ABI
)
2717 case EF_RISCV_FLOAT_ABI_SOFT
:
2718 return "soft-float";
2720 case EF_RISCV_FLOAT_ABI_SINGLE
:
2721 return "single-float";
2723 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2724 return "double-float";
2726 case EF_RISCV_FLOAT_ABI_QUAD
:
2727 return "quad-float";
2734 /* The information of architecture attribute. */
2735 static riscv_subset_list_t in_subsets
;
2736 static riscv_subset_list_t out_subsets
;
2737 static riscv_subset_list_t merged_subsets
;
2739 /* Predicator for standard extension. */
2742 riscv_std_ext_p (const char *name
)
2744 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2747 /* Error handler when version mis-match. */
2750 riscv_version_mismatch (bfd
*ibfd
,
2751 struct riscv_subset_t
*in
,
2752 struct riscv_subset_t
*out
)
2755 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2758 in
->major_version
, in
->minor_version
,
2759 out
->major_version
, out
->minor_version
);
2762 /* Return true if subset is 'i' or 'e'. */
2765 riscv_i_or_e_p (bfd
*ibfd
,
2767 struct riscv_subset_t
*subset
)
2769 if ((strcasecmp (subset
->name
, "e") != 0)
2770 && (strcasecmp (subset
->name
, "i") != 0))
2773 (_("error: %pB: corrupted ISA string '%s'. "
2774 "First letter should be 'i' or 'e' but got '%s'."),
2775 ibfd
, arch
, subset
->name
);
2781 /* Merge standard extensions.
2784 Return FALSE if failed to merge.
2788 `in_arch`: Raw arch string for input object.
2789 `out_arch`: Raw arch string for output object.
2790 `pin`: subset list for input object, and it'll skip all merged subset after
2792 `pout`: Like `pin`, but for output object. */
2795 riscv_merge_std_ext (bfd
*ibfd
,
2796 const char *in_arch
,
2797 const char *out_arch
,
2798 struct riscv_subset_t
**pin
,
2799 struct riscv_subset_t
**pout
)
2801 const char *standard_exts
= riscv_supported_std_ext ();
2803 struct riscv_subset_t
*in
= *pin
;
2804 struct riscv_subset_t
*out
= *pout
;
2806 /* First letter should be 'i' or 'e'. */
2807 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2810 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2813 if (in
->name
[0] != out
->name
[0])
2815 /* TODO: We might allow merge 'i' with 'e'. */
2817 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2818 ibfd
, in
->name
, out
->name
);
2821 else if ((in
->major_version
!= out
->major_version
) ||
2822 (in
->minor_version
!= out
->minor_version
))
2824 /* TODO: Allow different merge policy. */
2825 riscv_version_mismatch (ibfd
, in
, out
);
2829 riscv_add_subset (&merged_subsets
,
2830 in
->name
, in
->major_version
, in
->minor_version
);
2835 /* Handle standard extension first. */
2836 for (p
= standard_exts
; *p
; ++p
)
2838 char find_ext
[2] = {*p
, '\0'};
2839 struct riscv_subset_t
*find_in
=
2840 riscv_lookup_subset (&in_subsets
, find_ext
);
2841 struct riscv_subset_t
*find_out
=
2842 riscv_lookup_subset (&out_subsets
, find_ext
);
2844 if (find_in
== NULL
&& find_out
== NULL
)
2847 /* Check version is same or not. */
2848 /* TODO: Allow different merge policy. */
2849 if ((find_in
!= NULL
&& find_out
!= NULL
)
2850 && ((find_in
->major_version
!= find_out
->major_version
)
2851 || (find_in
->minor_version
!= find_out
->minor_version
)))
2853 riscv_version_mismatch (ibfd
, in
, out
);
2857 struct riscv_subset_t
*merged
= find_in
? find_in
: find_out
;
2858 riscv_add_subset (&merged_subsets
, merged
->name
,
2859 merged
->major_version
, merged
->minor_version
);
2862 /* Skip all standard extensions. */
2863 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2864 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2872 /* If C is a prefix class, then return the EXT string without the prefix.
2873 Otherwise return the entire EXT string. */
2876 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
2880 case RV_ISA_CLASS_X
: return &ext
[1];
2881 case RV_ISA_CLASS_S
: return &ext
[1];
2882 case RV_ISA_CLASS_Z
: return &ext
[1];
2883 default: return ext
;
2887 /* Compare prefixed extension names canonically. */
2890 riscv_prefix_cmp (const char *a
, const char *b
)
2892 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
2893 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
2895 /* Extension name without prefix */
2896 const char *anp
= riscv_skip_prefix (a
, ca
);
2897 const char *bnp
= riscv_skip_prefix (b
, cb
);
2900 return strcasecmp (anp
, bnp
);
2902 return (int)ca
- (int)cb
;
2905 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2906 object subset list. Likewise for POUT and the output object. Return TRUE
2907 on success and FALSE when a conflict is found. */
2910 riscv_merge_multi_letter_ext (bfd
*ibfd
,
2911 riscv_subset_t
**pin
,
2912 riscv_subset_t
**pout
)
2914 riscv_subset_t
*in
= *pin
;
2915 riscv_subset_t
*out
= *pout
;
2916 riscv_subset_t
*tail
;
2922 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
2926 /* `in' comes before `out', append `in' and increment. */
2927 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2933 /* `out' comes before `in', append `out' and increment. */
2934 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2935 out
->minor_version
);
2940 /* Both present, check version and increment both. */
2941 if ((in
->major_version
!= out
->major_version
)
2942 || (in
->minor_version
!= out
->minor_version
))
2944 riscv_version_mismatch (ibfd
, in
, out
);
2948 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2949 out
->minor_version
);
2956 /* If we're here, either `in' or `out' is running longer than
2957 the other. So, we need to append the corresponding tail. */
2958 tail
= in
? in
: out
;
2962 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
2963 tail
->minor_version
);
2971 /* Merge Tag_RISCV_arch attribute. */
2974 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2976 riscv_subset_t
*in
, *out
;
2977 char *merged_arch_str
;
2979 unsigned xlen_in
, xlen_out
;
2980 merged_subsets
.head
= NULL
;
2981 merged_subsets
.tail
= NULL
;
2983 riscv_parse_subset_t rpe_in
;
2984 riscv_parse_subset_t rpe_out
;
2986 rpe_in
.subset_list
= &in_subsets
;
2987 rpe_in
.error_handler
= _bfd_error_handler
;
2988 rpe_in
.xlen
= &xlen_in
;
2990 rpe_out
.subset_list
= &out_subsets
;
2991 rpe_out
.error_handler
= _bfd_error_handler
;
2992 rpe_out
.xlen
= &xlen_out
;
2994 if (in_arch
== NULL
&& out_arch
== NULL
)
2997 if (in_arch
== NULL
&& out_arch
!= NULL
)
3000 if (in_arch
!= NULL
&& out_arch
== NULL
)
3003 /* Parse subset from arch string. */
3004 if (!riscv_parse_subset (&rpe_in
, in_arch
))
3007 if (!riscv_parse_subset (&rpe_out
, out_arch
))
3010 /* Checking XLEN. */
3011 if (xlen_out
!= xlen_in
)
3014 (_("error: %pB: ISA string of input (%s) doesn't match "
3015 "output (%s)."), ibfd
, in_arch
, out_arch
);
3019 /* Merge subset list. */
3020 in
= in_subsets
.head
;
3021 out
= out_subsets
.head
;
3023 /* Merge standard extension. */
3024 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3027 /* Merge all non-single letter extensions with single call. */
3028 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3031 if (xlen_in
!= xlen_out
)
3034 (_("error: %pB: XLEN of input (%u) doesn't match "
3035 "output (%u)."), ibfd
, xlen_in
, xlen_out
);
3039 if (xlen_in
!= ARCH_SIZE
)
3042 (_("error: %pB: Unsupported XLEN (%u), you might be "
3043 "using wrong emulation."), ibfd
, xlen_in
);
3047 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3049 /* Release the subset lists. */
3050 riscv_release_subset_list (&in_subsets
);
3051 riscv_release_subset_list (&out_subsets
);
3052 riscv_release_subset_list (&merged_subsets
);
3054 return merged_arch_str
;
3057 /* Merge object attributes from IBFD into output_bfd of INFO.
3058 Raise an error if there are conflicting attributes. */
3061 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3063 bfd
*obfd
= info
->output_bfd
;
3064 obj_attribute
*in_attr
;
3065 obj_attribute
*out_attr
;
3066 bfd_boolean result
= TRUE
;
3067 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3070 /* Skip linker created files. */
3071 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3074 /* Skip any input that doesn't have an attribute section.
3075 This enables to link object files without attribute section with
3077 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3080 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3082 /* This is the first object. Copy the attributes. */
3083 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3085 out_attr
= elf_known_obj_attributes_proc (obfd
);
3087 /* Use the Tag_null value to indicate the attributes have been
3094 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3095 out_attr
= elf_known_obj_attributes_proc (obfd
);
3097 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3101 case Tag_RISCV_arch
:
3102 if (!out_attr
[Tag_RISCV_arch
].s
)
3103 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3104 else if (in_attr
[Tag_RISCV_arch
].s
3105 && out_attr
[Tag_RISCV_arch
].s
)
3107 /* Check arch compatible. */
3109 riscv_merge_arch_attr_info (ibfd
,
3110 in_attr
[Tag_RISCV_arch
].s
,
3111 out_attr
[Tag_RISCV_arch
].s
);
3112 if (merged_arch
== NULL
)
3115 out_attr
[Tag_RISCV_arch
].s
= "";
3118 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3121 case Tag_RISCV_priv_spec
:
3122 case Tag_RISCV_priv_spec_minor
:
3123 case Tag_RISCV_priv_spec_revision
:
3124 if (out_attr
[i
].i
!= in_attr
[i
].i
)
3127 (_("error: %pB: conflicting priv spec version "
3128 "(major/minor/revision)."), ibfd
);
3132 case Tag_RISCV_unaligned_access
:
3133 out_attr
[i
].i
|= in_attr
[i
].i
;
3135 case Tag_RISCV_stack_align
:
3136 if (out_attr
[i
].i
== 0)
3137 out_attr
[i
].i
= in_attr
[i
].i
;
3138 else if (in_attr
[i
].i
!= 0
3139 && out_attr
[i
].i
!= 0
3140 && out_attr
[i
].i
!= in_attr
[i
].i
)
3143 (_("error: %pB use %u-byte stack aligned but the output "
3144 "use %u-byte stack aligned."),
3145 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3150 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3153 /* If out_attr was copied from in_attr then it won't have a type yet. */
3154 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3155 out_attr
[i
].type
= in_attr
[i
].type
;
3158 /* Merge Tag_compatibility attributes and any common GNU ones. */
3159 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3162 /* Check for any attributes not known on RISC-V. */
3163 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3168 /* Merge backend specific data from an object file to the output
3169 object file when linking. */
3172 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3174 bfd
*obfd
= info
->output_bfd
;
3175 flagword new_flags
, old_flags
;
3177 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3180 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3182 (*_bfd_error_handler
)
3183 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3184 " target emulation `%s' does not match `%s'"),
3185 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3189 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3192 if (!riscv_merge_attributes (ibfd
, info
))
3195 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3196 old_flags
= elf_elfheader (obfd
)->e_flags
;
3198 if (! elf_flags_init (obfd
))
3200 elf_flags_init (obfd
) = TRUE
;
3201 elf_elfheader (obfd
)->e_flags
= new_flags
;
3205 /* Check to see if the input BFD actually contains any sections. If not,
3206 its flags may not have been initialized either, but it cannot actually
3207 cause any incompatibility. Do not short-circuit dynamic objects; their
3208 section list may be emptied by elf_link_add_object_symbols.
3210 Also check to see if there are no code sections in the input. In this
3211 case, there is no need to check for code specific flags. */
3212 if (!(ibfd
->flags
& DYNAMIC
))
3214 bfd_boolean null_input_bfd
= TRUE
;
3215 bfd_boolean only_data_sections
= TRUE
;
3218 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3220 if ((bfd_section_flags (sec
)
3221 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3222 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3223 only_data_sections
= FALSE
;
3225 null_input_bfd
= FALSE
;
3229 if (null_input_bfd
|| only_data_sections
)
3233 /* Disallow linking different float ABIs. */
3234 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3236 (*_bfd_error_handler
)
3237 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3238 riscv_float_abi_string (new_flags
),
3239 riscv_float_abi_string (old_flags
));
3243 /* Disallow linking RVE and non-RVE. */
3244 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3246 (*_bfd_error_handler
)
3247 (_("%pB: can't link RVE with other target"), ibfd
);
3251 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3252 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3257 bfd_set_error (bfd_error_bad_value
);
3261 /* Delete some bytes from a section while relaxing. */
3264 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3265 struct bfd_link_info
*link_info
)
3267 unsigned int i
, symcount
;
3268 bfd_vma toaddr
= sec
->size
;
3269 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3270 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3271 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3272 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3273 bfd_byte
*contents
= data
->this_hdr
.contents
;
3275 /* Actually delete the bytes. */
3277 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3279 /* Adjust the location of all of the relocs. Note that we need not
3280 adjust the addends, since all PC-relative references must be against
3281 symbols, which we will adjust below. */
3282 for (i
= 0; i
< sec
->reloc_count
; i
++)
3283 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3284 data
->relocs
[i
].r_offset
-= count
;
3286 /* Adjust the local symbols defined in this section. */
3287 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3289 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3290 if (sym
->st_shndx
== sec_shndx
)
3292 /* If the symbol is in the range of memory we just moved, we
3293 have to adjust its value. */
3294 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3295 sym
->st_value
-= count
;
3297 /* If the symbol *spans* the bytes we just deleted (i.e. its
3298 *end* is in the moved bytes but its *start* isn't), then we
3299 must adjust its size.
3301 This test needs to use the original value of st_value, otherwise
3302 we might accidentally decrease size when deleting bytes right
3303 before the symbol. But since deleted relocs can't span across
3304 symbols, we can't have both a st_value and a st_size decrease,
3305 so it is simpler to just use an else. */
3306 else if (sym
->st_value
<= addr
3307 && sym
->st_value
+ sym
->st_size
> addr
3308 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3309 sym
->st_size
-= count
;
3313 /* Now adjust the global symbols defined in this section. */
3314 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3315 - symtab_hdr
->sh_info
);
3317 for (i
= 0; i
< symcount
; i
++)
3319 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3321 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3322 containing the definition of __wrap_SYMBOL, includes a direct
3323 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3324 the same symbol (which is __wrap_SYMBOL), but still exist as two
3325 different symbols in 'sym_hashes', we don't want to adjust
3326 the global symbol __wrap_SYMBOL twice. */
3327 /* The same problem occurs with symbols that are versioned_hidden, as
3328 foo becomes an alias for foo@BAR, and hence they need the same
3330 if (link_info
->wrap_hash
!= NULL
3331 || sym_hash
->versioned
== versioned_hidden
)
3333 struct elf_link_hash_entry
**cur_sym_hashes
;
3335 /* Loop only over the symbols which have already been checked. */
3336 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3339 /* If the current symbol is identical to 'sym_hash', that means
3340 the symbol was already adjusted (or at least checked). */
3341 if (*cur_sym_hashes
== sym_hash
)
3344 /* Don't adjust the symbol again. */
3345 if (cur_sym_hashes
< &sym_hashes
[i
])
3349 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3350 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3351 && sym_hash
->root
.u
.def
.section
== sec
)
3353 /* As above, adjust the value if needed. */
3354 if (sym_hash
->root
.u
.def
.value
> addr
3355 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3356 sym_hash
->root
.u
.def
.value
-= count
;
3358 /* As above, adjust the size if needed. */
3359 else if (sym_hash
->root
.u
.def
.value
<= addr
3360 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3361 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3362 sym_hash
->size
-= count
;
3369 /* A second format for recording PC-relative hi relocations. This stores the
3370 information required to relax them to GP-relative addresses. */
3372 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3373 struct riscv_pcgp_hi_reloc
3380 bfd_boolean undefined_weak
;
3381 riscv_pcgp_hi_reloc
*next
;
3384 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3385 struct riscv_pcgp_lo_reloc
3388 riscv_pcgp_lo_reloc
*next
;
3393 riscv_pcgp_hi_reloc
*hi
;
3394 riscv_pcgp_lo_reloc
*lo
;
3395 } riscv_pcgp_relocs
;
3397 /* Initialize the pcgp reloc info in P. */
3400 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3407 /* Free the pcgp reloc info in P. */
3410 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3411 bfd
*abfd ATTRIBUTE_UNUSED
,
3412 asection
*sec ATTRIBUTE_UNUSED
)
3414 riscv_pcgp_hi_reloc
*c
;
3415 riscv_pcgp_lo_reloc
*l
;
3417 for (c
= p
->hi
; c
!= NULL
;)
3419 riscv_pcgp_hi_reloc
*next
= c
->next
;
3424 for (l
= p
->lo
; l
!= NULL
;)
3426 riscv_pcgp_lo_reloc
*next
= l
->next
;
3432 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3433 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3434 relax the corresponding lo part reloc. */
3437 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3438 bfd_vma hi_addend
, bfd_vma hi_addr
,
3439 unsigned hi_sym
, asection
*sym_sec
,
3440 bfd_boolean undefined_weak
)
3442 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3445 new->hi_sec_off
= hi_sec_off
;
3446 new->hi_addend
= hi_addend
;
3447 new->hi_addr
= hi_addr
;
3448 new->hi_sym
= hi_sym
;
3449 new->sym_sec
= sym_sec
;
3450 new->undefined_weak
= undefined_weak
;
3456 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3457 This is used by a lo part reloc to find the corresponding hi part reloc. */
3459 static riscv_pcgp_hi_reloc
*
3460 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3462 riscv_pcgp_hi_reloc
*c
;
3464 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3465 if (c
->hi_sec_off
== hi_sec_off
)
3470 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3471 This is used to record relocs that can't be relaxed. */
3474 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3476 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3479 new->hi_sec_off
= hi_sec_off
;
3485 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3486 This is used by a hi part reloc to find the corresponding lo part reloc. */
3489 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3491 riscv_pcgp_lo_reloc
*c
;
3493 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3494 if (c
->hi_sec_off
== hi_sec_off
)
3499 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3500 struct bfd_link_info
*,
3501 Elf_Internal_Rela
*,
3502 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3503 riscv_pcgp_relocs
*,
3504 bfd_boolean undefined_weak
);
3506 /* Relax AUIPC + JALR into JAL. */
3509 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3510 struct bfd_link_info
*link_info
,
3511 Elf_Internal_Rela
*rel
,
3513 bfd_vma max_alignment
,
3514 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3516 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3517 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3519 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3520 bfd_signed_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3521 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3522 bfd_vma auipc
, jalr
;
3523 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3525 /* If the call crosses section boundaries, an alignment directive could
3526 cause the PC-relative offset to later increase, so we need to add in the
3527 max alignment of any section inclusive from the call to the target.
3528 Otherwise, we only need to use the alignment of the current section. */
3529 if (VALID_UJTYPE_IMM (foff
))
3531 if (sym_sec
->output_section
== sec
->output_section
3532 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3533 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3534 foff
+= (foff
< 0 ? -max_alignment
: max_alignment
);
3537 /* See if this function call can be shortened. */
3538 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3541 /* Shorten the function call. */
3542 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3544 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3545 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3546 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3547 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3549 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3550 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3554 /* Relax to C.J[AL] rd, addr. */
3555 r_type
= R_RISCV_RVC_JUMP
;
3556 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3559 else if (VALID_UJTYPE_IMM (foff
))
3561 /* Relax to JAL rd, addr. */
3562 r_type
= R_RISCV_JAL
;
3563 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3565 else /* near_zero */
3567 /* Relax to JALR rd, x0, addr. */
3568 r_type
= R_RISCV_LO12_I
;
3569 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3572 /* Replace the R_RISCV_CALL reloc. */
3573 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3574 /* Replace the AUIPC. */
3575 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3577 /* Delete unnecessary JALR. */
3579 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3583 /* Traverse all output sections and return the max alignment. */
3586 _bfd_riscv_get_max_alignment (asection
*sec
)
3588 unsigned int max_alignment_power
= 0;
3591 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3593 if (o
->alignment_power
> max_alignment_power
)
3594 max_alignment_power
= o
->alignment_power
;
3597 return (bfd_vma
) 1 << max_alignment_power
;
3600 /* Relax non-PIC global variable references. */
3603 _bfd_riscv_relax_lui (bfd
*abfd
,
3606 struct bfd_link_info
*link_info
,
3607 Elf_Internal_Rela
*rel
,
3609 bfd_vma max_alignment
,
3610 bfd_vma reserve_size
,
3612 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3613 bfd_boolean undefined_weak
)
3615 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3616 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3617 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3619 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3623 /* If gp and the symbol are in the same output section, which is not the
3624 abs section, then consider only that output section's alignment. */
3625 struct bfd_link_hash_entry
*h
=
3626 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3628 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3629 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3630 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3633 /* Is the reference in range of x0 or gp?
3634 Valid gp range conservatively because of alignment issue. */
3636 || (VALID_ITYPE_IMM (symval
)
3638 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3640 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3642 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3643 switch (ELFNN_R_TYPE (rel
->r_info
))
3645 case R_RISCV_LO12_I
:
3648 /* Change the RS1 to zero. */
3649 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3650 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3651 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3654 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3657 case R_RISCV_LO12_S
:
3660 /* Change the RS1 to zero. */
3661 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3662 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3663 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3666 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3670 /* We can delete the unnecessary LUI and reloc. */
3671 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3673 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3681 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3682 account for this assuming page alignment at worst. In the presence of
3683 RELRO segment the linker aligns it by one page size, therefore sections
3684 after the segment can be moved more than one page. */
3687 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3688 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3689 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3690 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3691 : ELF_MAXPAGESIZE
)))
3693 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3694 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3695 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3696 if (rd
== 0 || rd
== X_SP
)
3699 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3700 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3702 /* Replace the R_RISCV_HI20 reloc. */
3703 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3706 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3713 /* Relax non-PIC TLS references. */
3716 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3718 asection
*sym_sec ATTRIBUTE_UNUSED
,
3719 struct bfd_link_info
*link_info
,
3720 Elf_Internal_Rela
*rel
,
3722 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3723 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3725 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3726 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3728 /* See if this symbol is in range of tp. */
3729 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3732 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3733 switch (ELFNN_R_TYPE (rel
->r_info
))
3735 case R_RISCV_TPREL_LO12_I
:
3736 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3739 case R_RISCV_TPREL_LO12_S
:
3740 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3743 case R_RISCV_TPREL_HI20
:
3744 case R_RISCV_TPREL_ADD
:
3745 /* We can delete the unnecessary instruction and reloc. */
3746 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3748 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3755 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3758 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3760 struct bfd_link_info
*link_info
,
3761 Elf_Internal_Rela
*rel
,
3763 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3764 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3765 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3766 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3767 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3769 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3770 bfd_vma alignment
= 1, pos
;
3771 while (alignment
<= rel
->r_addend
)
3774 symval
-= rel
->r_addend
;
3775 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3776 bfd_vma nop_bytes
= aligned_addr
- symval
;
3778 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3779 sec
->sec_flg0
= TRUE
;
3781 /* Make sure there are enough NOPs to actually achieve the alignment. */
3782 if (rel
->r_addend
< nop_bytes
)
3785 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3786 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3787 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3788 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3789 bfd_set_error (bfd_error_bad_value
);
3793 /* Delete the reloc. */
3794 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3796 /* If the number of NOPs is already correct, there's nothing to do. */
3797 if (nop_bytes
== rel
->r_addend
)
3800 /* Write as many RISC-V NOPs as we need. */
3801 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3802 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3804 /* Write a final RVC NOP if need be. */
3805 if (nop_bytes
% 4 != 0)
3806 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3808 /* Delete the excess bytes. */
3809 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3810 rel
->r_addend
- nop_bytes
, link_info
);
3813 /* Relax PC-relative references to GP-relative references. */
3816 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3819 struct bfd_link_info
*link_info
,
3820 Elf_Internal_Rela
*rel
,
3822 bfd_vma max_alignment
,
3823 bfd_vma reserve_size
,
3824 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3825 riscv_pcgp_relocs
*pcgp_relocs
,
3826 bfd_boolean undefined_weak
)
3828 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3829 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3831 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3833 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3834 * actual target address. */
3835 riscv_pcgp_hi_reloc hi_reloc
;
3836 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3837 switch (ELFNN_R_TYPE (rel
->r_info
))
3839 case R_RISCV_PCREL_LO12_I
:
3840 case R_RISCV_PCREL_LO12_S
:
3842 /* If the %lo has an addend, it isn't for the label pointing at the
3843 hi part instruction, but rather for the symbol pointed at by the
3844 hi part instruction. So we must subtract it here for the lookup.
3845 It is still used below in the final symbol address. */
3846 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3847 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3851 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3856 symval
= hi_reloc
.hi_addr
;
3857 sym_sec
= hi_reloc
.sym_sec
;
3859 /* We can not know whether the undefined weak symbol is referenced
3860 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3861 we have to record the 'undefined_weak' flag when handling the
3862 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3863 undefined_weak
= hi_reloc
.undefined_weak
;
3867 case R_RISCV_PCREL_HI20
:
3868 /* Mergeable symbols and code might later move out of range. */
3869 if (! undefined_weak
3870 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3873 /* If the cooresponding lo relocation has already been seen then it's not
3874 * safe to relax this relocation. */
3875 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3886 /* If gp and the symbol are in the same output section, which is not the
3887 abs section, then consider only that output section's alignment. */
3888 struct bfd_link_hash_entry
*h
=
3889 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3891 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3892 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3893 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3896 /* Is the reference in range of x0 or gp?
3897 Valid gp range conservatively because of alignment issue. */
3899 || (VALID_ITYPE_IMM (symval
)
3901 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3903 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3905 unsigned sym
= hi_reloc
.hi_sym
;
3906 switch (ELFNN_R_TYPE (rel
->r_info
))
3908 case R_RISCV_PCREL_LO12_I
:
3911 /* Change the RS1 to zero, and then modify the relocation
3912 type to R_RISCV_LO12_I. */
3913 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3914 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3915 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3916 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3917 rel
->r_addend
= hi_reloc
.hi_addend
;
3921 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3922 rel
->r_addend
+= hi_reloc
.hi_addend
;
3926 case R_RISCV_PCREL_LO12_S
:
3929 /* Change the RS1 to zero, and then modify the relocation
3930 type to R_RISCV_LO12_S. */
3931 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3932 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3933 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3934 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3935 rel
->r_addend
= hi_reloc
.hi_addend
;
3939 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3940 rel
->r_addend
+= hi_reloc
.hi_addend
;
3944 case R_RISCV_PCREL_HI20
:
3945 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3949 ELFNN_R_SYM(rel
->r_info
),
3952 /* We can delete the unnecessary AUIPC and reloc. */
3953 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3965 /* Relax PC-relative references to GP-relative references. */
3968 _bfd_riscv_relax_delete (bfd
*abfd
,
3970 asection
*sym_sec ATTRIBUTE_UNUSED
,
3971 struct bfd_link_info
*link_info
,
3972 Elf_Internal_Rela
*rel
,
3973 bfd_vma symval ATTRIBUTE_UNUSED
,
3974 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3975 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3976 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3977 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3978 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3980 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3983 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3987 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3988 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3989 disabled, handles code alignment directives. */
3992 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3993 struct bfd_link_info
*info
,
3996 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3997 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3998 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3999 Elf_Internal_Rela
*relocs
;
4000 bfd_boolean ret
= FALSE
;
4002 bfd_vma max_alignment
, reserve_size
= 0;
4003 riscv_pcgp_relocs pcgp_relocs
;
4007 if (bfd_link_relocatable (info
)
4009 || (sec
->flags
& SEC_RELOC
) == 0
4010 || sec
->reloc_count
== 0
4011 || (info
->disable_target_specific_optimizations
4012 && info
->relax_pass
== 0))
4015 riscv_init_pcgp_relocs (&pcgp_relocs
);
4017 /* Read this BFD's relocs if we haven't done so already. */
4019 relocs
= data
->relocs
;
4020 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4021 info
->keep_memory
)))
4026 max_alignment
= htab
->max_alignment
;
4027 if (max_alignment
== (bfd_vma
) -1)
4029 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4030 htab
->max_alignment
= max_alignment
;
4034 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4036 /* Examine and consider relaxing each reloc. */
4037 for (i
= 0; i
< sec
->reloc_count
; i
++)
4040 Elf_Internal_Rela
*rel
= relocs
+ i
;
4041 relax_func_t relax_func
;
4042 int type
= ELFNN_R_TYPE (rel
->r_info
);
4045 bfd_boolean undefined_weak
= FALSE
;
4048 if (info
->relax_pass
== 0)
4050 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
4051 relax_func
= _bfd_riscv_relax_call
;
4052 else if (type
== R_RISCV_HI20
4053 || type
== R_RISCV_LO12_I
4054 || type
== R_RISCV_LO12_S
)
4055 relax_func
= _bfd_riscv_relax_lui
;
4056 else if (!bfd_link_pic(info
)
4057 && (type
== R_RISCV_PCREL_HI20
4058 || type
== R_RISCV_PCREL_LO12_I
4059 || type
== R_RISCV_PCREL_LO12_S
))
4060 relax_func
= _bfd_riscv_relax_pc
;
4061 else if (type
== R_RISCV_TPREL_HI20
4062 || type
== R_RISCV_TPREL_ADD
4063 || type
== R_RISCV_TPREL_LO12_I
4064 || type
== R_RISCV_TPREL_LO12_S
)
4065 relax_func
= _bfd_riscv_relax_tls_le
;
4069 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4070 if (i
== sec
->reloc_count
- 1
4071 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4072 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4075 /* Skip over the R_RISCV_RELAX. */
4078 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4079 relax_func
= _bfd_riscv_relax_delete
;
4080 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4081 relax_func
= _bfd_riscv_relax_align
;
4085 data
->relocs
= relocs
;
4087 /* Read this BFD's contents if we haven't done so already. */
4088 if (!data
->this_hdr
.contents
4089 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4092 /* Read this BFD's symbols if we haven't done so already. */
4093 if (symtab_hdr
->sh_info
!= 0
4094 && !symtab_hdr
->contents
4095 && !(symtab_hdr
->contents
=
4096 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4097 symtab_hdr
->sh_info
,
4098 0, NULL
, NULL
, NULL
)))
4101 /* Get the value of the symbol referred to by the reloc. */
4102 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4104 /* A local symbol. */
4105 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4106 + ELFNN_R_SYM (rel
->r_info
));
4107 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4108 ? 0 : isym
->st_size
- rel
->r_addend
;
4110 if (isym
->st_shndx
== SHN_UNDEF
)
4111 sym_sec
= sec
, symval
= rel
->r_offset
;
4114 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4115 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4117 /* The purpose of this code is unknown. It breaks linker scripts
4118 for embedded development that place sections at address zero.
4119 This code is believed to be unnecessary. Disabling it but not
4120 yet removing it, in case something breaks. */
4121 if (sec_addr (sym_sec
) == 0)
4124 symval
= isym
->st_value
;
4126 symtype
= ELF_ST_TYPE (isym
->st_info
);
4131 struct elf_link_hash_entry
*h
;
4133 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4134 h
= elf_sym_hashes (abfd
)[indx
];
4136 while (h
->root
.type
== bfd_link_hash_indirect
4137 || h
->root
.type
== bfd_link_hash_warning
)
4138 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4140 if (h
->root
.type
== bfd_link_hash_undefweak
4141 && (relax_func
== _bfd_riscv_relax_lui
4142 || relax_func
== _bfd_riscv_relax_pc
))
4144 /* For the lui and auipc relaxations, since the symbol
4145 value of an undefined weak symbol is always be zero,
4146 we can optimize the patterns into a single LI/MV/ADDI
4149 Note that, creating shared libraries and pie output may
4150 break the rule above. Fortunately, since we do not relax
4151 pc relocs when creating shared libraries and pie output,
4152 and the absolute address access for R_RISCV_HI20 isn't
4153 allowed when "-fPIC" is set, the problem of creating shared
4154 libraries can not happen currently. Once we support the
4155 auipc relaxations when creating shared libraries, then we will
4156 need the more rigorous checking for this optimization. */
4157 undefined_weak
= TRUE
;
4160 /* This line has to match the check in riscv_elf_relocate_section
4161 in the R_RISCV_CALL[_PLT] case. */
4162 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4164 sym_sec
= htab
->elf
.splt
;
4165 symval
= h
->plt
.offset
;
4167 else if (undefined_weak
)
4170 sym_sec
= bfd_und_section_ptr
;
4172 else if ((h
->root
.type
== bfd_link_hash_defined
4173 || h
->root
.type
== bfd_link_hash_defweak
)
4174 && h
->root
.u
.def
.section
!= NULL
4175 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4177 symval
= h
->root
.u
.def
.value
;
4178 sym_sec
= h
->root
.u
.def
.section
;
4183 if (h
->type
!= STT_FUNC
)
4185 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4189 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4190 && (sym_sec
->flags
& SEC_MERGE
))
4192 /* At this stage in linking, no SEC_MERGE symbol has been
4193 adjusted, so all references to such symbols need to be
4194 passed through _bfd_merged_section_offset. (Later, in
4195 relocate_section, all SEC_MERGE symbols *except* for
4196 section symbols have been adjusted.)
4198 gas may reduce relocations against symbols in SEC_MERGE
4199 sections to a relocation against the section symbol when
4200 the original addend was zero. When the reloc is against
4201 a section symbol we should include the addend in the
4202 offset passed to _bfd_merged_section_offset, since the
4203 location of interest is the original symbol. On the
4204 other hand, an access to "sym+addend" where "sym" is not
4205 a section symbol should not include the addend; Such an
4206 access is presumed to be an offset from "sym"; The
4207 location of interest is just "sym". */
4208 if (symtype
== STT_SECTION
)
4209 symval
+= rel
->r_addend
;
4211 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4212 elf_section_data (sym_sec
)->sec_info
,
4215 if (symtype
!= STT_SECTION
)
4216 symval
+= rel
->r_addend
;
4219 symval
+= rel
->r_addend
;
4221 symval
+= sec_addr (sym_sec
);
4223 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4224 max_alignment
, reserve_size
, again
,
4225 &pcgp_relocs
, undefined_weak
))
4232 if (relocs
!= data
->relocs
)
4234 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4240 # define PRSTATUS_SIZE 204
4241 # define PRSTATUS_OFFSET_PR_CURSIG 12
4242 # define PRSTATUS_OFFSET_PR_PID 24
4243 # define PRSTATUS_OFFSET_PR_REG 72
4244 # define ELF_GREGSET_T_SIZE 128
4245 # define PRPSINFO_SIZE 128
4246 # define PRPSINFO_OFFSET_PR_PID 16
4247 # define PRPSINFO_OFFSET_PR_FNAME 32
4248 # define PRPSINFO_OFFSET_PR_PSARGS 48
4250 # define PRSTATUS_SIZE 376
4251 # define PRSTATUS_OFFSET_PR_CURSIG 12
4252 # define PRSTATUS_OFFSET_PR_PID 32
4253 # define PRSTATUS_OFFSET_PR_REG 112
4254 # define ELF_GREGSET_T_SIZE 256
4255 # define PRPSINFO_SIZE 136
4256 # define PRPSINFO_OFFSET_PR_PID 24
4257 # define PRPSINFO_OFFSET_PR_FNAME 40
4258 # define PRPSINFO_OFFSET_PR_PSARGS 56
4261 /* Support for core dump NOTE sections. */
4264 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4266 switch (note
->descsz
)
4271 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4273 elf_tdata (abfd
)->core
->signal
4274 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4277 elf_tdata (abfd
)->core
->lwpid
4278 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4282 /* Make a ".reg/999" section. */
4283 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4284 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4288 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4290 switch (note
->descsz
)
4295 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4297 elf_tdata (abfd
)->core
->pid
4298 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4301 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4302 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4305 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4306 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4310 /* Note that for some reason, a spurious space is tacked
4311 onto the end of the args in some (at least one anyway)
4312 implementations, so strip it off if it exists. */
4315 char *command
= elf_tdata (abfd
)->core
->command
;
4316 int n
= strlen (command
);
4318 if (0 < n
&& command
[n
- 1] == ' ')
4319 command
[n
- 1] = '\0';
4325 /* Set the right mach type. */
4327 riscv_elf_object_p (bfd
*abfd
)
4329 /* There are only two mach types in RISCV currently. */
4330 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4331 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4333 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4338 /* Determine whether an object attribute tag takes an integer, a
4342 riscv_elf_obj_attrs_arg_type (int tag
)
4344 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4347 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4348 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4350 #define elf_backend_reloc_type_class riscv_reloc_type_class
4352 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4353 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4354 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4355 #define bfd_elfNN_bfd_merge_private_bfd_data \
4356 _bfd_riscv_elf_merge_private_bfd_data
4358 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4359 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4360 #define elf_backend_check_relocs riscv_elf_check_relocs
4361 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4362 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4363 #define elf_backend_relocate_section riscv_elf_relocate_section
4364 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4365 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4366 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4367 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4368 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4369 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4370 #define elf_backend_object_p riscv_elf_object_p
4371 #define elf_info_to_howto_rel NULL
4372 #define elf_info_to_howto riscv_info_to_howto_rela
4373 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4374 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4376 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4378 #define elf_backend_can_gc_sections 1
4379 #define elf_backend_can_refcount 1
4380 #define elf_backend_want_got_plt 1
4381 #define elf_backend_plt_readonly 1
4382 #define elf_backend_plt_alignment 4
4383 #define elf_backend_want_plt_sym 1
4384 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4385 #define elf_backend_want_dynrelro 1
4386 #define elf_backend_rela_normal 1
4387 #define elf_backend_default_execstack 0
4389 #undef elf_backend_obj_attrs_vendor
4390 #define elf_backend_obj_attrs_vendor "riscv"
4391 #undef elf_backend_obj_attrs_arg_type
4392 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4393 #undef elf_backend_obj_attrs_section_type
4394 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4395 #undef elf_backend_obj_attrs_section
4396 #define elf_backend_obj_attrs_section ".riscv.attributes"
4398 #include "elfNN-target.h"