1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
23 /* This file handles RISC-V ELF targets. */
31 #include "elfxx-riscv.h"
32 #include "elf/riscv.h"
33 #include "opcode/riscv.h"
35 /* Internal relocations used exclusively by the relaxation pass. */
36 #define R_RISCV_DELETE (R_RISCV_max + 1)
40 #define MINUS_ONE ((bfd_vma)0 - 1)
42 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
44 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
46 /* The name of the dynamic interpreter. This is put in the .interp
49 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
50 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
52 #define ELF_ARCH bfd_arch_riscv
53 #define ELF_TARGET_ID RISCV_ELF_DATA
54 #define ELF_MACHINE_CODE EM_RISCV
55 #define ELF_MAXPAGESIZE 0x1000
56 #define ELF_COMMONPAGESIZE 0x1000
58 /* RISC-V ELF linker hash entry. */
60 struct riscv_elf_link_hash_entry
62 struct elf_link_hash_entry elf
;
72 #define riscv_elf_hash_entry(ent) \
73 ((struct riscv_elf_link_hash_entry *)(ent))
75 struct _bfd_riscv_elf_obj_tdata
77 struct elf_obj_tdata root
;
79 /* tls_type for each local got entry. */
80 char *local_got_tls_type
;
83 #define _bfd_riscv_elf_tdata(abfd) \
84 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
86 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
87 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
89 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
90 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
91 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
93 #define is_riscv_elf(bfd) \
94 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
95 && elf_tdata (bfd) != NULL \
96 && elf_object_id (bfd) == RISCV_ELF_DATA)
99 elfNN_riscv_mkobject (bfd
*abfd
)
101 return bfd_elf_allocate_object (abfd
,
102 sizeof (struct _bfd_riscv_elf_obj_tdata
),
106 #include "elf/common.h"
107 #include "elf/internal.h"
109 struct riscv_elf_link_hash_table
111 struct elf_link_hash_table elf
;
113 /* Short-cuts to get to dynamic linker sections. */
116 /* The max alignment of output sections. */
117 bfd_vma max_alignment
;
121 /* Get the RISC-V ELF linker hash table from a link_info structure. */
122 #define riscv_elf_hash_table(p) \
123 ((is_elf_hash_table ((p)->hash) \
124 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
125 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
128 riscv_info_to_howto_rela (bfd
*abfd
,
130 Elf_Internal_Rela
*dst
)
132 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
133 return cache_ptr
->howto
!= NULL
;
137 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
139 const struct elf_backend_data
*bed
;
142 bed
= get_elf_backend_data (abfd
);
143 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
144 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
149 #define PLT_HEADER_INSNS 8
150 #define PLT_ENTRY_INSNS 4
151 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
152 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
154 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
156 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
158 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
161 riscv_elf_got_plt_val (bfd_vma plt_index
, struct bfd_link_info
*info
)
163 return sec_addr (riscv_elf_hash_table (info
)->elf
.sgotplt
)
164 + GOTPLT_HEADER_SIZE
+ (plt_index
* GOT_ENTRY_SIZE
);
168 # define MATCH_LREG MATCH_LW
170 # define MATCH_LREG MATCH_LD
173 /* Generate a PLT header. */
176 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
179 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
180 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
182 /* RVE has no t3 register, so this won't work, and is not supported. */
183 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
185 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
190 /* auipc t2, %hi(.got.plt)
191 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
192 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
193 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
194 addi t0, t2, %lo(.got.plt) # &.got.plt
195 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
196 l[w|d] t0, PTRSIZE(t0) # link map
199 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
200 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
201 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
202 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, -(PLT_HEADER_SIZE
+ 12));
203 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
204 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
205 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
206 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
211 /* Generate a PLT entry. */
214 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
217 /* RVE has no t3 register, so this won't work, and is not supported. */
218 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
220 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
225 /* auipc t3, %hi(.got.plt entry)
226 l[w|d] t3, %lo(.got.plt entry)(t3)
230 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
231 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
232 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
233 entry
[3] = RISCV_NOP
;
238 /* Create an entry in an RISC-V ELF linker hash table. */
240 static struct bfd_hash_entry
*
241 link_hash_newfunc (struct bfd_hash_entry
*entry
,
242 struct bfd_hash_table
*table
, const char *string
)
244 /* Allocate the structure if it has not already been allocated by a
249 bfd_hash_allocate (table
,
250 sizeof (struct riscv_elf_link_hash_entry
));
255 /* Call the allocation method of the superclass. */
256 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
259 struct riscv_elf_link_hash_entry
*eh
;
261 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
262 eh
->tls_type
= GOT_UNKNOWN
;
268 /* Create a RISC-V ELF linker hash table. */
270 static struct bfd_link_hash_table
*
271 riscv_elf_link_hash_table_create (bfd
*abfd
)
273 struct riscv_elf_link_hash_table
*ret
;
274 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
276 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
280 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
281 sizeof (struct riscv_elf_link_hash_entry
),
288 ret
->max_alignment
= (bfd_vma
) -1;
289 return &ret
->elf
.root
;
292 /* Create the .got section. */
295 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
299 struct elf_link_hash_entry
*h
;
300 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
301 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
303 /* This function may be called more than once. */
304 if (htab
->sgot
!= NULL
)
307 flags
= bed
->dynamic_sec_flags
;
309 s
= bfd_make_section_anyway_with_flags (abfd
,
310 (bed
->rela_plts_and_copies_p
311 ? ".rela.got" : ".rel.got"),
312 (bed
->dynamic_sec_flags
315 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
319 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
321 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
325 /* The first bit of the global offset table is the header. */
326 s
->size
+= bed
->got_header_size
;
328 if (bed
->want_got_plt
)
330 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
332 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
336 /* Reserve room for the header. */
337 s
->size
+= GOTPLT_HEADER_SIZE
;
340 if (bed
->want_got_sym
)
342 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
343 section. We don't do this in the linker script because we don't want
344 to define the symbol if we are not creating a global offset
346 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
347 "_GLOBAL_OFFSET_TABLE_");
348 elf_hash_table (info
)->hgot
= h
;
356 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
357 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
361 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
362 struct bfd_link_info
*info
)
364 struct riscv_elf_link_hash_table
*htab
;
366 htab
= riscv_elf_hash_table (info
);
367 BFD_ASSERT (htab
!= NULL
);
369 if (!riscv_elf_create_got_section (dynobj
, info
))
372 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
375 if (!bfd_link_pic (info
))
377 /* Technically, this section doesn't have contents. It is used as the
378 target of TLS copy relocs, to copy TLS data from shared libraries into
379 the executable. However, if we don't mark it as loadable, then it
380 matches the IS_TBSS test in ldlang.c, and there is no run-time address
381 space allocated for it even though it has SEC_ALLOC. That test is
382 correct for .tbss, but not correct for this section. There is also
383 a second problem that having a section with no contents can only work
384 if it comes after all sections with contents in the same segment,
385 but the linker script does not guarantee that. This is just mixed in
386 with other .tdata.* sections. We can fix both problems by lying and
387 saying that there are contents. This section is expected to be small
388 so this should not cause a significant extra program startup cost. */
390 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
391 (SEC_ALLOC
| SEC_THREAD_LOCAL
392 | SEC_LOAD
| SEC_DATA
394 | SEC_LINKER_CREATED
));
397 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
398 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
404 /* Copy the extra info we tack onto an elf_link_hash_entry. */
407 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
408 struct elf_link_hash_entry
*dir
,
409 struct elf_link_hash_entry
*ind
)
411 struct riscv_elf_link_hash_entry
*edir
, *eind
;
413 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
414 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
416 if (ind
->root
.type
== bfd_link_hash_indirect
417 && dir
->got
.refcount
<= 0)
419 edir
->tls_type
= eind
->tls_type
;
420 eind
->tls_type
= GOT_UNKNOWN
;
422 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
426 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
427 unsigned long symndx
, char tls_type
)
429 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
431 *new_tls_type
|= tls_type
;
432 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
434 (*_bfd_error_handler
)
435 (_("%pB: `%s' accessed both as normal and thread local symbol"),
436 abfd
, h
? h
->root
.root
.string
: "<local>");
443 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
444 struct elf_link_hash_entry
*h
, long symndx
)
446 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
447 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
449 if (htab
->elf
.sgot
== NULL
)
451 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
457 h
->got
.refcount
+= 1;
461 /* This is a global offset table entry for a local symbol. */
462 if (elf_local_got_refcounts (abfd
) == NULL
)
464 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
465 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
467 _bfd_riscv_elf_local_got_tls_type (abfd
)
468 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
470 elf_local_got_refcounts (abfd
) [symndx
] += 1;
476 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
478 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
480 (*_bfd_error_handler
)
481 (_("%pB: relocation %s against `%s' can not be used when making a shared "
482 "object; recompile with -fPIC"),
483 abfd
, r
? r
->name
: _("<unknown>"),
484 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
485 bfd_set_error (bfd_error_bad_value
);
488 /* Look through the relocs for a section during the first phase, and
489 allocate space in the global offset table or procedure linkage
493 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
494 asection
*sec
, const Elf_Internal_Rela
*relocs
)
496 struct riscv_elf_link_hash_table
*htab
;
497 Elf_Internal_Shdr
*symtab_hdr
;
498 struct elf_link_hash_entry
**sym_hashes
;
499 const Elf_Internal_Rela
*rel
;
500 asection
*sreloc
= NULL
;
502 if (bfd_link_relocatable (info
))
505 htab
= riscv_elf_hash_table (info
);
506 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
507 sym_hashes
= elf_sym_hashes (abfd
);
509 if (htab
->elf
.dynobj
== NULL
)
510 htab
->elf
.dynobj
= abfd
;
512 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
515 unsigned int r_symndx
;
516 struct elf_link_hash_entry
*h
;
518 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
519 r_type
= ELFNN_R_TYPE (rel
->r_info
);
521 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
523 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
528 if (r_symndx
< symtab_hdr
->sh_info
)
532 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
533 while (h
->root
.type
== bfd_link_hash_indirect
534 || h
->root
.type
== bfd_link_hash_warning
)
535 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
540 case R_RISCV_TLS_GD_HI20
:
541 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
542 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
546 case R_RISCV_TLS_GOT_HI20
:
547 if (bfd_link_pic (info
))
548 info
->flags
|= DF_STATIC_TLS
;
549 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
550 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
554 case R_RISCV_GOT_HI20
:
555 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
556 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
560 case R_RISCV_CALL_PLT
:
561 /* This symbol requires a procedure linkage table entry. We
562 actually build the entry in adjust_dynamic_symbol,
563 because this might be a case of linking PIC code without
564 linking in any dynamic objects, in which case we don't
565 need to generate a procedure linkage table after all. */
570 h
->plt
.refcount
+= 1;
577 case R_RISCV_RVC_BRANCH
:
578 case R_RISCV_RVC_JUMP
:
579 case R_RISCV_PCREL_HI20
:
580 /* In shared libraries, these relocs are known to bind locally. */
581 if (bfd_link_pic (info
))
585 case R_RISCV_TPREL_HI20
:
586 if (!bfd_link_executable (info
))
587 return bad_static_reloc (abfd
, r_type
, h
);
589 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
593 if (bfd_link_pic (info
))
594 return bad_static_reloc (abfd
, r_type
, h
);
598 case R_RISCV_JUMP_SLOT
:
599 case R_RISCV_RELATIVE
:
605 /* This reloc might not bind locally. */
609 if (h
!= NULL
&& !bfd_link_pic (info
))
611 /* We may need a .plt entry if the function this reloc
612 refers to is in a shared lib. */
613 h
->plt
.refcount
+= 1;
616 /* If we are creating a shared library, and this is a reloc
617 against a global symbol, or a non PC relative reloc
618 against a local symbol, then we need to copy the reloc
619 into the shared library. However, if we are linking with
620 -Bsymbolic, we do not need to copy a reloc against a
621 global symbol which is defined in an object we are
622 including in the link (i.e., DEF_REGULAR is set). At
623 this point we have not seen all the input files, so it is
624 possible that DEF_REGULAR is not set now but will be set
625 later (it is never cleared). In case of a weak definition,
626 DEF_REGULAR may be cleared later by a strong definition in
627 a shared library. We account for that possibility below by
628 storing information in the relocs_copied field of the hash
629 table entry. A similar situation occurs when creating
630 shared libraries and symbol visibility changes render the
633 If on the other hand, we are creating an executable, we
634 may need to keep relocations for symbols satisfied by a
635 dynamic library if we manage to avoid copy relocs for the
637 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
639 if ((bfd_link_pic (info
)
640 && (sec
->flags
& SEC_ALLOC
) != 0
641 && ((r
!= NULL
&& ! r
->pc_relative
)
644 || h
->root
.type
== bfd_link_hash_defweak
645 || !h
->def_regular
))))
646 || (!bfd_link_pic (info
)
647 && (sec
->flags
& SEC_ALLOC
) != 0
649 && (h
->root
.type
== bfd_link_hash_defweak
650 || !h
->def_regular
)))
652 struct elf_dyn_relocs
*p
;
653 struct elf_dyn_relocs
**head
;
655 /* When creating a shared object, we must copy these
656 relocs into the output file. We create a reloc
657 section in dynobj and make room for the reloc. */
660 sreloc
= _bfd_elf_make_dynamic_reloc_section
661 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
662 abfd
, /*rela?*/ TRUE
);
668 /* If this is a global symbol, we count the number of
669 relocations we need for this symbol. */
671 head
= &h
->dyn_relocs
;
674 /* Track dynamic relocs needed for local syms too.
675 We really need local syms available to do this
680 Elf_Internal_Sym
*isym
;
682 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
687 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
691 vpp
= &elf_section_data (s
)->local_dynrel
;
692 head
= (struct elf_dyn_relocs
**) vpp
;
696 if (p
== NULL
|| p
->sec
!= sec
)
698 size_t amt
= sizeof *p
;
699 p
= ((struct elf_dyn_relocs
*)
700 bfd_alloc (htab
->elf
.dynobj
, amt
));
711 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
716 case R_RISCV_GNU_VTINHERIT
:
717 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
721 case R_RISCV_GNU_VTENTRY
:
722 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
735 riscv_elf_gc_mark_hook (asection
*sec
,
736 struct bfd_link_info
*info
,
737 Elf_Internal_Rela
*rel
,
738 struct elf_link_hash_entry
*h
,
739 Elf_Internal_Sym
*sym
)
742 switch (ELFNN_R_TYPE (rel
->r_info
))
744 case R_RISCV_GNU_VTINHERIT
:
745 case R_RISCV_GNU_VTENTRY
:
749 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
752 /* Adjust a symbol defined by a dynamic object and referenced by a
753 regular object. The current definition is in some section of the
754 dynamic object, but we're not including those sections. We have to
755 change the definition to something the rest of the link can
759 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
760 struct elf_link_hash_entry
*h
)
762 struct riscv_elf_link_hash_table
*htab
;
763 struct riscv_elf_link_hash_entry
* eh
;
767 htab
= riscv_elf_hash_table (info
);
768 BFD_ASSERT (htab
!= NULL
);
770 dynobj
= htab
->elf
.dynobj
;
772 /* Make sure we know what is going on here. */
773 BFD_ASSERT (dynobj
!= NULL
775 || h
->type
== STT_GNU_IFUNC
779 && !h
->def_regular
)));
781 /* If this is a function, put it in the procedure linkage table. We
782 will fill in the contents of the procedure linkage table later
783 (although we could actually do it here). */
784 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
786 if (h
->plt
.refcount
<= 0
787 || SYMBOL_CALLS_LOCAL (info
, h
)
788 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
789 && h
->root
.type
== bfd_link_hash_undefweak
))
791 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
792 input file, but the symbol was never referred to by a dynamic
793 object, or if all references were garbage collected. In such
794 a case, we don't actually need to build a PLT entry. */
795 h
->plt
.offset
= (bfd_vma
) -1;
802 h
->plt
.offset
= (bfd_vma
) -1;
804 /* If this is a weak symbol, and there is a real definition, the
805 processor independent code will have arranged for us to see the
806 real definition first, and we can just use the same value. */
809 struct elf_link_hash_entry
*def
= weakdef (h
);
810 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
811 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
812 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
816 /* This is a reference to a symbol defined by a dynamic object which
817 is not a function. */
819 /* If we are creating a shared library, we must presume that the
820 only references to the symbol are via the global offset table.
821 For such cases we need not do anything here; the relocations will
822 be handled correctly by relocate_section. */
823 if (bfd_link_pic (info
))
826 /* If there are no references to this symbol that do not use the
827 GOT, we don't need to generate a copy reloc. */
831 /* If -z nocopyreloc was given, we won't generate them either. */
832 if (info
->nocopyreloc
)
838 /* If we don't find any dynamic relocs in read-only sections, then
839 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
840 if (!_bfd_elf_readonly_dynrelocs (h
))
846 /* We must allocate the symbol in our .dynbss section, which will
847 become part of the .bss section of the executable. There will be
848 an entry for this symbol in the .dynsym section. The dynamic
849 object will contain position independent code, so all references
850 from the dynamic object to this symbol will go through the global
851 offset table. The dynamic linker will use the .dynsym entry to
852 determine the address it must put in the global offset table, so
853 both the dynamic object and the regular object will refer to the
854 same memory location for the variable. */
856 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
857 to copy the initial value out of the dynamic object and into the
858 runtime process image. We need to remember the offset into the
859 .rel.bss section we are going to use. */
860 eh
= (struct riscv_elf_link_hash_entry
*) h
;
861 if (eh
->tls_type
& ~GOT_NORMAL
)
864 srel
= htab
->elf
.srelbss
;
866 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
868 s
= htab
->elf
.sdynrelro
;
869 srel
= htab
->elf
.sreldynrelro
;
873 s
= htab
->elf
.sdynbss
;
874 srel
= htab
->elf
.srelbss
;
876 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
878 srel
->size
+= sizeof (ElfNN_External_Rela
);
882 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
885 /* Allocate space in .plt, .got and associated reloc sections for
889 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
891 struct bfd_link_info
*info
;
892 struct riscv_elf_link_hash_table
*htab
;
893 struct elf_dyn_relocs
*p
;
895 if (h
->root
.type
== bfd_link_hash_indirect
)
898 info
= (struct bfd_link_info
*) inf
;
899 htab
= riscv_elf_hash_table (info
);
900 BFD_ASSERT (htab
!= NULL
);
902 if (htab
->elf
.dynamic_sections_created
903 && h
->plt
.refcount
> 0)
905 /* Make sure this symbol is output as a dynamic symbol.
906 Undefined weak syms won't yet be marked as dynamic. */
910 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
914 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
916 asection
*s
= htab
->elf
.splt
;
919 s
->size
= PLT_HEADER_SIZE
;
921 h
->plt
.offset
= s
->size
;
923 /* Make room for this entry. */
924 s
->size
+= PLT_ENTRY_SIZE
;
926 /* We also need to make an entry in the .got.plt section. */
927 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
929 /* We also need to make an entry in the .rela.plt section. */
930 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
932 /* If this symbol is not defined in a regular file, and we are
933 not generating a shared library, then set the symbol to this
934 location in the .plt. This is required to make function
935 pointers compare as equal between the normal executable and
936 the shared library. */
937 if (! bfd_link_pic (info
)
940 h
->root
.u
.def
.section
= s
;
941 h
->root
.u
.def
.value
= h
->plt
.offset
;
946 h
->plt
.offset
= (bfd_vma
) -1;
952 h
->plt
.offset
= (bfd_vma
) -1;
956 if (h
->got
.refcount
> 0)
960 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
962 /* Make sure this symbol is output as a dynamic symbol.
963 Undefined weak syms won't yet be marked as dynamic. */
967 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
972 h
->got
.offset
= s
->size
;
973 dyn
= htab
->elf
.dynamic_sections_created
;
974 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
976 /* TLS_GD needs two dynamic relocs and two GOT slots. */
977 if (tls_type
& GOT_TLS_GD
)
979 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
980 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
983 /* TLS_IE needs one dynamic reloc and one GOT slot. */
984 if (tls_type
& GOT_TLS_IE
)
986 s
->size
+= RISCV_ELF_WORD_BYTES
;
987 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
992 s
->size
+= RISCV_ELF_WORD_BYTES
;
993 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
994 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
995 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
999 h
->got
.offset
= (bfd_vma
) -1;
1001 if (h
->dyn_relocs
== NULL
)
1004 /* In the shared -Bsymbolic case, discard space allocated for
1005 dynamic pc-relative relocs against symbols which turn out to be
1006 defined in regular objects. For the normal shared case, discard
1007 space for pc-relative relocs that have become local due to symbol
1008 visibility changes. */
1010 if (bfd_link_pic (info
))
1012 if (SYMBOL_CALLS_LOCAL (info
, h
))
1014 struct elf_dyn_relocs
**pp
;
1016 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1018 p
->count
-= p
->pc_count
;
1027 /* Also discard relocs on undefined weak syms with non-default
1029 if (h
->dyn_relocs
!= NULL
1030 && h
->root
.type
== bfd_link_hash_undefweak
)
1032 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1033 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1034 h
->dyn_relocs
= NULL
;
1036 /* Make sure undefined weak symbols are output as a dynamic
1038 else if (h
->dynindx
== -1
1039 && !h
->forced_local
)
1041 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1048 /* For the non-shared case, discard space for relocs against
1049 symbols which turn out to need copy relocs or are not
1055 || (htab
->elf
.dynamic_sections_created
1056 && (h
->root
.type
== bfd_link_hash_undefweak
1057 || h
->root
.type
== bfd_link_hash_undefined
))))
1059 /* Make sure this symbol is output as a dynamic symbol.
1060 Undefined weak syms won't yet be marked as dynamic. */
1061 if (h
->dynindx
== -1
1062 && !h
->forced_local
)
1064 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1068 /* If that succeeded, we know we'll be keeping all the
1070 if (h
->dynindx
!= -1)
1074 h
->dyn_relocs
= NULL
;
1079 /* Finally, allocate space. */
1080 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1082 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1083 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1090 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1092 struct riscv_elf_link_hash_table
*htab
;
1097 htab
= riscv_elf_hash_table (info
);
1098 BFD_ASSERT (htab
!= NULL
);
1099 dynobj
= htab
->elf
.dynobj
;
1100 BFD_ASSERT (dynobj
!= NULL
);
1102 if (elf_hash_table (info
)->dynamic_sections_created
)
1104 /* Set the contents of the .interp section to the interpreter. */
1105 if (bfd_link_executable (info
) && !info
->nointerp
)
1107 s
= bfd_get_linker_section (dynobj
, ".interp");
1108 BFD_ASSERT (s
!= NULL
);
1109 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1110 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1114 /* Set up .got offsets for local syms, and space for local dynamic
1116 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1118 bfd_signed_vma
*local_got
;
1119 bfd_signed_vma
*end_local_got
;
1120 char *local_tls_type
;
1121 bfd_size_type locsymcount
;
1122 Elf_Internal_Shdr
*symtab_hdr
;
1125 if (! is_riscv_elf (ibfd
))
1128 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1130 struct elf_dyn_relocs
*p
;
1132 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1134 if (!bfd_is_abs_section (p
->sec
)
1135 && bfd_is_abs_section (p
->sec
->output_section
))
1137 /* Input section has been discarded, either because
1138 it is a copy of a linkonce section or due to
1139 linker script /DISCARD/, so we'll be discarding
1142 else if (p
->count
!= 0)
1144 srel
= elf_section_data (p
->sec
)->sreloc
;
1145 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1146 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1147 info
->flags
|= DF_TEXTREL
;
1152 local_got
= elf_local_got_refcounts (ibfd
);
1156 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1157 locsymcount
= symtab_hdr
->sh_info
;
1158 end_local_got
= local_got
+ locsymcount
;
1159 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1161 srel
= htab
->elf
.srelgot
;
1162 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1166 *local_got
= s
->size
;
1167 s
->size
+= RISCV_ELF_WORD_BYTES
;
1168 if (*local_tls_type
& GOT_TLS_GD
)
1169 s
->size
+= RISCV_ELF_WORD_BYTES
;
1170 if (bfd_link_pic (info
)
1171 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1172 srel
->size
+= sizeof (ElfNN_External_Rela
);
1175 *local_got
= (bfd_vma
) -1;
1179 /* Allocate global sym .plt and .got entries, and space for global
1180 sym dynamic relocs. */
1181 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1183 if (htab
->elf
.sgotplt
)
1185 struct elf_link_hash_entry
*got
;
1186 got
= elf_link_hash_lookup (elf_hash_table (info
),
1187 "_GLOBAL_OFFSET_TABLE_",
1188 FALSE
, FALSE
, FALSE
);
1190 /* Don't allocate .got.plt section if there are no GOT nor PLT
1191 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1193 || !got
->ref_regular_nonweak
)
1194 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1195 && (htab
->elf
.splt
== NULL
1196 || htab
->elf
.splt
->size
== 0)
1197 && (htab
->elf
.sgot
== NULL
1198 || (htab
->elf
.sgot
->size
1199 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1200 htab
->elf
.sgotplt
->size
= 0;
1203 /* The check_relocs and adjust_dynamic_symbol entry points have
1204 determined the sizes of the various dynamic sections. Allocate
1206 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1208 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1211 if (s
== htab
->elf
.splt
1212 || s
== htab
->elf
.sgot
1213 || s
== htab
->elf
.sgotplt
1214 || s
== htab
->elf
.sdynbss
1215 || s
== htab
->elf
.sdynrelro
1216 || s
== htab
->sdyntdata
)
1218 /* Strip this section if we don't need it; see the
1221 else if (strncmp (s
->name
, ".rela", 5) == 0)
1225 /* We use the reloc_count field as a counter if we need
1226 to copy relocs into the output file. */
1232 /* It's not one of our sections. */
1238 /* If we don't need this section, strip it from the
1239 output file. This is mostly to handle .rela.bss and
1240 .rela.plt. We must create both sections in
1241 create_dynamic_sections, because they must be created
1242 before the linker maps input sections to output
1243 sections. The linker does that before
1244 adjust_dynamic_symbol is called, and it is that
1245 function which decides whether anything needs to go
1246 into these sections. */
1247 s
->flags
|= SEC_EXCLUDE
;
1251 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1254 /* Allocate memory for the section contents. Zero the memory
1255 for the benefit of .rela.plt, which has 4 unused entries
1256 at the beginning, and we don't want garbage. */
1257 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1258 if (s
->contents
== NULL
)
1262 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, TRUE
);
1266 #define DTP_OFFSET 0x800
1268 /* Return the relocation value for a TLS dtp-relative reloc. */
1271 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1273 /* If tls_sec is NULL, we should have signalled an error already. */
1274 if (elf_hash_table (info
)->tls_sec
== NULL
)
1276 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1279 /* Return the relocation value for a static TLS tp-relative relocation. */
1282 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1284 /* If tls_sec is NULL, we should have signalled an error already. */
1285 if (elf_hash_table (info
)->tls_sec
== NULL
)
1287 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1290 /* Return the global pointer's value, or 0 if it is not in use. */
1293 riscv_global_pointer_value (struct bfd_link_info
*info
)
1295 struct bfd_link_hash_entry
*h
;
1297 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1298 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1301 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1304 /* Emplace a static relocation. */
1306 static bfd_reloc_status_type
1307 perform_relocation (const reloc_howto_type
*howto
,
1308 const Elf_Internal_Rela
*rel
,
1310 asection
*input_section
,
1314 if (howto
->pc_relative
)
1315 value
-= sec_addr (input_section
) + rel
->r_offset
;
1316 value
+= rel
->r_addend
;
1318 switch (ELFNN_R_TYPE (rel
->r_info
))
1321 case R_RISCV_TPREL_HI20
:
1322 case R_RISCV_PCREL_HI20
:
1323 case R_RISCV_GOT_HI20
:
1324 case R_RISCV_TLS_GOT_HI20
:
1325 case R_RISCV_TLS_GD_HI20
:
1326 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1327 return bfd_reloc_overflow
;
1328 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1331 case R_RISCV_LO12_I
:
1332 case R_RISCV_GPREL_I
:
1333 case R_RISCV_TPREL_LO12_I
:
1334 case R_RISCV_TPREL_I
:
1335 case R_RISCV_PCREL_LO12_I
:
1336 value
= ENCODE_ITYPE_IMM (value
);
1339 case R_RISCV_LO12_S
:
1340 case R_RISCV_GPREL_S
:
1341 case R_RISCV_TPREL_LO12_S
:
1342 case R_RISCV_TPREL_S
:
1343 case R_RISCV_PCREL_LO12_S
:
1344 value
= ENCODE_STYPE_IMM (value
);
1348 case R_RISCV_CALL_PLT
:
1349 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1350 return bfd_reloc_overflow
;
1351 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1352 | (ENCODE_ITYPE_IMM (value
) << 32);
1356 if (!VALID_UJTYPE_IMM (value
))
1357 return bfd_reloc_overflow
;
1358 value
= ENCODE_UJTYPE_IMM (value
);
1361 case R_RISCV_BRANCH
:
1362 if (!VALID_SBTYPE_IMM (value
))
1363 return bfd_reloc_overflow
;
1364 value
= ENCODE_SBTYPE_IMM (value
);
1367 case R_RISCV_RVC_BRANCH
:
1368 if (!VALID_RVC_B_IMM (value
))
1369 return bfd_reloc_overflow
;
1370 value
= ENCODE_RVC_B_IMM (value
);
1373 case R_RISCV_RVC_JUMP
:
1374 if (!VALID_RVC_J_IMM (value
))
1375 return bfd_reloc_overflow
;
1376 value
= ENCODE_RVC_J_IMM (value
);
1379 case R_RISCV_RVC_LUI
:
1380 if (RISCV_CONST_HIGH_PART (value
) == 0)
1382 /* Linker relaxation can convert an address equal to or greater than
1383 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1384 valid immediate. We can fix this by converting it to a C.LI. */
1385 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1386 contents
+ rel
->r_offset
);
1387 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1388 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1389 value
= ENCODE_RVC_IMM (0);
1391 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1392 return bfd_reloc_overflow
;
1394 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1412 case R_RISCV_32_PCREL
:
1413 case R_RISCV_TLS_DTPREL32
:
1414 case R_RISCV_TLS_DTPREL64
:
1417 case R_RISCV_DELETE
:
1418 return bfd_reloc_ok
;
1421 return bfd_reloc_notsupported
;
1424 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1425 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1426 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1428 return bfd_reloc_ok
;
1431 /* Remember all PC-relative high-part relocs we've encountered to help us
1432 later resolve the corresponding low-part relocs. */
1438 } riscv_pcrel_hi_reloc
;
1440 typedef struct riscv_pcrel_lo_reloc
1442 asection
* input_section
;
1443 struct bfd_link_info
* info
;
1444 reloc_howto_type
* howto
;
1445 const Elf_Internal_Rela
* reloc
;
1448 bfd_byte
* contents
;
1449 struct riscv_pcrel_lo_reloc
* next
;
1450 } riscv_pcrel_lo_reloc
;
1455 riscv_pcrel_lo_reloc
*lo_relocs
;
1456 } riscv_pcrel_relocs
;
1459 riscv_pcrel_reloc_hash (const void *entry
)
1461 const riscv_pcrel_hi_reloc
*e
= entry
;
1462 return (hashval_t
)(e
->address
>> 2);
1466 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1468 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1469 return e1
->address
== e2
->address
;
1473 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1476 p
->lo_relocs
= NULL
;
1477 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1478 riscv_pcrel_reloc_eq
, free
);
1479 return p
->hi_relocs
!= NULL
;
1483 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1485 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1489 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1494 htab_delete (p
->hi_relocs
);
1498 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1499 struct bfd_link_info
*info
,
1503 const reloc_howto_type
*howto
,
1506 /* We may need to reference low addreses in PC-relative modes even when the
1507 * PC is far away from these addresses. For example, undefweak references
1508 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1509 * addresses that we can link PC-relative programs at, the linker can't
1510 * actually relocate references to those symbols. In order to allow these
1511 * programs to work we simply convert the PC-relative auipc sequences to
1512 * 0-relative lui sequences. */
1513 if (bfd_link_pic (info
))
1516 /* If it's possible to reference the symbol using auipc we do so, as that's
1517 * more in the spirit of the PC-relative relocations we're processing. */
1518 bfd_vma offset
= addr
- pc
;
1519 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1522 /* If it's impossible to reference this with a LUI-based offset then don't
1523 * bother to convert it at all so users still see the PC-relative relocation
1524 * in the truncation message. */
1525 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1528 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1530 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1531 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1532 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1537 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1538 bfd_vma value
, bfd_boolean absolute
)
1540 bfd_vma offset
= absolute
? value
: value
- addr
;
1541 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1542 riscv_pcrel_hi_reloc
**slot
=
1543 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1545 BFD_ASSERT (*slot
== NULL
);
1546 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1554 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1555 asection
*input_section
,
1556 struct bfd_link_info
*info
,
1557 reloc_howto_type
*howto
,
1558 const Elf_Internal_Rela
*reloc
,
1563 riscv_pcrel_lo_reloc
*entry
;
1564 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1567 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1568 name
, contents
, p
->lo_relocs
};
1569 p
->lo_relocs
= entry
;
1574 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1576 riscv_pcrel_lo_reloc
*r
;
1578 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1580 bfd
*input_bfd
= r
->input_section
->owner
;
1582 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1583 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1585 /* Check for overflow into bit 11 when adding reloc addend. */
1586 || (! (entry
->value
& 0x800)
1587 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1589 char *string
= (entry
== NULL
1590 ? "%pcrel_lo missing matching %pcrel_hi"
1591 : "%pcrel_lo overflow with an addend");
1592 (*r
->info
->callbacks
->reloc_dangerous
)
1593 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1597 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1598 input_bfd
, r
->contents
);
1604 /* Relocate a RISC-V ELF section.
1606 The RELOCATE_SECTION function is called by the new ELF backend linker
1607 to handle the relocations for a section.
1609 The relocs are always passed as Rela structures.
1611 This function is responsible for adjusting the section contents as
1612 necessary, and (if generating a relocatable output file) adjusting
1613 the reloc addend as necessary.
1615 This function does not have to worry about setting the reloc
1616 address or the reloc symbol index.
1618 LOCAL_SYMS is a pointer to the swapped in local symbols.
1620 LOCAL_SECTIONS is an array giving the section in the input file
1621 corresponding to the st_shndx field of each local symbol.
1623 The global hash table entry for the global symbols can be found
1624 via elf_sym_hashes (input_bfd).
1626 When generating relocatable output, this function must handle
1627 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1628 going to be the section symbol corresponding to the output
1629 section, which means that the addend must be adjusted
1633 riscv_elf_relocate_section (bfd
*output_bfd
,
1634 struct bfd_link_info
*info
,
1636 asection
*input_section
,
1638 Elf_Internal_Rela
*relocs
,
1639 Elf_Internal_Sym
*local_syms
,
1640 asection
**local_sections
)
1642 Elf_Internal_Rela
*rel
;
1643 Elf_Internal_Rela
*relend
;
1644 riscv_pcrel_relocs pcrel_relocs
;
1645 bfd_boolean ret
= FALSE
;
1646 asection
*sreloc
= elf_section_data (input_section
)->sreloc
;
1647 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1648 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1649 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1650 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1651 bfd_boolean absolute
;
1653 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1656 relend
= relocs
+ input_section
->reloc_count
;
1657 for (rel
= relocs
; rel
< relend
; rel
++)
1659 unsigned long r_symndx
;
1660 struct elf_link_hash_entry
*h
;
1661 Elf_Internal_Sym
*sym
;
1664 bfd_reloc_status_type r
= bfd_reloc_ok
;
1666 bfd_vma off
, ie_off
;
1667 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1668 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1669 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1670 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1671 const char *msg
= NULL
;
1672 char *msg_buf
= NULL
;
1673 bfd_boolean resolved_to_zero
;
1676 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1679 /* This is a final link. */
1680 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1684 unresolved_reloc
= FALSE
;
1685 if (r_symndx
< symtab_hdr
->sh_info
)
1687 sym
= local_syms
+ r_symndx
;
1688 sec
= local_sections
[r_symndx
];
1689 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1693 bfd_boolean warned
, ignored
;
1695 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1696 r_symndx
, symtab_hdr
, sym_hashes
,
1698 unresolved_reloc
, warned
, ignored
);
1701 /* To avoid generating warning messages about truncated
1702 relocations, set the relocation's address to be the same as
1703 the start of this section. */
1704 if (input_section
->output_section
!= NULL
)
1705 relocation
= input_section
->output_section
->vma
;
1711 if (sec
!= NULL
&& discarded_section (sec
))
1712 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1713 rel
, 1, relend
, howto
, 0, contents
);
1715 if (bfd_link_relocatable (info
))
1719 name
= h
->root
.root
.string
;
1722 name
= (bfd_elf_string_from_elf_section
1723 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1724 if (name
== NULL
|| *name
== '\0')
1725 name
= bfd_section_name (sec
);
1728 resolved_to_zero
= (h
!= NULL
1729 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
1735 case R_RISCV_TPREL_ADD
:
1737 case R_RISCV_JUMP_SLOT
:
1738 case R_RISCV_RELATIVE
:
1739 /* These require nothing of us at all. */
1743 case R_RISCV_BRANCH
:
1744 case R_RISCV_RVC_BRANCH
:
1745 case R_RISCV_RVC_LUI
:
1746 case R_RISCV_LO12_I
:
1747 case R_RISCV_LO12_S
:
1752 case R_RISCV_32_PCREL
:
1753 case R_RISCV_DELETE
:
1754 /* These require no special handling beyond perform_relocation. */
1757 case R_RISCV_GOT_HI20
:
1760 bfd_boolean dyn
, pic
;
1762 off
= h
->got
.offset
;
1763 BFD_ASSERT (off
!= (bfd_vma
) -1);
1764 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
1765 pic
= bfd_link_pic (info
);
1767 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
1768 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
1770 /* This is actually a static link, or it is a
1771 -Bsymbolic link and the symbol is defined
1772 locally, or the symbol was forced to be local
1773 because of a version file. We must initialize
1774 this entry in the global offset table. Since the
1775 offset must always be a multiple of the word size,
1776 we use the least significant bit to record whether
1777 we have initialized it already.
1779 When doing a dynamic link, we create a .rela.got
1780 relocation entry to initialize the value. This
1781 is done in the finish_dynamic_symbol routine. */
1786 bfd_put_NN (output_bfd
, relocation
,
1787 htab
->elf
.sgot
->contents
+ off
);
1792 unresolved_reloc
= FALSE
;
1796 BFD_ASSERT (local_got_offsets
!= NULL
1797 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1799 off
= local_got_offsets
[r_symndx
];
1801 /* The offset must always be a multiple of the word size.
1802 So, we can use the least significant bit to record
1803 whether we have already processed this entry. */
1808 if (bfd_link_pic (info
))
1811 Elf_Internal_Rela outrel
;
1813 /* We need to generate a R_RISCV_RELATIVE reloc
1814 for the dynamic linker. */
1815 s
= htab
->elf
.srelgot
;
1816 BFD_ASSERT (s
!= NULL
);
1818 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
1820 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
1821 outrel
.r_addend
= relocation
;
1823 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
1826 bfd_put_NN (output_bfd
, relocation
,
1827 htab
->elf
.sgot
->contents
+ off
);
1828 local_got_offsets
[r_symndx
] |= 1;
1831 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
1832 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1839 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1840 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1842 r
= bfd_reloc_notsupported
;
1843 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1844 relocation
, absolute
))
1845 r
= bfd_reloc_overflow
;
1853 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1854 contents
+ rel
->r_offset
);
1855 relocation
= old_value
+ relocation
;
1865 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
1866 contents
+ rel
->r_offset
);
1867 relocation
= old_value
- relocation
;
1872 case R_RISCV_CALL_PLT
:
1873 /* Handle a call to an undefined weak function. This won't be
1874 relaxed, so we have to handle it here. */
1875 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
1876 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
1878 /* We can use x0 as the base register. */
1879 bfd_vma insn
= bfd_get_32 (input_bfd
,
1880 contents
+ rel
->r_offset
+ 4);
1881 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1882 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
1883 /* Set the relocation value so that we get 0 after the pc
1884 relative adjustment. */
1885 relocation
= sec_addr (input_section
) + rel
->r_offset
;
1890 case R_RISCV_RVC_JUMP
:
1891 /* This line has to match the check in _bfd_riscv_relax_section. */
1892 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
1894 /* Refer to the PLT entry. */
1895 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
1896 unresolved_reloc
= FALSE
;
1900 case R_RISCV_TPREL_HI20
:
1901 relocation
= tpoff (info
, relocation
);
1904 case R_RISCV_TPREL_LO12_I
:
1905 case R_RISCV_TPREL_LO12_S
:
1906 relocation
= tpoff (info
, relocation
);
1909 case R_RISCV_TPREL_I
:
1910 case R_RISCV_TPREL_S
:
1911 relocation
= tpoff (info
, relocation
);
1912 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
1914 /* We can use tp as the base register. */
1915 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1916 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1917 insn
|= X_TP
<< OP_SH_RS1
;
1918 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
1921 r
= bfd_reloc_overflow
;
1924 case R_RISCV_GPREL_I
:
1925 case R_RISCV_GPREL_S
:
1927 bfd_vma gp
= riscv_global_pointer_value (info
);
1928 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
1929 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
1931 /* We can use x0 or gp as the base register. */
1932 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
1933 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
1936 rel
->r_addend
-= gp
;
1937 insn
|= X_GP
<< OP_SH_RS1
;
1939 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
1942 r
= bfd_reloc_overflow
;
1946 case R_RISCV_PCREL_HI20
:
1947 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
1954 r_type
= ELFNN_R_TYPE (rel
->r_info
);
1955 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1957 r
= bfd_reloc_notsupported
;
1958 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
1959 relocation
+ rel
->r_addend
,
1961 r
= bfd_reloc_overflow
;
1964 case R_RISCV_PCREL_LO12_I
:
1965 case R_RISCV_PCREL_LO12_S
:
1966 /* We don't allow section symbols plus addends as the auipc address,
1967 because then riscv_relax_delete_bytes would have to search through
1968 all relocs to update these addends. This is also ambiguous, as
1969 we do allow offsets to be added to the target address, which are
1970 not to be used to find the auipc address. */
1971 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
1972 || (h
!= NULL
&& h
->type
== STT_SECTION
))
1975 msg
= _("%pcrel_lo section symbol with an addend");
1976 r
= bfd_reloc_dangerous
;
1980 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
1981 howto
, rel
, relocation
, name
,
1984 r
= bfd_reloc_overflow
;
1987 case R_RISCV_TLS_DTPREL32
:
1988 case R_RISCV_TLS_DTPREL64
:
1989 relocation
= dtpoff (info
, relocation
);
1994 if ((input_section
->flags
& SEC_ALLOC
) == 0)
1997 if ((bfd_link_pic (info
)
1999 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2000 && !resolved_to_zero
)
2001 || h
->root
.type
!= bfd_link_hash_undefweak
)
2002 && (! howto
->pc_relative
2003 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2004 || (!bfd_link_pic (info
)
2010 || h
->root
.type
== bfd_link_hash_undefweak
2011 || h
->root
.type
== bfd_link_hash_undefined
)))
2013 Elf_Internal_Rela outrel
;
2014 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2016 /* When generating a shared object, these relocations
2017 are copied into the output file to be resolved at run
2021 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2023 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2024 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2025 outrel
.r_offset
+= sec_addr (input_section
);
2027 if (skip_dynamic_relocation
)
2028 memset (&outrel
, 0, sizeof outrel
);
2029 else if (h
!= NULL
&& h
->dynindx
!= -1
2030 && !(bfd_link_pic (info
)
2031 && SYMBOLIC_BIND (info
, h
)
2034 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2035 outrel
.r_addend
= rel
->r_addend
;
2039 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2040 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2043 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2044 if (skip_static_relocation
)
2049 case R_RISCV_TLS_GOT_HI20
:
2053 case R_RISCV_TLS_GD_HI20
:
2056 off
= h
->got
.offset
;
2061 off
= local_got_offsets
[r_symndx
];
2062 local_got_offsets
[r_symndx
] |= 1;
2065 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2066 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2067 /* If this symbol is referenced by both GD and IE TLS, the IE
2068 reference's GOT slot follows the GD reference's slots. */
2070 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2071 ie_off
= 2 * GOT_ENTRY_SIZE
;
2077 Elf_Internal_Rela outrel
;
2079 bfd_boolean need_relocs
= FALSE
;
2081 if (htab
->elf
.srelgot
== NULL
)
2086 bfd_boolean dyn
, pic
;
2087 dyn
= htab
->elf
.dynamic_sections_created
;
2088 pic
= bfd_link_pic (info
);
2090 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2091 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2095 /* The GOT entries have not been initialized yet. Do it
2096 now, and emit any relocations. */
2097 if ((bfd_link_pic (info
) || indx
!= 0)
2099 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2100 || h
->root
.type
!= bfd_link_hash_undefweak
))
2103 if (tls_type
& GOT_TLS_GD
)
2107 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2108 outrel
.r_addend
= 0;
2109 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2110 bfd_put_NN (output_bfd
, 0,
2111 htab
->elf
.sgot
->contents
+ off
);
2112 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2115 BFD_ASSERT (! unresolved_reloc
);
2116 bfd_put_NN (output_bfd
,
2117 dtpoff (info
, relocation
),
2118 (htab
->elf
.sgot
->contents
+ off
+
2119 RISCV_ELF_WORD_BYTES
));
2123 bfd_put_NN (output_bfd
, 0,
2124 (htab
->elf
.sgot
->contents
+ off
+
2125 RISCV_ELF_WORD_BYTES
));
2126 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2127 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2128 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2133 /* If we are not emitting relocations for a
2134 general dynamic reference, then we must be in a
2135 static link or an executable link with the
2136 symbol binding locally. Mark it as belonging
2137 to module 1, the executable. */
2138 bfd_put_NN (output_bfd
, 1,
2139 htab
->elf
.sgot
->contents
+ off
);
2140 bfd_put_NN (output_bfd
,
2141 dtpoff (info
, relocation
),
2142 (htab
->elf
.sgot
->contents
+ off
+
2143 RISCV_ELF_WORD_BYTES
));
2147 if (tls_type
& GOT_TLS_IE
)
2151 bfd_put_NN (output_bfd
, 0,
2152 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2153 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2155 outrel
.r_addend
= 0;
2157 outrel
.r_addend
= tpoff (info
, relocation
);
2158 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2159 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2163 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2164 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2169 BFD_ASSERT (off
< (bfd_vma
) -2);
2170 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2171 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2173 r
= bfd_reloc_overflow
;
2174 unresolved_reloc
= FALSE
;
2178 r
= bfd_reloc_notsupported
;
2181 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2182 because such sections are not SEC_ALLOC and thus ld.so will
2183 not process them. */
2184 if (unresolved_reloc
2185 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2187 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2188 rel
->r_offset
) != (bfd_vma
) -1)
2194 case R_RISCV_RVC_JUMP
:
2195 if (asprintf (&msg_buf
,
2196 _("%%X%%P: relocation %s against `%s' can "
2197 "not be used when making a shared object; "
2198 "recompile with -fPIC\n"),
2200 h
->root
.root
.string
) == -1)
2205 if (asprintf (&msg_buf
,
2206 _("%%X%%P: unresolvable %s relocation against "
2209 h
->root
.root
.string
) == -1)
2215 r
= bfd_reloc_notsupported
;
2218 if (r
== bfd_reloc_ok
)
2219 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2220 input_bfd
, contents
);
2222 /* We should have already detected the error and set message before.
2223 If the error message isn't set since the linker runs out of memory
2224 or we don't set it before, then we should set the default message
2225 with the "internal error" string here. */
2231 case bfd_reloc_overflow
:
2232 info
->callbacks
->reloc_overflow
2233 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2234 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2237 case bfd_reloc_undefined
:
2238 info
->callbacks
->undefined_symbol
2239 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2243 case bfd_reloc_outofrange
:
2245 msg
= _("%X%P: internal error: out of range error\n");
2248 case bfd_reloc_notsupported
:
2250 msg
= _("%X%P: internal error: unsupported relocation error\n");
2253 case bfd_reloc_dangerous
:
2254 /* The error message should already be set. */
2256 msg
= _("dangerous relocation error");
2257 info
->callbacks
->reloc_dangerous
2258 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2262 msg
= _("%X%P: internal error: unknown error\n");
2266 /* Do not report error message for the dangerous relocation again. */
2267 if (msg
&& r
!= bfd_reloc_dangerous
)
2268 info
->callbacks
->einfo (msg
);
2270 /* Free the unused `msg_buf`. */
2273 /* We already reported the error via a callback, so don't try to report
2274 it again by returning false. That leads to spurious errors. */
2279 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2281 riscv_free_pcrel_relocs (&pcrel_relocs
);
2285 /* Finish up dynamic symbol handling. We set the contents of various
2286 dynamic sections here. */
2289 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2290 struct bfd_link_info
*info
,
2291 struct elf_link_hash_entry
*h
,
2292 Elf_Internal_Sym
*sym
)
2294 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2295 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2297 if (h
->plt
.offset
!= (bfd_vma
) -1)
2299 /* We've decided to create a PLT entry for this symbol. */
2301 bfd_vma i
, header_address
, plt_idx
, got_address
;
2302 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2303 Elf_Internal_Rela rela
;
2305 BFD_ASSERT (h
->dynindx
!= -1);
2307 /* Calculate the address of the PLT header. */
2308 header_address
= sec_addr (htab
->elf
.splt
);
2310 /* Calculate the index of the entry. */
2311 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2313 /* Calculate the address of the .got.plt entry. */
2314 got_address
= riscv_elf_got_plt_val (plt_idx
, info
);
2316 /* Find out where the .plt entry should go. */
2317 loc
= htab
->elf
.splt
->contents
+ h
->plt
.offset
;
2319 /* Fill in the PLT entry itself. */
2320 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2321 header_address
+ h
->plt
.offset
,
2325 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2326 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2328 /* Fill in the initial value of the .got.plt entry. */
2329 loc
= htab
->elf
.sgotplt
->contents
2330 + (got_address
- sec_addr (htab
->elf
.sgotplt
));
2331 bfd_put_NN (output_bfd
, sec_addr (htab
->elf
.splt
), loc
);
2333 /* Fill in the entry in the .rela.plt section. */
2334 rela
.r_offset
= got_address
;
2336 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2338 loc
= htab
->elf
.srelplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2339 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2341 if (!h
->def_regular
)
2343 /* Mark the symbol as undefined, rather than as defined in
2344 the .plt section. Leave the value alone. */
2345 sym
->st_shndx
= SHN_UNDEF
;
2346 /* If the symbol is weak, we do need to clear the value.
2347 Otherwise, the PLT entry would provide a definition for
2348 the symbol even if the symbol wasn't defined anywhere,
2349 and so the symbol would never be NULL. */
2350 if (!h
->ref_regular_nonweak
)
2355 if (h
->got
.offset
!= (bfd_vma
) -1
2356 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2357 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2361 Elf_Internal_Rela rela
;
2363 /* This symbol has an entry in the GOT. Set it up. */
2365 sgot
= htab
->elf
.sgot
;
2366 srela
= htab
->elf
.srelgot
;
2367 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2369 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2371 /* If this is a local symbol reference, we just want to emit a RELATIVE
2372 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2373 the symbol was forced to be local because of a version file.
2374 The entry in the global offset table will already have been
2375 initialized in the relocate_section function. */
2376 if (bfd_link_pic (info
)
2377 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2379 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2380 asection
*sec
= h
->root
.u
.def
.section
;
2381 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2382 rela
.r_addend
= (h
->root
.u
.def
.value
2383 + sec
->output_section
->vma
2384 + sec
->output_offset
);
2388 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2389 BFD_ASSERT (h
->dynindx
!= -1);
2390 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2394 bfd_put_NN (output_bfd
, 0,
2395 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
2396 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
2401 Elf_Internal_Rela rela
;
2404 /* This symbols needs a copy reloc. Set it up. */
2405 BFD_ASSERT (h
->dynindx
!= -1);
2407 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
2408 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
2410 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
2411 s
= htab
->elf
.sreldynrelro
;
2413 s
= htab
->elf
.srelbss
;
2414 riscv_elf_append_rela (output_bfd
, s
, &rela
);
2417 /* Mark some specially defined symbols as absolute. */
2418 if (h
== htab
->elf
.hdynamic
2419 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
2420 sym
->st_shndx
= SHN_ABS
;
2425 /* Finish up the dynamic sections. */
2428 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
2429 bfd
*dynobj
, asection
*sdyn
)
2431 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2432 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2433 size_t dynsize
= bed
->s
->sizeof_dyn
;
2434 bfd_byte
*dyncon
, *dynconend
;
2436 dynconend
= sdyn
->contents
+ sdyn
->size
;
2437 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
2439 Elf_Internal_Dyn dyn
;
2442 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
2447 s
= htab
->elf
.sgotplt
;
2448 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2451 s
= htab
->elf
.srelplt
;
2452 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
2455 s
= htab
->elf
.srelplt
;
2456 dyn
.d_un
.d_val
= s
->size
;
2462 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2468 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
2469 struct bfd_link_info
*info
)
2473 struct riscv_elf_link_hash_table
*htab
;
2475 htab
= riscv_elf_hash_table (info
);
2476 BFD_ASSERT (htab
!= NULL
);
2477 dynobj
= htab
->elf
.dynobj
;
2479 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
2481 if (elf_hash_table (info
)->dynamic_sections_created
)
2486 splt
= htab
->elf
.splt
;
2487 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
2489 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
2494 /* Fill in the head and tail entries in the procedure linkage table. */
2498 uint32_t plt_header
[PLT_HEADER_INSNS
];
2499 ret
= riscv_make_plt_header (output_bfd
,
2500 sec_addr (htab
->elf
.sgotplt
),
2501 sec_addr (splt
), plt_header
);
2505 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
2506 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
2508 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
2513 if (htab
->elf
.sgotplt
)
2515 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
2517 if (bfd_is_abs_section (output_section
))
2519 (*_bfd_error_handler
)
2520 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
2524 if (htab
->elf
.sgotplt
->size
> 0)
2526 /* Write the first two entries in .got.plt, needed for the dynamic
2528 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
2529 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
2530 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2533 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2538 asection
*output_section
= htab
->elf
.sgot
->output_section
;
2540 if (htab
->elf
.sgot
->size
> 0)
2542 /* Set the first entry in the global offset table to the address of
2543 the dynamic section. */
2544 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
2545 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
2548 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
2554 /* Return address for Ith PLT stub in section PLT, for relocation REL
2555 or (bfd_vma) -1 if it should not be included. */
2558 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
2559 const arelent
*rel ATTRIBUTE_UNUSED
)
2561 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
2564 static enum elf_reloc_type_class
2565 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
2566 const asection
*rel_sec ATTRIBUTE_UNUSED
,
2567 const Elf_Internal_Rela
*rela
)
2569 switch (ELFNN_R_TYPE (rela
->r_info
))
2571 case R_RISCV_RELATIVE
:
2572 return reloc_class_relative
;
2573 case R_RISCV_JUMP_SLOT
:
2574 return reloc_class_plt
;
2576 return reloc_class_copy
;
2578 return reloc_class_normal
;
2582 /* Given the ELF header flags in FLAGS, it returns a string that describes the
2586 riscv_float_abi_string (flagword flags
)
2588 switch (flags
& EF_RISCV_FLOAT_ABI
)
2590 case EF_RISCV_FLOAT_ABI_SOFT
:
2591 return "soft-float";
2593 case EF_RISCV_FLOAT_ABI_SINGLE
:
2594 return "single-float";
2596 case EF_RISCV_FLOAT_ABI_DOUBLE
:
2597 return "double-float";
2599 case EF_RISCV_FLOAT_ABI_QUAD
:
2600 return "quad-float";
2607 /* The information of architecture attribute. */
2608 static riscv_subset_list_t in_subsets
;
2609 static riscv_subset_list_t out_subsets
;
2610 static riscv_subset_list_t merged_subsets
;
2612 /* Predicator for standard extension. */
2615 riscv_std_ext_p (const char *name
)
2617 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
2620 /* Error handler when version mis-match. */
2623 riscv_version_mismatch (bfd
*ibfd
,
2624 struct riscv_subset_t
*in
,
2625 struct riscv_subset_t
*out
)
2628 (_("error: %pB: Mis-matched ISA version for '%s' extension. "
2631 in
->major_version
, in
->minor_version
,
2632 out
->major_version
, out
->minor_version
);
2635 /* Return true if subset is 'i' or 'e'. */
2638 riscv_i_or_e_p (bfd
*ibfd
,
2640 struct riscv_subset_t
*subset
)
2642 if ((strcasecmp (subset
->name
, "e") != 0)
2643 && (strcasecmp (subset
->name
, "i") != 0))
2646 (_("error: %pB: corrupted ISA string '%s'. "
2647 "First letter should be 'i' or 'e' but got '%s'."),
2648 ibfd
, arch
, subset
->name
);
2654 /* Merge standard extensions.
2657 Return FALSE if failed to merge.
2661 `in_arch`: Raw arch string for input object.
2662 `out_arch`: Raw arch string for output object.
2663 `pin`: subset list for input object, and it'll skip all merged subset after
2665 `pout`: Like `pin`, but for output object. */
2668 riscv_merge_std_ext (bfd
*ibfd
,
2669 const char *in_arch
,
2670 const char *out_arch
,
2671 struct riscv_subset_t
**pin
,
2672 struct riscv_subset_t
**pout
)
2674 const char *standard_exts
= riscv_supported_std_ext ();
2676 struct riscv_subset_t
*in
= *pin
;
2677 struct riscv_subset_t
*out
= *pout
;
2679 /* First letter should be 'i' or 'e'. */
2680 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
2683 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
2686 if (strcasecmp (in
->name
, out
->name
) != 0)
2688 /* TODO: We might allow merge 'i' with 'e'. */
2690 (_("error: %pB: Mis-matched ISA string to merge '%s' and '%s'."),
2691 ibfd
, in
->name
, out
->name
);
2694 else if ((in
->major_version
!= out
->major_version
) ||
2695 (in
->minor_version
!= out
->minor_version
))
2697 /* TODO: Allow different merge policy. */
2698 riscv_version_mismatch (ibfd
, in
, out
);
2702 riscv_add_subset (&merged_subsets
,
2703 in
->name
, in
->major_version
, in
->minor_version
);
2708 /* Handle standard extension first. */
2709 for (p
= standard_exts
; *p
; ++p
)
2711 char find_ext
[2] = {*p
, '\0'};
2712 struct riscv_subset_t
*find_in
=
2713 riscv_lookup_subset (&in_subsets
, find_ext
);
2714 struct riscv_subset_t
*find_out
=
2715 riscv_lookup_subset (&out_subsets
, find_ext
);
2717 if (find_in
== NULL
&& find_out
== NULL
)
2720 /* Check version is same or not. */
2721 /* TODO: Allow different merge policy. */
2722 if ((find_in
!= NULL
&& find_out
!= NULL
)
2723 && ((find_in
->major_version
!= find_out
->major_version
)
2724 || (find_in
->minor_version
!= find_out
->minor_version
)))
2726 riscv_version_mismatch (ibfd
, in
, out
);
2730 struct riscv_subset_t
*merged
= find_in
? find_in
: find_out
;
2731 riscv_add_subset (&merged_subsets
, merged
->name
,
2732 merged
->major_version
, merged
->minor_version
);
2735 /* Skip all standard extensions. */
2736 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
2737 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
2745 /* If C is a prefix class, then return the EXT string without the prefix.
2746 Otherwise return the entire EXT string. */
2749 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
2753 case RV_ISA_CLASS_X
: return &ext
[1];
2754 case RV_ISA_CLASS_S
: return &ext
[1];
2755 case RV_ISA_CLASS_Z
: return &ext
[1];
2756 default: return ext
;
2760 /* Compare prefixed extension names canonically. */
2763 riscv_prefix_cmp (const char *a
, const char *b
)
2765 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
2766 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
2768 /* Extension name without prefix */
2769 const char *anp
= riscv_skip_prefix (a
, ca
);
2770 const char *bnp
= riscv_skip_prefix (b
, cb
);
2773 return strcasecmp (anp
, bnp
);
2775 return (int)ca
- (int)cb
;
2778 /* Merge multi letter extensions. PIN is a pointer to the head of the input
2779 object subset list. Likewise for POUT and the output object. Return TRUE
2780 on success and FALSE when a conflict is found. */
2783 riscv_merge_multi_letter_ext (bfd
*ibfd
,
2784 riscv_subset_t
**pin
,
2785 riscv_subset_t
**pout
)
2787 riscv_subset_t
*in
= *pin
;
2788 riscv_subset_t
*out
= *pout
;
2789 riscv_subset_t
*tail
;
2795 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
2799 /* `in' comes before `out', append `in' and increment. */
2800 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
2806 /* `out' comes before `in', append `out' and increment. */
2807 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2808 out
->minor_version
);
2813 /* Both present, check version and increment both. */
2814 if ((in
->major_version
!= out
->major_version
)
2815 || (in
->minor_version
!= out
->minor_version
))
2817 riscv_version_mismatch (ibfd
, in
, out
);
2821 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
2822 out
->minor_version
);
2829 /* If we're here, either `in' or `out' is running longer than
2830 the other. So, we need to append the corresponding tail. */
2831 tail
= in
? in
: out
;
2835 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
2836 tail
->minor_version
);
2844 /* Merge Tag_RISCV_arch attribute. */
2847 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
2849 riscv_subset_t
*in
, *out
;
2850 char *merged_arch_str
;
2852 unsigned xlen_in
, xlen_out
;
2853 merged_subsets
.head
= NULL
;
2854 merged_subsets
.tail
= NULL
;
2856 riscv_parse_subset_t rpe_in
;
2857 riscv_parse_subset_t rpe_out
;
2859 /* Only assembler needs to check the default version of ISA, so just set
2860 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
2861 rpe_in
.subset_list
= &in_subsets
;
2862 rpe_in
.error_handler
= _bfd_error_handler
;
2863 rpe_in
.xlen
= &xlen_in
;
2864 rpe_in
.get_default_version
= NULL
;
2866 rpe_out
.subset_list
= &out_subsets
;
2867 rpe_out
.error_handler
= _bfd_error_handler
;
2868 rpe_out
.xlen
= &xlen_out
;
2869 rpe_out
.get_default_version
= NULL
;
2871 if (in_arch
== NULL
&& out_arch
== NULL
)
2874 if (in_arch
== NULL
&& out_arch
!= NULL
)
2877 if (in_arch
!= NULL
&& out_arch
== NULL
)
2880 /* Parse subset from arch string. */
2881 if (!riscv_parse_subset (&rpe_in
, in_arch
))
2884 if (!riscv_parse_subset (&rpe_out
, out_arch
))
2887 /* Checking XLEN. */
2888 if (xlen_out
!= xlen_in
)
2891 (_("error: %pB: ISA string of input (%s) doesn't match "
2892 "output (%s)."), ibfd
, in_arch
, out_arch
);
2896 /* Merge subset list. */
2897 in
= in_subsets
.head
;
2898 out
= out_subsets
.head
;
2900 /* Merge standard extension. */
2901 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
2904 /* Merge all non-single letter extensions with single call. */
2905 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
2908 if (xlen_in
!= xlen_out
)
2911 (_("error: %pB: XLEN of input (%u) doesn't match "
2912 "output (%u)."), ibfd
, xlen_in
, xlen_out
);
2916 if (xlen_in
!= ARCH_SIZE
)
2919 (_("error: %pB: Unsupported XLEN (%u), you might be "
2920 "using wrong emulation."), ibfd
, xlen_in
);
2924 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
2926 /* Release the subset lists. */
2927 riscv_release_subset_list (&in_subsets
);
2928 riscv_release_subset_list (&out_subsets
);
2929 riscv_release_subset_list (&merged_subsets
);
2931 return merged_arch_str
;
2934 /* Merge object attributes from IBFD into output_bfd of INFO.
2935 Raise an error if there are conflicting attributes. */
2938 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
2940 bfd
*obfd
= info
->output_bfd
;
2941 obj_attribute
*in_attr
;
2942 obj_attribute
*out_attr
;
2943 bfd_boolean result
= TRUE
;
2944 bfd_boolean priv_attrs_merged
= FALSE
;
2945 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
2948 /* Skip linker created files. */
2949 if (ibfd
->flags
& BFD_LINKER_CREATED
)
2952 /* Skip any input that doesn't have an attribute section.
2953 This enables to link object files without attribute section with
2955 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
2958 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
2960 /* This is the first object. Copy the attributes. */
2961 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
2963 out_attr
= elf_known_obj_attributes_proc (obfd
);
2965 /* Use the Tag_null value to indicate the attributes have been
2972 in_attr
= elf_known_obj_attributes_proc (ibfd
);
2973 out_attr
= elf_known_obj_attributes_proc (obfd
);
2975 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
2979 case Tag_RISCV_arch
:
2980 if (!out_attr
[Tag_RISCV_arch
].s
)
2981 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
2982 else if (in_attr
[Tag_RISCV_arch
].s
2983 && out_attr
[Tag_RISCV_arch
].s
)
2985 /* Check arch compatible. */
2987 riscv_merge_arch_attr_info (ibfd
,
2988 in_attr
[Tag_RISCV_arch
].s
,
2989 out_attr
[Tag_RISCV_arch
].s
);
2990 if (merged_arch
== NULL
)
2993 out_attr
[Tag_RISCV_arch
].s
= "";
2996 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3000 case Tag_RISCV_priv_spec
:
3001 case Tag_RISCV_priv_spec_minor
:
3002 case Tag_RISCV_priv_spec_revision
:
3003 /* If we have handled the priv attributes, then skip it. */
3004 if (!priv_attrs_merged
)
3006 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3007 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3008 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3009 enum riscv_priv_spec_class in_priv_spec
;
3010 enum riscv_priv_spec_class out_priv_spec
;
3012 /* Get the priv spec class from elf attribute numbers. */
3013 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3017 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3022 /* Allow to link the object without the priv specs. */
3023 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3025 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3026 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3027 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3029 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3030 && in_priv_spec
!= out_priv_spec
)
3033 (_("warning: %pB use privilege spec version %u.%u.%u but "
3034 "the output use version %u.%u.%u."),
3043 /* The priv spec v1.9.1 can be linked with other spec
3044 versions since the conflicts. We plan to drop the
3045 v1.9.1 in a year or two, so this confict should be
3046 removed in the future. */
3047 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3048 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3051 (_("warning: privilege spec version 1.9.1 can not be "
3052 "linked with other spec versions."));
3055 /* Update the output priv attributes to the newest. */
3056 if (in_priv_spec
> out_priv_spec
)
3058 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3059 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3060 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3063 priv_attrs_merged
= TRUE
;
3067 case Tag_RISCV_unaligned_access
:
3068 out_attr
[i
].i
|= in_attr
[i
].i
;
3071 case Tag_RISCV_stack_align
:
3072 if (out_attr
[i
].i
== 0)
3073 out_attr
[i
].i
= in_attr
[i
].i
;
3074 else if (in_attr
[i
].i
!= 0
3075 && out_attr
[i
].i
!= 0
3076 && out_attr
[i
].i
!= in_attr
[i
].i
)
3079 (_("error: %pB use %u-byte stack aligned but the output "
3080 "use %u-byte stack aligned."),
3081 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3087 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3090 /* If out_attr was copied from in_attr then it won't have a type yet. */
3091 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3092 out_attr
[i
].type
= in_attr
[i
].type
;
3095 /* Merge Tag_compatibility attributes and any common GNU ones. */
3096 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3099 /* Check for any attributes not known on RISC-V. */
3100 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3105 /* Merge backend specific data from an object file to the output
3106 object file when linking. */
3109 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3111 bfd
*obfd
= info
->output_bfd
;
3112 flagword new_flags
, old_flags
;
3114 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3117 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3119 (*_bfd_error_handler
)
3120 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3121 " target emulation `%s' does not match `%s'"),
3122 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3126 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3129 if (!riscv_merge_attributes (ibfd
, info
))
3132 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3133 old_flags
= elf_elfheader (obfd
)->e_flags
;
3135 if (! elf_flags_init (obfd
))
3137 elf_flags_init (obfd
) = TRUE
;
3138 elf_elfheader (obfd
)->e_flags
= new_flags
;
3142 /* Check to see if the input BFD actually contains any sections. If not,
3143 its flags may not have been initialized either, but it cannot actually
3144 cause any incompatibility. Do not short-circuit dynamic objects; their
3145 section list may be emptied by elf_link_add_object_symbols.
3147 Also check to see if there are no code sections in the input. In this
3148 case, there is no need to check for code specific flags. */
3149 if (!(ibfd
->flags
& DYNAMIC
))
3151 bfd_boolean null_input_bfd
= TRUE
;
3152 bfd_boolean only_data_sections
= TRUE
;
3155 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3157 if ((bfd_section_flags (sec
)
3158 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3159 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3160 only_data_sections
= FALSE
;
3162 null_input_bfd
= FALSE
;
3166 if (null_input_bfd
|| only_data_sections
)
3170 /* Disallow linking different float ABIs. */
3171 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3173 (*_bfd_error_handler
)
3174 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3175 riscv_float_abi_string (new_flags
),
3176 riscv_float_abi_string (old_flags
));
3180 /* Disallow linking RVE and non-RVE. */
3181 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3183 (*_bfd_error_handler
)
3184 (_("%pB: can't link RVE with other target"), ibfd
);
3188 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3189 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3194 bfd_set_error (bfd_error_bad_value
);
3198 /* Delete some bytes from a section while relaxing. */
3201 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3202 struct bfd_link_info
*link_info
)
3204 unsigned int i
, symcount
;
3205 bfd_vma toaddr
= sec
->size
;
3206 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3207 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3208 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3209 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3210 bfd_byte
*contents
= data
->this_hdr
.contents
;
3212 /* Actually delete the bytes. */
3214 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3216 /* Adjust the location of all of the relocs. Note that we need not
3217 adjust the addends, since all PC-relative references must be against
3218 symbols, which we will adjust below. */
3219 for (i
= 0; i
< sec
->reloc_count
; i
++)
3220 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3221 data
->relocs
[i
].r_offset
-= count
;
3223 /* Adjust the local symbols defined in this section. */
3224 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3226 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3227 if (sym
->st_shndx
== sec_shndx
)
3229 /* If the symbol is in the range of memory we just moved, we
3230 have to adjust its value. */
3231 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3232 sym
->st_value
-= count
;
3234 /* If the symbol *spans* the bytes we just deleted (i.e. its
3235 *end* is in the moved bytes but its *start* isn't), then we
3236 must adjust its size.
3238 This test needs to use the original value of st_value, otherwise
3239 we might accidentally decrease size when deleting bytes right
3240 before the symbol. But since deleted relocs can't span across
3241 symbols, we can't have both a st_value and a st_size decrease,
3242 so it is simpler to just use an else. */
3243 else if (sym
->st_value
<= addr
3244 && sym
->st_value
+ sym
->st_size
> addr
3245 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3246 sym
->st_size
-= count
;
3250 /* Now adjust the global symbols defined in this section. */
3251 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3252 - symtab_hdr
->sh_info
);
3254 for (i
= 0; i
< symcount
; i
++)
3256 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3258 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3259 containing the definition of __wrap_SYMBOL, includes a direct
3260 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3261 the same symbol (which is __wrap_SYMBOL), but still exist as two
3262 different symbols in 'sym_hashes', we don't want to adjust
3263 the global symbol __wrap_SYMBOL twice. */
3264 /* The same problem occurs with symbols that are versioned_hidden, as
3265 foo becomes an alias for foo@BAR, and hence they need the same
3267 if (link_info
->wrap_hash
!= NULL
3268 || sym_hash
->versioned
== versioned_hidden
)
3270 struct elf_link_hash_entry
**cur_sym_hashes
;
3272 /* Loop only over the symbols which have already been checked. */
3273 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3276 /* If the current symbol is identical to 'sym_hash', that means
3277 the symbol was already adjusted (or at least checked). */
3278 if (*cur_sym_hashes
== sym_hash
)
3281 /* Don't adjust the symbol again. */
3282 if (cur_sym_hashes
< &sym_hashes
[i
])
3286 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3287 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3288 && sym_hash
->root
.u
.def
.section
== sec
)
3290 /* As above, adjust the value if needed. */
3291 if (sym_hash
->root
.u
.def
.value
> addr
3292 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3293 sym_hash
->root
.u
.def
.value
-= count
;
3295 /* As above, adjust the size if needed. */
3296 else if (sym_hash
->root
.u
.def
.value
<= addr
3297 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3298 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3299 sym_hash
->size
-= count
;
3306 /* A second format for recording PC-relative hi relocations. This stores the
3307 information required to relax them to GP-relative addresses. */
3309 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3310 struct riscv_pcgp_hi_reloc
3317 bfd_boolean undefined_weak
;
3318 riscv_pcgp_hi_reloc
*next
;
3321 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3322 struct riscv_pcgp_lo_reloc
3325 riscv_pcgp_lo_reloc
*next
;
3330 riscv_pcgp_hi_reloc
*hi
;
3331 riscv_pcgp_lo_reloc
*lo
;
3332 } riscv_pcgp_relocs
;
3334 /* Initialize the pcgp reloc info in P. */
3337 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3344 /* Free the pcgp reloc info in P. */
3347 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3348 bfd
*abfd ATTRIBUTE_UNUSED
,
3349 asection
*sec ATTRIBUTE_UNUSED
)
3351 riscv_pcgp_hi_reloc
*c
;
3352 riscv_pcgp_lo_reloc
*l
;
3354 for (c
= p
->hi
; c
!= NULL
;)
3356 riscv_pcgp_hi_reloc
*next
= c
->next
;
3361 for (l
= p
->lo
; l
!= NULL
;)
3363 riscv_pcgp_lo_reloc
*next
= l
->next
;
3369 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3370 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3371 relax the corresponding lo part reloc. */
3374 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
3375 bfd_vma hi_addend
, bfd_vma hi_addr
,
3376 unsigned hi_sym
, asection
*sym_sec
,
3377 bfd_boolean undefined_weak
)
3379 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
3382 new->hi_sec_off
= hi_sec_off
;
3383 new->hi_addend
= hi_addend
;
3384 new->hi_addr
= hi_addr
;
3385 new->hi_sym
= hi_sym
;
3386 new->sym_sec
= sym_sec
;
3387 new->undefined_weak
= undefined_weak
;
3393 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3394 This is used by a lo part reloc to find the corresponding hi part reloc. */
3396 static riscv_pcgp_hi_reloc
*
3397 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3399 riscv_pcgp_hi_reloc
*c
;
3401 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
3402 if (c
->hi_sec_off
== hi_sec_off
)
3407 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3408 This is used to record relocs that can't be relaxed. */
3411 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3413 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
3416 new->hi_sec_off
= hi_sec_off
;
3422 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3423 This is used by a hi part reloc to find the corresponding lo part reloc. */
3426 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
3428 riscv_pcgp_lo_reloc
*c
;
3430 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
3431 if (c
->hi_sec_off
== hi_sec_off
)
3436 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
3437 struct bfd_link_info
*,
3438 Elf_Internal_Rela
*,
3439 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
3440 riscv_pcgp_relocs
*,
3441 bfd_boolean undefined_weak
);
3443 /* Relax AUIPC + JALR into JAL. */
3446 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
3447 struct bfd_link_info
*link_info
,
3448 Elf_Internal_Rela
*rel
,
3450 bfd_vma max_alignment
,
3451 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3453 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3454 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3456 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3457 bfd_signed_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
3458 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
3459 bfd_vma auipc
, jalr
;
3460 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3462 /* If the call crosses section boundaries, an alignment directive could
3463 cause the PC-relative offset to later increase, so we need to add in the
3464 max alignment of any section inclusive from the call to the target.
3465 Otherwise, we only need to use the alignment of the current section. */
3466 if (VALID_UJTYPE_IMM (foff
))
3468 if (sym_sec
->output_section
== sec
->output_section
3469 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3470 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3471 foff
+= (foff
< 0 ? -max_alignment
: max_alignment
);
3474 /* See if this function call can be shortened. */
3475 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
3478 /* Shorten the function call. */
3479 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
3481 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3482 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
3483 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
3484 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
3486 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
3487 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
3491 /* Relax to C.J[AL] rd, addr. */
3492 r_type
= R_RISCV_RVC_JUMP
;
3493 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
3496 else if (VALID_UJTYPE_IMM (foff
))
3498 /* Relax to JAL rd, addr. */
3499 r_type
= R_RISCV_JAL
;
3500 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
3502 else /* near_zero */
3504 /* Relax to JALR rd, x0, addr. */
3505 r_type
= R_RISCV_LO12_I
;
3506 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
3509 /* Replace the R_RISCV_CALL reloc. */
3510 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
3511 /* Replace the AUIPC. */
3512 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
3514 /* Delete unnecessary JALR. */
3516 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
3520 /* Traverse all output sections and return the max alignment. */
3523 _bfd_riscv_get_max_alignment (asection
*sec
)
3525 unsigned int max_alignment_power
= 0;
3528 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
3530 if (o
->alignment_power
> max_alignment_power
)
3531 max_alignment_power
= o
->alignment_power
;
3534 return (bfd_vma
) 1 << max_alignment_power
;
3537 /* Relax non-PIC global variable references. */
3540 _bfd_riscv_relax_lui (bfd
*abfd
,
3543 struct bfd_link_info
*link_info
,
3544 Elf_Internal_Rela
*rel
,
3546 bfd_vma max_alignment
,
3547 bfd_vma reserve_size
,
3549 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3550 bfd_boolean undefined_weak
)
3552 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3553 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3554 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
3556 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3560 /* If gp and the symbol are in the same output section, which is not the
3561 abs section, then consider only that output section's alignment. */
3562 struct bfd_link_hash_entry
*h
=
3563 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3565 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3566 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3567 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3570 /* Is the reference in range of x0 or gp?
3571 Valid gp range conservatively because of alignment issue. */
3573 || (VALID_ITYPE_IMM (symval
)
3575 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3577 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3579 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
3580 switch (ELFNN_R_TYPE (rel
->r_info
))
3582 case R_RISCV_LO12_I
:
3585 /* Change the RS1 to zero. */
3586 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3587 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3588 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3591 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3594 case R_RISCV_LO12_S
:
3597 /* Change the RS1 to zero. */
3598 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3599 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3600 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3603 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3607 /* We can delete the unnecessary LUI and reloc. */
3608 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3610 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
3618 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
3619 account for this assuming page alignment at worst. In the presence of
3620 RELRO segment the linker aligns it by one page size, therefore sections
3621 after the segment can be moved more than one page. */
3624 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
3625 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
3626 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
3627 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
3628 : ELF_MAXPAGESIZE
)))
3630 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
3631 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3632 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
3633 if (rd
== 0 || rd
== X_SP
)
3636 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
3637 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
3639 /* Replace the R_RISCV_HI20 reloc. */
3640 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
3643 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
3650 /* Relax non-PIC TLS references. */
3653 _bfd_riscv_relax_tls_le (bfd
*abfd
,
3655 asection
*sym_sec ATTRIBUTE_UNUSED
,
3656 struct bfd_link_info
*link_info
,
3657 Elf_Internal_Rela
*rel
,
3659 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3660 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3662 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
3663 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3665 /* See if this symbol is in range of tp. */
3666 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
3669 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3670 switch (ELFNN_R_TYPE (rel
->r_info
))
3672 case R_RISCV_TPREL_LO12_I
:
3673 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
3676 case R_RISCV_TPREL_LO12_S
:
3677 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
3680 case R_RISCV_TPREL_HI20
:
3681 case R_RISCV_TPREL_ADD
:
3682 /* We can delete the unnecessary instruction and reloc. */
3683 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3685 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
3692 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
3695 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
3697 struct bfd_link_info
*link_info
,
3698 Elf_Internal_Rela
*rel
,
3700 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3701 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3702 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3703 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
3704 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3706 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3707 bfd_vma alignment
= 1, pos
;
3708 while (alignment
<= rel
->r_addend
)
3711 symval
-= rel
->r_addend
;
3712 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
3713 bfd_vma nop_bytes
= aligned_addr
- symval
;
3715 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
3716 sec
->sec_flg0
= TRUE
;
3718 /* Make sure there are enough NOPs to actually achieve the alignment. */
3719 if (rel
->r_addend
< nop_bytes
)
3722 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
3723 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
3724 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
3725 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
3726 bfd_set_error (bfd_error_bad_value
);
3730 /* Delete the reloc. */
3731 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
3733 /* If the number of NOPs is already correct, there's nothing to do. */
3734 if (nop_bytes
== rel
->r_addend
)
3737 /* Write as many RISC-V NOPs as we need. */
3738 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
3739 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
3741 /* Write a final RVC NOP if need be. */
3742 if (nop_bytes
% 4 != 0)
3743 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
3745 /* Delete the excess bytes. */
3746 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
3747 rel
->r_addend
- nop_bytes
, link_info
);
3750 /* Relax PC-relative references to GP-relative references. */
3753 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
3756 struct bfd_link_info
*link_info
,
3757 Elf_Internal_Rela
*rel
,
3759 bfd_vma max_alignment
,
3760 bfd_vma reserve_size
,
3761 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3762 riscv_pcgp_relocs
*pcgp_relocs
,
3763 bfd_boolean undefined_weak
)
3765 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
3766 bfd_vma gp
= riscv_global_pointer_value (link_info
);
3768 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
3770 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
3771 * actual target address. */
3772 riscv_pcgp_hi_reloc hi_reloc
;
3773 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
3774 switch (ELFNN_R_TYPE (rel
->r_info
))
3776 case R_RISCV_PCREL_LO12_I
:
3777 case R_RISCV_PCREL_LO12_S
:
3779 /* If the %lo has an addend, it isn't for the label pointing at the
3780 hi part instruction, but rather for the symbol pointed at by the
3781 hi part instruction. So we must subtract it here for the lookup.
3782 It is still used below in the final symbol address. */
3783 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
3784 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
3788 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
3793 symval
= hi_reloc
.hi_addr
;
3794 sym_sec
= hi_reloc
.sym_sec
;
3796 /* We can not know whether the undefined weak symbol is referenced
3797 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
3798 we have to record the 'undefined_weak' flag when handling the
3799 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
3800 undefined_weak
= hi_reloc
.undefined_weak
;
3804 case R_RISCV_PCREL_HI20
:
3805 /* Mergeable symbols and code might later move out of range. */
3806 if (! undefined_weak
3807 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
3810 /* If the cooresponding lo relocation has already been seen then it's not
3811 * safe to relax this relocation. */
3812 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
3823 /* If gp and the symbol are in the same output section, which is not the
3824 abs section, then consider only that output section's alignment. */
3825 struct bfd_link_hash_entry
*h
=
3826 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
3828 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
3829 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
3830 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
3833 /* Is the reference in range of x0 or gp?
3834 Valid gp range conservatively because of alignment issue. */
3836 || (VALID_ITYPE_IMM (symval
)
3838 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
3840 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
3842 unsigned sym
= hi_reloc
.hi_sym
;
3843 switch (ELFNN_R_TYPE (rel
->r_info
))
3845 case R_RISCV_PCREL_LO12_I
:
3848 /* Change the RS1 to zero, and then modify the relocation
3849 type to R_RISCV_LO12_I. */
3850 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3851 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3852 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3853 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
3854 rel
->r_addend
= hi_reloc
.hi_addend
;
3858 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
3859 rel
->r_addend
+= hi_reloc
.hi_addend
;
3863 case R_RISCV_PCREL_LO12_S
:
3866 /* Change the RS1 to zero, and then modify the relocation
3867 type to R_RISCV_LO12_S. */
3868 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
3869 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
3870 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
3871 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
3872 rel
->r_addend
= hi_reloc
.hi_addend
;
3876 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
3877 rel
->r_addend
+= hi_reloc
.hi_addend
;
3881 case R_RISCV_PCREL_HI20
:
3882 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
3886 ELFNN_R_SYM(rel
->r_info
),
3889 /* We can delete the unnecessary AUIPC and reloc. */
3890 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
3902 /* Relax PC-relative references to GP-relative references. */
3905 _bfd_riscv_relax_delete (bfd
*abfd
,
3907 asection
*sym_sec ATTRIBUTE_UNUSED
,
3908 struct bfd_link_info
*link_info
,
3909 Elf_Internal_Rela
*rel
,
3910 bfd_vma symval ATTRIBUTE_UNUSED
,
3911 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
3912 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
3913 bfd_boolean
*again ATTRIBUTE_UNUSED
,
3914 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
3915 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
3917 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
3920 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
3924 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
3925 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
3926 disabled, handles code alignment directives. */
3929 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
3930 struct bfd_link_info
*info
,
3933 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
3934 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3935 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3936 Elf_Internal_Rela
*relocs
;
3937 bfd_boolean ret
= FALSE
;
3939 bfd_vma max_alignment
, reserve_size
= 0;
3940 riscv_pcgp_relocs pcgp_relocs
;
3944 if (bfd_link_relocatable (info
)
3946 || (sec
->flags
& SEC_RELOC
) == 0
3947 || sec
->reloc_count
== 0
3948 || (info
->disable_target_specific_optimizations
3949 && info
->relax_pass
== 0))
3952 riscv_init_pcgp_relocs (&pcgp_relocs
);
3954 /* Read this BFD's relocs if we haven't done so already. */
3956 relocs
= data
->relocs
;
3957 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
3958 info
->keep_memory
)))
3963 max_alignment
= htab
->max_alignment
;
3964 if (max_alignment
== (bfd_vma
) -1)
3966 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3967 htab
->max_alignment
= max_alignment
;
3971 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
3973 /* Examine and consider relaxing each reloc. */
3974 for (i
= 0; i
< sec
->reloc_count
; i
++)
3977 Elf_Internal_Rela
*rel
= relocs
+ i
;
3978 relax_func_t relax_func
;
3979 int type
= ELFNN_R_TYPE (rel
->r_info
);
3982 bfd_boolean undefined_weak
= FALSE
;
3985 if (info
->relax_pass
== 0)
3987 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
3988 relax_func
= _bfd_riscv_relax_call
;
3989 else if (type
== R_RISCV_HI20
3990 || type
== R_RISCV_LO12_I
3991 || type
== R_RISCV_LO12_S
)
3992 relax_func
= _bfd_riscv_relax_lui
;
3993 else if (!bfd_link_pic(info
)
3994 && (type
== R_RISCV_PCREL_HI20
3995 || type
== R_RISCV_PCREL_LO12_I
3996 || type
== R_RISCV_PCREL_LO12_S
))
3997 relax_func
= _bfd_riscv_relax_pc
;
3998 else if (type
== R_RISCV_TPREL_HI20
3999 || type
== R_RISCV_TPREL_ADD
4000 || type
== R_RISCV_TPREL_LO12_I
4001 || type
== R_RISCV_TPREL_LO12_S
)
4002 relax_func
= _bfd_riscv_relax_tls_le
;
4006 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4007 if (i
== sec
->reloc_count
- 1
4008 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4009 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4012 /* Skip over the R_RISCV_RELAX. */
4015 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4016 relax_func
= _bfd_riscv_relax_delete
;
4017 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4018 relax_func
= _bfd_riscv_relax_align
;
4022 data
->relocs
= relocs
;
4024 /* Read this BFD's contents if we haven't done so already. */
4025 if (!data
->this_hdr
.contents
4026 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4029 /* Read this BFD's symbols if we haven't done so already. */
4030 if (symtab_hdr
->sh_info
!= 0
4031 && !symtab_hdr
->contents
4032 && !(symtab_hdr
->contents
=
4033 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4034 symtab_hdr
->sh_info
,
4035 0, NULL
, NULL
, NULL
)))
4038 /* Get the value of the symbol referred to by the reloc. */
4039 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4041 /* A local symbol. */
4042 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4043 + ELFNN_R_SYM (rel
->r_info
));
4044 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4045 ? 0 : isym
->st_size
- rel
->r_addend
;
4047 if (isym
->st_shndx
== SHN_UNDEF
)
4048 sym_sec
= sec
, symval
= rel
->r_offset
;
4051 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4052 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4054 /* The purpose of this code is unknown. It breaks linker scripts
4055 for embedded development that place sections at address zero.
4056 This code is believed to be unnecessary. Disabling it but not
4057 yet removing it, in case something breaks. */
4058 if (sec_addr (sym_sec
) == 0)
4061 symval
= isym
->st_value
;
4063 symtype
= ELF_ST_TYPE (isym
->st_info
);
4068 struct elf_link_hash_entry
*h
;
4070 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4071 h
= elf_sym_hashes (abfd
)[indx
];
4073 while (h
->root
.type
== bfd_link_hash_indirect
4074 || h
->root
.type
== bfd_link_hash_warning
)
4075 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4077 if (h
->root
.type
== bfd_link_hash_undefweak
4078 && (relax_func
== _bfd_riscv_relax_lui
4079 || relax_func
== _bfd_riscv_relax_pc
))
4081 /* For the lui and auipc relaxations, since the symbol
4082 value of an undefined weak symbol is always be zero,
4083 we can optimize the patterns into a single LI/MV/ADDI
4086 Note that, creating shared libraries and pie output may
4087 break the rule above. Fortunately, since we do not relax
4088 pc relocs when creating shared libraries and pie output,
4089 and the absolute address access for R_RISCV_HI20 isn't
4090 allowed when "-fPIC" is set, the problem of creating shared
4091 libraries can not happen currently. Once we support the
4092 auipc relaxations when creating shared libraries, then we will
4093 need the more rigorous checking for this optimization. */
4094 undefined_weak
= TRUE
;
4097 /* This line has to match the check in riscv_elf_relocate_section
4098 in the R_RISCV_CALL[_PLT] case. */
4099 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4101 sym_sec
= htab
->elf
.splt
;
4102 symval
= h
->plt
.offset
;
4104 else if (undefined_weak
)
4107 sym_sec
= bfd_und_section_ptr
;
4109 else if ((h
->root
.type
== bfd_link_hash_defined
4110 || h
->root
.type
== bfd_link_hash_defweak
)
4111 && h
->root
.u
.def
.section
!= NULL
4112 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4114 symval
= h
->root
.u
.def
.value
;
4115 sym_sec
= h
->root
.u
.def
.section
;
4120 if (h
->type
!= STT_FUNC
)
4122 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4126 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4127 && (sym_sec
->flags
& SEC_MERGE
))
4129 /* At this stage in linking, no SEC_MERGE symbol has been
4130 adjusted, so all references to such symbols need to be
4131 passed through _bfd_merged_section_offset. (Later, in
4132 relocate_section, all SEC_MERGE symbols *except* for
4133 section symbols have been adjusted.)
4135 gas may reduce relocations against symbols in SEC_MERGE
4136 sections to a relocation against the section symbol when
4137 the original addend was zero. When the reloc is against
4138 a section symbol we should include the addend in the
4139 offset passed to _bfd_merged_section_offset, since the
4140 location of interest is the original symbol. On the
4141 other hand, an access to "sym+addend" where "sym" is not
4142 a section symbol should not include the addend; Such an
4143 access is presumed to be an offset from "sym"; The
4144 location of interest is just "sym". */
4145 if (symtype
== STT_SECTION
)
4146 symval
+= rel
->r_addend
;
4148 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4149 elf_section_data (sym_sec
)->sec_info
,
4152 if (symtype
!= STT_SECTION
)
4153 symval
+= rel
->r_addend
;
4156 symval
+= rel
->r_addend
;
4158 symval
+= sec_addr (sym_sec
);
4160 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4161 max_alignment
, reserve_size
, again
,
4162 &pcgp_relocs
, undefined_weak
))
4169 if (relocs
!= data
->relocs
)
4171 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4177 # define PRSTATUS_SIZE 204
4178 # define PRSTATUS_OFFSET_PR_CURSIG 12
4179 # define PRSTATUS_OFFSET_PR_PID 24
4180 # define PRSTATUS_OFFSET_PR_REG 72
4181 # define ELF_GREGSET_T_SIZE 128
4182 # define PRPSINFO_SIZE 128
4183 # define PRPSINFO_OFFSET_PR_PID 16
4184 # define PRPSINFO_OFFSET_PR_FNAME 32
4185 # define PRPSINFO_OFFSET_PR_PSARGS 48
4187 # define PRSTATUS_SIZE 376
4188 # define PRSTATUS_OFFSET_PR_CURSIG 12
4189 # define PRSTATUS_OFFSET_PR_PID 32
4190 # define PRSTATUS_OFFSET_PR_REG 112
4191 # define ELF_GREGSET_T_SIZE 256
4192 # define PRPSINFO_SIZE 136
4193 # define PRPSINFO_OFFSET_PR_PID 24
4194 # define PRPSINFO_OFFSET_PR_FNAME 40
4195 # define PRPSINFO_OFFSET_PR_PSARGS 56
4198 /* Support for core dump NOTE sections. */
4201 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4203 switch (note
->descsz
)
4208 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4210 elf_tdata (abfd
)->core
->signal
4211 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4214 elf_tdata (abfd
)->core
->lwpid
4215 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4219 /* Make a ".reg/999" section. */
4220 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4221 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4225 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4227 switch (note
->descsz
)
4232 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4234 elf_tdata (abfd
)->core
->pid
4235 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4238 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4239 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4242 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4243 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4247 /* Note that for some reason, a spurious space is tacked
4248 onto the end of the args in some (at least one anyway)
4249 implementations, so strip it off if it exists. */
4252 char *command
= elf_tdata (abfd
)->core
->command
;
4253 int n
= strlen (command
);
4255 if (0 < n
&& command
[n
- 1] == ' ')
4256 command
[n
- 1] = '\0';
4262 /* Set the right mach type. */
4264 riscv_elf_object_p (bfd
*abfd
)
4266 /* There are only two mach types in RISCV currently. */
4267 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4268 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4270 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4275 /* Determine whether an object attribute tag takes an integer, a
4279 riscv_elf_obj_attrs_arg_type (int tag
)
4281 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4284 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4285 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4287 #define elf_backend_reloc_type_class riscv_reloc_type_class
4289 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4290 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4291 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4292 #define bfd_elfNN_bfd_merge_private_bfd_data \
4293 _bfd_riscv_elf_merge_private_bfd_data
4295 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4296 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4297 #define elf_backend_check_relocs riscv_elf_check_relocs
4298 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4299 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4300 #define elf_backend_relocate_section riscv_elf_relocate_section
4301 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4302 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4303 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4304 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4305 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4306 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4307 #define elf_backend_object_p riscv_elf_object_p
4308 #define elf_info_to_howto_rel NULL
4309 #define elf_info_to_howto riscv_info_to_howto_rela
4310 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4311 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4313 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4315 #define elf_backend_can_gc_sections 1
4316 #define elf_backend_can_refcount 1
4317 #define elf_backend_want_got_plt 1
4318 #define elf_backend_plt_readonly 1
4319 #define elf_backend_plt_alignment 4
4320 #define elf_backend_want_plt_sym 1
4321 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4322 #define elf_backend_want_dynrelro 1
4323 #define elf_backend_rela_normal 1
4324 #define elf_backend_default_execstack 0
4326 #undef elf_backend_obj_attrs_vendor
4327 #define elf_backend_obj_attrs_vendor "riscv"
4328 #undef elf_backend_obj_attrs_arg_type
4329 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4330 #undef elf_backend_obj_attrs_section_type
4331 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4332 #undef elf_backend_obj_attrs_section
4333 #define elf_backend_obj_attrs_section ".riscv.attributes"
4335 #include "elfNN-target.h"