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 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
124 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
127 riscv_info_to_howto_rela (bfd
*abfd
,
129 Elf_Internal_Rela
*dst
)
131 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
132 return cache_ptr
->howto
!= NULL
;
136 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
138 const struct elf_backend_data
*bed
;
141 bed
= get_elf_backend_data (abfd
);
142 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
143 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
148 #define PLT_HEADER_INSNS 8
149 #define PLT_ENTRY_INSNS 4
150 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
151 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
153 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
155 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
157 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
160 riscv_elf_got_plt_val (bfd_vma plt_index
, struct bfd_link_info
*info
)
162 return sec_addr (riscv_elf_hash_table (info
)->elf
.sgotplt
)
163 + GOTPLT_HEADER_SIZE
+ (plt_index
* GOT_ENTRY_SIZE
);
167 # define MATCH_LREG MATCH_LW
169 # define MATCH_LREG MATCH_LD
172 /* Generate a PLT header. */
175 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
178 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
179 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
181 /* RVE has no t3 register, so this won't work, and is not supported. */
182 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
184 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
189 /* auipc t2, %hi(.got.plt)
190 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
191 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
192 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
193 addi t0, t2, %lo(.got.plt) # &.got.plt
194 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
195 l[w|d] t0, PTRSIZE(t0) # link map
198 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
199 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
200 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
201 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, -(PLT_HEADER_SIZE
+ 12));
202 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
203 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
204 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
205 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
210 /* Generate a PLT entry. */
213 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
216 /* RVE has no t3 register, so this won't work, and is not supported. */
217 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
219 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
224 /* auipc t3, %hi(.got.plt entry)
225 l[w|d] t3, %lo(.got.plt entry)(t3)
229 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
230 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
231 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
232 entry
[3] = RISCV_NOP
;
237 /* Create an entry in an RISC-V ELF linker hash table. */
239 static struct bfd_hash_entry
*
240 link_hash_newfunc (struct bfd_hash_entry
*entry
,
241 struct bfd_hash_table
*table
, const char *string
)
243 /* Allocate the structure if it has not already been allocated by a
248 bfd_hash_allocate (table
,
249 sizeof (struct riscv_elf_link_hash_entry
));
254 /* Call the allocation method of the superclass. */
255 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
258 struct riscv_elf_link_hash_entry
*eh
;
260 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
261 eh
->tls_type
= GOT_UNKNOWN
;
267 /* Create a RISC-V ELF linker hash table. */
269 static struct bfd_link_hash_table
*
270 riscv_elf_link_hash_table_create (bfd
*abfd
)
272 struct riscv_elf_link_hash_table
*ret
;
273 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
275 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
279 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
280 sizeof (struct riscv_elf_link_hash_entry
),
287 ret
->max_alignment
= (bfd_vma
) -1;
288 return &ret
->elf
.root
;
291 /* Create the .got section. */
294 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
298 struct elf_link_hash_entry
*h
;
299 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
300 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
302 /* This function may be called more than once. */
303 if (htab
->sgot
!= NULL
)
306 flags
= bed
->dynamic_sec_flags
;
308 s
= bfd_make_section_anyway_with_flags (abfd
,
309 (bed
->rela_plts_and_copies_p
310 ? ".rela.got" : ".rel.got"),
311 (bed
->dynamic_sec_flags
314 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
318 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
320 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
324 /* The first bit of the global offset table is the header. */
325 s
->size
+= bed
->got_header_size
;
327 if (bed
->want_got_plt
)
329 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
331 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
335 /* Reserve room for the header. */
336 s
->size
+= GOTPLT_HEADER_SIZE
;
339 if (bed
->want_got_sym
)
341 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
342 section. We don't do this in the linker script because we don't want
343 to define the symbol if we are not creating a global offset
345 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
346 "_GLOBAL_OFFSET_TABLE_");
347 elf_hash_table (info
)->hgot
= h
;
355 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
356 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
360 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
361 struct bfd_link_info
*info
)
363 struct riscv_elf_link_hash_table
*htab
;
365 htab
= riscv_elf_hash_table (info
);
366 BFD_ASSERT (htab
!= NULL
);
368 if (!riscv_elf_create_got_section (dynobj
, info
))
371 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
374 if (!bfd_link_pic (info
))
376 /* Technically, this section doesn't have contents. It is used as the
377 target of TLS copy relocs, to copy TLS data from shared libraries into
378 the executable. However, if we don't mark it as loadable, then it
379 matches the IS_TBSS test in ldlang.c, and there is no run-time address
380 space allocated for it even though it has SEC_ALLOC. That test is
381 correct for .tbss, but not correct for this section. There is also
382 a second problem that having a section with no contents can only work
383 if it comes after all sections with contents in the same segment,
384 but the linker script does not guarantee that. This is just mixed in
385 with other .tdata.* sections. We can fix both problems by lying and
386 saying that there are contents. This section is expected to be small
387 so this should not cause a significant extra program startup cost. */
389 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
390 (SEC_ALLOC
| SEC_THREAD_LOCAL
391 | SEC_LOAD
| SEC_DATA
393 | SEC_LINKER_CREATED
));
396 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
397 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
403 /* Copy the extra info we tack onto an elf_link_hash_entry. */
406 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
407 struct elf_link_hash_entry
*dir
,
408 struct elf_link_hash_entry
*ind
)
410 struct riscv_elf_link_hash_entry
*edir
, *eind
;
412 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
413 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
415 if (ind
->root
.type
== bfd_link_hash_indirect
416 && dir
->got
.refcount
<= 0)
418 edir
->tls_type
= eind
->tls_type
;
419 eind
->tls_type
= GOT_UNKNOWN
;
421 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
425 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
426 unsigned long symndx
, char tls_type
)
428 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
430 *new_tls_type
|= tls_type
;
431 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
433 (*_bfd_error_handler
)
434 (_("%pB: `%s' accessed both as normal and thread local symbol"),
435 abfd
, h
? h
->root
.root
.string
: "<local>");
442 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
443 struct elf_link_hash_entry
*h
, long symndx
)
445 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
446 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
448 if (htab
->elf
.sgot
== NULL
)
450 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
456 h
->got
.refcount
+= 1;
460 /* This is a global offset table entry for a local symbol. */
461 if (elf_local_got_refcounts (abfd
) == NULL
)
463 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
464 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
466 _bfd_riscv_elf_local_got_tls_type (abfd
)
467 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
469 elf_local_got_refcounts (abfd
) [symndx
] += 1;
475 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
477 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
479 (*_bfd_error_handler
)
480 (_("%pB: relocation %s against `%s' can not be used when making a shared "
481 "object; recompile with -fPIC"),
482 abfd
, r
? r
->name
: _("<unknown>"),
483 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
484 bfd_set_error (bfd_error_bad_value
);
487 /* Look through the relocs for a section during the first phase, and
488 allocate space in the global offset table or procedure linkage
492 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
493 asection
*sec
, const Elf_Internal_Rela
*relocs
)
495 struct riscv_elf_link_hash_table
*htab
;
496 Elf_Internal_Shdr
*symtab_hdr
;
497 struct elf_link_hash_entry
**sym_hashes
;
498 const Elf_Internal_Rela
*rel
;
499 asection
*sreloc
= NULL
;
501 if (bfd_link_relocatable (info
))
504 htab
= riscv_elf_hash_table (info
);
505 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
506 sym_hashes
= elf_sym_hashes (abfd
);
508 if (htab
->elf
.dynobj
== NULL
)
509 htab
->elf
.dynobj
= abfd
;
511 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
514 unsigned int r_symndx
;
515 struct elf_link_hash_entry
*h
;
517 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
518 r_type
= ELFNN_R_TYPE (rel
->r_info
);
520 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
522 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
527 if (r_symndx
< symtab_hdr
->sh_info
)
531 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
532 while (h
->root
.type
== bfd_link_hash_indirect
533 || h
->root
.type
== bfd_link_hash_warning
)
534 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
539 case R_RISCV_TLS_GD_HI20
:
540 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
541 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
545 case R_RISCV_TLS_GOT_HI20
:
546 if (bfd_link_pic (info
))
547 info
->flags
|= DF_STATIC_TLS
;
548 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
549 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
553 case R_RISCV_GOT_HI20
:
554 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
555 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
559 case R_RISCV_CALL_PLT
:
560 /* This symbol requires a procedure linkage table entry. We
561 actually build the entry in adjust_dynamic_symbol,
562 because this might be a case of linking PIC code without
563 linking in any dynamic objects, in which case we don't
564 need to generate a procedure linkage table after all. */
569 h
->plt
.refcount
+= 1;
576 case R_RISCV_RVC_BRANCH
:
577 case R_RISCV_RVC_JUMP
:
578 case R_RISCV_PCREL_HI20
:
579 /* In shared libraries, these relocs are known to bind locally. */
580 if (bfd_link_pic (info
))
584 case R_RISCV_TPREL_HI20
:
585 if (!bfd_link_executable (info
))
586 return bad_static_reloc (abfd
, r_type
, h
);
588 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
592 if (bfd_link_pic (info
))
593 return bad_static_reloc (abfd
, r_type
, h
);
597 case R_RISCV_JUMP_SLOT
:
598 case R_RISCV_RELATIVE
:
604 /* This reloc might not bind locally. */
608 if (h
!= NULL
&& !bfd_link_pic (info
))
610 /* We may need a .plt entry if the function this reloc
611 refers to is in a shared lib. */
612 h
->plt
.refcount
+= 1;
615 /* If we are creating a shared library, and this is a reloc
616 against a global symbol, or a non PC relative reloc
617 against a local symbol, then we need to copy the reloc
618 into the shared library. However, if we are linking with
619 -Bsymbolic, we do not need to copy a reloc against a
620 global symbol which is defined in an object we are
621 including in the link (i.e., DEF_REGULAR is set). At
622 this point we have not seen all the input files, so it is
623 possible that DEF_REGULAR is not set now but will be set
624 later (it is never cleared). In case of a weak definition,
625 DEF_REGULAR may be cleared later by a strong definition in
626 a shared library. We account for that possibility below by
627 storing information in the relocs_copied field of the hash
628 table entry. A similar situation occurs when creating
629 shared libraries and symbol visibility changes render the
632 If on the other hand, we are creating an executable, we
633 may need to keep relocations for symbols satisfied by a
634 dynamic library if we manage to avoid copy relocs for the
636 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
638 if ((bfd_link_pic (info
)
639 && (sec
->flags
& SEC_ALLOC
) != 0
640 && ((r
!= NULL
&& ! r
->pc_relative
)
643 || h
->root
.type
== bfd_link_hash_defweak
644 || !h
->def_regular
))))
645 || (!bfd_link_pic (info
)
646 && (sec
->flags
& SEC_ALLOC
) != 0
648 && (h
->root
.type
== bfd_link_hash_defweak
649 || !h
->def_regular
)))
651 struct elf_dyn_relocs
*p
;
652 struct elf_dyn_relocs
**head
;
654 /* When creating a shared object, we must copy these
655 relocs into the output file. We create a reloc
656 section in dynobj and make room for the reloc. */
659 sreloc
= _bfd_elf_make_dynamic_reloc_section
660 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
661 abfd
, /*rela?*/ TRUE
);
667 /* If this is a global symbol, we count the number of
668 relocations we need for this symbol. */
670 head
= &h
->dyn_relocs
;
673 /* Track dynamic relocs needed for local syms too.
674 We really need local syms available to do this
679 Elf_Internal_Sym
*isym
;
681 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
686 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
690 vpp
= &elf_section_data (s
)->local_dynrel
;
691 head
= (struct elf_dyn_relocs
**) vpp
;
695 if (p
== NULL
|| p
->sec
!= sec
)
697 size_t amt
= sizeof *p
;
698 p
= ((struct elf_dyn_relocs
*)
699 bfd_alloc (htab
->elf
.dynobj
, amt
));
710 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
715 case R_RISCV_GNU_VTINHERIT
:
716 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
720 case R_RISCV_GNU_VTENTRY
:
721 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
734 riscv_elf_gc_mark_hook (asection
*sec
,
735 struct bfd_link_info
*info
,
736 Elf_Internal_Rela
*rel
,
737 struct elf_link_hash_entry
*h
,
738 Elf_Internal_Sym
*sym
)
741 switch (ELFNN_R_TYPE (rel
->r_info
))
743 case R_RISCV_GNU_VTINHERIT
:
744 case R_RISCV_GNU_VTENTRY
:
748 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
751 /* Adjust a symbol defined by a dynamic object and referenced by a
752 regular object. The current definition is in some section of the
753 dynamic object, but we're not including those sections. We have to
754 change the definition to something the rest of the link can
758 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
759 struct elf_link_hash_entry
*h
)
761 struct riscv_elf_link_hash_table
*htab
;
762 struct riscv_elf_link_hash_entry
* eh
;
766 htab
= riscv_elf_hash_table (info
);
767 BFD_ASSERT (htab
!= NULL
);
769 dynobj
= htab
->elf
.dynobj
;
771 /* Make sure we know what is going on here. */
772 BFD_ASSERT (dynobj
!= NULL
774 || h
->type
== STT_GNU_IFUNC
778 && !h
->def_regular
)));
780 /* If this is a function, put it in the procedure linkage table. We
781 will fill in the contents of the procedure linkage table later
782 (although we could actually do it here). */
783 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
785 if (h
->plt
.refcount
<= 0
786 || SYMBOL_CALLS_LOCAL (info
, h
)
787 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
788 && h
->root
.type
== bfd_link_hash_undefweak
))
790 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
791 input file, but the symbol was never referred to by a dynamic
792 object, or if all references were garbage collected. In such
793 a case, we don't actually need to build a PLT entry. */
794 h
->plt
.offset
= (bfd_vma
) -1;
801 h
->plt
.offset
= (bfd_vma
) -1;
803 /* If this is a weak symbol, and there is a real definition, the
804 processor independent code will have arranged for us to see the
805 real definition first, and we can just use the same value. */
808 struct elf_link_hash_entry
*def
= weakdef (h
);
809 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
810 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
811 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
815 /* This is a reference to a symbol defined by a dynamic object which
816 is not a function. */
818 /* If we are creating a shared library, we must presume that the
819 only references to the symbol are via the global offset table.
820 For such cases we need not do anything here; the relocations will
821 be handled correctly by relocate_section. */
822 if (bfd_link_pic (info
))
825 /* If there are no references to this symbol that do not use the
826 GOT, we don't need to generate a copy reloc. */
830 /* If -z nocopyreloc was given, we won't generate them either. */
831 if (info
->nocopyreloc
)
837 /* If we don't find any dynamic relocs in read-only sections, then
838 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
839 if (!_bfd_elf_readonly_dynrelocs (h
))
845 /* We must allocate the symbol in our .dynbss section, which will
846 become part of the .bss section of the executable. There will be
847 an entry for this symbol in the .dynsym section. The dynamic
848 object will contain position independent code, so all references
849 from the dynamic object to this symbol will go through the global
850 offset table. The dynamic linker will use the .dynsym entry to
851 determine the address it must put in the global offset table, so
852 both the dynamic object and the regular object will refer to the
853 same memory location for the variable. */
855 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
856 to copy the initial value out of the dynamic object and into the
857 runtime process image. We need to remember the offset into the
858 .rel.bss section we are going to use. */
859 eh
= (struct riscv_elf_link_hash_entry
*) h
;
860 if (eh
->tls_type
& ~GOT_NORMAL
)
863 srel
= htab
->elf
.srelbss
;
865 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
867 s
= htab
->elf
.sdynrelro
;
868 srel
= htab
->elf
.sreldynrelro
;
872 s
= htab
->elf
.sdynbss
;
873 srel
= htab
->elf
.srelbss
;
875 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
877 srel
->size
+= sizeof (ElfNN_External_Rela
);
881 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
884 /* Allocate space in .plt, .got and associated reloc sections for
888 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
890 struct bfd_link_info
*info
;
891 struct riscv_elf_link_hash_table
*htab
;
892 struct elf_dyn_relocs
*p
;
894 if (h
->root
.type
== bfd_link_hash_indirect
)
897 info
= (struct bfd_link_info
*) inf
;
898 htab
= riscv_elf_hash_table (info
);
899 BFD_ASSERT (htab
!= NULL
);
901 if (htab
->elf
.dynamic_sections_created
902 && h
->plt
.refcount
> 0)
904 /* Make sure this symbol is output as a dynamic symbol.
905 Undefined weak syms won't yet be marked as dynamic. */
909 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
913 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
915 asection
*s
= htab
->elf
.splt
;
918 s
->size
= PLT_HEADER_SIZE
;
920 h
->plt
.offset
= s
->size
;
922 /* Make room for this entry. */
923 s
->size
+= PLT_ENTRY_SIZE
;
925 /* We also need to make an entry in the .got.plt section. */
926 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
928 /* We also need to make an entry in the .rela.plt section. */
929 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
931 /* If this symbol is not defined in a regular file, and we are
932 not generating a shared library, then set the symbol to this
933 location in the .plt. This is required to make function
934 pointers compare as equal between the normal executable and
935 the shared library. */
936 if (! bfd_link_pic (info
)
939 h
->root
.u
.def
.section
= s
;
940 h
->root
.u
.def
.value
= h
->plt
.offset
;
945 h
->plt
.offset
= (bfd_vma
) -1;
951 h
->plt
.offset
= (bfd_vma
) -1;
955 if (h
->got
.refcount
> 0)
959 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
961 /* Make sure this symbol is output as a dynamic symbol.
962 Undefined weak syms won't yet be marked as dynamic. */
966 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
971 h
->got
.offset
= s
->size
;
972 dyn
= htab
->elf
.dynamic_sections_created
;
973 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
975 /* TLS_GD needs two dynamic relocs and two GOT slots. */
976 if (tls_type
& GOT_TLS_GD
)
978 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
979 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
982 /* TLS_IE needs one dynamic reloc and one GOT slot. */
983 if (tls_type
& GOT_TLS_IE
)
985 s
->size
+= RISCV_ELF_WORD_BYTES
;
986 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
991 s
->size
+= RISCV_ELF_WORD_BYTES
;
992 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
993 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
994 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
998 h
->got
.offset
= (bfd_vma
) -1;
1000 if (h
->dyn_relocs
== NULL
)
1003 /* In the shared -Bsymbolic case, discard space allocated for
1004 dynamic pc-relative relocs against symbols which turn out to be
1005 defined in regular objects. For the normal shared case, discard
1006 space for pc-relative relocs that have become local due to symbol
1007 visibility changes. */
1009 if (bfd_link_pic (info
))
1011 if (SYMBOL_CALLS_LOCAL (info
, h
))
1013 struct elf_dyn_relocs
**pp
;
1015 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1017 p
->count
-= p
->pc_count
;
1026 /* Also discard relocs on undefined weak syms with non-default
1028 if (h
->dyn_relocs
!= NULL
1029 && h
->root
.type
== bfd_link_hash_undefweak
)
1031 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1032 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1033 h
->dyn_relocs
= NULL
;
1035 /* Make sure undefined weak symbols are output as a dynamic
1037 else if (h
->dynindx
== -1
1038 && !h
->forced_local
)
1040 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1047 /* For the non-shared case, discard space for relocs against
1048 symbols which turn out to need copy relocs or are not
1054 || (htab
->elf
.dynamic_sections_created
1055 && (h
->root
.type
== bfd_link_hash_undefweak
1056 || h
->root
.type
== bfd_link_hash_undefined
))))
1058 /* Make sure this symbol is output as a dynamic symbol.
1059 Undefined weak syms won't yet be marked as dynamic. */
1060 if (h
->dynindx
== -1
1061 && !h
->forced_local
)
1063 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1067 /* If that succeeded, we know we'll be keeping all the
1069 if (h
->dynindx
!= -1)
1073 h
->dyn_relocs
= NULL
;
1078 /* Finally, allocate space. */
1079 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1081 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1082 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1089 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1091 struct riscv_elf_link_hash_table
*htab
;
1096 htab
= riscv_elf_hash_table (info
);
1097 BFD_ASSERT (htab
!= NULL
);
1098 dynobj
= htab
->elf
.dynobj
;
1099 BFD_ASSERT (dynobj
!= NULL
);
1101 if (elf_hash_table (info
)->dynamic_sections_created
)
1103 /* Set the contents of the .interp section to the interpreter. */
1104 if (bfd_link_executable (info
) && !info
->nointerp
)
1106 s
= bfd_get_linker_section (dynobj
, ".interp");
1107 BFD_ASSERT (s
!= NULL
);
1108 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1109 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1113 /* Set up .got offsets for local syms, and space for local dynamic
1115 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1117 bfd_signed_vma
*local_got
;
1118 bfd_signed_vma
*end_local_got
;
1119 char *local_tls_type
;
1120 bfd_size_type locsymcount
;
1121 Elf_Internal_Shdr
*symtab_hdr
;
1124 if (! is_riscv_elf (ibfd
))
1127 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1129 struct elf_dyn_relocs
*p
;
1131 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1133 if (!bfd_is_abs_section (p
->sec
)
1134 && bfd_is_abs_section (p
->sec
->output_section
))
1136 /* Input section has been discarded, either because
1137 it is a copy of a linkonce section or due to
1138 linker script /DISCARD/, so we'll be discarding
1141 else if (p
->count
!= 0)
1143 srel
= elf_section_data (p
->sec
)->sreloc
;
1144 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1145 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1146 info
->flags
|= DF_TEXTREL
;
1151 local_got
= elf_local_got_refcounts (ibfd
);
1155 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1156 locsymcount
= symtab_hdr
->sh_info
;
1157 end_local_got
= local_got
+ locsymcount
;
1158 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1160 srel
= htab
->elf
.srelgot
;
1161 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1165 *local_got
= s
->size
;
1166 s
->size
+= RISCV_ELF_WORD_BYTES
;
1167 if (*local_tls_type
& GOT_TLS_GD
)
1168 s
->size
+= RISCV_ELF_WORD_BYTES
;
1169 if (bfd_link_pic (info
)
1170 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1171 srel
->size
+= sizeof (ElfNN_External_Rela
);
1174 *local_got
= (bfd_vma
) -1;
1178 /* Allocate global sym .plt and .got entries, and space for global
1179 sym dynamic relocs. */
1180 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1182 if (htab
->elf
.sgotplt
)
1184 struct elf_link_hash_entry
*got
;
1185 got
= elf_link_hash_lookup (elf_hash_table (info
),
1186 "_GLOBAL_OFFSET_TABLE_",
1187 FALSE
, FALSE
, FALSE
);
1189 /* Don't allocate .got.plt section if there are no GOT nor PLT
1190 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1192 || !got
->ref_regular_nonweak
)
1193 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1194 && (htab
->elf
.splt
== NULL
1195 || htab
->elf
.splt
->size
== 0)
1196 && (htab
->elf
.sgot
== NULL
1197 || (htab
->elf
.sgot
->size
1198 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1199 htab
->elf
.sgotplt
->size
= 0;
1202 /* The check_relocs and adjust_dynamic_symbol entry points have
1203 determined the sizes of the various dynamic sections. Allocate
1205 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1207 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1210 if (s
== htab
->elf
.splt
1211 || s
== htab
->elf
.sgot
1212 || s
== htab
->elf
.sgotplt
1213 || s
== htab
->elf
.sdynbss
1214 || s
== htab
->elf
.sdynrelro
1215 || s
== htab
->sdyntdata
)
1217 /* Strip this section if we don't need it; see the
1220 else if (strncmp (s
->name
, ".rela", 5) == 0)
1224 /* We use the reloc_count field as a counter if we need
1225 to copy relocs into the output file. */
1231 /* It's not one of our sections. */
1237 /* If we don't need this section, strip it from the
1238 output file. This is mostly to handle .rela.bss and
1239 .rela.plt. We must create both sections in
1240 create_dynamic_sections, because they must be created
1241 before the linker maps input sections to output
1242 sections. The linker does that before
1243 adjust_dynamic_symbol is called, and it is that
1244 function which decides whether anything needs to go
1245 into these sections. */
1246 s
->flags
|= SEC_EXCLUDE
;
1250 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1253 /* Allocate memory for the section contents. Zero the memory
1254 for the benefit of .rela.plt, which has 4 unused entries
1255 at the beginning, and we don't want garbage. */
1256 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1257 if (s
->contents
== NULL
)
1261 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, TRUE
);
1265 #define DTP_OFFSET 0x800
1267 /* Return the relocation value for a TLS dtp-relative reloc. */
1270 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1272 /* If tls_sec is NULL, we should have signalled an error already. */
1273 if (elf_hash_table (info
)->tls_sec
== NULL
)
1275 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1278 /* Return the relocation value for a static TLS tp-relative relocation. */
1281 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1283 /* If tls_sec is NULL, we should have signalled an error already. */
1284 if (elf_hash_table (info
)->tls_sec
== NULL
)
1286 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1289 /* Return the global pointer's value, or 0 if it is not in use. */
1292 riscv_global_pointer_value (struct bfd_link_info
*info
)
1294 struct bfd_link_hash_entry
*h
;
1296 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1297 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1300 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1303 /* Emplace a static relocation. */
1305 static bfd_reloc_status_type
1306 perform_relocation (const reloc_howto_type
*howto
,
1307 const Elf_Internal_Rela
*rel
,
1309 asection
*input_section
,
1313 if (howto
->pc_relative
)
1314 value
-= sec_addr (input_section
) + rel
->r_offset
;
1315 value
+= rel
->r_addend
;
1317 switch (ELFNN_R_TYPE (rel
->r_info
))
1320 case R_RISCV_TPREL_HI20
:
1321 case R_RISCV_PCREL_HI20
:
1322 case R_RISCV_GOT_HI20
:
1323 case R_RISCV_TLS_GOT_HI20
:
1324 case R_RISCV_TLS_GD_HI20
:
1325 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1326 return bfd_reloc_overflow
;
1327 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1330 case R_RISCV_LO12_I
:
1331 case R_RISCV_GPREL_I
:
1332 case R_RISCV_TPREL_LO12_I
:
1333 case R_RISCV_TPREL_I
:
1334 case R_RISCV_PCREL_LO12_I
:
1335 value
= ENCODE_ITYPE_IMM (value
);
1338 case R_RISCV_LO12_S
:
1339 case R_RISCV_GPREL_S
:
1340 case R_RISCV_TPREL_LO12_S
:
1341 case R_RISCV_TPREL_S
:
1342 case R_RISCV_PCREL_LO12_S
:
1343 value
= ENCODE_STYPE_IMM (value
);
1347 case R_RISCV_CALL_PLT
:
1348 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1349 return bfd_reloc_overflow
;
1350 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1351 | (ENCODE_ITYPE_IMM (value
) << 32);
1355 if (!VALID_UJTYPE_IMM (value
))
1356 return bfd_reloc_overflow
;
1357 value
= ENCODE_UJTYPE_IMM (value
);
1360 case R_RISCV_BRANCH
:
1361 if (!VALID_SBTYPE_IMM (value
))
1362 return bfd_reloc_overflow
;
1363 value
= ENCODE_SBTYPE_IMM (value
);
1366 case R_RISCV_RVC_BRANCH
:
1367 if (!VALID_RVC_B_IMM (value
))
1368 return bfd_reloc_overflow
;
1369 value
= ENCODE_RVC_B_IMM (value
);
1372 case R_RISCV_RVC_JUMP
:
1373 if (!VALID_RVC_J_IMM (value
))
1374 return bfd_reloc_overflow
;
1375 value
= ENCODE_RVC_J_IMM (value
);
1378 case R_RISCV_RVC_LUI
:
1379 if (RISCV_CONST_HIGH_PART (value
) == 0)
1381 /* Linker relaxation can convert an address equal to or greater than
1382 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1383 valid immediate. We can fix this by converting it to a C.LI. */
1384 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1385 contents
+ rel
->r_offset
);
1386 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1387 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1388 value
= ENCODE_RVC_IMM (0);
1390 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1391 return bfd_reloc_overflow
;
1393 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1411 case R_RISCV_32_PCREL
:
1412 case R_RISCV_TLS_DTPREL32
:
1413 case R_RISCV_TLS_DTPREL64
:
1416 case R_RISCV_DELETE
:
1417 return bfd_reloc_ok
;
1420 return bfd_reloc_notsupported
;
1423 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1424 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1425 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1427 return bfd_reloc_ok
;
1430 /* Remember all PC-relative high-part relocs we've encountered to help us
1431 later resolve the corresponding low-part relocs. */
1437 } riscv_pcrel_hi_reloc
;
1439 typedef struct riscv_pcrel_lo_reloc
1441 asection
* input_section
;
1442 struct bfd_link_info
* info
;
1443 reloc_howto_type
* howto
;
1444 const Elf_Internal_Rela
* reloc
;
1447 bfd_byte
* contents
;
1448 struct riscv_pcrel_lo_reloc
* next
;
1449 } riscv_pcrel_lo_reloc
;
1454 riscv_pcrel_lo_reloc
*lo_relocs
;
1455 } riscv_pcrel_relocs
;
1458 riscv_pcrel_reloc_hash (const void *entry
)
1460 const riscv_pcrel_hi_reloc
*e
= entry
;
1461 return (hashval_t
)(e
->address
>> 2);
1465 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1467 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1468 return e1
->address
== e2
->address
;
1472 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1475 p
->lo_relocs
= NULL
;
1476 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1477 riscv_pcrel_reloc_eq
, free
);
1478 return p
->hi_relocs
!= NULL
;
1482 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1484 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1488 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1493 htab_delete (p
->hi_relocs
);
1497 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1498 struct bfd_link_info
*info
,
1502 const reloc_howto_type
*howto
,
1505 /* We may need to reference low addreses in PC-relative modes even when the
1506 * PC is far away from these addresses. For example, undefweak references
1507 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1508 * addresses that we can link PC-relative programs at, the linker can't
1509 * actually relocate references to those symbols. In order to allow these
1510 * programs to work we simply convert the PC-relative auipc sequences to
1511 * 0-relative lui sequences. */
1512 if (bfd_link_pic (info
))
1515 /* If it's possible to reference the symbol using auipc we do so, as that's
1516 * more in the spirit of the PC-relative relocations we're processing. */
1517 bfd_vma offset
= addr
- pc
;
1518 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1521 /* If it's impossible to reference this with a LUI-based offset then don't
1522 * bother to convert it at all so users still see the PC-relative relocation
1523 * in the truncation message. */
1524 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1527 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1529 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1530 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1531 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1536 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1537 bfd_vma value
, bfd_boolean absolute
)
1539 bfd_vma offset
= absolute
? value
: value
- addr
;
1540 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1541 riscv_pcrel_hi_reloc
**slot
=
1542 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1544 BFD_ASSERT (*slot
== NULL
);
1545 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1553 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1554 asection
*input_section
,
1555 struct bfd_link_info
*info
,
1556 reloc_howto_type
*howto
,
1557 const Elf_Internal_Rela
*reloc
,
1562 riscv_pcrel_lo_reloc
*entry
;
1563 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1566 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1567 name
, contents
, p
->lo_relocs
};
1568 p
->lo_relocs
= entry
;
1573 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1575 riscv_pcrel_lo_reloc
*r
;
1577 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1579 bfd
*input_bfd
= r
->input_section
->owner
;
1581 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1582 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1584 /* Check for overflow into bit 11 when adding reloc addend. */
1585 || (! (entry
->value
& 0x800)
1586 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1588 char *string
= (entry
== NULL
1589 ? "%pcrel_lo missing matching %pcrel_hi"
1590 : "%pcrel_lo overflow with an addend");
1591 (*r
->info
->callbacks
->reloc_dangerous
)
1592 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1596 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1597 input_bfd
, r
->contents
);
1603 /* Relocate a RISC-V ELF section.
1605 The RELOCATE_SECTION function is called by the new ELF backend linker
1606 to handle the relocations for a section.
1608 The relocs are always passed as Rela structures.
1610 This function is responsible for adjusting the section contents as
1611 necessary, and (if generating a relocatable output file) adjusting
1612 the reloc addend as necessary.
1614 This function does not have to worry about setting the reloc
1615 address or the reloc symbol index.
1617 LOCAL_SYMS is a pointer to the swapped in local symbols.
1619 LOCAL_SECTIONS is an array giving the section in the input file
1620 corresponding to the st_shndx field of each local symbol.
1622 The global hash table entry for the global symbols can be found
1623 via elf_sym_hashes (input_bfd).
1625 When generating relocatable output, this function must handle
1626 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1627 going to be the section symbol corresponding to the output
1628 section, which means that the addend must be adjusted
1632 riscv_elf_relocate_section (bfd
*output_bfd
,
1633 struct bfd_link_info
*info
,
1635 asection
*input_section
,
1637 Elf_Internal_Rela
*relocs
,
1638 Elf_Internal_Sym
*local_syms
,
1639 asection
**local_sections
)
1641 Elf_Internal_Rela
*rel
;
1642 Elf_Internal_Rela
*relend
;
1643 riscv_pcrel_relocs pcrel_relocs
;
1644 bfd_boolean ret
= FALSE
;
1645 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1646 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1647 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1648 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1649 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1650 bfd_boolean absolute
;
1652 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1655 relend
= relocs
+ input_section
->reloc_count
;
1656 for (rel
= relocs
; rel
< relend
; rel
++)
1658 unsigned long r_symndx
;
1659 struct elf_link_hash_entry
*h
;
1660 Elf_Internal_Sym
*sym
;
1663 bfd_reloc_status_type r
= bfd_reloc_ok
;
1665 bfd_vma off
, ie_off
;
1666 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1667 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1668 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1669 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1670 const char *msg
= NULL
;
1671 char *msg_buf
= NULL
;
1672 bfd_boolean resolved_to_zero
;
1675 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1678 /* This is a final link. */
1679 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1683 unresolved_reloc
= FALSE
;
1684 if (r_symndx
< symtab_hdr
->sh_info
)
1686 sym
= local_syms
+ r_symndx
;
1687 sec
= local_sections
[r_symndx
];
1688 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1692 bfd_boolean warned
, ignored
;
1694 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1695 r_symndx
, symtab_hdr
, sym_hashes
,
1697 unresolved_reloc
, warned
, ignored
);
1700 /* To avoid generating warning messages about truncated
1701 relocations, set the relocation's address to be the same as
1702 the start of this section. */
1703 if (input_section
->output_section
!= NULL
)
1704 relocation
= input_section
->output_section
->vma
;
1710 if (sec
!= NULL
&& discarded_section (sec
))
1711 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1712 rel
, 1, relend
, howto
, 0, contents
);
1714 if (bfd_link_relocatable (info
))
1718 name
= h
->root
.root
.string
;
1721 name
= (bfd_elf_string_from_elf_section
1722 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1723 if (name
== NULL
|| *name
== '\0')
1724 name
= bfd_section_name (sec
);
1727 resolved_to_zero
= (h
!= NULL
1728 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1734 case R_RISCV_TPREL_ADD
:
1736 case R_RISCV_JUMP_SLOT
:
1737 case R_RISCV_RELATIVE
:
1738 /* These require nothing of us at all. */
1742 case R_RISCV_BRANCH
:
1743 case R_RISCV_RVC_BRANCH
:
1744 case R_RISCV_RVC_LUI
:
1745 case R_RISCV_LO12_I
:
1746 case R_RISCV_LO12_S
:
1751 case R_RISCV_32_PCREL
:
1752 case R_RISCV_DELETE
:
1753 /* These require no special handling beyond perform_relocation. */
1756 case R_RISCV_GOT_HI20
:
1759 bfd_boolean dyn
, pic
;
1761 off
= h
->got
.offset
;
1762 BFD_ASSERT (off
!= (bfd_vma
) -1);
1763 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1764 pic
= bfd_link_pic (info
);
1766 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1767 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1769 /* This is actually a static link, or it is a
1770 -Bsymbolic link and the symbol is defined
1771 locally, or the symbol was forced to be local
1772 because of a version file. We must initialize
1773 this entry in the global offset table. Since the
1774 offset must always be a multiple of the word size,
1775 we use the least significant bit to record whether
1776 we have initialized it already.
1778 When doing a dynamic link, we create a .rela.got
1779 relocation entry to initialize the value. This
1780 is done in the finish_dynamic_symbol routine. */
1785 bfd_put_NN (output_bfd
, relocation
,
1786 htab
->elf
.sgot
->contents
+ off
);
1791 unresolved_reloc
= FALSE
;
1795 BFD_ASSERT (local_got_offsets
!= NULL
1796 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1798 off
= local_got_offsets
[r_symndx
];
1800 /* The offset must always be a multiple of the word size.
1801 So, we can use the least significant bit to record
1802 whether we have already processed this entry. */
1807 if (bfd_link_pic (info
))
1810 Elf_Internal_Rela outrel
;
1812 /* We need to generate a R_RISCV_RELATIVE reloc
1813 for the dynamic linker. */
1814 s
= htab
->elf
.srelgot
;
1815 BFD_ASSERT (s
!= NULL
);
1817 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1819 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1820 outrel
.r_addend
= relocation
;
1822 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1825 bfd_put_NN (output_bfd
, relocation
,
1826 htab
->elf
.sgot
->contents
+ off
);
1827 local_got_offsets
[r_symndx
] |= 1;
1830 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1831 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1838 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1839 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1841 r
= bfd_reloc_notsupported
;
1842 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1843 relocation
, absolute
))
1844 r
= bfd_reloc_overflow
;
1852 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1853 contents
+ rel
->r_offset
);
1854 relocation
= old_value
+ relocation
;
1864 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1865 contents
+ rel
->r_offset
);
1866 relocation
= old_value
- relocation
;
1871 case R_RISCV_CALL_PLT
:
1872 /* Handle a call to an undefined weak function. This won't be
1873 relaxed, so we have to handle it here. */
1874 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
1875 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
1877 /* We can use x0 as the base register. */
1878 bfd_vma insn
= bfd_get_32 (input_bfd
,
1879 contents
+ rel
->r_offset
+ 4);
1880 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1881 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
1882 /* Set the relocation value so that we get 0 after the pc
1883 relative adjustment. */
1884 relocation
= sec_addr (input_section
) + rel
->r_offset
;
1889 case R_RISCV_RVC_JUMP
:
1890 /* This line has to match the check in _bfd_riscv_relax_section. */
1891 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
1893 /* Refer to the PLT entry. */
1894 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
1895 unresolved_reloc
= FALSE
;
1899 case R_RISCV_TPREL_HI20
:
1900 relocation
= tpoff (info
, relocation
);
1903 case R_RISCV_TPREL_LO12_I
:
1904 case R_RISCV_TPREL_LO12_S
:
1905 relocation
= tpoff (info
, relocation
);
1908 case R_RISCV_TPREL_I
:
1909 case R_RISCV_TPREL_S
:
1910 relocation
= tpoff (info
, relocation
);
1911 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
1913 /* We can use tp as the base register. */
1914 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1915 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1916 insn
|= X_TP
<< OP_SH_RS1
;
1917 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
1920 r
= bfd_reloc_overflow
;
1923 case R_RISCV_GPREL_I
:
1924 case R_RISCV_GPREL_S
:
1926 bfd_vma gp
= riscv_global_pointer_value (info
);
1927 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
1928 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
1930 /* We can use x0 or gp as the base register. */
1931 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1932 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1935 rel
->r_addend
-= gp
;
1936 insn
|= X_GP
<< OP_SH_RS1
;
1938 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
1941 r
= bfd_reloc_overflow
;
1945 case R_RISCV_PCREL_HI20
:
1946 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1953 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1954 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1956 r
= bfd_reloc_notsupported
;
1957 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1958 relocation
+ rel
->r_addend
,
1960 r
= bfd_reloc_overflow
;
1963 case R_RISCV_PCREL_LO12_I
:
1964 case R_RISCV_PCREL_LO12_S
:
1965 /* We don't allow section symbols plus addends as the auipc address,
1966 because then riscv_relax_delete_bytes would have to search through
1967 all relocs to update these addends. This is also ambiguous, as
1968 we do allow offsets to be added to the target address, which are
1969 not to be used to find the auipc address. */
1970 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
1971 || (h
!= NULL
&& h
->type
== STT_SECTION
))
1974 msg
= _("%pcrel_lo section symbol with an addend");
1975 r
= bfd_reloc_dangerous
;
1979 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
1980 howto
, rel
, relocation
, name
,
1983 r
= bfd_reloc_overflow
;
1986 case R_RISCV_TLS_DTPREL32
:
1987 case R_RISCV_TLS_DTPREL64
:
1988 relocation
= dtpoff (info
, relocation
);
1993 if ((input_section
->flags
& SEC_ALLOC
) == 0)
1996 if ((bfd_link_pic (info
)
1998 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
1999 && !resolved_to_zero
)
2000 || h
->root
.type
!= bfd_link_hash_undefweak
)
2001 && (! howto
->pc_relative
2002 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2003 || (!bfd_link_pic (info
)
2009 || h
->root
.type
== bfd_link_hash_undefweak
2010 || h
->root
.type
== bfd_link_hash_undefined
)))
2012 Elf_Internal_Rela outrel
;
2013 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2015 /* When generating a shared object, these relocations
2016 are copied into the output file to be resolved at run
2020 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2022 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2023 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2024 outrel
.r_offset
+= sec_addr (input_section
);
2026 if (skip_dynamic_relocation
)
2027 memset (&outrel
, 0, sizeof outrel
);
2028 else if (h
!= NULL
&& h
->dynindx
!= -1
2029 && !(bfd_link_pic (info
)
2030 && SYMBOLIC_BIND (info
, h
)
2033 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2034 outrel
.r_addend
= rel
->r_addend
;
2038 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2039 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2042 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2043 if (skip_static_relocation
)
2048 case R_RISCV_TLS_GOT_HI20
:
2052 case R_RISCV_TLS_GD_HI20
:
2055 off
= h
->got
.offset
;
2060 off
= local_got_offsets
[r_symndx
];
2061 local_got_offsets
[r_symndx
] |= 1;
2064 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2065 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2066 /* If this symbol is referenced by both GD and IE TLS, the IE
2067 reference's GOT slot follows the GD reference's slots. */
2069 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2070 ie_off
= 2 * GOT_ENTRY_SIZE
;
2076 Elf_Internal_Rela outrel
;
2078 bfd_boolean need_relocs
= FALSE
;
2080 if (htab
->elf
.srelgot
== NULL
)
2085 bfd_boolean dyn
, pic
;
2086 dyn
= htab
->elf
.dynamic_sections_created
;
2087 pic
= bfd_link_pic (info
);
2089 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2090 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2094 /* The GOT entries have not been initialized yet. Do it
2095 now, and emit any relocations. */
2096 if ((bfd_link_pic (info
) || indx
!= 0)
2098 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2099 || h
->root
.type
!= bfd_link_hash_undefweak
))
2102 if (tls_type
& GOT_TLS_GD
)
2106 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2107 outrel
.r_addend
= 0;
2108 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2109 bfd_put_NN (output_bfd
, 0,
2110 htab
->elf
.sgot
->contents
+ off
);
2111 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2114 BFD_ASSERT (! unresolved_reloc
);
2115 bfd_put_NN (output_bfd
,
2116 dtpoff (info
, relocation
),
2117 (htab
->elf
.sgot
->contents
+ off
+
2118 RISCV_ELF_WORD_BYTES
));
2122 bfd_put_NN (output_bfd
, 0,
2123 (htab
->elf
.sgot
->contents
+ off
+
2124 RISCV_ELF_WORD_BYTES
));
2125 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2126 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2127 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2132 /* If we are not emitting relocations for a
2133 general dynamic reference, then we must be in a
2134 static link or an executable link with the
2135 symbol binding locally. Mark it as belonging
2136 to module 1, the executable. */
2137 bfd_put_NN (output_bfd
, 1,
2138 htab
->elf
.sgot
->contents
+ off
);
2139 bfd_put_NN (output_bfd
,
2140 dtpoff (info
, relocation
),
2141 (htab
->elf
.sgot
->contents
+ off
+
2142 RISCV_ELF_WORD_BYTES
));
2146 if (tls_type
& GOT_TLS_IE
)
2150 bfd_put_NN (output_bfd
, 0,
2151 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2152 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2154 outrel
.r_addend
= 0;
2156 outrel
.r_addend
= tpoff (info
, relocation
);
2157 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2158 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2162 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2163 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2168 BFD_ASSERT (off
< (bfd_vma
) -2);
2169 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2170 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2172 r
= bfd_reloc_overflow
;
2173 unresolved_reloc
= FALSE
;
2177 r
= bfd_reloc_notsupported
;
2180 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2181 because such sections are not SEC_ALLOC and thus ld.so will
2182 not process them. */
2183 if (unresolved_reloc
2184 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2186 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2187 rel
->r_offset
) != (bfd_vma
) -1)
2193 case R_RISCV_RVC_JUMP
:
2194 if (asprintf (&msg_buf
,
2195 _("%%X%%P: relocation %s against `%s' can "
2196 "not be used when making a shared object; "
2197 "recompile with -fPIC\n"),
2199 h
->root
.root
.string
) == -1)
2204 if (asprintf (&msg_buf
,
2205 _("%%X%%P: unresolvable %s relocation against "
2208 h
->root
.root
.string
) == -1)
2214 r
= bfd_reloc_notsupported
;
2217 if (r
== bfd_reloc_ok
)
2218 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2219 input_bfd
, contents
);
2221 /* We should have already detected the error and set message before.
2222 If the error message isn't set since the linker runs out of memory
2223 or we don't set it before, then we should set the default message
2224 with the "internal error" string here. */
2230 case bfd_reloc_overflow
:
2231 info
->callbacks
->reloc_overflow
2232 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2233 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2236 case bfd_reloc_undefined
:
2237 info
->callbacks
->undefined_symbol
2238 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2242 case bfd_reloc_outofrange
:
2244 msg
= _("%X%P: internal error: out of range error\n");
2247 case bfd_reloc_notsupported
:
2249 msg
= _("%X%P: internal error: unsupported relocation error\n");
2252 case bfd_reloc_dangerous
:
2253 /* The error message should already be set. */
2255 msg
= _("dangerous relocation error");
2256 info
->callbacks
->reloc_dangerous
2257 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2261 msg
= _("%X%P: internal error: unknown error\n");
2265 /* Do not report error message for the dangerous relocation again. */
2266 if (msg
&& r
!= bfd_reloc_dangerous
)
2267 info
->callbacks
->einfo (msg
);
2269 /* Free the unused `msg_buf`. */
2272 /* We already reported the error via a callback, so don't try to report
2273 it again by returning false. That leads to spurious errors. */
2278 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2280 riscv_free_pcrel_relocs (&pcrel_relocs
);
2284 /* Finish up dynamic symbol handling. We set the contents of various
2285 dynamic sections here. */
2288 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2289 struct bfd_link_info
*info
,
2290 struct elf_link_hash_entry
*h
,
2291 Elf_Internal_Sym
*sym
)
2293 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2294 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2296 if (h
->plt
.offset
!= (bfd_vma
) -1)
2298 /* We've decided to create a PLT entry for this symbol. */
2300 bfd_vma i
, header_address
, plt_idx
, got_address
;
2301 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2302 Elf_Internal_Rela rela
;
2304 BFD_ASSERT (h
->dynindx
!= -1);
2306 /* Calculate the address of the PLT header. */
2307 header_address
= sec_addr (htab
->elf
.splt
);
2309 /* Calculate the index of the entry. */
2310 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2312 /* Calculate the address of the .got.plt entry. */
2313 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2315 /* Find out where the .plt entry should go. */
2316 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2318 /* Fill in the PLT entry itself. */
2319 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2320 header_address
+ h
->plt
.offset
,
2324 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2325 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2327 /* Fill in the initial value of the .got.plt entry. */
2328 loc
= htab
->elf
.sgotplt
->contents
2329 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2330 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2332 /* Fill in the entry in the .rela.plt section. */
2333 rela
.r_offset
= got_address
;
2335 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2337 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2338 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2340 if (!h
->def_regular
)
2342 /* Mark the symbol as undefined, rather than as defined in
2343 the .plt section. Leave the value alone. */
2344 sym
->st_shndx
= SHN_UNDEF
;
2345 /* If the symbol is weak, we do need to clear the value.
2346 Otherwise, the PLT entry would provide a definition for
2347 the symbol even if the symbol wasn't defined anywhere,
2348 and so the symbol would never be NULL. */
2349 if (!h
->ref_regular_nonweak
)
2354 if (h
->got
.offset
!= (bfd_vma
) -1
2355 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2356 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2360 Elf_Internal_Rela rela
;
2362 /* This symbol has an entry in the GOT. Set it up. */
2364 sgot
= htab
->elf
.sgot
;
2365 srela
= htab
->elf
.srelgot
;
2366 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2368 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2370 /* If this is a local symbol reference, we just want to emit a RELATIVE
2371 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2372 the symbol was forced to be local because of a version file.
2373 The entry in the global offset table will already have been
2374 initialized in the relocate_section function. */
2375 if (bfd_link_pic (info
)
2376 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2378 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2379 asection
*sec
= h
->root
.u
.def
.section
;
2380 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2381 rela
.r_addend
= (h
->root
.u
.def
.value
2382 + sec
->output_section
->vma
2383 + sec
->output_offset
);
2387 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2388 BFD_ASSERT (h
->dynindx
!= -1);
2389 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2393 bfd_put_NN (output_bfd
, 0,
2394 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2395 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2400 Elf_Internal_Rela rela
;
2403 /* This symbols needs a copy reloc. Set it up. */
2404 BFD_ASSERT (h
->dynindx
!= -1);
2406 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2407 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2409 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2410 s
= htab
->elf
.sreldynrelro
;
2412 s
= htab
->elf
.srelbss
;
2413 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2416 /* Mark some specially defined symbols as absolute. */
2417 if (h
== htab
->elf
.hdynamic
2418 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2419 sym
->st_shndx
= SHN_ABS
;
2424 /* Finish up the dynamic sections. */
2427 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2428 bfd
*dynobj
, asection
*sdyn
)
2430 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2431 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2432 size_t dynsize
= bed
->s
->sizeof_dyn
;
2433 bfd_byte
*dyncon
, *dynconend
;
2435 dynconend
= sdyn
->contents
+ sdyn
->size
;
2436 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2438 Elf_Internal_Dyn dyn
;
2441 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2446 s
= htab
->elf
.sgotplt
;
2447 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2450 s
= htab
->elf
.srelplt
;
2451 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2454 s
= htab
->elf
.srelplt
;
2455 dyn
.d_un
.d_val
= s
->size
;
2461 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2467 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2468 struct bfd_link_info
*info
)
2472 struct riscv_elf_link_hash_table
*htab
;
2474 htab
= riscv_elf_hash_table (info
);
2475 BFD_ASSERT (htab
!= NULL
);
2476 dynobj
= htab
->elf
.dynobj
;
2478 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2480 if (elf_hash_table (info
)->dynamic_sections_created
)
2485 splt
= htab
->elf
.splt
;
2486 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2488 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2493 /* Fill in the head and tail entries in the procedure linkage table. */
2497 uint32_t plt_header
[PLT_HEADER_INSNS
];
2498 ret
= riscv_make_plt_header (output_bfd
,
2499 sec_addr (htab
->elf
.sgotplt
),
2500 sec_addr (splt
), plt_header
);
2504 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2505 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2507 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2512 if (htab
->elf
.sgotplt
)
2514 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2516 if (bfd_is_abs_section (output_section
))
2518 (*_bfd_error_handler
)
2519 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2523 if (htab
->elf
.sgotplt
->size
> 0)
2525 /* Write the first two entries in .got.plt, needed for the dynamic
2527 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2528 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2529 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2532 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2537 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2539 if (htab
->elf
.sgot
->size
> 0)
2541 /* Set the first entry in the global offset table to the address of
2542 the dynamic section. */
2543 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2544 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2547 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2553 /* Return address for Ith PLT stub in section PLT, for relocation REL
2554 or (bfd_vma) -1 if it should not be included. */
2557 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2558 const arelent
*rel ATTRIBUTE_UNUSED
)
2560 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2563 static enum elf_reloc_type_class
2564 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2565 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2566 const Elf_Internal_Rela
*rela
)
2568 switch (ELFNN_R_TYPE (rela
->r_info
))
2570 case R_RISCV_RELATIVE
:
2571 return reloc_class_relative
;
2572 case R_RISCV_JUMP_SLOT
:
2573 return reloc_class_plt
;
2575 return reloc_class_copy
;
2577 return reloc_class_normal
;
2581 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2585 riscv_float_abi_string (flagword flags
)
2587 switch (flags
& EF_RISCV_FLOAT_ABI
)
2589 case EF_RISCV_FLOAT_ABI_SOFT
:
2590 return "soft-float";
2592 case EF_RISCV_FLOAT_ABI_SINGLE
:
2593 return "single-float";
2595 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2596 return "double-float";
2598 case EF_RISCV_FLOAT_ABI_QUAD
:
2599 return "quad-float";
2606 /* The information of architecture attribute. */
2607 static riscv_subset_list_t in_subsets
;
2608 static riscv_subset_list_t out_subsets
;
2609 static riscv_subset_list_t merged_subsets
;
2611 /* Predicator for standard extension. */
2614 riscv_std_ext_p (const char *name
)
2616 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2619 /* Error handler when version mis-match. */
2622 riscv_version_mismatch (bfd
*ibfd
,
2623 struct riscv_subset_t
*in
,
2624 struct riscv_subset_t
*out
)
2627 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2630 in
->major_version
, in
->minor_version
,
2631 out
->major_version
, out
->minor_version
);
2634 /* Return true if subset is 'i' or 'e'. */
2637 riscv_i_or_e_p (bfd
*ibfd
,
2639 struct riscv_subset_t
*subset
)
2641 if ((strcasecmp (subset
->name
, "e") != 0)
2642 && (strcasecmp (subset
->name
, "i") != 0))
2645 (_("error: %pB: corrupted ISA string '%s'. "
2646 "First letter should be 'i' or 'e' but got '%s'."),
2647 ibfd
, arch
, subset
->name
);
2653 /* Merge standard extensions.
2656 Return FALSE if failed to merge.
2660 `in_arch`: Raw arch string for input object.
2661 `out_arch`: Raw arch string for output object.
2662 `pin`: subset list for input object, and it'll skip all merged subset after
2664 `pout`: Like `pin`, but for output object. */
2667 riscv_merge_std_ext (bfd
*ibfd
,
2668 const char *in_arch
,
2669 const char *out_arch
,
2670 struct riscv_subset_t
**pin
,
2671 struct riscv_subset_t
**pout
)
2673 const char *standard_exts
= riscv_supported_std_ext ();
2675 struct riscv_subset_t
*in
= *pin
;
2676 struct riscv_subset_t
*out
= *pout
;
2678 /* First letter should be 'i' or 'e'. */
2679 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2682 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2685 if (strcasecmp (in
->name
, out
->name
) != 0)
2687 /* TODO: We might allow merge 'i' with 'e'. */
2689 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2690 ibfd
, in
->name
, out
->name
);
2693 else if ((in
->major_version
!= out
->major_version
) ||
2694 (in
->minor_version
!= out
->minor_version
))
2696 /* TODO: Allow different merge policy. */
2697 riscv_version_mismatch (ibfd
, in
, out
);
2701 riscv_add_subset (&merged_subsets
,
2702 in
->name
, in
->major_version
, in
->minor_version
);
2707 /* Handle standard extension first. */
2708 for (p
= standard_exts
; *p
; ++p
)
2710 char find_ext
[2] = {*p
, '\0'};
2711 struct riscv_subset_t
*find_in
=
2712 riscv_lookup_subset (&in_subsets
, find_ext
);
2713 struct riscv_subset_t
*find_out
=
2714 riscv_lookup_subset (&out_subsets
, find_ext
);
2716 if (find_in
== NULL
&& find_out
== NULL
)
2719 /* Check version is same or not. */
2720 /* TODO: Allow different merge policy. */
2721 if ((find_in
!= NULL
&& find_out
!= NULL
)
2722 && ((find_in
->major_version
!= find_out
->major_version
)
2723 || (find_in
->minor_version
!= find_out
->minor_version
)))
2725 riscv_version_mismatch (ibfd
, in
, out
);
2729 struct riscv_subset_t
*merged
= find_in
? find_in
: find_out
;
2730 riscv_add_subset (&merged_subsets
, merged
->name
,
2731 merged
->major_version
, merged
->minor_version
);
2734 /* Skip all standard extensions. */
2735 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2736 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2744 /* If C is a prefix class, then return the EXT string without the prefix.
2745 Otherwise return the entire EXT string. */
2748 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
2752 case RV_ISA_CLASS_X
: return &ext
[1];
2753 case RV_ISA_CLASS_S
: return &ext
[1];
2754 case RV_ISA_CLASS_Z
: return &ext
[1];
2755 default: return ext
;
2759 /* Compare prefixed extension names canonically. */
2762 riscv_prefix_cmp (const char *a
, const char *b
)
2764 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
2765 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
2767 /* Extension name without prefix */
2768 const char *anp
= riscv_skip_prefix (a
, ca
);
2769 const char *bnp
= riscv_skip_prefix (b
, cb
);
2772 return strcasecmp (anp
, bnp
);
2774 return (int)ca
- (int)cb
;
2777 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2778 object subset list. Likewise for POUT and the output object. Return TRUE
2779 on success and FALSE when a conflict is found. */
2782 riscv_merge_multi_letter_ext (bfd
*ibfd
,
2783 riscv_subset_t
**pin
,
2784 riscv_subset_t
**pout
)
2786 riscv_subset_t
*in
= *pin
;
2787 riscv_subset_t
*out
= *pout
;
2788 riscv_subset_t
*tail
;
2794 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
2798 /* `in' comes before `out', append `in' and increment. */
2799 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2805 /* `out' comes before `in', append `out' and increment. */
2806 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2807 out
->minor_version
);
2812 /* Both present, check version and increment both. */
2813 if ((in
->major_version
!= out
->major_version
)
2814 || (in
->minor_version
!= out
->minor_version
))
2816 riscv_version_mismatch (ibfd
, in
, out
);
2820 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2821 out
->minor_version
);
2828 /* If we're here, either `in' or `out' is running longer than
2829 the other. So, we need to append the corresponding tail. */
2830 tail
= in
? in
: out
;
2834 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
2835 tail
->minor_version
);
2843 /* Merge Tag_RISCV_arch attribute. */
2846 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2848 riscv_subset_t
*in
, *out
;
2849 char *merged_arch_str
;
2851 unsigned xlen_in
, xlen_out
;
2852 merged_subsets
.head
= NULL
;
2853 merged_subsets
.tail
= NULL
;
2855 riscv_parse_subset_t rpe_in
;
2856 riscv_parse_subset_t rpe_out
;
2858 /* Only assembler needs to check the default version of ISA, so just set
2859 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
2860 rpe_in
.subset_list
= &in_subsets
;
2861 rpe_in
.error_handler
= _bfd_error_handler
;
2862 rpe_in
.xlen
= &xlen_in
;
2863 rpe_in
.get_default_version
= NULL
;
2865 rpe_out
.subset_list
= &out_subsets
;
2866 rpe_out
.error_handler
= _bfd_error_handler
;
2867 rpe_out
.xlen
= &xlen_out
;
2868 rpe_out
.get_default_version
= NULL
;
2870 if (in_arch
== NULL
&& out_arch
== NULL
)
2873 if (in_arch
== NULL
&& out_arch
!= NULL
)
2876 if (in_arch
!= NULL
&& out_arch
== NULL
)
2879 /* Parse subset from arch string. */
2880 if (!riscv_parse_subset (&rpe_in
, in_arch
))
2883 if (!riscv_parse_subset (&rpe_out
, out_arch
))
2886 /* Checking XLEN. */
2887 if (xlen_out
!= xlen_in
)
2890 (_("error: %pB: ISA string of input (%s) doesn't match "
2891 "output (%s)."), ibfd
, in_arch
, out_arch
);
2895 /* Merge subset list. */
2896 in
= in_subsets
.head
;
2897 out
= out_subsets
.head
;
2899 /* Merge standard extension. */
2900 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
2903 /* Merge all non-single letter extensions with single call. */
2904 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
2907 if (xlen_in
!= xlen_out
)
2910 (_("error: %pB: XLEN of input (%u) doesn't match "
2911 "output (%u)."), ibfd
, xlen_in
, xlen_out
);
2915 if (xlen_in
!= ARCH_SIZE
)
2918 (_("error: %pB: Unsupported XLEN (%u), you might be "
2919 "using wrong emulation."), ibfd
, xlen_in
);
2923 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
2925 /* Release the subset lists. */
2926 riscv_release_subset_list (&in_subsets
);
2927 riscv_release_subset_list (&out_subsets
);
2928 riscv_release_subset_list (&merged_subsets
);
2930 return merged_arch_str
;
2933 /* Merge object attributes from IBFD into output_bfd of INFO.
2934 Raise an error if there are conflicting attributes. */
2937 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
2939 bfd
*obfd
= info
->output_bfd
;
2940 obj_attribute
*in_attr
;
2941 obj_attribute
*out_attr
;
2942 bfd_boolean result
= TRUE
;
2943 bfd_boolean priv_attrs_merged
= FALSE
;
2944 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
2947 /* Skip linker created files. */
2948 if (ibfd
->flags
& BFD_LINKER_CREATED
)
2951 /* Skip any input that doesn't have an attribute section.
2952 This enables to link object files without attribute section with
2954 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
2957 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
2959 /* This is the first object. Copy the attributes. */
2960 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
2962 out_attr
= elf_known_obj_attributes_proc (obfd
);
2964 /* Use the Tag_null value to indicate the attributes have been
2971 in_attr
= elf_known_obj_attributes_proc (ibfd
);
2972 out_attr
= elf_known_obj_attributes_proc (obfd
);
2974 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
2978 case Tag_RISCV_arch
:
2979 if (!out_attr
[Tag_RISCV_arch
].s
)
2980 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
2981 else if (in_attr
[Tag_RISCV_arch
].s
2982 && out_attr
[Tag_RISCV_arch
].s
)
2984 /* Check arch compatible. */
2986 riscv_merge_arch_attr_info (ibfd
,
2987 in_attr
[Tag_RISCV_arch
].s
,
2988 out_attr
[Tag_RISCV_arch
].s
);
2989 if (merged_arch
== NULL
)
2992 out_attr
[Tag_RISCV_arch
].s
= "";
2995 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
2999 case Tag_RISCV_priv_spec
:
3000 case Tag_RISCV_priv_spec_minor
:
3001 case Tag_RISCV_priv_spec_revision
:
3002 /* If we have handled the priv attributes, then skip it. */
3003 if (!priv_attrs_merged
)
3005 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3006 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3007 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3008 enum riscv_priv_spec_class in_priv_spec
;
3009 enum riscv_priv_spec_class out_priv_spec
;
3011 /* Get the priv spec class from elf attribute numbers. */
3012 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3016 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3021 /* Allow to link the object without the priv specs. */
3022 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3024 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3025 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3026 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3028 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3029 && in_priv_spec
!= out_priv_spec
)
3032 (_("warning: %pB use privilege spec version %u.%u.%u but "
3033 "the output use version %u.%u.%u."),
3042 /* The priv spec v1.9.1 can be linked with other spec
3043 versions since the conflicts. We plan to drop the
3044 v1.9.1 in a year or two, so this confict should be
3045 removed in the future. */
3046 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3047 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3050 (_("warning: privilege spec version 1.9.1 can not be "
3051 "linked with other spec versions."));
3054 /* Update the output priv attributes to the newest. */
3055 if (in_priv_spec
> out_priv_spec
)
3057 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3058 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3059 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3062 priv_attrs_merged
= TRUE
;
3066 case Tag_RISCV_unaligned_access
:
3067 out_attr
[i
].i
|= in_attr
[i
].i
;
3070 case Tag_RISCV_stack_align
:
3071 if (out_attr
[i
].i
== 0)
3072 out_attr
[i
].i
= in_attr
[i
].i
;
3073 else if (in_attr
[i
].i
!= 0
3074 && out_attr
[i
].i
!= 0
3075 && out_attr
[i
].i
!= in_attr
[i
].i
)
3078 (_("error: %pB use %u-byte stack aligned but the output "
3079 "use %u-byte stack aligned."),
3080 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3086 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3089 /* If out_attr was copied from in_attr then it won't have a type yet. */
3090 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3091 out_attr
[i
].type
= in_attr
[i
].type
;
3094 /* Merge Tag_compatibility attributes and any common GNU ones. */
3095 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3098 /* Check for any attributes not known on RISC-V. */
3099 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3104 /* Merge backend specific data from an object file to the output
3105 object file when linking. */
3108 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3110 bfd
*obfd
= info
->output_bfd
;
3111 flagword new_flags
, old_flags
;
3113 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3116 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3118 (*_bfd_error_handler
)
3119 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3120 " target emulation `%s' does not match `%s'"),
3121 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3125 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3128 if (!riscv_merge_attributes (ibfd
, info
))
3131 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3132 old_flags
= elf_elfheader (obfd
)->e_flags
;
3134 if (! elf_flags_init (obfd
))
3136 elf_flags_init (obfd
) = TRUE
;
3137 elf_elfheader (obfd
)->e_flags
= new_flags
;
3141 /* Check to see if the input BFD actually contains any sections. If not,
3142 its flags may not have been initialized either, but it cannot actually
3143 cause any incompatibility. Do not short-circuit dynamic objects; their
3144 section list may be emptied by elf_link_add_object_symbols.
3146 Also check to see if there are no code sections in the input. In this
3147 case, there is no need to check for code specific flags. */
3148 if (!(ibfd
->flags
& DYNAMIC
))
3150 bfd_boolean null_input_bfd
= TRUE
;
3151 bfd_boolean only_data_sections
= TRUE
;
3154 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3156 if ((bfd_section_flags (sec
)
3157 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3158 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3159 only_data_sections
= FALSE
;
3161 null_input_bfd
= FALSE
;
3165 if (null_input_bfd
|| only_data_sections
)
3169 /* Disallow linking different float ABIs. */
3170 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3172 (*_bfd_error_handler
)
3173 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3174 riscv_float_abi_string (new_flags
),
3175 riscv_float_abi_string (old_flags
));
3179 /* Disallow linking RVE and non-RVE. */
3180 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3182 (*_bfd_error_handler
)
3183 (_("%pB: can't link RVE with other target"), ibfd
);
3187 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3188 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3193 bfd_set_error (bfd_error_bad_value
);
3197 /* Delete some bytes from a section while relaxing. */
3200 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3201 struct bfd_link_info
*link_info
)
3203 unsigned int i
, symcount
;
3204 bfd_vma toaddr
= sec
->size
;
3205 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3206 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3207 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3208 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3209 bfd_byte
*contents
= data
->this_hdr
.contents
;
3211 /* Actually delete the bytes. */
3213 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3215 /* Adjust the location of all of the relocs. Note that we need not
3216 adjust the addends, since all PC-relative references must be against
3217 symbols, which we will adjust below. */
3218 for (i
= 0; i
< sec
->reloc_count
; i
++)
3219 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3220 data
->relocs
[i
].r_offset
-= count
;
3222 /* Adjust the local symbols defined in this section. */
3223 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3225 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3226 if (sym
->st_shndx
== sec_shndx
)
3228 /* If the symbol is in the range of memory we just moved, we
3229 have to adjust its value. */
3230 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3231 sym
->st_value
-= count
;
3233 /* If the symbol *spans* the bytes we just deleted (i.e. its
3234 *end* is in the moved bytes but its *start* isn't), then we
3235 must adjust its size.
3237 This test needs to use the original value of st_value, otherwise
3238 we might accidentally decrease size when deleting bytes right
3239 before the symbol. But since deleted relocs can't span across
3240 symbols, we can't have both a st_value and a st_size decrease,
3241 so it is simpler to just use an else. */
3242 else if (sym
->st_value
<= addr
3243 && sym
->st_value
+ sym
->st_size
> addr
3244 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3245 sym
->st_size
-= count
;
3249 /* Now adjust the global symbols defined in this section. */
3250 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3251 - symtab_hdr
->sh_info
);
3253 for (i
= 0; i
< symcount
; i
++)
3255 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3257 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3258 containing the definition of __wrap_SYMBOL, includes a direct
3259 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3260 the same symbol (which is __wrap_SYMBOL), but still exist as two
3261 different symbols in 'sym_hashes', we don't want to adjust
3262 the global symbol __wrap_SYMBOL twice. */
3263 /* The same problem occurs with symbols that are versioned_hidden, as
3264 foo becomes an alias for foo@BAR, and hence they need the same
3266 if (link_info
->wrap_hash
!= NULL
3267 || sym_hash
->versioned
== versioned_hidden
)
3269 struct elf_link_hash_entry
**cur_sym_hashes
;
3271 /* Loop only over the symbols which have already been checked. */
3272 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3275 /* If the current symbol is identical to 'sym_hash', that means
3276 the symbol was already adjusted (or at least checked). */
3277 if (*cur_sym_hashes
== sym_hash
)
3280 /* Don't adjust the symbol again. */
3281 if (cur_sym_hashes
< &sym_hashes
[i
])
3285 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3286 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3287 && sym_hash
->root
.u
.def
.section
== sec
)
3289 /* As above, adjust the value if needed. */
3290 if (sym_hash
->root
.u
.def
.value
> addr
3291 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3292 sym_hash
->root
.u
.def
.value
-= count
;
3294 /* As above, adjust the size if needed. */
3295 else if (sym_hash
->root
.u
.def
.value
<= addr
3296 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3297 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3298 sym_hash
->size
-= count
;
3305 /* A second format for recording PC-relative hi relocations. This stores the
3306 information required to relax them to GP-relative addresses. */
3308 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3309 struct riscv_pcgp_hi_reloc
3316 bfd_boolean undefined_weak
;
3317 riscv_pcgp_hi_reloc
*next
;
3320 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3321 struct riscv_pcgp_lo_reloc
3324 riscv_pcgp_lo_reloc
*next
;
3329 riscv_pcgp_hi_reloc
*hi
;
3330 riscv_pcgp_lo_reloc
*lo
;
3331 } riscv_pcgp_relocs
;
3333 /* Initialize the pcgp reloc info in P. */
3336 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3343 /* Free the pcgp reloc info in P. */
3346 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3347 bfd
*abfd ATTRIBUTE_UNUSED
,
3348 asection
*sec ATTRIBUTE_UNUSED
)
3350 riscv_pcgp_hi_reloc
*c
;
3351 riscv_pcgp_lo_reloc
*l
;
3353 for (c
= p
->hi
; c
!= NULL
;)
3355 riscv_pcgp_hi_reloc
*next
= c
->next
;
3360 for (l
= p
->lo
; l
!= NULL
;)
3362 riscv_pcgp_lo_reloc
*next
= l
->next
;
3368 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3369 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3370 relax the corresponding lo part reloc. */
3373 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3374 bfd_vma hi_addend
, bfd_vma hi_addr
,
3375 unsigned hi_sym
, asection
*sym_sec
,
3376 bfd_boolean undefined_weak
)
3378 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3381 new->hi_sec_off
= hi_sec_off
;
3382 new->hi_addend
= hi_addend
;
3383 new->hi_addr
= hi_addr
;
3384 new->hi_sym
= hi_sym
;
3385 new->sym_sec
= sym_sec
;
3386 new->undefined_weak
= undefined_weak
;
3392 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3393 This is used by a lo part reloc to find the corresponding hi part reloc. */
3395 static riscv_pcgp_hi_reloc
*
3396 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3398 riscv_pcgp_hi_reloc
*c
;
3400 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3401 if (c
->hi_sec_off
== hi_sec_off
)
3406 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3407 This is used to record relocs that can't be relaxed. */
3410 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3412 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3415 new->hi_sec_off
= hi_sec_off
;
3421 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3422 This is used by a hi part reloc to find the corresponding lo part reloc. */
3425 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3427 riscv_pcgp_lo_reloc
*c
;
3429 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3430 if (c
->hi_sec_off
== hi_sec_off
)
3435 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3436 struct bfd_link_info
*,
3437 Elf_Internal_Rela
*,
3438 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3439 riscv_pcgp_relocs
*,
3440 bfd_boolean undefined_weak
);
3442 /* Relax AUIPC + JALR into JAL. */
3445 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3446 struct bfd_link_info
*link_info
,
3447 Elf_Internal_Rela
*rel
,
3449 bfd_vma max_alignment
,
3450 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3452 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3453 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3455 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3456 bfd_signed_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3457 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3458 bfd_vma auipc
, jalr
;
3459 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3461 /* If the call crosses section boundaries, an alignment directive could
3462 cause the PC-relative offset to later increase, so we need to add in the
3463 max alignment of any section inclusive from the call to the target.
3464 Otherwise, we only need to use the alignment of the current section. */
3465 if (VALID_UJTYPE_IMM (foff
))
3467 if (sym_sec
->output_section
== sec
->output_section
3468 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3469 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3470 foff
+= (foff
< 0 ? -max_alignment
: max_alignment
);
3473 /* See if this function call can be shortened. */
3474 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3477 /* Shorten the function call. */
3478 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3480 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3481 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3482 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3483 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3485 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3486 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3490 /* Relax to C.J[AL] rd, addr. */
3491 r_type
= R_RISCV_RVC_JUMP
;
3492 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3495 else if (VALID_UJTYPE_IMM (foff
))
3497 /* Relax to JAL rd, addr. */
3498 r_type
= R_RISCV_JAL
;
3499 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3501 else /* near_zero */
3503 /* Relax to JALR rd, x0, addr. */
3504 r_type
= R_RISCV_LO12_I
;
3505 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3508 /* Replace the R_RISCV_CALL reloc. */
3509 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3510 /* Replace the AUIPC. */
3511 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3513 /* Delete unnecessary JALR. */
3515 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3519 /* Traverse all output sections and return the max alignment. */
3522 _bfd_riscv_get_max_alignment (asection
*sec
)
3524 unsigned int max_alignment_power
= 0;
3527 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3529 if (o
->alignment_power
> max_alignment_power
)
3530 max_alignment_power
= o
->alignment_power
;
3533 return (bfd_vma
) 1 << max_alignment_power
;
3536 /* Relax non-PIC global variable references. */
3539 _bfd_riscv_relax_lui (bfd
*abfd
,
3542 struct bfd_link_info
*link_info
,
3543 Elf_Internal_Rela
*rel
,
3545 bfd_vma max_alignment
,
3546 bfd_vma reserve_size
,
3548 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3549 bfd_boolean undefined_weak
)
3551 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3552 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3553 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3555 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3559 /* If gp and the symbol are in the same output section, which is not the
3560 abs section, then consider only that output section's alignment. */
3561 struct bfd_link_hash_entry
*h
=
3562 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3564 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3565 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3566 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3569 /* Is the reference in range of x0 or gp?
3570 Valid gp range conservatively because of alignment issue. */
3572 || (VALID_ITYPE_IMM (symval
)
3574 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3576 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3578 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3579 switch (ELFNN_R_TYPE (rel
->r_info
))
3581 case R_RISCV_LO12_I
:
3584 /* Change the RS1 to zero. */
3585 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3586 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3587 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3590 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3593 case R_RISCV_LO12_S
:
3596 /* Change the RS1 to zero. */
3597 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3598 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3599 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3602 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3606 /* We can delete the unnecessary LUI and reloc. */
3607 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3609 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3617 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3618 account for this assuming page alignment at worst. In the presence of
3619 RELRO segment the linker aligns it by one page size, therefore sections
3620 after the segment can be moved more than one page. */
3623 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3624 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3625 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3626 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3627 : ELF_MAXPAGESIZE
)))
3629 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3630 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3631 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3632 if (rd
== 0 || rd
== X_SP
)
3635 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3636 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3638 /* Replace the R_RISCV_HI20 reloc. */
3639 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3642 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3649 /* Relax non-PIC TLS references. */
3652 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3654 asection
*sym_sec ATTRIBUTE_UNUSED
,
3655 struct bfd_link_info
*link_info
,
3656 Elf_Internal_Rela
*rel
,
3658 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3659 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3661 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3662 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3664 /* See if this symbol is in range of tp. */
3665 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3668 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3669 switch (ELFNN_R_TYPE (rel
->r_info
))
3671 case R_RISCV_TPREL_LO12_I
:
3672 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3675 case R_RISCV_TPREL_LO12_S
:
3676 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3679 case R_RISCV_TPREL_HI20
:
3680 case R_RISCV_TPREL_ADD
:
3681 /* We can delete the unnecessary instruction and reloc. */
3682 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3684 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3691 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3694 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3696 struct bfd_link_info
*link_info
,
3697 Elf_Internal_Rela
*rel
,
3699 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3700 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3701 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3702 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3703 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3705 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3706 bfd_vma alignment
= 1, pos
;
3707 while (alignment
<= rel
->r_addend
)
3710 symval
-= rel
->r_addend
;
3711 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3712 bfd_vma nop_bytes
= aligned_addr
- symval
;
3714 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3715 sec
->sec_flg0
= TRUE
;
3717 /* Make sure there are enough NOPs to actually achieve the alignment. */
3718 if (rel
->r_addend
< nop_bytes
)
3721 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3722 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3723 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3724 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3725 bfd_set_error (bfd_error_bad_value
);
3729 /* Delete the reloc. */
3730 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3732 /* If the number of NOPs is already correct, there's nothing to do. */
3733 if (nop_bytes
== rel
->r_addend
)
3736 /* Write as many RISC-V NOPs as we need. */
3737 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3738 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3740 /* Write a final RVC NOP if need be. */
3741 if (nop_bytes
% 4 != 0)
3742 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3744 /* Delete the excess bytes. */
3745 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3746 rel
->r_addend
- nop_bytes
, link_info
);
3749 /* Relax PC-relative references to GP-relative references. */
3752 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3755 struct bfd_link_info
*link_info
,
3756 Elf_Internal_Rela
*rel
,
3758 bfd_vma max_alignment
,
3759 bfd_vma reserve_size
,
3760 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3761 riscv_pcgp_relocs
*pcgp_relocs
,
3762 bfd_boolean undefined_weak
)
3764 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3765 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3767 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3769 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3770 * actual target address. */
3771 riscv_pcgp_hi_reloc hi_reloc
;
3772 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3773 switch (ELFNN_R_TYPE (rel
->r_info
))
3775 case R_RISCV_PCREL_LO12_I
:
3776 case R_RISCV_PCREL_LO12_S
:
3778 /* If the %lo has an addend, it isn't for the label pointing at the
3779 hi part instruction, but rather for the symbol pointed at by the
3780 hi part instruction. So we must subtract it here for the lookup.
3781 It is still used below in the final symbol address. */
3782 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3783 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3787 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3792 symval
= hi_reloc
.hi_addr
;
3793 sym_sec
= hi_reloc
.sym_sec
;
3795 /* We can not know whether the undefined weak symbol is referenced
3796 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3797 we have to record the 'undefined_weak' flag when handling the
3798 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3799 undefined_weak
= hi_reloc
.undefined_weak
;
3803 case R_RISCV_PCREL_HI20
:
3804 /* Mergeable symbols and code might later move out of range. */
3805 if (! undefined_weak
3806 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3809 /* If the cooresponding lo relocation has already been seen then it's not
3810 * safe to relax this relocation. */
3811 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3822 /* If gp and the symbol are in the same output section, which is not the
3823 abs section, then consider only that output section's alignment. */
3824 struct bfd_link_hash_entry
*h
=
3825 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3827 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3828 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3829 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3832 /* Is the reference in range of x0 or gp?
3833 Valid gp range conservatively because of alignment issue. */
3835 || (VALID_ITYPE_IMM (symval
)
3837 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3839 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3841 unsigned sym
= hi_reloc
.hi_sym
;
3842 switch (ELFNN_R_TYPE (rel
->r_info
))
3844 case R_RISCV_PCREL_LO12_I
:
3847 /* Change the RS1 to zero, and then modify the relocation
3848 type to R_RISCV_LO12_I. */
3849 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3850 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3851 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3852 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3853 rel
->r_addend
= hi_reloc
.hi_addend
;
3857 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3858 rel
->r_addend
+= hi_reloc
.hi_addend
;
3862 case R_RISCV_PCREL_LO12_S
:
3865 /* Change the RS1 to zero, and then modify the relocation
3866 type to R_RISCV_LO12_S. */
3867 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3868 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3869 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3870 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3871 rel
->r_addend
= hi_reloc
.hi_addend
;
3875 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3876 rel
->r_addend
+= hi_reloc
.hi_addend
;
3880 case R_RISCV_PCREL_HI20
:
3881 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3885 ELFNN_R_SYM(rel
->r_info
),
3888 /* We can delete the unnecessary AUIPC and reloc. */
3889 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3901 /* Relax PC-relative references to GP-relative references. */
3904 _bfd_riscv_relax_delete (bfd
*abfd
,
3906 asection
*sym_sec ATTRIBUTE_UNUSED
,
3907 struct bfd_link_info
*link_info
,
3908 Elf_Internal_Rela
*rel
,
3909 bfd_vma symval ATTRIBUTE_UNUSED
,
3910 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3911 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3912 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3913 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3914 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3916 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3919 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3923 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3924 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3925 disabled, handles code alignment directives. */
3928 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3929 struct bfd_link_info
*info
,
3932 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3933 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3934 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3935 Elf_Internal_Rela
*relocs
;
3936 bfd_boolean ret
= FALSE
;
3938 bfd_vma max_alignment
, reserve_size
= 0;
3939 riscv_pcgp_relocs pcgp_relocs
;
3943 if (bfd_link_relocatable (info
)
3945 || (sec
->flags
& SEC_RELOC
) == 0
3946 || sec
->reloc_count
== 0
3947 || (info
->disable_target_specific_optimizations
3948 && info
->relax_pass
== 0))
3951 riscv_init_pcgp_relocs (&pcgp_relocs
);
3953 /* Read this BFD's relocs if we haven't done so already. */
3955 relocs
= data
->relocs
;
3956 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3957 info
->keep_memory
)))
3962 max_alignment
= htab
->max_alignment
;
3963 if (max_alignment
== (bfd_vma
) -1)
3965 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3966 htab
->max_alignment
= max_alignment
;
3970 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3972 /* Examine and consider relaxing each reloc. */
3973 for (i
= 0; i
< sec
->reloc_count
; i
++)
3976 Elf_Internal_Rela
*rel
= relocs
+ i
;
3977 relax_func_t relax_func
;
3978 int type
= ELFNN_R_TYPE (rel
->r_info
);
3981 bfd_boolean undefined_weak
= FALSE
;
3984 if (info
->relax_pass
== 0)
3986 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
3987 relax_func
= _bfd_riscv_relax_call
;
3988 else if (type
== R_RISCV_HI20
3989 || type
== R_RISCV_LO12_I
3990 || type
== R_RISCV_LO12_S
)
3991 relax_func
= _bfd_riscv_relax_lui
;
3992 else if (!bfd_link_pic(info
)
3993 && (type
== R_RISCV_PCREL_HI20
3994 || type
== R_RISCV_PCREL_LO12_I
3995 || type
== R_RISCV_PCREL_LO12_S
))
3996 relax_func
= _bfd_riscv_relax_pc
;
3997 else if (type
== R_RISCV_TPREL_HI20
3998 || type
== R_RISCV_TPREL_ADD
3999 || type
== R_RISCV_TPREL_LO12_I
4000 || type
== R_RISCV_TPREL_LO12_S
)
4001 relax_func
= _bfd_riscv_relax_tls_le
;
4005 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4006 if (i
== sec
->reloc_count
- 1
4007 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4008 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4011 /* Skip over the R_RISCV_RELAX. */
4014 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4015 relax_func
= _bfd_riscv_relax_delete
;
4016 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4017 relax_func
= _bfd_riscv_relax_align
;
4021 data
->relocs
= relocs
;
4023 /* Read this BFD's contents if we haven't done so already. */
4024 if (!data
->this_hdr
.contents
4025 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4028 /* Read this BFD's symbols if we haven't done so already. */
4029 if (symtab_hdr
->sh_info
!= 0
4030 && !symtab_hdr
->contents
4031 && !(symtab_hdr
->contents
=
4032 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4033 symtab_hdr
->sh_info
,
4034 0, NULL
, NULL
, NULL
)))
4037 /* Get the value of the symbol referred to by the reloc. */
4038 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4040 /* A local symbol. */
4041 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4042 + ELFNN_R_SYM (rel
->r_info
));
4043 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4044 ? 0 : isym
->st_size
- rel
->r_addend
;
4046 if (isym
->st_shndx
== SHN_UNDEF
)
4047 sym_sec
= sec
, symval
= rel
->r_offset
;
4050 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4051 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4053 /* The purpose of this code is unknown. It breaks linker scripts
4054 for embedded development that place sections at address zero.
4055 This code is believed to be unnecessary. Disabling it but not
4056 yet removing it, in case something breaks. */
4057 if (sec_addr (sym_sec
) == 0)
4060 symval
= isym
->st_value
;
4062 symtype
= ELF_ST_TYPE (isym
->st_info
);
4067 struct elf_link_hash_entry
*h
;
4069 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4070 h
= elf_sym_hashes (abfd
)[indx
];
4072 while (h
->root
.type
== bfd_link_hash_indirect
4073 || h
->root
.type
== bfd_link_hash_warning
)
4074 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4076 if (h
->root
.type
== bfd_link_hash_undefweak
4077 && (relax_func
== _bfd_riscv_relax_lui
4078 || relax_func
== _bfd_riscv_relax_pc
))
4080 /* For the lui and auipc relaxations, since the symbol
4081 value of an undefined weak symbol is always be zero,
4082 we can optimize the patterns into a single LI/MV/ADDI
4085 Note that, creating shared libraries and pie output may
4086 break the rule above. Fortunately, since we do not relax
4087 pc relocs when creating shared libraries and pie output,
4088 and the absolute address access for R_RISCV_HI20 isn't
4089 allowed when "-fPIC" is set, the problem of creating shared
4090 libraries can not happen currently. Once we support the
4091 auipc relaxations when creating shared libraries, then we will
4092 need the more rigorous checking for this optimization. */
4093 undefined_weak
= TRUE
;
4096 /* This line has to match the check in riscv_elf_relocate_section
4097 in the R_RISCV_CALL[_PLT] case. */
4098 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4100 sym_sec
= htab
->elf
.splt
;
4101 symval
= h
->plt
.offset
;
4103 else if (undefined_weak
)
4106 sym_sec
= bfd_und_section_ptr
;
4108 else if ((h
->root
.type
== bfd_link_hash_defined
4109 || h
->root
.type
== bfd_link_hash_defweak
)
4110 && h
->root
.u
.def
.section
!= NULL
4111 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4113 symval
= h
->root
.u
.def
.value
;
4114 sym_sec
= h
->root
.u
.def
.section
;
4119 if (h
->type
!= STT_FUNC
)
4121 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4125 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4126 && (sym_sec
->flags
& SEC_MERGE
))
4128 /* At this stage in linking, no SEC_MERGE symbol has been
4129 adjusted, so all references to such symbols need to be
4130 passed through _bfd_merged_section_offset. (Later, in
4131 relocate_section, all SEC_MERGE symbols *except* for
4132 section symbols have been adjusted.)
4134 gas may reduce relocations against symbols in SEC_MERGE
4135 sections to a relocation against the section symbol when
4136 the original addend was zero. When the reloc is against
4137 a section symbol we should include the addend in the
4138 offset passed to _bfd_merged_section_offset, since the
4139 location of interest is the original symbol. On the
4140 other hand, an access to "sym+addend" where "sym" is not
4141 a section symbol should not include the addend; Such an
4142 access is presumed to be an offset from "sym"; The
4143 location of interest is just "sym". */
4144 if (symtype
== STT_SECTION
)
4145 symval
+= rel
->r_addend
;
4147 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4148 elf_section_data (sym_sec
)->sec_info
,
4151 if (symtype
!= STT_SECTION
)
4152 symval
+= rel
->r_addend
;
4155 symval
+= rel
->r_addend
;
4157 symval
+= sec_addr (sym_sec
);
4159 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4160 max_alignment
, reserve_size
, again
,
4161 &pcgp_relocs
, undefined_weak
))
4168 if (relocs
!= data
->relocs
)
4170 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4176 # define PRSTATUS_SIZE 204
4177 # define PRSTATUS_OFFSET_PR_CURSIG 12
4178 # define PRSTATUS_OFFSET_PR_PID 24
4179 # define PRSTATUS_OFFSET_PR_REG 72
4180 # define ELF_GREGSET_T_SIZE 128
4181 # define PRPSINFO_SIZE 128
4182 # define PRPSINFO_OFFSET_PR_PID 16
4183 # define PRPSINFO_OFFSET_PR_FNAME 32
4184 # define PRPSINFO_OFFSET_PR_PSARGS 48
4186 # define PRSTATUS_SIZE 376
4187 # define PRSTATUS_OFFSET_PR_CURSIG 12
4188 # define PRSTATUS_OFFSET_PR_PID 32
4189 # define PRSTATUS_OFFSET_PR_REG 112
4190 # define ELF_GREGSET_T_SIZE 256
4191 # define PRPSINFO_SIZE 136
4192 # define PRPSINFO_OFFSET_PR_PID 24
4193 # define PRPSINFO_OFFSET_PR_FNAME 40
4194 # define PRPSINFO_OFFSET_PR_PSARGS 56
4197 /* Support for core dump NOTE sections. */
4200 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4202 switch (note
->descsz
)
4207 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4209 elf_tdata (abfd
)->core
->signal
4210 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4213 elf_tdata (abfd
)->core
->lwpid
4214 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4218 /* Make a ".reg/999" section. */
4219 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4220 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4224 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4226 switch (note
->descsz
)
4231 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4233 elf_tdata (abfd
)->core
->pid
4234 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4237 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4238 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4241 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4242 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4246 /* Note that for some reason, a spurious space is tacked
4247 onto the end of the args in some (at least one anyway)
4248 implementations, so strip it off if it exists. */
4251 char *command
= elf_tdata (abfd
)->core
->command
;
4252 int n
= strlen (command
);
4254 if (0 < n
&& command
[n
- 1] == ' ')
4255 command
[n
- 1] = '\0';
4261 /* Set the right mach type. */
4263 riscv_elf_object_p (bfd
*abfd
)
4265 /* There are only two mach types in RISCV currently. */
4266 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4267 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4269 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4274 /* Determine whether an object attribute tag takes an integer, a
4278 riscv_elf_obj_attrs_arg_type (int tag
)
4280 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4283 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4284 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4286 #define elf_backend_reloc_type_class riscv_reloc_type_class
4288 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4289 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4290 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4291 #define bfd_elfNN_bfd_merge_private_bfd_data \
4292 _bfd_riscv_elf_merge_private_bfd_data
4294 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4295 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4296 #define elf_backend_check_relocs riscv_elf_check_relocs
4297 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4298 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4299 #define elf_backend_relocate_section riscv_elf_relocate_section
4300 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4301 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4302 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4303 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4304 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4305 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4306 #define elf_backend_object_p riscv_elf_object_p
4307 #define elf_info_to_howto_rel NULL
4308 #define elf_info_to_howto riscv_info_to_howto_rela
4309 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4310 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4312 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4314 #define elf_backend_can_gc_sections 1
4315 #define elf_backend_can_refcount 1
4316 #define elf_backend_want_got_plt 1
4317 #define elf_backend_plt_readonly 1
4318 #define elf_backend_plt_alignment 4
4319 #define elf_backend_want_plt_sym 1
4320 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4321 #define elf_backend_want_dynrelro 1
4322 #define elf_backend_rela_normal 1
4323 #define elf_backend_default_execstack 0
4325 #undef elf_backend_obj_attrs_vendor
4326 #define elf_backend_obj_attrs_vendor "riscv"
4327 #undef elf_backend_obj_attrs_arg_type
4328 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4329 #undef elf_backend_obj_attrs_section_type
4330 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4331 #undef elf_backend_obj_attrs_section
4332 #define elf_backend_obj_attrs_section ".riscv.attributes"
4334 #include "elfNN-target.h"