1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
35 /* Internal relocations used exclusively by the relaxation pass. */
36 #define R_RISCV_DELETE (R_RISCV_max + 1)
40 #define MINUS_ONE ((bfd_vma)0 - 1)
42 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
44 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
46 /* The name of the dynamic interpreter. This is put in the .interp
49 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
50 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
52 #define ELF_ARCH bfd_arch_riscv
53 #define ELF_TARGET_ID RISCV_ELF_DATA
54 #define ELF_MACHINE_CODE EM_RISCV
55 #define ELF_MAXPAGESIZE 0x1000
56 #define ELF_COMMONPAGESIZE 0x1000
58 /* RISC-V ELF linker hash entry. */
60 struct riscv_elf_link_hash_entry
62 struct elf_link_hash_entry elf
;
72 #define riscv_elf_hash_entry(ent) \
73 ((struct riscv_elf_link_hash_entry *)(ent))
75 struct _bfd_riscv_elf_obj_tdata
77 struct elf_obj_tdata root
;
79 /* tls_type for each local got entry. */
80 char *local_got_tls_type
;
83 #define _bfd_riscv_elf_tdata(abfd) \
84 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
86 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
87 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
89 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
90 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
91 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
93 #define is_riscv_elf(bfd) \
94 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
95 && elf_tdata (bfd) != NULL \
96 && elf_object_id (bfd) == RISCV_ELF_DATA)
99 elfNN_riscv_mkobject (bfd
*abfd
)
101 return bfd_elf_allocate_object (abfd
,
102 sizeof (struct _bfd_riscv_elf_obj_tdata
),
106 #include "elf/common.h"
107 #include "elf/internal.h"
109 struct riscv_elf_link_hash_table
111 struct elf_link_hash_table elf
;
113 /* Short-cuts to get to dynamic linker sections. */
116 /* The max alignment of output sections. */
117 bfd_vma max_alignment
;
121 /* Get the RISC-V ELF linker hash table from a link_info structure. */
122 #define riscv_elf_hash_table(p) \
123 ((is_elf_hash_table ((p)->hash) \
124 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
125 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
128 riscv_info_to_howto_rela (bfd
*abfd
,
130 Elf_Internal_Rela
*dst
)
132 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
133 return cache_ptr
->howto
!= NULL
;
137 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
139 const struct elf_backend_data
*bed
;
142 bed
= get_elf_backend_data (abfd
);
143 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
144 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
149 #define PLT_HEADER_INSNS 8
150 #define PLT_ENTRY_INSNS 4
151 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
152 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
154 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
156 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
158 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
161 riscv_elf_got_plt_val (bfd_vma plt_index
, struct bfd_link_info
*info
)
163 return sec_addr (riscv_elf_hash_table (info
)->elf
.sgotplt
)
164 + GOTPLT_HEADER_SIZE
+ (plt_index
* GOT_ENTRY_SIZE
);
168 # define MATCH_LREG MATCH_LW
170 # define MATCH_LREG MATCH_LD
173 /* Generate a PLT header. */
176 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
179 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
180 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
182 /* RVE has no t3 register, so this won't work, and is not supported. */
183 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
185 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
190 /* auipc t2, %hi(.got.plt)
191 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
192 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
193 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
194 addi t0, t2, %lo(.got.plt) # &.got.plt
195 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
196 l[w|d] t0, PTRSIZE(t0) # link map
199 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
200 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
201 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
202 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
203 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
204 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
205 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
206 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
211 /* Generate a PLT entry. */
214 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
217 /* RVE has no t3 register, so this won't work, and is not supported. */
218 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
220 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
225 /* auipc t3, %hi(.got.plt entry)
226 l[w|d] t3, %lo(.got.plt entry)(t3)
230 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
231 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
232 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
233 entry
[3] = RISCV_NOP
;
238 /* Create an entry in an RISC-V ELF linker hash table. */
240 static struct bfd_hash_entry
*
241 link_hash_newfunc (struct bfd_hash_entry
*entry
,
242 struct bfd_hash_table
*table
, const char *string
)
244 /* Allocate the structure if it has not already been allocated by a
249 bfd_hash_allocate (table
,
250 sizeof (struct riscv_elf_link_hash_entry
));
255 /* Call the allocation method of the superclass. */
256 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
259 struct riscv_elf_link_hash_entry
*eh
;
261 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
262 eh
->tls_type
= GOT_UNKNOWN
;
268 /* Create a RISC-V ELF linker hash table. */
270 static struct bfd_link_hash_table
*
271 riscv_elf_link_hash_table_create (bfd
*abfd
)
273 struct riscv_elf_link_hash_table
*ret
;
274 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
276 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
280 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
281 sizeof (struct riscv_elf_link_hash_entry
),
288 ret
->max_alignment
= (bfd_vma
) -1;
289 return &ret
->elf
.root
;
292 /* Create the .got section. */
295 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
299 struct elf_link_hash_entry
*h
;
300 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
301 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
303 /* This function may be called more than once. */
304 if (htab
->sgot
!= NULL
)
307 flags
= bed
->dynamic_sec_flags
;
309 s
= bfd_make_section_anyway_with_flags (abfd
,
310 (bed
->rela_plts_and_copies_p
311 ? ".rela.got" : ".rel.got"),
312 (bed
->dynamic_sec_flags
315 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
319 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
321 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 /* The first bit of the global offset table is the header. */
326 s
->size
+= bed
->got_header_size
;
328 if (bed
->want_got_plt
)
330 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
332 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
336 /* Reserve room for the header. */
337 s
->size
+= GOTPLT_HEADER_SIZE
;
340 if (bed
->want_got_sym
)
342 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
343 section. We don't do this in the linker script because we don't want
344 to define the symbol if we are not creating a global offset
346 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
347 "_GLOBAL_OFFSET_TABLE_");
348 elf_hash_table (info
)->hgot
= h
;
356 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
357 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
361 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
362 struct bfd_link_info
*info
)
364 struct riscv_elf_link_hash_table
*htab
;
366 htab
= riscv_elf_hash_table (info
);
367 BFD_ASSERT (htab
!= NULL
);
369 if (!riscv_elf_create_got_section (dynobj
, info
))
372 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
375 if (!bfd_link_pic (info
))
377 /* Technically, this section doesn't have contents. It is used as the
378 target of TLS copy relocs, to copy TLS data from shared libraries into
379 the executable. However, if we don't mark it as loadable, then it
380 matches the IS_TBSS test in ldlang.c, and there is no run-time address
381 space allocated for it even though it has SEC_ALLOC. That test is
382 correct for .tbss, but not correct for this section. There is also
383 a second problem that having a section with no contents can only work
384 if it comes after all sections with contents in the same segment,
385 but the linker script does not guarantee that. This is just mixed in
386 with other .tdata.* sections. We can fix both problems by lying and
387 saying that there are contents. This section is expected to be small
388 so this should not cause a significant extra program startup cost. */
390 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
391 (SEC_ALLOC
| SEC_THREAD_LOCAL
392 | SEC_LOAD
| SEC_DATA
394 | SEC_LINKER_CREATED
));
397 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
398 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
404 /* Copy the extra info we tack onto an elf_link_hash_entry. */
407 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
408 struct elf_link_hash_entry
*dir
,
409 struct elf_link_hash_entry
*ind
)
411 struct riscv_elf_link_hash_entry
*edir
, *eind
;
413 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
414 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
416 if (ind
->root
.type
== bfd_link_hash_indirect
417 && dir
->got
.refcount
<= 0)
419 edir
->tls_type
= eind
->tls_type
;
420 eind
->tls_type
= GOT_UNKNOWN
;
422 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
426 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
427 unsigned long symndx
, char tls_type
)
429 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
431 *new_tls_type
|= tls_type
;
432 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
434 (*_bfd_error_handler
)
435 (_("%pB: `%s' accessed both as normal and thread local symbol"),
436 abfd
, h
? h
->root
.root
.string
: "<local>");
443 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
444 struct elf_link_hash_entry
*h
, long symndx
)
446 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
447 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
449 if (htab
->elf
.sgot
== NULL
)
451 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
457 h
->got
.refcount
+= 1;
461 /* This is a global offset table entry for a local symbol. */
462 if (elf_local_got_refcounts (abfd
) == NULL
)
464 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
465 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
467 _bfd_riscv_elf_local_got_tls_type (abfd
)
468 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
470 elf_local_got_refcounts (abfd
) [symndx
] += 1;
476 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
478 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
480 (*_bfd_error_handler
)
481 (_("%pB: relocation %s against `%s' can not be used when making a shared "
482 "object; recompile with -fPIC"),
483 abfd
, r
? r
->name
: _("<unknown>"),
484 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
485 bfd_set_error (bfd_error_bad_value
);
488 /* Look through the relocs for a section during the first phase, and
489 allocate space in the global offset table or procedure linkage
493 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
494 asection
*sec
, const Elf_Internal_Rela
*relocs
)
496 struct riscv_elf_link_hash_table
*htab
;
497 Elf_Internal_Shdr
*symtab_hdr
;
498 struct elf_link_hash_entry
**sym_hashes
;
499 const Elf_Internal_Rela
*rel
;
500 asection
*sreloc
= NULL
;
502 if (bfd_link_relocatable (info
))
505 htab
= riscv_elf_hash_table (info
);
506 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
507 sym_hashes
= elf_sym_hashes (abfd
);
509 if (htab
->elf
.dynobj
== NULL
)
510 htab
->elf
.dynobj
= abfd
;
512 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
515 unsigned int r_symndx
;
516 struct elf_link_hash_entry
*h
;
518 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
519 r_type
= ELFNN_R_TYPE (rel
->r_info
);
521 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
523 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
528 if (r_symndx
< symtab_hdr
->sh_info
)
532 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
533 while (h
->root
.type
== bfd_link_hash_indirect
534 || h
->root
.type
== bfd_link_hash_warning
)
535 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
540 case R_RISCV_TLS_GD_HI20
:
541 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
542 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
546 case R_RISCV_TLS_GOT_HI20
:
547 if (bfd_link_pic (info
))
548 info
->flags
|= DF_STATIC_TLS
;
549 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
550 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
554 case R_RISCV_GOT_HI20
:
555 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
556 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
561 case R_RISCV_CALL_PLT
:
562 /* These symbol requires a procedure linkage table entry. We
563 actually build the entry in adjust_dynamic_symbol,
564 because these might be a case of linking PIC code without
565 linking in any dynamic objects, in which case we don't
566 need to generate a procedure linkage table after all. */
568 /* If it is a local symbol, then we resolve it directly
569 without creating a PLT entry. */
574 h
->plt
.refcount
+= 1;
579 case R_RISCV_RVC_BRANCH
:
580 case R_RISCV_RVC_JUMP
:
581 case R_RISCV_PCREL_HI20
:
582 /* In shared libraries, these relocs are known to bind locally. */
583 if (bfd_link_pic (info
))
587 case R_RISCV_TPREL_HI20
:
588 if (!bfd_link_executable (info
))
589 return bad_static_reloc (abfd
, r_type
, h
);
591 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
595 if (bfd_link_pic (info
))
596 return bad_static_reloc (abfd
, r_type
, h
);
600 case R_RISCV_JUMP_SLOT
:
601 case R_RISCV_RELATIVE
:
607 /* This reloc might not bind locally. */
611 if (h
!= NULL
&& !bfd_link_pic (info
))
613 /* We may need a .plt entry if the function this reloc
614 refers to is in a shared lib. */
615 h
->plt
.refcount
+= 1;
618 /* If we are creating a shared library, and this is a reloc
619 against a global symbol, or a non PC relative reloc
620 against a local symbol, then we need to copy the reloc
621 into the shared library. However, if we are linking with
622 -Bsymbolic, we do not need to copy a reloc against a
623 global symbol which is defined in an object we are
624 including in the link (i.e., DEF_REGULAR is set). At
625 this point we have not seen all the input files, so it is
626 possible that DEF_REGULAR is not set now but will be set
627 later (it is never cleared). In case of a weak definition,
628 DEF_REGULAR may be cleared later by a strong definition in
629 a shared library. We account for that possibility below by
630 storing information in the relocs_copied field of the hash
631 table entry. A similar situation occurs when creating
632 shared libraries and symbol visibility changes render the
635 If on the other hand, we are creating an executable, we
636 may need to keep relocations for symbols satisfied by a
637 dynamic library if we manage to avoid copy relocs for the
639 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
641 if ((bfd_link_pic (info
)
642 && (sec
->flags
& SEC_ALLOC
) != 0
643 && ((r
!= NULL
&& ! r
->pc_relative
)
646 || h
->root
.type
== bfd_link_hash_defweak
647 || !h
->def_regular
))))
648 || (!bfd_link_pic (info
)
649 && (sec
->flags
& SEC_ALLOC
) != 0
651 && (h
->root
.type
== bfd_link_hash_defweak
652 || !h
->def_regular
)))
654 struct elf_dyn_relocs
*p
;
655 struct elf_dyn_relocs
**head
;
657 /* When creating a shared object, we must copy these
658 relocs into the output file. We create a reloc
659 section in dynobj and make room for the reloc. */
662 sreloc
= _bfd_elf_make_dynamic_reloc_section
663 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
664 abfd
, /*rela?*/ TRUE
);
670 /* If this is a global symbol, we count the number of
671 relocations we need for this symbol. */
673 head
= &h
->dyn_relocs
;
676 /* Track dynamic relocs needed for local syms too.
677 We really need local syms available to do this
682 Elf_Internal_Sym
*isym
;
684 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
689 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
693 vpp
= &elf_section_data (s
)->local_dynrel
;
694 head
= (struct elf_dyn_relocs
**) vpp
;
698 if (p
== NULL
|| p
->sec
!= sec
)
700 size_t amt
= sizeof *p
;
701 p
= ((struct elf_dyn_relocs
*)
702 bfd_alloc (htab
->elf
.dynobj
, amt
));
713 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
718 case R_RISCV_GNU_VTINHERIT
:
719 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
723 case R_RISCV_GNU_VTENTRY
:
724 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
737 riscv_elf_gc_mark_hook (asection
*sec
,
738 struct bfd_link_info
*info
,
739 Elf_Internal_Rela
*rel
,
740 struct elf_link_hash_entry
*h
,
741 Elf_Internal_Sym
*sym
)
744 switch (ELFNN_R_TYPE (rel
->r_info
))
746 case R_RISCV_GNU_VTINHERIT
:
747 case R_RISCV_GNU_VTENTRY
:
751 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
754 /* Adjust a symbol defined by a dynamic object and referenced by a
755 regular object. The current definition is in some section of the
756 dynamic object, but we're not including those sections. We have to
757 change the definition to something the rest of the link can
761 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
762 struct elf_link_hash_entry
*h
)
764 struct riscv_elf_link_hash_table
*htab
;
765 struct riscv_elf_link_hash_entry
* eh
;
769 htab
= riscv_elf_hash_table (info
);
770 BFD_ASSERT (htab
!= NULL
);
772 dynobj
= htab
->elf
.dynobj
;
774 /* Make sure we know what is going on here. */
775 BFD_ASSERT (dynobj
!= NULL
777 || h
->type
== STT_GNU_IFUNC
781 && !h
->def_regular
)));
783 /* If this is a function, put it in the procedure linkage table. We
784 will fill in the contents of the procedure linkage table later
785 (although we could actually do it here). */
786 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
788 if (h
->plt
.refcount
<= 0
789 || SYMBOL_CALLS_LOCAL (info
, h
)
790 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
791 && h
->root
.type
== bfd_link_hash_undefweak
))
793 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
794 input file, but the symbol was never referred to by a dynamic
795 object, or if all references were garbage collected. In such
796 a case, we don't actually need to build a PLT entry. */
797 h
->plt
.offset
= (bfd_vma
) -1;
804 h
->plt
.offset
= (bfd_vma
) -1;
806 /* If this is a weak symbol, and there is a real definition, the
807 processor independent code will have arranged for us to see the
808 real definition first, and we can just use the same value. */
811 struct elf_link_hash_entry
*def
= weakdef (h
);
812 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
813 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
814 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
818 /* This is a reference to a symbol defined by a dynamic object which
819 is not a function. */
821 /* If we are creating a shared library, we must presume that the
822 only references to the symbol are via the global offset table.
823 For such cases we need not do anything here; the relocations will
824 be handled correctly by relocate_section. */
825 if (bfd_link_pic (info
))
828 /* If there are no references to this symbol that do not use the
829 GOT, we don't need to generate a copy reloc. */
833 /* If -z nocopyreloc was given, we won't generate them either. */
834 if (info
->nocopyreloc
)
840 /* If we don't find any dynamic relocs in read-only sections, then
841 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
842 if (!_bfd_elf_readonly_dynrelocs (h
))
848 /* We must allocate the symbol in our .dynbss section, which will
849 become part of the .bss section of the executable. There will be
850 an entry for this symbol in the .dynsym section. The dynamic
851 object will contain position independent code, so all references
852 from the dynamic object to this symbol will go through the global
853 offset table. The dynamic linker will use the .dynsym entry to
854 determine the address it must put in the global offset table, so
855 both the dynamic object and the regular object will refer to the
856 same memory location for the variable. */
858 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
859 to copy the initial value out of the dynamic object and into the
860 runtime process image. We need to remember the offset into the
861 .rel.bss section we are going to use. */
862 eh
= (struct riscv_elf_link_hash_entry
*) h
;
863 if (eh
->tls_type
& ~GOT_NORMAL
)
866 srel
= htab
->elf
.srelbss
;
868 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
870 s
= htab
->elf
.sdynrelro
;
871 srel
= htab
->elf
.sreldynrelro
;
875 s
= htab
->elf
.sdynbss
;
876 srel
= htab
->elf
.srelbss
;
878 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
880 srel
->size
+= sizeof (ElfNN_External_Rela
);
884 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
887 /* Allocate space in .plt, .got and associated reloc sections for
891 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
893 struct bfd_link_info
*info
;
894 struct riscv_elf_link_hash_table
*htab
;
895 struct elf_dyn_relocs
*p
;
897 if (h
->root
.type
== bfd_link_hash_indirect
)
900 info
= (struct bfd_link_info
*) inf
;
901 htab
= riscv_elf_hash_table (info
);
902 BFD_ASSERT (htab
!= NULL
);
904 if (htab
->elf
.dynamic_sections_created
905 && h
->plt
.refcount
> 0)
907 /* Make sure this symbol is output as a dynamic symbol.
908 Undefined weak syms won't yet be marked as dynamic. */
912 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
916 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
918 asection
*s
= htab
->elf
.splt
;
921 s
->size
= PLT_HEADER_SIZE
;
923 h
->plt
.offset
= s
->size
;
925 /* Make room for this entry. */
926 s
->size
+= PLT_ENTRY_SIZE
;
928 /* We also need to make an entry in the .got.plt section. */
929 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
931 /* We also need to make an entry in the .rela.plt section. */
932 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
934 /* If this symbol is not defined in a regular file, and we are
935 not generating a shared library, then set the symbol to this
936 location in the .plt. This is required to make function
937 pointers compare as equal between the normal executable and
938 the shared library. */
939 if (! bfd_link_pic (info
)
942 h
->root
.u
.def
.section
= s
;
943 h
->root
.u
.def
.value
= h
->plt
.offset
;
948 h
->plt
.offset
= (bfd_vma
) -1;
954 h
->plt
.offset
= (bfd_vma
) -1;
958 if (h
->got
.refcount
> 0)
962 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
964 /* Make sure this symbol is output as a dynamic symbol.
965 Undefined weak syms won't yet be marked as dynamic. */
969 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
974 h
->got
.offset
= s
->size
;
975 dyn
= htab
->elf
.dynamic_sections_created
;
976 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
978 /* TLS_GD needs two dynamic relocs and two GOT slots. */
979 if (tls_type
& GOT_TLS_GD
)
981 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
982 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
985 /* TLS_IE needs one dynamic reloc and one GOT slot. */
986 if (tls_type
& GOT_TLS_IE
)
988 s
->size
+= RISCV_ELF_WORD_BYTES
;
989 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
994 s
->size
+= RISCV_ELF_WORD_BYTES
;
995 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
996 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
997 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1001 h
->got
.offset
= (bfd_vma
) -1;
1003 if (h
->dyn_relocs
== NULL
)
1006 /* In the shared -Bsymbolic case, discard space allocated for
1007 dynamic pc-relative relocs against symbols which turn out to be
1008 defined in regular objects. For the normal shared case, discard
1009 space for pc-relative relocs that have become local due to symbol
1010 visibility changes. */
1012 if (bfd_link_pic (info
))
1014 if (SYMBOL_CALLS_LOCAL (info
, h
))
1016 struct elf_dyn_relocs
**pp
;
1018 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1020 p
->count
-= p
->pc_count
;
1029 /* Also discard relocs on undefined weak syms with non-default
1031 if (h
->dyn_relocs
!= NULL
1032 && h
->root
.type
== bfd_link_hash_undefweak
)
1034 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1035 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1036 h
->dyn_relocs
= NULL
;
1038 /* Make sure undefined weak symbols are output as a dynamic
1040 else if (h
->dynindx
== -1
1041 && !h
->forced_local
)
1043 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1050 /* For the non-shared case, discard space for relocs against
1051 symbols which turn out to need copy relocs or are not
1057 || (htab
->elf
.dynamic_sections_created
1058 && (h
->root
.type
== bfd_link_hash_undefweak
1059 || h
->root
.type
== bfd_link_hash_undefined
))))
1061 /* Make sure this symbol is output as a dynamic symbol.
1062 Undefined weak syms won't yet be marked as dynamic. */
1063 if (h
->dynindx
== -1
1064 && !h
->forced_local
)
1066 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1070 /* If that succeeded, we know we'll be keeping all the
1072 if (h
->dynindx
!= -1)
1076 h
->dyn_relocs
= NULL
;
1081 /* Finally, allocate space. */
1082 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1084 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1085 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1092 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1094 struct riscv_elf_link_hash_table
*htab
;
1099 htab
= riscv_elf_hash_table (info
);
1100 BFD_ASSERT (htab
!= NULL
);
1101 dynobj
= htab
->elf
.dynobj
;
1102 BFD_ASSERT (dynobj
!= NULL
);
1104 if (elf_hash_table (info
)->dynamic_sections_created
)
1106 /* Set the contents of the .interp section to the interpreter. */
1107 if (bfd_link_executable (info
) && !info
->nointerp
)
1109 s
= bfd_get_linker_section (dynobj
, ".interp");
1110 BFD_ASSERT (s
!= NULL
);
1111 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1112 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1116 /* Set up .got offsets for local syms, and space for local dynamic
1118 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1120 bfd_signed_vma
*local_got
;
1121 bfd_signed_vma
*end_local_got
;
1122 char *local_tls_type
;
1123 bfd_size_type locsymcount
;
1124 Elf_Internal_Shdr
*symtab_hdr
;
1127 if (! is_riscv_elf (ibfd
))
1130 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1132 struct elf_dyn_relocs
*p
;
1134 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1136 if (!bfd_is_abs_section (p
->sec
)
1137 && bfd_is_abs_section (p
->sec
->output_section
))
1139 /* Input section has been discarded, either because
1140 it is a copy of a linkonce section or due to
1141 linker script /DISCARD/, so we'll be discarding
1144 else if (p
->count
!= 0)
1146 srel
= elf_section_data (p
->sec
)->sreloc
;
1147 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1148 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1149 info
->flags
|= DF_TEXTREL
;
1154 local_got
= elf_local_got_refcounts (ibfd
);
1158 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1159 locsymcount
= symtab_hdr
->sh_info
;
1160 end_local_got
= local_got
+ locsymcount
;
1161 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1163 srel
= htab
->elf
.srelgot
;
1164 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1168 *local_got
= s
->size
;
1169 s
->size
+= RISCV_ELF_WORD_BYTES
;
1170 if (*local_tls_type
& GOT_TLS_GD
)
1171 s
->size
+= RISCV_ELF_WORD_BYTES
;
1172 if (bfd_link_pic (info
)
1173 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1174 srel
->size
+= sizeof (ElfNN_External_Rela
);
1177 *local_got
= (bfd_vma
) -1;
1181 /* Allocate global sym .plt and .got entries, and space for global
1182 sym dynamic relocs. */
1183 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1185 if (htab
->elf
.sgotplt
)
1187 struct elf_link_hash_entry
*got
;
1188 got
= elf_link_hash_lookup (elf_hash_table (info
),
1189 "_GLOBAL_OFFSET_TABLE_",
1190 FALSE
, FALSE
, FALSE
);
1192 /* Don't allocate .got.plt section if there are no GOT nor PLT
1193 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1195 || !got
->ref_regular_nonweak
)
1196 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1197 && (htab
->elf
.splt
== NULL
1198 || htab
->elf
.splt
->size
== 0)
1199 && (htab
->elf
.sgot
== NULL
1200 || (htab
->elf
.sgot
->size
1201 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1202 htab
->elf
.sgotplt
->size
= 0;
1205 /* The check_relocs and adjust_dynamic_symbol entry points have
1206 determined the sizes of the various dynamic sections. Allocate
1208 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1210 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1213 if (s
== htab
->elf
.splt
1214 || s
== htab
->elf
.sgot
1215 || s
== htab
->elf
.sgotplt
1216 || s
== htab
->elf
.sdynbss
1217 || s
== htab
->elf
.sdynrelro
1218 || s
== htab
->sdyntdata
)
1220 /* Strip this section if we don't need it; see the
1223 else if (strncmp (s
->name
, ".rela", 5) == 0)
1227 /* We use the reloc_count field as a counter if we need
1228 to copy relocs into the output file. */
1234 /* It's not one of our sections. */
1240 /* If we don't need this section, strip it from the
1241 output file. This is mostly to handle .rela.bss and
1242 .rela.plt. We must create both sections in
1243 create_dynamic_sections, because they must be created
1244 before the linker maps input sections to output
1245 sections. The linker does that before
1246 adjust_dynamic_symbol is called, and it is that
1247 function which decides whether anything needs to go
1248 into these sections. */
1249 s
->flags
|= SEC_EXCLUDE
;
1253 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1256 /* Allocate memory for the section contents. Zero the memory
1257 for the benefit of .rela.plt, which has 4 unused entries
1258 at the beginning, and we don't want garbage. */
1259 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1260 if (s
->contents
== NULL
)
1264 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, TRUE
);
1268 #define DTP_OFFSET 0x800
1270 /* Return the relocation value for a TLS dtp-relative reloc. */
1273 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1275 /* If tls_sec is NULL, we should have signalled an error already. */
1276 if (elf_hash_table (info
)->tls_sec
== NULL
)
1278 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1281 /* Return the relocation value for a static TLS tp-relative relocation. */
1284 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1286 /* If tls_sec is NULL, we should have signalled an error already. */
1287 if (elf_hash_table (info
)->tls_sec
== NULL
)
1289 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1292 /* Return the global pointer's value, or 0 if it is not in use. */
1295 riscv_global_pointer_value (struct bfd_link_info
*info
)
1297 struct bfd_link_hash_entry
*h
;
1299 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1300 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1303 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1306 /* Emplace a static relocation. */
1308 static bfd_reloc_status_type
1309 perform_relocation (const reloc_howto_type
*howto
,
1310 const Elf_Internal_Rela
*rel
,
1312 asection
*input_section
,
1316 if (howto
->pc_relative
)
1317 value
-= sec_addr (input_section
) + rel
->r_offset
;
1318 value
+= rel
->r_addend
;
1320 switch (ELFNN_R_TYPE (rel
->r_info
))
1323 case R_RISCV_TPREL_HI20
:
1324 case R_RISCV_PCREL_HI20
:
1325 case R_RISCV_GOT_HI20
:
1326 case R_RISCV_TLS_GOT_HI20
:
1327 case R_RISCV_TLS_GD_HI20
:
1328 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1329 return bfd_reloc_overflow
;
1330 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1333 case R_RISCV_LO12_I
:
1334 case R_RISCV_GPREL_I
:
1335 case R_RISCV_TPREL_LO12_I
:
1336 case R_RISCV_TPREL_I
:
1337 case R_RISCV_PCREL_LO12_I
:
1338 value
= ENCODE_ITYPE_IMM (value
);
1341 case R_RISCV_LO12_S
:
1342 case R_RISCV_GPREL_S
:
1343 case R_RISCV_TPREL_LO12_S
:
1344 case R_RISCV_TPREL_S
:
1345 case R_RISCV_PCREL_LO12_S
:
1346 value
= ENCODE_STYPE_IMM (value
);
1350 case R_RISCV_CALL_PLT
:
1351 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1352 return bfd_reloc_overflow
;
1353 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1354 | (ENCODE_ITYPE_IMM (value
) << 32);
1358 if (!VALID_UJTYPE_IMM (value
))
1359 return bfd_reloc_overflow
;
1360 value
= ENCODE_UJTYPE_IMM (value
);
1363 case R_RISCV_BRANCH
:
1364 if (!VALID_SBTYPE_IMM (value
))
1365 return bfd_reloc_overflow
;
1366 value
= ENCODE_SBTYPE_IMM (value
);
1369 case R_RISCV_RVC_BRANCH
:
1370 if (!VALID_RVC_B_IMM (value
))
1371 return bfd_reloc_overflow
;
1372 value
= ENCODE_RVC_B_IMM (value
);
1375 case R_RISCV_RVC_JUMP
:
1376 if (!VALID_RVC_J_IMM (value
))
1377 return bfd_reloc_overflow
;
1378 value
= ENCODE_RVC_J_IMM (value
);
1381 case R_RISCV_RVC_LUI
:
1382 if (RISCV_CONST_HIGH_PART (value
) == 0)
1384 /* Linker relaxation can convert an address equal to or greater than
1385 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1386 valid immediate. We can fix this by converting it to a C.LI. */
1387 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1388 contents
+ rel
->r_offset
);
1389 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1390 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1391 value
= ENCODE_RVC_IMM (0);
1393 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1394 return bfd_reloc_overflow
;
1396 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1414 case R_RISCV_32_PCREL
:
1415 case R_RISCV_TLS_DTPREL32
:
1416 case R_RISCV_TLS_DTPREL64
:
1419 case R_RISCV_DELETE
:
1420 return bfd_reloc_ok
;
1423 return bfd_reloc_notsupported
;
1426 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1427 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1428 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1430 return bfd_reloc_ok
;
1433 /* Remember all PC-relative high-part relocs we've encountered to help us
1434 later resolve the corresponding low-part relocs. */
1440 } riscv_pcrel_hi_reloc
;
1442 typedef struct riscv_pcrel_lo_reloc
1444 asection
* input_section
;
1445 struct bfd_link_info
* info
;
1446 reloc_howto_type
* howto
;
1447 const Elf_Internal_Rela
* reloc
;
1450 bfd_byte
* contents
;
1451 struct riscv_pcrel_lo_reloc
* next
;
1452 } riscv_pcrel_lo_reloc
;
1457 riscv_pcrel_lo_reloc
*lo_relocs
;
1458 } riscv_pcrel_relocs
;
1461 riscv_pcrel_reloc_hash (const void *entry
)
1463 const riscv_pcrel_hi_reloc
*e
= entry
;
1464 return (hashval_t
)(e
->address
>> 2);
1468 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1470 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1471 return e1
->address
== e2
->address
;
1475 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1478 p
->lo_relocs
= NULL
;
1479 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1480 riscv_pcrel_reloc_eq
, free
);
1481 return p
->hi_relocs
!= NULL
;
1485 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1487 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1491 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1496 htab_delete (p
->hi_relocs
);
1500 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1501 struct bfd_link_info
*info
,
1505 const reloc_howto_type
*howto
,
1508 /* We may need to reference low addreses in PC-relative modes even when the
1509 * PC is far away from these addresses. For example, undefweak references
1510 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1511 * addresses that we can link PC-relative programs at, the linker can't
1512 * actually relocate references to those symbols. In order to allow these
1513 * programs to work we simply convert the PC-relative auipc sequences to
1514 * 0-relative lui sequences. */
1515 if (bfd_link_pic (info
))
1518 /* If it's possible to reference the symbol using auipc we do so, as that's
1519 * more in the spirit of the PC-relative relocations we're processing. */
1520 bfd_vma offset
= addr
- pc
;
1521 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1524 /* If it's impossible to reference this with a LUI-based offset then don't
1525 * bother to convert it at all so users still see the PC-relative relocation
1526 * in the truncation message. */
1527 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1530 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1532 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1533 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1534 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1539 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1540 bfd_vma value
, bfd_boolean absolute
)
1542 bfd_vma offset
= absolute
? value
: value
- addr
;
1543 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1544 riscv_pcrel_hi_reloc
**slot
=
1545 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1547 BFD_ASSERT (*slot
== NULL
);
1548 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1556 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1557 asection
*input_section
,
1558 struct bfd_link_info
*info
,
1559 reloc_howto_type
*howto
,
1560 const Elf_Internal_Rela
*reloc
,
1565 riscv_pcrel_lo_reloc
*entry
;
1566 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1569 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1570 name
, contents
, p
->lo_relocs
};
1571 p
->lo_relocs
= entry
;
1576 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1578 riscv_pcrel_lo_reloc
*r
;
1580 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1582 bfd
*input_bfd
= r
->input_section
->owner
;
1584 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1585 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1587 /* Check for overflow into bit 11 when adding reloc addend. */
1588 || (! (entry
->value
& 0x800)
1589 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1591 char *string
= (entry
== NULL
1592 ? "%pcrel_lo missing matching %pcrel_hi"
1593 : "%pcrel_lo overflow with an addend");
1594 (*r
->info
->callbacks
->reloc_dangerous
)
1595 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1599 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1600 input_bfd
, r
->contents
);
1606 /* Relocate a RISC-V ELF section.
1608 The RELOCATE_SECTION function is called by the new ELF backend linker
1609 to handle the relocations for a section.
1611 The relocs are always passed as Rela structures.
1613 This function is responsible for adjusting the section contents as
1614 necessary, and (if generating a relocatable output file) adjusting
1615 the reloc addend as necessary.
1617 This function does not have to worry about setting the reloc
1618 address or the reloc symbol index.
1620 LOCAL_SYMS is a pointer to the swapped in local symbols.
1622 LOCAL_SECTIONS is an array giving the section in the input file
1623 corresponding to the st_shndx field of each local symbol.
1625 The global hash table entry for the global symbols can be found
1626 via elf_sym_hashes (input_bfd).
1628 When generating relocatable output, this function must handle
1629 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1630 going to be the section symbol corresponding to the output
1631 section, which means that the addend must be adjusted
1635 riscv_elf_relocate_section (bfd
*output_bfd
,
1636 struct bfd_link_info
*info
,
1638 asection
*input_section
,
1640 Elf_Internal_Rela
*relocs
,
1641 Elf_Internal_Sym
*local_syms
,
1642 asection
**local_sections
)
1644 Elf_Internal_Rela
*rel
;
1645 Elf_Internal_Rela
*relend
;
1646 riscv_pcrel_relocs pcrel_relocs
;
1647 bfd_boolean ret
= FALSE
;
1648 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1649 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1650 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1651 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1652 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1653 bfd_boolean absolute
;
1655 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1658 relend
= relocs
+ input_section
->reloc_count
;
1659 for (rel
= relocs
; rel
< relend
; rel
++)
1661 unsigned long r_symndx
;
1662 struct elf_link_hash_entry
*h
;
1663 Elf_Internal_Sym
*sym
;
1666 bfd_reloc_status_type r
= bfd_reloc_ok
;
1668 bfd_vma off
, ie_off
;
1669 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1670 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1671 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1672 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1673 const char *msg
= NULL
;
1674 char *msg_buf
= NULL
;
1675 bfd_boolean resolved_to_zero
;
1678 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1681 /* This is a final link. */
1682 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1686 unresolved_reloc
= FALSE
;
1687 if (r_symndx
< symtab_hdr
->sh_info
)
1689 sym
= local_syms
+ r_symndx
;
1690 sec
= local_sections
[r_symndx
];
1691 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1695 bfd_boolean warned
, ignored
;
1697 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1698 r_symndx
, symtab_hdr
, sym_hashes
,
1700 unresolved_reloc
, warned
, ignored
);
1703 /* To avoid generating warning messages about truncated
1704 relocations, set the relocation's address to be the same as
1705 the start of this section. */
1706 if (input_section
->output_section
!= NULL
)
1707 relocation
= input_section
->output_section
->vma
;
1713 if (sec
!= NULL
&& discarded_section (sec
))
1714 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1715 rel
, 1, relend
, howto
, 0, contents
);
1717 if (bfd_link_relocatable (info
))
1721 name
= h
->root
.root
.string
;
1724 name
= (bfd_elf_string_from_elf_section
1725 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1726 if (name
== NULL
|| *name
== '\0')
1727 name
= bfd_section_name (sec
);
1730 resolved_to_zero
= (h
!= NULL
1731 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1737 case R_RISCV_TPREL_ADD
:
1739 case R_RISCV_JUMP_SLOT
:
1740 case R_RISCV_RELATIVE
:
1741 /* These require nothing of us at all. */
1745 case R_RISCV_BRANCH
:
1746 case R_RISCV_RVC_BRANCH
:
1747 case R_RISCV_RVC_LUI
:
1748 case R_RISCV_LO12_I
:
1749 case R_RISCV_LO12_S
:
1754 case R_RISCV_32_PCREL
:
1755 case R_RISCV_DELETE
:
1756 /* These require no special handling beyond perform_relocation. */
1759 case R_RISCV_GOT_HI20
:
1762 bfd_boolean dyn
, pic
;
1764 off
= h
->got
.offset
;
1765 BFD_ASSERT (off
!= (bfd_vma
) -1);
1766 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1767 pic
= bfd_link_pic (info
);
1769 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1770 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1772 /* This is actually a static link, or it is a
1773 -Bsymbolic link and the symbol is defined
1774 locally, or the symbol was forced to be local
1775 because of a version file. We must initialize
1776 this entry in the global offset table. Since the
1777 offset must always be a multiple of the word size,
1778 we use the least significant bit to record whether
1779 we have initialized it already.
1781 When doing a dynamic link, we create a .rela.got
1782 relocation entry to initialize the value. This
1783 is done in the finish_dynamic_symbol routine. */
1788 bfd_put_NN (output_bfd
, relocation
,
1789 htab
->elf
.sgot
->contents
+ off
);
1794 unresolved_reloc
= FALSE
;
1798 BFD_ASSERT (local_got_offsets
!= NULL
1799 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1801 off
= local_got_offsets
[r_symndx
];
1803 /* The offset must always be a multiple of the word size.
1804 So, we can use the least significant bit to record
1805 whether we have already processed this entry. */
1810 if (bfd_link_pic (info
))
1813 Elf_Internal_Rela outrel
;
1815 /* We need to generate a R_RISCV_RELATIVE reloc
1816 for the dynamic linker. */
1817 s
= htab
->elf
.srelgot
;
1818 BFD_ASSERT (s
!= NULL
);
1820 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1822 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1823 outrel
.r_addend
= relocation
;
1825 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1828 bfd_put_NN (output_bfd
, relocation
,
1829 htab
->elf
.sgot
->contents
+ off
);
1830 local_got_offsets
[r_symndx
] |= 1;
1833 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1834 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1841 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1842 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1844 r
= bfd_reloc_notsupported
;
1845 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1846 relocation
, absolute
))
1847 r
= bfd_reloc_overflow
;
1855 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1856 contents
+ rel
->r_offset
);
1857 relocation
= old_value
+ relocation
;
1867 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1868 contents
+ rel
->r_offset
);
1869 relocation
= old_value
- relocation
;
1874 case R_RISCV_CALL_PLT
:
1875 /* Handle a call to an undefined weak function. This won't be
1876 relaxed, so we have to handle it here. */
1877 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
1878 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
1880 /* We can use x0 as the base register. */
1881 bfd_vma insn
= bfd_get_32 (input_bfd
,
1882 contents
+ rel
->r_offset
+ 4);
1883 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1884 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
1885 /* Set the relocation value so that we get 0 after the pc
1886 relative adjustment. */
1887 relocation
= sec_addr (input_section
) + rel
->r_offset
;
1892 case R_RISCV_RVC_JUMP
:
1893 /* This line has to match the check in _bfd_riscv_relax_section. */
1894 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
1896 /* Refer to the PLT entry. */
1897 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
1898 unresolved_reloc
= FALSE
;
1902 case R_RISCV_TPREL_HI20
:
1903 relocation
= tpoff (info
, relocation
);
1906 case R_RISCV_TPREL_LO12_I
:
1907 case R_RISCV_TPREL_LO12_S
:
1908 relocation
= tpoff (info
, relocation
);
1911 case R_RISCV_TPREL_I
:
1912 case R_RISCV_TPREL_S
:
1913 relocation
= tpoff (info
, relocation
);
1914 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
1916 /* We can use tp as the base register. */
1917 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1918 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1919 insn
|= X_TP
<< OP_SH_RS1
;
1920 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
1923 r
= bfd_reloc_overflow
;
1926 case R_RISCV_GPREL_I
:
1927 case R_RISCV_GPREL_S
:
1929 bfd_vma gp
= riscv_global_pointer_value (info
);
1930 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
1931 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
1933 /* We can use x0 or gp as the base register. */
1934 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1935 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1938 rel
->r_addend
-= gp
;
1939 insn
|= X_GP
<< OP_SH_RS1
;
1941 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
1944 r
= bfd_reloc_overflow
;
1948 case R_RISCV_PCREL_HI20
:
1949 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1956 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1957 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1959 r
= bfd_reloc_notsupported
;
1960 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1961 relocation
+ rel
->r_addend
,
1963 r
= bfd_reloc_overflow
;
1966 case R_RISCV_PCREL_LO12_I
:
1967 case R_RISCV_PCREL_LO12_S
:
1968 /* We don't allow section symbols plus addends as the auipc address,
1969 because then riscv_relax_delete_bytes would have to search through
1970 all relocs to update these addends. This is also ambiguous, as
1971 we do allow offsets to be added to the target address, which are
1972 not to be used to find the auipc address. */
1973 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
1974 || (h
!= NULL
&& h
->type
== STT_SECTION
))
1977 msg
= _("%pcrel_lo section symbol with an addend");
1978 r
= bfd_reloc_dangerous
;
1982 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
1983 howto
, rel
, relocation
, name
,
1986 r
= bfd_reloc_overflow
;
1989 case R_RISCV_TLS_DTPREL32
:
1990 case R_RISCV_TLS_DTPREL64
:
1991 relocation
= dtpoff (info
, relocation
);
1996 if ((input_section
->flags
& SEC_ALLOC
) == 0)
1999 if ((bfd_link_pic (info
)
2001 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2002 && !resolved_to_zero
)
2003 || h
->root
.type
!= bfd_link_hash_undefweak
)
2004 && (! howto
->pc_relative
2005 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2006 || (!bfd_link_pic (info
)
2012 || h
->root
.type
== bfd_link_hash_undefweak
2013 || h
->root
.type
== bfd_link_hash_undefined
)))
2015 Elf_Internal_Rela outrel
;
2016 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2018 /* When generating a shared object, these relocations
2019 are copied into the output file to be resolved at run
2023 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2025 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2026 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2027 outrel
.r_offset
+= sec_addr (input_section
);
2029 if (skip_dynamic_relocation
)
2030 memset (&outrel
, 0, sizeof outrel
);
2031 else if (h
!= NULL
&& h
->dynindx
!= -1
2032 && !(bfd_link_pic (info
)
2033 && SYMBOLIC_BIND (info
, h
)
2036 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2037 outrel
.r_addend
= rel
->r_addend
;
2041 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2042 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2045 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2046 if (skip_static_relocation
)
2051 case R_RISCV_TLS_GOT_HI20
:
2055 case R_RISCV_TLS_GD_HI20
:
2058 off
= h
->got
.offset
;
2063 off
= local_got_offsets
[r_symndx
];
2064 local_got_offsets
[r_symndx
] |= 1;
2067 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2068 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2069 /* If this symbol is referenced by both GD and IE TLS, the IE
2070 reference's GOT slot follows the GD reference's slots. */
2072 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2073 ie_off
= 2 * GOT_ENTRY_SIZE
;
2079 Elf_Internal_Rela outrel
;
2081 bfd_boolean need_relocs
= FALSE
;
2083 if (htab
->elf
.srelgot
== NULL
)
2088 bfd_boolean dyn
, pic
;
2089 dyn
= htab
->elf
.dynamic_sections_created
;
2090 pic
= bfd_link_pic (info
);
2092 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2093 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2097 /* The GOT entries have not been initialized yet. Do it
2098 now, and emit any relocations. */
2099 if ((bfd_link_pic (info
) || indx
!= 0)
2101 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2102 || h
->root
.type
!= bfd_link_hash_undefweak
))
2105 if (tls_type
& GOT_TLS_GD
)
2109 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2110 outrel
.r_addend
= 0;
2111 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2112 bfd_put_NN (output_bfd
, 0,
2113 htab
->elf
.sgot
->contents
+ off
);
2114 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2117 BFD_ASSERT (! unresolved_reloc
);
2118 bfd_put_NN (output_bfd
,
2119 dtpoff (info
, relocation
),
2120 (htab
->elf
.sgot
->contents
+ off
+
2121 RISCV_ELF_WORD_BYTES
));
2125 bfd_put_NN (output_bfd
, 0,
2126 (htab
->elf
.sgot
->contents
+ off
+
2127 RISCV_ELF_WORD_BYTES
));
2128 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2129 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2130 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2135 /* If we are not emitting relocations for a
2136 general dynamic reference, then we must be in a
2137 static link or an executable link with the
2138 symbol binding locally. Mark it as belonging
2139 to module 1, the executable. */
2140 bfd_put_NN (output_bfd
, 1,
2141 htab
->elf
.sgot
->contents
+ off
);
2142 bfd_put_NN (output_bfd
,
2143 dtpoff (info
, relocation
),
2144 (htab
->elf
.sgot
->contents
+ off
+
2145 RISCV_ELF_WORD_BYTES
));
2149 if (tls_type
& GOT_TLS_IE
)
2153 bfd_put_NN (output_bfd
, 0,
2154 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2155 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2157 outrel
.r_addend
= 0;
2159 outrel
.r_addend
= tpoff (info
, relocation
);
2160 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2161 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2165 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2166 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2171 BFD_ASSERT (off
< (bfd_vma
) -2);
2172 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2173 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2175 r
= bfd_reloc_overflow
;
2176 unresolved_reloc
= FALSE
;
2180 r
= bfd_reloc_notsupported
;
2183 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2184 because such sections are not SEC_ALLOC and thus ld.so will
2185 not process them. */
2186 if (unresolved_reloc
2187 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2189 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2190 rel
->r_offset
) != (bfd_vma
) -1)
2195 case R_RISCV_RVC_JUMP
:
2196 if (asprintf (&msg_buf
,
2197 _("%%X%%P: relocation %s against `%s' can "
2198 "not be used when making a shared object; "
2199 "recompile with -fPIC\n"),
2201 h
->root
.root
.string
) == -1)
2206 if (asprintf (&msg_buf
,
2207 _("%%X%%P: unresolvable %s relocation against "
2210 h
->root
.root
.string
) == -1)
2216 r
= bfd_reloc_notsupported
;
2219 if (r
== bfd_reloc_ok
)
2220 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2221 input_bfd
, contents
);
2223 /* We should have already detected the error and set message before.
2224 If the error message isn't set since the linker runs out of memory
2225 or we don't set it before, then we should set the default message
2226 with the "internal error" string here. */
2232 case bfd_reloc_overflow
:
2233 info
->callbacks
->reloc_overflow
2234 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2235 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2238 case bfd_reloc_undefined
:
2239 info
->callbacks
->undefined_symbol
2240 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2244 case bfd_reloc_outofrange
:
2246 msg
= _("%X%P: internal error: out of range error\n");
2249 case bfd_reloc_notsupported
:
2251 msg
= _("%X%P: internal error: unsupported relocation error\n");
2254 case bfd_reloc_dangerous
:
2255 /* The error message should already be set. */
2257 msg
= _("dangerous relocation error");
2258 info
->callbacks
->reloc_dangerous
2259 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2263 msg
= _("%X%P: internal error: unknown error\n");
2267 /* Do not report error message for the dangerous relocation again. */
2268 if (msg
&& r
!= bfd_reloc_dangerous
)
2269 info
->callbacks
->einfo (msg
);
2271 /* Free the unused `msg_buf`. */
2274 /* We already reported the error via a callback, so don't try to report
2275 it again by returning false. That leads to spurious errors. */
2280 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2282 riscv_free_pcrel_relocs (&pcrel_relocs
);
2286 /* Finish up dynamic symbol handling. We set the contents of various
2287 dynamic sections here. */
2290 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2291 struct bfd_link_info
*info
,
2292 struct elf_link_hash_entry
*h
,
2293 Elf_Internal_Sym
*sym
)
2295 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2296 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2298 if (h
->plt
.offset
!= (bfd_vma
) -1)
2300 /* We've decided to create a PLT entry for this symbol. */
2302 bfd_vma i
, header_address
, plt_idx
, got_address
;
2303 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2304 Elf_Internal_Rela rela
;
2306 BFD_ASSERT (h
->dynindx
!= -1);
2308 /* Calculate the address of the PLT header. */
2309 header_address
= sec_addr (htab
->elf
.splt
);
2311 /* Calculate the index of the entry. */
2312 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2314 /* Calculate the address of the .got.plt entry. */
2315 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2317 /* Find out where the .plt entry should go. */
2318 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2320 /* Fill in the PLT entry itself. */
2321 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2322 header_address
+ h
->plt
.offset
,
2326 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2327 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2329 /* Fill in the initial value of the .got.plt entry. */
2330 loc
= htab
->elf
.sgotplt
->contents
2331 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2332 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2334 /* Fill in the entry in the .rela.plt section. */
2335 rela
.r_offset
= got_address
;
2337 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2339 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2340 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2342 if (!h
->def_regular
)
2344 /* Mark the symbol as undefined, rather than as defined in
2345 the .plt section. Leave the value alone. */
2346 sym
->st_shndx
= SHN_UNDEF
;
2347 /* If the symbol is weak, we do need to clear the value.
2348 Otherwise, the PLT entry would provide a definition for
2349 the symbol even if the symbol wasn't defined anywhere,
2350 and so the symbol would never be NULL. */
2351 if (!h
->ref_regular_nonweak
)
2356 if (h
->got
.offset
!= (bfd_vma
) -1
2357 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2358 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2362 Elf_Internal_Rela rela
;
2364 /* This symbol has an entry in the GOT. Set it up. */
2366 sgot
= htab
->elf
.sgot
;
2367 srela
= htab
->elf
.srelgot
;
2368 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2370 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2372 /* If this is a local symbol reference, we just want to emit a RELATIVE
2373 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2374 the symbol was forced to be local because of a version file.
2375 The entry in the global offset table will already have been
2376 initialized in the relocate_section function. */
2377 if (bfd_link_pic (info
)
2378 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2380 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2381 asection
*sec
= h
->root
.u
.def
.section
;
2382 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2383 rela
.r_addend
= (h
->root
.u
.def
.value
2384 + sec
->output_section
->vma
2385 + sec
->output_offset
);
2389 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2390 BFD_ASSERT (h
->dynindx
!= -1);
2391 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2395 bfd_put_NN (output_bfd
, 0,
2396 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2397 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2402 Elf_Internal_Rela rela
;
2405 /* This symbols needs a copy reloc. Set it up. */
2406 BFD_ASSERT (h
->dynindx
!= -1);
2408 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2409 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2411 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2412 s
= htab
->elf
.sreldynrelro
;
2414 s
= htab
->elf
.srelbss
;
2415 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2418 /* Mark some specially defined symbols as absolute. */
2419 if (h
== htab
->elf
.hdynamic
2420 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2421 sym
->st_shndx
= SHN_ABS
;
2426 /* Finish up the dynamic sections. */
2429 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2430 bfd
*dynobj
, asection
*sdyn
)
2432 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2433 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2434 size_t dynsize
= bed
->s
->sizeof_dyn
;
2435 bfd_byte
*dyncon
, *dynconend
;
2437 dynconend
= sdyn
->contents
+ sdyn
->size
;
2438 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2440 Elf_Internal_Dyn dyn
;
2443 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2448 s
= htab
->elf
.sgotplt
;
2449 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2452 s
= htab
->elf
.srelplt
;
2453 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2456 s
= htab
->elf
.srelplt
;
2457 dyn
.d_un
.d_val
= s
->size
;
2463 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2469 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2470 struct bfd_link_info
*info
)
2474 struct riscv_elf_link_hash_table
*htab
;
2476 htab
= riscv_elf_hash_table (info
);
2477 BFD_ASSERT (htab
!= NULL
);
2478 dynobj
= htab
->elf
.dynobj
;
2480 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2482 if (elf_hash_table (info
)->dynamic_sections_created
)
2487 splt
= htab
->elf
.splt
;
2488 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2490 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2495 /* Fill in the head and tail entries in the procedure linkage table. */
2499 uint32_t plt_header
[PLT_HEADER_INSNS
];
2500 ret
= riscv_make_plt_header (output_bfd
,
2501 sec_addr (htab
->elf
.sgotplt
),
2502 sec_addr (splt
), plt_header
);
2506 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2507 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2509 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2514 if (htab
->elf
.sgotplt
)
2516 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2518 if (bfd_is_abs_section (output_section
))
2520 (*_bfd_error_handler
)
2521 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2525 if (htab
->elf
.sgotplt
->size
> 0)
2527 /* Write the first two entries in .got.plt, needed for the dynamic
2529 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2530 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2531 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2534 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2539 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2541 if (htab
->elf
.sgot
->size
> 0)
2543 /* Set the first entry in the global offset table to the address of
2544 the dynamic section. */
2545 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2546 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2549 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2555 /* Return address for Ith PLT stub in section PLT, for relocation REL
2556 or (bfd_vma) -1 if it should not be included. */
2559 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2560 const arelent
*rel ATTRIBUTE_UNUSED
)
2562 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2565 static enum elf_reloc_type_class
2566 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2567 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2568 const Elf_Internal_Rela
*rela
)
2570 switch (ELFNN_R_TYPE (rela
->r_info
))
2572 case R_RISCV_RELATIVE
:
2573 return reloc_class_relative
;
2574 case R_RISCV_JUMP_SLOT
:
2575 return reloc_class_plt
;
2577 return reloc_class_copy
;
2579 return reloc_class_normal
;
2583 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2587 riscv_float_abi_string (flagword flags
)
2589 switch (flags
& EF_RISCV_FLOAT_ABI
)
2591 case EF_RISCV_FLOAT_ABI_SOFT
:
2592 return "soft-float";
2594 case EF_RISCV_FLOAT_ABI_SINGLE
:
2595 return "single-float";
2597 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2598 return "double-float";
2600 case EF_RISCV_FLOAT_ABI_QUAD
:
2601 return "quad-float";
2608 /* The information of architecture attribute. */
2609 static riscv_subset_list_t in_subsets
;
2610 static riscv_subset_list_t out_subsets
;
2611 static riscv_subset_list_t merged_subsets
;
2613 /* Predicator for standard extension. */
2616 riscv_std_ext_p (const char *name
)
2618 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2621 /* Check if the versions are compatible. */
2624 riscv_version_mismatch (bfd
*ibfd
,
2625 struct riscv_subset_t
*in
,
2626 struct riscv_subset_t
*out
)
2628 if (in
== NULL
|| out
== NULL
)
2631 /* Since there are no version conflicts for now, we just report
2632 warning when the versions are mis-matched. */
2633 if (in
->major_version
!= out
->major_version
2634 || in
->minor_version
!= out
->minor_version
)
2637 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
2638 "extension, the output version is %d.%d"),
2644 out
->minor_version
);
2646 /* Update the output ISA versions to the newest ones. */
2647 if ((in
->major_version
> out
->major_version
)
2648 || (in
->major_version
== out
->major_version
2649 && in
->minor_version
> out
->minor_version
))
2651 out
->major_version
= in
->major_version
;
2652 out
->minor_version
= in
->minor_version
;
2659 /* Return true if subset is 'i' or 'e'. */
2662 riscv_i_or_e_p (bfd
*ibfd
,
2664 struct riscv_subset_t
*subset
)
2666 if ((strcasecmp (subset
->name
, "e") != 0)
2667 && (strcasecmp (subset
->name
, "i") != 0))
2670 (_("error: %pB: corrupted ISA string '%s'. "
2671 "First letter should be 'i' or 'e' but got '%s'"),
2672 ibfd
, arch
, subset
->name
);
2678 /* Merge standard extensions.
2681 Return FALSE if failed to merge.
2685 `in_arch`: Raw arch string for input object.
2686 `out_arch`: Raw arch string for output object.
2687 `pin`: subset list for input object, and it'll skip all merged subset after
2689 `pout`: Like `pin`, but for output object. */
2692 riscv_merge_std_ext (bfd
*ibfd
,
2693 const char *in_arch
,
2694 const char *out_arch
,
2695 struct riscv_subset_t
**pin
,
2696 struct riscv_subset_t
**pout
)
2698 const char *standard_exts
= riscv_supported_std_ext ();
2700 struct riscv_subset_t
*in
= *pin
;
2701 struct riscv_subset_t
*out
= *pout
;
2703 /* First letter should be 'i' or 'e'. */
2704 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2707 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2710 if (strcasecmp (in
->name
, out
->name
) != 0)
2712 /* TODO: We might allow merge 'i' with 'e'. */
2714 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
2715 ibfd
, in
->name
, out
->name
);
2718 else if (!riscv_version_mismatch (ibfd
, in
, out
))
2721 riscv_add_subset (&merged_subsets
,
2722 out
->name
, out
->major_version
, out
->minor_version
);
2727 /* Handle standard extension first. */
2728 for (p
= standard_exts
; *p
; ++p
)
2730 char find_ext
[2] = {*p
, '\0'};
2731 struct riscv_subset_t
*find_in
=
2732 riscv_lookup_subset (&in_subsets
, find_ext
);
2733 struct riscv_subset_t
*find_out
=
2734 riscv_lookup_subset (&out_subsets
, find_ext
);
2736 if (find_in
== NULL
&& find_out
== NULL
)
2739 if (!riscv_version_mismatch (ibfd
, find_in
, find_out
))
2742 struct riscv_subset_t
*merged
= find_out
? find_out
: find_in
;
2743 riscv_add_subset (&merged_subsets
, merged
->name
,
2744 merged
->major_version
, merged
->minor_version
);
2747 /* Skip all standard extensions. */
2748 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2749 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2757 /* If C is a prefix class, then return the EXT string without the prefix.
2758 Otherwise return the entire EXT string. */
2761 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
2765 case RV_ISA_CLASS_X
: return &ext
[1];
2766 case RV_ISA_CLASS_S
: return &ext
[1];
2767 case RV_ISA_CLASS_Z
: return &ext
[1];
2768 default: return ext
;
2772 /* Compare prefixed extension names canonically. */
2775 riscv_prefix_cmp (const char *a
, const char *b
)
2777 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
2778 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
2780 /* Extension name without prefix */
2781 const char *anp
= riscv_skip_prefix (a
, ca
);
2782 const char *bnp
= riscv_skip_prefix (b
, cb
);
2785 return strcasecmp (anp
, bnp
);
2787 return (int)ca
- (int)cb
;
2790 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2791 object subset list. Likewise for POUT and the output object. Return TRUE
2792 on success and FALSE when a conflict is found. */
2795 riscv_merge_multi_letter_ext (bfd
*ibfd
,
2796 riscv_subset_t
**pin
,
2797 riscv_subset_t
**pout
)
2799 riscv_subset_t
*in
= *pin
;
2800 riscv_subset_t
*out
= *pout
;
2801 riscv_subset_t
*tail
;
2807 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
2811 /* `in' comes before `out', append `in' and increment. */
2812 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2818 /* `out' comes before `in', append `out' and increment. */
2819 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2820 out
->minor_version
);
2825 /* Both present, check version and increment both. */
2826 if (!riscv_version_mismatch (ibfd
, in
, out
))
2829 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2830 out
->minor_version
);
2837 /* If we're here, either `in' or `out' is running longer than
2838 the other. So, we need to append the corresponding tail. */
2839 tail
= in
? in
: out
;
2843 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
2844 tail
->minor_version
);
2852 /* Merge Tag_RISCV_arch attribute. */
2855 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2857 riscv_subset_t
*in
, *out
;
2858 char *merged_arch_str
;
2860 unsigned xlen_in
, xlen_out
;
2861 merged_subsets
.head
= NULL
;
2862 merged_subsets
.tail
= NULL
;
2864 riscv_parse_subset_t rpe_in
;
2865 riscv_parse_subset_t rpe_out
;
2867 /* Only assembler needs to check the default version of ISA, so just set
2868 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
2869 rpe_in
.subset_list
= &in_subsets
;
2870 rpe_in
.error_handler
= _bfd_error_handler
;
2871 rpe_in
.xlen
= &xlen_in
;
2872 rpe_in
.get_default_version
= NULL
;
2874 rpe_out
.subset_list
= &out_subsets
;
2875 rpe_out
.error_handler
= _bfd_error_handler
;
2876 rpe_out
.xlen
= &xlen_out
;
2877 rpe_out
.get_default_version
= NULL
;
2879 if (in_arch
== NULL
&& out_arch
== NULL
)
2882 if (in_arch
== NULL
&& out_arch
!= NULL
)
2885 if (in_arch
!= NULL
&& out_arch
== NULL
)
2888 /* Parse subset from arch string. */
2889 if (!riscv_parse_subset (&rpe_in
, in_arch
))
2892 if (!riscv_parse_subset (&rpe_out
, out_arch
))
2895 /* Checking XLEN. */
2896 if (xlen_out
!= xlen_in
)
2899 (_("error: %pB: ISA string of input (%s) doesn't match "
2900 "output (%s)"), ibfd
, in_arch
, out_arch
);
2904 /* Merge subset list. */
2905 in
= in_subsets
.head
;
2906 out
= out_subsets
.head
;
2908 /* Merge standard extension. */
2909 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
2912 /* Merge all non-single letter extensions with single call. */
2913 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
2916 if (xlen_in
!= xlen_out
)
2919 (_("error: %pB: XLEN of input (%u) doesn't match "
2920 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
2924 if (xlen_in
!= ARCH_SIZE
)
2927 (_("error: %pB: unsupported XLEN (%u), you might be "
2928 "using wrong emulation"), ibfd
, xlen_in
);
2932 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
2934 /* Release the subset lists. */
2935 riscv_release_subset_list (&in_subsets
);
2936 riscv_release_subset_list (&out_subsets
);
2937 riscv_release_subset_list (&merged_subsets
);
2939 return merged_arch_str
;
2942 /* Merge object attributes from IBFD into output_bfd of INFO.
2943 Raise an error if there are conflicting attributes. */
2946 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
2948 bfd
*obfd
= info
->output_bfd
;
2949 obj_attribute
*in_attr
;
2950 obj_attribute
*out_attr
;
2951 bfd_boolean result
= TRUE
;
2952 bfd_boolean priv_attrs_merged
= FALSE
;
2953 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
2956 /* Skip linker created files. */
2957 if (ibfd
->flags
& BFD_LINKER_CREATED
)
2960 /* Skip any input that doesn't have an attribute section.
2961 This enables to link object files without attribute section with
2963 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
2966 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
2968 /* This is the first object. Copy the attributes. */
2969 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
2971 out_attr
= elf_known_obj_attributes_proc (obfd
);
2973 /* Use the Tag_null value to indicate the attributes have been
2980 in_attr
= elf_known_obj_attributes_proc (ibfd
);
2981 out_attr
= elf_known_obj_attributes_proc (obfd
);
2983 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
2987 case Tag_RISCV_arch
:
2988 if (!out_attr
[Tag_RISCV_arch
].s
)
2989 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
2990 else if (in_attr
[Tag_RISCV_arch
].s
2991 && out_attr
[Tag_RISCV_arch
].s
)
2993 /* Check arch compatible. */
2995 riscv_merge_arch_attr_info (ibfd
,
2996 in_attr
[Tag_RISCV_arch
].s
,
2997 out_attr
[Tag_RISCV_arch
].s
);
2998 if (merged_arch
== NULL
)
3001 out_attr
[Tag_RISCV_arch
].s
= "";
3004 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3008 case Tag_RISCV_priv_spec
:
3009 case Tag_RISCV_priv_spec_minor
:
3010 case Tag_RISCV_priv_spec_revision
:
3011 /* If we have handled the priv attributes, then skip it. */
3012 if (!priv_attrs_merged
)
3014 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3015 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3016 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3017 enum riscv_priv_spec_class in_priv_spec
;
3018 enum riscv_priv_spec_class out_priv_spec
;
3020 /* Get the priv spec class from elf attribute numbers. */
3021 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3025 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3030 /* Allow to link the object without the priv specs. */
3031 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3033 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3034 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3035 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3037 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3038 && in_priv_spec
!= out_priv_spec
)
3041 (_("warning: %pB use privilege spec version %u.%u.%u but "
3042 "the output use version %u.%u.%u"),
3051 /* The priv spec v1.9.1 can not be linked with other spec
3052 versions since the conflicts. We plan to drop the
3053 v1.9.1 in a year or two, so this confict should be
3054 removed in the future. */
3055 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3056 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3059 (_("warning: privilege spec version 1.9.1 can not be "
3060 "linked with other spec versions"));
3063 /* Update the output priv spec to the newest one. */
3064 if (in_priv_spec
> out_priv_spec
)
3066 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3067 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3068 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3071 priv_attrs_merged
= TRUE
;
3075 case Tag_RISCV_unaligned_access
:
3076 out_attr
[i
].i
|= in_attr
[i
].i
;
3079 case Tag_RISCV_stack_align
:
3080 if (out_attr
[i
].i
== 0)
3081 out_attr
[i
].i
= in_attr
[i
].i
;
3082 else if (in_attr
[i
].i
!= 0
3083 && out_attr
[i
].i
!= 0
3084 && out_attr
[i
].i
!= in_attr
[i
].i
)
3087 (_("error: %pB use %u-byte stack aligned but the output "
3088 "use %u-byte stack aligned"),
3089 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3095 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3098 /* If out_attr was copied from in_attr then it won't have a type yet. */
3099 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3100 out_attr
[i
].type
= in_attr
[i
].type
;
3103 /* Merge Tag_compatibility attributes and any common GNU ones. */
3104 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3107 /* Check for any attributes not known on RISC-V. */
3108 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3113 /* Merge backend specific data from an object file to the output
3114 object file when linking. */
3117 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3119 bfd
*obfd
= info
->output_bfd
;
3120 flagword new_flags
, old_flags
;
3122 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3125 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3127 (*_bfd_error_handler
)
3128 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3129 " target emulation `%s' does not match `%s'"),
3130 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3134 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3137 if (!riscv_merge_attributes (ibfd
, info
))
3140 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3141 old_flags
= elf_elfheader (obfd
)->e_flags
;
3143 if (! elf_flags_init (obfd
))
3145 elf_flags_init (obfd
) = TRUE
;
3146 elf_elfheader (obfd
)->e_flags
= new_flags
;
3150 /* Check to see if the input BFD actually contains any sections. If not,
3151 its flags may not have been initialized either, but it cannot actually
3152 cause any incompatibility. Do not short-circuit dynamic objects; their
3153 section list may be emptied by elf_link_add_object_symbols.
3155 Also check to see if there are no code sections in the input. In this
3156 case, there is no need to check for code specific flags. */
3157 if (!(ibfd
->flags
& DYNAMIC
))
3159 bfd_boolean null_input_bfd
= TRUE
;
3160 bfd_boolean only_data_sections
= TRUE
;
3163 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3165 if ((bfd_section_flags (sec
)
3166 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3167 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3168 only_data_sections
= FALSE
;
3170 null_input_bfd
= FALSE
;
3174 if (null_input_bfd
|| only_data_sections
)
3178 /* Disallow linking different float ABIs. */
3179 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3181 (*_bfd_error_handler
)
3182 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3183 riscv_float_abi_string (new_flags
),
3184 riscv_float_abi_string (old_flags
));
3188 /* Disallow linking RVE and non-RVE. */
3189 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3191 (*_bfd_error_handler
)
3192 (_("%pB: can't link RVE with other target"), ibfd
);
3196 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3197 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3202 bfd_set_error (bfd_error_bad_value
);
3206 /* Delete some bytes from a section while relaxing. */
3209 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3210 struct bfd_link_info
*link_info
)
3212 unsigned int i
, symcount
;
3213 bfd_vma toaddr
= sec
->size
;
3214 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3215 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3216 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3217 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3218 bfd_byte
*contents
= data
->this_hdr
.contents
;
3220 /* Actually delete the bytes. */
3222 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3224 /* Adjust the location of all of the relocs. Note that we need not
3225 adjust the addends, since all PC-relative references must be against
3226 symbols, which we will adjust below. */
3227 for (i
= 0; i
< sec
->reloc_count
; i
++)
3228 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3229 data
->relocs
[i
].r_offset
-= count
;
3231 /* Adjust the local symbols defined in this section. */
3232 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3234 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3235 if (sym
->st_shndx
== sec_shndx
)
3237 /* If the symbol is in the range of memory we just moved, we
3238 have to adjust its value. */
3239 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3240 sym
->st_value
-= count
;
3242 /* If the symbol *spans* the bytes we just deleted (i.e. its
3243 *end* is in the moved bytes but its *start* isn't), then we
3244 must adjust its size.
3246 This test needs to use the original value of st_value, otherwise
3247 we might accidentally decrease size when deleting bytes right
3248 before the symbol. But since deleted relocs can't span across
3249 symbols, we can't have both a st_value and a st_size decrease,
3250 so it is simpler to just use an else. */
3251 else if (sym
->st_value
<= addr
3252 && sym
->st_value
+ sym
->st_size
> addr
3253 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3254 sym
->st_size
-= count
;
3258 /* Now adjust the global symbols defined in this section. */
3259 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3260 - symtab_hdr
->sh_info
);
3262 for (i
= 0; i
< symcount
; i
++)
3264 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3266 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3267 containing the definition of __wrap_SYMBOL, includes a direct
3268 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3269 the same symbol (which is __wrap_SYMBOL), but still exist as two
3270 different symbols in 'sym_hashes', we don't want to adjust
3271 the global symbol __wrap_SYMBOL twice. */
3272 /* The same problem occurs with symbols that are versioned_hidden, as
3273 foo becomes an alias for foo@BAR, and hence they need the same
3275 if (link_info
->wrap_hash
!= NULL
3276 || sym_hash
->versioned
== versioned_hidden
)
3278 struct elf_link_hash_entry
**cur_sym_hashes
;
3280 /* Loop only over the symbols which have already been checked. */
3281 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3284 /* If the current symbol is identical to 'sym_hash', that means
3285 the symbol was already adjusted (or at least checked). */
3286 if (*cur_sym_hashes
== sym_hash
)
3289 /* Don't adjust the symbol again. */
3290 if (cur_sym_hashes
< &sym_hashes
[i
])
3294 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3295 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3296 && sym_hash
->root
.u
.def
.section
== sec
)
3298 /* As above, adjust the value if needed. */
3299 if (sym_hash
->root
.u
.def
.value
> addr
3300 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3301 sym_hash
->root
.u
.def
.value
-= count
;
3303 /* As above, adjust the size if needed. */
3304 else if (sym_hash
->root
.u
.def
.value
<= addr
3305 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3306 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3307 sym_hash
->size
-= count
;
3314 /* A second format for recording PC-relative hi relocations. This stores the
3315 information required to relax them to GP-relative addresses. */
3317 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3318 struct riscv_pcgp_hi_reloc
3325 bfd_boolean undefined_weak
;
3326 riscv_pcgp_hi_reloc
*next
;
3329 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3330 struct riscv_pcgp_lo_reloc
3333 riscv_pcgp_lo_reloc
*next
;
3338 riscv_pcgp_hi_reloc
*hi
;
3339 riscv_pcgp_lo_reloc
*lo
;
3340 } riscv_pcgp_relocs
;
3342 /* Initialize the pcgp reloc info in P. */
3345 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3352 /* Free the pcgp reloc info in P. */
3355 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3356 bfd
*abfd ATTRIBUTE_UNUSED
,
3357 asection
*sec ATTRIBUTE_UNUSED
)
3359 riscv_pcgp_hi_reloc
*c
;
3360 riscv_pcgp_lo_reloc
*l
;
3362 for (c
= p
->hi
; c
!= NULL
;)
3364 riscv_pcgp_hi_reloc
*next
= c
->next
;
3369 for (l
= p
->lo
; l
!= NULL
;)
3371 riscv_pcgp_lo_reloc
*next
= l
->next
;
3377 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3378 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3379 relax the corresponding lo part reloc. */
3382 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3383 bfd_vma hi_addend
, bfd_vma hi_addr
,
3384 unsigned hi_sym
, asection
*sym_sec
,
3385 bfd_boolean undefined_weak
)
3387 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3390 new->hi_sec_off
= hi_sec_off
;
3391 new->hi_addend
= hi_addend
;
3392 new->hi_addr
= hi_addr
;
3393 new->hi_sym
= hi_sym
;
3394 new->sym_sec
= sym_sec
;
3395 new->undefined_weak
= undefined_weak
;
3401 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3402 This is used by a lo part reloc to find the corresponding hi part reloc. */
3404 static riscv_pcgp_hi_reloc
*
3405 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3407 riscv_pcgp_hi_reloc
*c
;
3409 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3410 if (c
->hi_sec_off
== hi_sec_off
)
3415 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3416 This is used to record relocs that can't be relaxed. */
3419 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3421 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3424 new->hi_sec_off
= hi_sec_off
;
3430 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3431 This is used by a hi part reloc to find the corresponding lo part reloc. */
3434 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3436 riscv_pcgp_lo_reloc
*c
;
3438 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3439 if (c
->hi_sec_off
== hi_sec_off
)
3444 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3445 struct bfd_link_info
*,
3446 Elf_Internal_Rela
*,
3447 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3448 riscv_pcgp_relocs
*,
3449 bfd_boolean undefined_weak
);
3451 /* Relax AUIPC + JALR into JAL. */
3454 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3455 struct bfd_link_info
*link_info
,
3456 Elf_Internal_Rela
*rel
,
3458 bfd_vma max_alignment
,
3459 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3461 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3462 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3464 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3465 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3466 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3467 bfd_vma auipc
, jalr
;
3468 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3470 /* If the call crosses section boundaries, an alignment directive could
3471 cause the PC-relative offset to later increase, so we need to add in the
3472 max alignment of any section inclusive from the call to the target.
3473 Otherwise, we only need to use the alignment of the current section. */
3474 if (VALID_UJTYPE_IMM (foff
))
3476 if (sym_sec
->output_section
== sec
->output_section
3477 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3478 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3479 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
3482 /* See if this function call can be shortened. */
3483 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3486 /* Shorten the function call. */
3487 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3489 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3490 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3491 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3492 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3494 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3495 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3499 /* Relax to C.J[AL] rd, addr. */
3500 r_type
= R_RISCV_RVC_JUMP
;
3501 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3504 else if (VALID_UJTYPE_IMM (foff
))
3506 /* Relax to JAL rd, addr. */
3507 r_type
= R_RISCV_JAL
;
3508 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3510 else /* near_zero */
3512 /* Relax to JALR rd, x0, addr. */
3513 r_type
= R_RISCV_LO12_I
;
3514 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3517 /* Replace the R_RISCV_CALL reloc. */
3518 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3519 /* Replace the AUIPC. */
3520 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3522 /* Delete unnecessary JALR. */
3524 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3528 /* Traverse all output sections and return the max alignment. */
3531 _bfd_riscv_get_max_alignment (asection
*sec
)
3533 unsigned int max_alignment_power
= 0;
3536 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3538 if (o
->alignment_power
> max_alignment_power
)
3539 max_alignment_power
= o
->alignment_power
;
3542 return (bfd_vma
) 1 << max_alignment_power
;
3545 /* Relax non-PIC global variable references. */
3548 _bfd_riscv_relax_lui (bfd
*abfd
,
3551 struct bfd_link_info
*link_info
,
3552 Elf_Internal_Rela
*rel
,
3554 bfd_vma max_alignment
,
3555 bfd_vma reserve_size
,
3557 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3558 bfd_boolean undefined_weak
)
3560 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3561 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3562 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3564 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3568 /* If gp and the symbol are in the same output section, which is not the
3569 abs section, then consider only that output section's alignment. */
3570 struct bfd_link_hash_entry
*h
=
3571 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3573 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3574 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3575 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3578 /* Is the reference in range of x0 or gp?
3579 Valid gp range conservatively because of alignment issue. */
3581 || (VALID_ITYPE_IMM (symval
)
3583 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3585 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3587 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3588 switch (ELFNN_R_TYPE (rel
->r_info
))
3590 case R_RISCV_LO12_I
:
3593 /* Change the RS1 to zero. */
3594 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3595 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3596 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3599 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3602 case R_RISCV_LO12_S
:
3605 /* Change the RS1 to zero. */
3606 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3607 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3608 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3611 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3615 /* We can delete the unnecessary LUI and reloc. */
3616 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3618 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3626 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3627 account for this assuming page alignment at worst. In the presence of
3628 RELRO segment the linker aligns it by one page size, therefore sections
3629 after the segment can be moved more than one page. */
3632 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3633 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3634 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3635 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3636 : ELF_MAXPAGESIZE
)))
3638 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3639 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3640 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3641 if (rd
== 0 || rd
== X_SP
)
3644 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3645 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3647 /* Replace the R_RISCV_HI20 reloc. */
3648 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3651 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3658 /* Relax non-PIC TLS references. */
3661 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3663 asection
*sym_sec ATTRIBUTE_UNUSED
,
3664 struct bfd_link_info
*link_info
,
3665 Elf_Internal_Rela
*rel
,
3667 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3668 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3670 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3671 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3673 /* See if this symbol is in range of tp. */
3674 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3677 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3678 switch (ELFNN_R_TYPE (rel
->r_info
))
3680 case R_RISCV_TPREL_LO12_I
:
3681 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3684 case R_RISCV_TPREL_LO12_S
:
3685 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3688 case R_RISCV_TPREL_HI20
:
3689 case R_RISCV_TPREL_ADD
:
3690 /* We can delete the unnecessary instruction and reloc. */
3691 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3693 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3700 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3703 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3705 struct bfd_link_info
*link_info
,
3706 Elf_Internal_Rela
*rel
,
3708 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3709 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3710 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3711 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3712 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3714 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3715 bfd_vma alignment
= 1, pos
;
3716 while (alignment
<= rel
->r_addend
)
3719 symval
-= rel
->r_addend
;
3720 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3721 bfd_vma nop_bytes
= aligned_addr
- symval
;
3723 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3724 sec
->sec_flg0
= TRUE
;
3726 /* Make sure there are enough NOPs to actually achieve the alignment. */
3727 if (rel
->r_addend
< nop_bytes
)
3730 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3731 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3732 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3733 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3734 bfd_set_error (bfd_error_bad_value
);
3738 /* Delete the reloc. */
3739 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3741 /* If the number of NOPs is already correct, there's nothing to do. */
3742 if (nop_bytes
== rel
->r_addend
)
3745 /* Write as many RISC-V NOPs as we need. */
3746 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3747 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3749 /* Write a final RVC NOP if need be. */
3750 if (nop_bytes
% 4 != 0)
3751 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3753 /* Delete the excess bytes. */
3754 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3755 rel
->r_addend
- nop_bytes
, link_info
);
3758 /* Relax PC-relative references to GP-relative references. */
3761 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3764 struct bfd_link_info
*link_info
,
3765 Elf_Internal_Rela
*rel
,
3767 bfd_vma max_alignment
,
3768 bfd_vma reserve_size
,
3769 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3770 riscv_pcgp_relocs
*pcgp_relocs
,
3771 bfd_boolean undefined_weak
)
3773 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3774 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3776 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3778 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3779 * actual target address. */
3780 riscv_pcgp_hi_reloc hi_reloc
;
3781 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3782 switch (ELFNN_R_TYPE (rel
->r_info
))
3784 case R_RISCV_PCREL_LO12_I
:
3785 case R_RISCV_PCREL_LO12_S
:
3787 /* If the %lo has an addend, it isn't for the label pointing at the
3788 hi part instruction, but rather for the symbol pointed at by the
3789 hi part instruction. So we must subtract it here for the lookup.
3790 It is still used below in the final symbol address. */
3791 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3792 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3796 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3801 symval
= hi_reloc
.hi_addr
;
3802 sym_sec
= hi_reloc
.sym_sec
;
3804 /* We can not know whether the undefined weak symbol is referenced
3805 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3806 we have to record the 'undefined_weak' flag when handling the
3807 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3808 undefined_weak
= hi_reloc
.undefined_weak
;
3812 case R_RISCV_PCREL_HI20
:
3813 /* Mergeable symbols and code might later move out of range. */
3814 if (! undefined_weak
3815 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3818 /* If the cooresponding lo relocation has already been seen then it's not
3819 * safe to relax this relocation. */
3820 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3831 /* If gp and the symbol are in the same output section, which is not the
3832 abs section, then consider only that output section's alignment. */
3833 struct bfd_link_hash_entry
*h
=
3834 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3836 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3837 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3838 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3841 /* Is the reference in range of x0 or gp?
3842 Valid gp range conservatively because of alignment issue. */
3844 || (VALID_ITYPE_IMM (symval
)
3846 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3848 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3850 unsigned sym
= hi_reloc
.hi_sym
;
3851 switch (ELFNN_R_TYPE (rel
->r_info
))
3853 case R_RISCV_PCREL_LO12_I
:
3856 /* Change the RS1 to zero, and then modify the relocation
3857 type to R_RISCV_LO12_I. */
3858 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3859 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3860 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3861 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3862 rel
->r_addend
= hi_reloc
.hi_addend
;
3866 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3867 rel
->r_addend
+= hi_reloc
.hi_addend
;
3871 case R_RISCV_PCREL_LO12_S
:
3874 /* Change the RS1 to zero, and then modify the relocation
3875 type to R_RISCV_LO12_S. */
3876 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3877 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3878 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3879 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3880 rel
->r_addend
= hi_reloc
.hi_addend
;
3884 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3885 rel
->r_addend
+= hi_reloc
.hi_addend
;
3889 case R_RISCV_PCREL_HI20
:
3890 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3894 ELFNN_R_SYM(rel
->r_info
),
3897 /* We can delete the unnecessary AUIPC and reloc. */
3898 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3910 /* Relax PC-relative references to GP-relative references. */
3913 _bfd_riscv_relax_delete (bfd
*abfd
,
3915 asection
*sym_sec ATTRIBUTE_UNUSED
,
3916 struct bfd_link_info
*link_info
,
3917 Elf_Internal_Rela
*rel
,
3918 bfd_vma symval ATTRIBUTE_UNUSED
,
3919 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3920 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3921 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3922 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3923 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3925 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3928 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3932 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3933 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3934 disabled, handles code alignment directives. */
3937 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3938 struct bfd_link_info
*info
,
3941 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3942 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3943 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3944 Elf_Internal_Rela
*relocs
;
3945 bfd_boolean ret
= FALSE
;
3947 bfd_vma max_alignment
, reserve_size
= 0;
3948 riscv_pcgp_relocs pcgp_relocs
;
3952 if (bfd_link_relocatable (info
)
3954 || (sec
->flags
& SEC_RELOC
) == 0
3955 || sec
->reloc_count
== 0
3956 || (info
->disable_target_specific_optimizations
3957 && info
->relax_pass
== 0))
3960 riscv_init_pcgp_relocs (&pcgp_relocs
);
3962 /* Read this BFD's relocs if we haven't done so already. */
3964 relocs
= data
->relocs
;
3965 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3966 info
->keep_memory
)))
3971 max_alignment
= htab
->max_alignment
;
3972 if (max_alignment
== (bfd_vma
) -1)
3974 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3975 htab
->max_alignment
= max_alignment
;
3979 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3981 /* Examine and consider relaxing each reloc. */
3982 for (i
= 0; i
< sec
->reloc_count
; i
++)
3985 Elf_Internal_Rela
*rel
= relocs
+ i
;
3986 relax_func_t relax_func
;
3987 int type
= ELFNN_R_TYPE (rel
->r_info
);
3990 bfd_boolean undefined_weak
= FALSE
;
3993 if (info
->relax_pass
== 0)
3995 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
3996 relax_func
= _bfd_riscv_relax_call
;
3997 else if (type
== R_RISCV_HI20
3998 || type
== R_RISCV_LO12_I
3999 || type
== R_RISCV_LO12_S
)
4000 relax_func
= _bfd_riscv_relax_lui
;
4001 else if (!bfd_link_pic(info
)
4002 && (type
== R_RISCV_PCREL_HI20
4003 || type
== R_RISCV_PCREL_LO12_I
4004 || type
== R_RISCV_PCREL_LO12_S
))
4005 relax_func
= _bfd_riscv_relax_pc
;
4006 else if (type
== R_RISCV_TPREL_HI20
4007 || type
== R_RISCV_TPREL_ADD
4008 || type
== R_RISCV_TPREL_LO12_I
4009 || type
== R_RISCV_TPREL_LO12_S
)
4010 relax_func
= _bfd_riscv_relax_tls_le
;
4014 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4015 if (i
== sec
->reloc_count
- 1
4016 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4017 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4020 /* Skip over the R_RISCV_RELAX. */
4023 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4024 relax_func
= _bfd_riscv_relax_delete
;
4025 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4026 relax_func
= _bfd_riscv_relax_align
;
4030 data
->relocs
= relocs
;
4032 /* Read this BFD's contents if we haven't done so already. */
4033 if (!data
->this_hdr
.contents
4034 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4037 /* Read this BFD's symbols if we haven't done so already. */
4038 if (symtab_hdr
->sh_info
!= 0
4039 && !symtab_hdr
->contents
4040 && !(symtab_hdr
->contents
=
4041 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4042 symtab_hdr
->sh_info
,
4043 0, NULL
, NULL
, NULL
)))
4046 /* Get the value of the symbol referred to by the reloc. */
4047 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4049 /* A local symbol. */
4050 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4051 + ELFNN_R_SYM (rel
->r_info
));
4052 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4053 ? 0 : isym
->st_size
- rel
->r_addend
;
4055 if (isym
->st_shndx
== SHN_UNDEF
)
4056 sym_sec
= sec
, symval
= rel
->r_offset
;
4059 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4060 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4062 /* The purpose of this code is unknown. It breaks linker scripts
4063 for embedded development that place sections at address zero.
4064 This code is believed to be unnecessary. Disabling it but not
4065 yet removing it, in case something breaks. */
4066 if (sec_addr (sym_sec
) == 0)
4069 symval
= isym
->st_value
;
4071 symtype
= ELF_ST_TYPE (isym
->st_info
);
4076 struct elf_link_hash_entry
*h
;
4078 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4079 h
= elf_sym_hashes (abfd
)[indx
];
4081 while (h
->root
.type
== bfd_link_hash_indirect
4082 || h
->root
.type
== bfd_link_hash_warning
)
4083 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4085 if (h
->root
.type
== bfd_link_hash_undefweak
4086 && (relax_func
== _bfd_riscv_relax_lui
4087 || relax_func
== _bfd_riscv_relax_pc
))
4089 /* For the lui and auipc relaxations, since the symbol
4090 value of an undefined weak symbol is always be zero,
4091 we can optimize the patterns into a single LI/MV/ADDI
4094 Note that, creating shared libraries and pie output may
4095 break the rule above. Fortunately, since we do not relax
4096 pc relocs when creating shared libraries and pie output,
4097 and the absolute address access for R_RISCV_HI20 isn't
4098 allowed when "-fPIC" is set, the problem of creating shared
4099 libraries can not happen currently. Once we support the
4100 auipc relaxations when creating shared libraries, then we will
4101 need the more rigorous checking for this optimization. */
4102 undefined_weak
= TRUE
;
4105 /* This line has to match the check in riscv_elf_relocate_section
4106 in the R_RISCV_CALL[_PLT] case. */
4107 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4109 sym_sec
= htab
->elf
.splt
;
4110 symval
= h
->plt
.offset
;
4112 else if (undefined_weak
)
4115 sym_sec
= bfd_und_section_ptr
;
4117 else if ((h
->root
.type
== bfd_link_hash_defined
4118 || h
->root
.type
== bfd_link_hash_defweak
)
4119 && h
->root
.u
.def
.section
!= NULL
4120 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4122 symval
= h
->root
.u
.def
.value
;
4123 sym_sec
= h
->root
.u
.def
.section
;
4128 if (h
->type
!= STT_FUNC
)
4130 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4134 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4135 && (sym_sec
->flags
& SEC_MERGE
))
4137 /* At this stage in linking, no SEC_MERGE symbol has been
4138 adjusted, so all references to such symbols need to be
4139 passed through _bfd_merged_section_offset. (Later, in
4140 relocate_section, all SEC_MERGE symbols *except* for
4141 section symbols have been adjusted.)
4143 gas may reduce relocations against symbols in SEC_MERGE
4144 sections to a relocation against the section symbol when
4145 the original addend was zero. When the reloc is against
4146 a section symbol we should include the addend in the
4147 offset passed to _bfd_merged_section_offset, since the
4148 location of interest is the original symbol. On the
4149 other hand, an access to "sym+addend" where "sym" is not
4150 a section symbol should not include the addend; Such an
4151 access is presumed to be an offset from "sym"; The
4152 location of interest is just "sym". */
4153 if (symtype
== STT_SECTION
)
4154 symval
+= rel
->r_addend
;
4156 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4157 elf_section_data (sym_sec
)->sec_info
,
4160 if (symtype
!= STT_SECTION
)
4161 symval
+= rel
->r_addend
;
4164 symval
+= rel
->r_addend
;
4166 symval
+= sec_addr (sym_sec
);
4168 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4169 max_alignment
, reserve_size
, again
,
4170 &pcgp_relocs
, undefined_weak
))
4177 if (relocs
!= data
->relocs
)
4179 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4185 # define PRSTATUS_SIZE 204
4186 # define PRSTATUS_OFFSET_PR_CURSIG 12
4187 # define PRSTATUS_OFFSET_PR_PID 24
4188 # define PRSTATUS_OFFSET_PR_REG 72
4189 # define ELF_GREGSET_T_SIZE 128
4190 # define PRPSINFO_SIZE 128
4191 # define PRPSINFO_OFFSET_PR_PID 16
4192 # define PRPSINFO_OFFSET_PR_FNAME 32
4193 # define PRPSINFO_OFFSET_PR_PSARGS 48
4195 # define PRSTATUS_SIZE 376
4196 # define PRSTATUS_OFFSET_PR_CURSIG 12
4197 # define PRSTATUS_OFFSET_PR_PID 32
4198 # define PRSTATUS_OFFSET_PR_REG 112
4199 # define ELF_GREGSET_T_SIZE 256
4200 # define PRPSINFO_SIZE 136
4201 # define PRPSINFO_OFFSET_PR_PID 24
4202 # define PRPSINFO_OFFSET_PR_FNAME 40
4203 # define PRPSINFO_OFFSET_PR_PSARGS 56
4206 /* Support for core dump NOTE sections. */
4209 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4211 switch (note
->descsz
)
4216 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4218 elf_tdata (abfd
)->core
->signal
4219 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4222 elf_tdata (abfd
)->core
->lwpid
4223 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4227 /* Make a ".reg/999" section. */
4228 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4229 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4233 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4235 switch (note
->descsz
)
4240 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4242 elf_tdata (abfd
)->core
->pid
4243 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4246 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4247 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4250 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4251 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4255 /* Note that for some reason, a spurious space is tacked
4256 onto the end of the args in some (at least one anyway)
4257 implementations, so strip it off if it exists. */
4260 char *command
= elf_tdata (abfd
)->core
->command
;
4261 int n
= strlen (command
);
4263 if (0 < n
&& command
[n
- 1] == ' ')
4264 command
[n
- 1] = '\0';
4270 /* Set the right mach type. */
4272 riscv_elf_object_p (bfd
*abfd
)
4274 /* There are only two mach types in RISCV currently. */
4275 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4276 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4278 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4283 /* Determine whether an object attribute tag takes an integer, a
4287 riscv_elf_obj_attrs_arg_type (int tag
)
4289 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4292 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4293 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4295 #define elf_backend_reloc_type_class riscv_reloc_type_class
4297 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4298 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4299 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4300 #define bfd_elfNN_bfd_merge_private_bfd_data \
4301 _bfd_riscv_elf_merge_private_bfd_data
4303 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4304 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4305 #define elf_backend_check_relocs riscv_elf_check_relocs
4306 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4307 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4308 #define elf_backend_relocate_section riscv_elf_relocate_section
4309 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4310 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4311 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4312 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4313 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4314 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4315 #define elf_backend_object_p riscv_elf_object_p
4316 #define elf_info_to_howto_rel NULL
4317 #define elf_info_to_howto riscv_info_to_howto_rela
4318 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4319 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4321 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4323 #define elf_backend_can_gc_sections 1
4324 #define elf_backend_can_refcount 1
4325 #define elf_backend_want_got_plt 1
4326 #define elf_backend_plt_readonly 1
4327 #define elf_backend_plt_alignment 4
4328 #define elf_backend_want_plt_sym 1
4329 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4330 #define elf_backend_want_dynrelro 1
4331 #define elf_backend_rela_normal 1
4332 #define elf_backend_default_execstack 0
4334 #undef elf_backend_obj_attrs_vendor
4335 #define elf_backend_obj_attrs_vendor "riscv"
4336 #undef elf_backend_obj_attrs_arg_type
4337 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4338 #undef elf_backend_obj_attrs_section_type
4339 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4340 #undef elf_backend_obj_attrs_section
4341 #define elf_backend_obj_attrs_section ".riscv.attributes"
4343 #include "elfNN-target.h"