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"
36 /* Internal relocations used exclusively by the relaxation pass. */
37 #define R_RISCV_DELETE (R_RISCV_max + 1)
41 #define MINUS_ONE ((bfd_vma)0 - 1)
43 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
45 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
47 /* The name of the dynamic interpreter. This is put in the .interp
50 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
51 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
53 #define ELF_ARCH bfd_arch_riscv
54 #define ELF_TARGET_ID RISCV_ELF_DATA
55 #define ELF_MACHINE_CODE EM_RISCV
56 #define ELF_MAXPAGESIZE 0x1000
57 #define ELF_COMMONPAGESIZE 0x1000
59 /* RISC-V ELF linker hash entry. */
61 struct riscv_elf_link_hash_entry
63 struct elf_link_hash_entry elf
;
73 #define riscv_elf_hash_entry(ent) \
74 ((struct riscv_elf_link_hash_entry *)(ent))
76 struct _bfd_riscv_elf_obj_tdata
78 struct elf_obj_tdata root
;
80 /* tls_type for each local got entry. */
81 char *local_got_tls_type
;
84 #define _bfd_riscv_elf_tdata(abfd) \
85 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
87 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
88 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
90 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
91 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
92 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
94 #define is_riscv_elf(bfd) \
95 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
96 && elf_tdata (bfd) != NULL \
97 && elf_object_id (bfd) == RISCV_ELF_DATA)
100 elfNN_riscv_mkobject (bfd
*abfd
)
102 return bfd_elf_allocate_object (abfd
,
103 sizeof (struct _bfd_riscv_elf_obj_tdata
),
107 #include "elf/common.h"
108 #include "elf/internal.h"
110 struct riscv_elf_link_hash_table
112 struct elf_link_hash_table elf
;
114 /* Short-cuts to get to dynamic linker sections. */
117 /* The max alignment of output sections. */
118 bfd_vma max_alignment
;
120 /* Used by local STT_GNU_IFUNC symbols. */
121 htab_t loc_hash_table
;
122 void * loc_hash_memory
;
126 /* Get the RISC-V ELF linker hash table from a link_info structure. */
127 #define riscv_elf_hash_table(p) \
128 ((is_elf_hash_table ((p)->hash) \
129 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
130 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
133 riscv_info_to_howto_rela (bfd
*abfd
,
135 Elf_Internal_Rela
*dst
)
137 cache_ptr
->howto
= riscv_elf_rtype_to_howto (abfd
, ELFNN_R_TYPE (dst
->r_info
));
138 return cache_ptr
->howto
!= NULL
;
142 riscv_elf_append_rela (bfd
*abfd
, asection
*s
, Elf_Internal_Rela
*rel
)
144 const struct elf_backend_data
*bed
;
147 bed
= get_elf_backend_data (abfd
);
148 loc
= s
->contents
+ (s
->reloc_count
++ * bed
->s
->sizeof_rela
);
149 bed
->s
->swap_reloca_out (abfd
, rel
, loc
);
154 #define PLT_HEADER_INSNS 8
155 #define PLT_ENTRY_INSNS 4
156 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
157 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
159 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
161 /* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
162 the other is used for link map. Other targets also reserve one more
163 entry used for runtime profile? */
164 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
166 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
169 # define MATCH_LREG MATCH_LW
171 # define MATCH_LREG MATCH_LD
174 /* Generate a PLT header. */
177 riscv_make_plt_header (bfd
*output_bfd
, bfd_vma gotplt_addr
, bfd_vma addr
,
180 bfd_vma gotplt_offset_high
= RISCV_PCREL_HIGH_PART (gotplt_addr
, addr
);
181 bfd_vma gotplt_offset_low
= RISCV_PCREL_LOW_PART (gotplt_addr
, addr
);
183 /* RVE has no t3 register, so this won't work, and is not supported. */
184 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
186 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
191 /* auipc t2, %hi(.got.plt)
192 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
193 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
194 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
195 addi t0, t2, %lo(.got.plt) # &.got.plt
196 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
197 l[w|d] t0, PTRSIZE(t0) # link map
200 entry
[0] = RISCV_UTYPE (AUIPC
, X_T2
, gotplt_offset_high
);
201 entry
[1] = RISCV_RTYPE (SUB
, X_T1
, X_T1
, X_T3
);
202 entry
[2] = RISCV_ITYPE (LREG
, X_T3
, X_T2
, gotplt_offset_low
);
203 entry
[3] = RISCV_ITYPE (ADDI
, X_T1
, X_T1
, (uint32_t) -(PLT_HEADER_SIZE
+ 12));
204 entry
[4] = RISCV_ITYPE (ADDI
, X_T0
, X_T2
, gotplt_offset_low
);
205 entry
[5] = RISCV_ITYPE (SRLI
, X_T1
, X_T1
, 4 - RISCV_ELF_LOG_WORD_BYTES
);
206 entry
[6] = RISCV_ITYPE (LREG
, X_T0
, X_T0
, RISCV_ELF_WORD_BYTES
);
207 entry
[7] = RISCV_ITYPE (JALR
, 0, X_T3
, 0);
212 /* Generate a PLT entry. */
215 riscv_make_plt_entry (bfd
*output_bfd
, bfd_vma got
, bfd_vma addr
,
218 /* RVE has no t3 register, so this won't work, and is not supported. */
219 if (elf_elfheader (output_bfd
)->e_flags
& EF_RISCV_RVE
)
221 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
226 /* auipc t3, %hi(.got.plt entry)
227 l[w|d] t3, %lo(.got.plt entry)(t3)
231 entry
[0] = RISCV_UTYPE (AUIPC
, X_T3
, RISCV_PCREL_HIGH_PART (got
, addr
));
232 entry
[1] = RISCV_ITYPE (LREG
, X_T3
, X_T3
, RISCV_PCREL_LOW_PART (got
, addr
));
233 entry
[2] = RISCV_ITYPE (JALR
, X_T1
, X_T3
, 0);
234 entry
[3] = RISCV_NOP
;
239 /* Create an entry in an RISC-V ELF linker hash table. */
241 static struct bfd_hash_entry
*
242 link_hash_newfunc (struct bfd_hash_entry
*entry
,
243 struct bfd_hash_table
*table
, const char *string
)
245 /* Allocate the structure if it has not already been allocated by a
250 bfd_hash_allocate (table
,
251 sizeof (struct riscv_elf_link_hash_entry
));
256 /* Call the allocation method of the superclass. */
257 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
260 struct riscv_elf_link_hash_entry
*eh
;
262 eh
= (struct riscv_elf_link_hash_entry
*) entry
;
263 eh
->tls_type
= GOT_UNKNOWN
;
269 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
270 for local symbol so that we can handle local STT_GNU_IFUNC symbols
271 as global symbol. We reuse indx and dynstr_index for local symbol
272 hash since they aren't used by global symbols in this backend. */
275 riscv_elf_local_htab_hash (const void *ptr
)
277 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) ptr
;
278 return ELF_LOCAL_SYMBOL_HASH (h
->indx
, h
->dynstr_index
);
281 /* Compare local hash entries. */
284 riscv_elf_local_htab_eq (const void *ptr1
, const void *ptr2
)
286 struct elf_link_hash_entry
*h1
= (struct elf_link_hash_entry
*) ptr1
;
287 struct elf_link_hash_entry
*h2
= (struct elf_link_hash_entry
*) ptr2
;
289 return h1
->indx
== h2
->indx
&& h1
->dynstr_index
== h2
->dynstr_index
;
292 /* Find and/or create a hash entry for local symbol. */
294 static struct elf_link_hash_entry
*
295 riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table
*htab
,
296 bfd
*abfd
, const Elf_Internal_Rela
*rel
,
299 struct riscv_elf_link_hash_entry eh
, *ret
;
300 asection
*sec
= abfd
->sections
;
301 hashval_t h
= ELF_LOCAL_SYMBOL_HASH (sec
->id
,
302 ELFNN_R_SYM (rel
->r_info
));
305 eh
.elf
.indx
= sec
->id
;
306 eh
.elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
307 slot
= htab_find_slot_with_hash (htab
->loc_hash_table
, &eh
, h
,
308 create
? INSERT
: NO_INSERT
);
315 ret
= (struct riscv_elf_link_hash_entry
*) *slot
;
319 ret
= (struct riscv_elf_link_hash_entry
*)
320 objalloc_alloc ((struct objalloc
*) htab
->loc_hash_memory
,
321 sizeof (struct riscv_elf_link_hash_entry
));
324 memset (ret
, 0, sizeof (*ret
));
325 ret
->elf
.indx
= sec
->id
;
326 ret
->elf
.dynstr_index
= ELFNN_R_SYM (rel
->r_info
);
327 ret
->elf
.dynindx
= -1;
333 /* Destroy a RISC-V elf linker hash table. */
336 riscv_elf_link_hash_table_free (bfd
*obfd
)
338 struct riscv_elf_link_hash_table
*ret
339 = (struct riscv_elf_link_hash_table
*) obfd
->link
.hash
;
341 if (ret
->loc_hash_table
)
342 htab_delete (ret
->loc_hash_table
);
343 if (ret
->loc_hash_memory
)
344 objalloc_free ((struct objalloc
*) ret
->loc_hash_memory
);
346 _bfd_elf_link_hash_table_free (obfd
);
349 /* Create a RISC-V ELF linker hash table. */
351 static struct bfd_link_hash_table
*
352 riscv_elf_link_hash_table_create (bfd
*abfd
)
354 struct riscv_elf_link_hash_table
*ret
;
355 size_t amt
= sizeof (struct riscv_elf_link_hash_table
);
357 ret
= (struct riscv_elf_link_hash_table
*) bfd_zmalloc (amt
);
361 if (!_bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
,
362 sizeof (struct riscv_elf_link_hash_entry
),
369 ret
->max_alignment
= (bfd_vma
) -1;
371 /* Create hash table for local ifunc. */
372 ret
->loc_hash_table
= htab_try_create (1024,
373 riscv_elf_local_htab_hash
,
374 riscv_elf_local_htab_eq
,
376 ret
->loc_hash_memory
= objalloc_create ();
377 if (!ret
->loc_hash_table
|| !ret
->loc_hash_memory
)
379 riscv_elf_link_hash_table_free (abfd
);
382 ret
->elf
.root
.hash_table_free
= riscv_elf_link_hash_table_free
;
384 return &ret
->elf
.root
;
387 /* Create the .got section. */
390 riscv_elf_create_got_section (bfd
*abfd
, struct bfd_link_info
*info
)
394 struct elf_link_hash_entry
*h
;
395 const struct elf_backend_data
*bed
= get_elf_backend_data (abfd
);
396 struct elf_link_hash_table
*htab
= elf_hash_table (info
);
398 /* This function may be called more than once. */
399 if (htab
->sgot
!= NULL
)
402 flags
= bed
->dynamic_sec_flags
;
404 s
= bfd_make_section_anyway_with_flags (abfd
,
405 (bed
->rela_plts_and_copies_p
406 ? ".rela.got" : ".rel.got"),
407 (bed
->dynamic_sec_flags
410 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
414 s
= s_got
= bfd_make_section_anyway_with_flags (abfd
, ".got", flags
);
416 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
420 /* The first bit of the global offset table is the header. */
421 s
->size
+= bed
->got_header_size
;
423 if (bed
->want_got_plt
)
425 s
= bfd_make_section_anyway_with_flags (abfd
, ".got.plt", flags
);
427 || !bfd_set_section_alignment (s
, bed
->s
->log_file_align
))
431 /* Reserve room for the header. */
432 s
->size
+= GOTPLT_HEADER_SIZE
;
435 if (bed
->want_got_sym
)
437 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
438 section. We don't do this in the linker script because we don't want
439 to define the symbol if we are not creating a global offset
441 h
= _bfd_elf_define_linkage_sym (abfd
, info
, s_got
,
442 "_GLOBAL_OFFSET_TABLE_");
443 elf_hash_table (info
)->hgot
= h
;
451 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
452 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
456 riscv_elf_create_dynamic_sections (bfd
*dynobj
,
457 struct bfd_link_info
*info
)
459 struct riscv_elf_link_hash_table
*htab
;
461 htab
= riscv_elf_hash_table (info
);
462 BFD_ASSERT (htab
!= NULL
);
464 if (!riscv_elf_create_got_section (dynobj
, info
))
467 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
470 if (!bfd_link_pic (info
))
472 /* Technically, this section doesn't have contents. It is used as the
473 target of TLS copy relocs, to copy TLS data from shared libraries into
474 the executable. However, if we don't mark it as loadable, then it
475 matches the IS_TBSS test in ldlang.c, and there is no run-time address
476 space allocated for it even though it has SEC_ALLOC. That test is
477 correct for .tbss, but not correct for this section. There is also
478 a second problem that having a section with no contents can only work
479 if it comes after all sections with contents in the same segment,
480 but the linker script does not guarantee that. This is just mixed in
481 with other .tdata.* sections. We can fix both problems by lying and
482 saying that there are contents. This section is expected to be small
483 so this should not cause a significant extra program startup cost. */
485 bfd_make_section_anyway_with_flags (dynobj
, ".tdata.dyn",
486 (SEC_ALLOC
| SEC_THREAD_LOCAL
487 | SEC_LOAD
| SEC_DATA
489 | SEC_LINKER_CREATED
));
492 if (!htab
->elf
.splt
|| !htab
->elf
.srelplt
|| !htab
->elf
.sdynbss
493 || (!bfd_link_pic (info
) && (!htab
->elf
.srelbss
|| !htab
->sdyntdata
)))
499 /* Copy the extra info we tack onto an elf_link_hash_entry. */
502 riscv_elf_copy_indirect_symbol (struct bfd_link_info
*info
,
503 struct elf_link_hash_entry
*dir
,
504 struct elf_link_hash_entry
*ind
)
506 struct riscv_elf_link_hash_entry
*edir
, *eind
;
508 edir
= (struct riscv_elf_link_hash_entry
*) dir
;
509 eind
= (struct riscv_elf_link_hash_entry
*) ind
;
511 if (ind
->root
.type
== bfd_link_hash_indirect
512 && dir
->got
.refcount
<= 0)
514 edir
->tls_type
= eind
->tls_type
;
515 eind
->tls_type
= GOT_UNKNOWN
;
517 _bfd_elf_link_hash_copy_indirect (info
, dir
, ind
);
521 riscv_elf_record_tls_type (bfd
*abfd
, struct elf_link_hash_entry
*h
,
522 unsigned long symndx
, char tls_type
)
524 char *new_tls_type
= &_bfd_riscv_elf_tls_type (abfd
, h
, symndx
);
526 *new_tls_type
|= tls_type
;
527 if ((*new_tls_type
& GOT_NORMAL
) && (*new_tls_type
& ~GOT_NORMAL
))
529 (*_bfd_error_handler
)
530 (_("%pB: `%s' accessed both as normal and thread local symbol"),
531 abfd
, h
? h
->root
.root
.string
: "<local>");
538 riscv_elf_record_got_reference (bfd
*abfd
, struct bfd_link_info
*info
,
539 struct elf_link_hash_entry
*h
, long symndx
)
541 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
542 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
544 if (htab
->elf
.sgot
== NULL
)
546 if (!riscv_elf_create_got_section (htab
->elf
.dynobj
, info
))
552 h
->got
.refcount
+= 1;
556 /* This is a global offset table entry for a local symbol. */
557 if (elf_local_got_refcounts (abfd
) == NULL
)
559 bfd_size_type size
= symtab_hdr
->sh_info
* (sizeof (bfd_vma
) + 1);
560 if (!(elf_local_got_refcounts (abfd
) = bfd_zalloc (abfd
, size
)))
562 _bfd_riscv_elf_local_got_tls_type (abfd
)
563 = (char *) (elf_local_got_refcounts (abfd
) + symtab_hdr
->sh_info
);
565 elf_local_got_refcounts (abfd
) [symndx
] += 1;
571 bad_static_reloc (bfd
*abfd
, unsigned r_type
, struct elf_link_hash_entry
*h
)
573 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
575 /* We propably can improve the information to tell users that they
576 should be recompile the code with -fPIC or -fPIE, just like what
578 (*_bfd_error_handler
)
579 (_("%pB: relocation %s against `%s' can not be used when making a shared "
580 "object; recompile with -fPIC"),
581 abfd
, r
? r
->name
: _("<unknown>"),
582 h
!= NULL
? h
->root
.root
.string
: "a local symbol");
583 bfd_set_error (bfd_error_bad_value
);
586 /* Look through the relocs for a section during the first phase, and
587 allocate space in the global offset table or procedure linkage
591 riscv_elf_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
592 asection
*sec
, const Elf_Internal_Rela
*relocs
)
594 struct riscv_elf_link_hash_table
*htab
;
595 Elf_Internal_Shdr
*symtab_hdr
;
596 struct elf_link_hash_entry
**sym_hashes
;
597 const Elf_Internal_Rela
*rel
;
598 asection
*sreloc
= NULL
;
600 if (bfd_link_relocatable (info
))
603 htab
= riscv_elf_hash_table (info
);
604 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
605 sym_hashes
= elf_sym_hashes (abfd
);
607 if (htab
->elf
.dynobj
== NULL
)
608 htab
->elf
.dynobj
= abfd
;
610 for (rel
= relocs
; rel
< relocs
+ sec
->reloc_count
; rel
++)
613 unsigned int r_symndx
;
614 struct elf_link_hash_entry
*h
;
616 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
617 r_type
= ELFNN_R_TYPE (rel
->r_info
);
619 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
621 (*_bfd_error_handler
) (_("%pB: bad symbol index: %d"),
626 if (r_symndx
< symtab_hdr
->sh_info
)
628 /* A local symbol. */
629 Elf_Internal_Sym
*isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
634 /* Check relocation against local STT_GNU_IFUNC symbol. */
635 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
637 h
= riscv_elf_get_local_sym_hash (htab
, abfd
, rel
, TRUE
);
641 /* Fake STT_GNU_IFUNC global symbol. */
642 h
->root
.root
.string
= bfd_elf_sym_name (abfd
, symtab_hdr
,
644 h
->type
= STT_GNU_IFUNC
;
648 h
->root
.type
= bfd_link_hash_defined
;
655 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
656 while (h
->root
.type
== bfd_link_hash_indirect
657 || h
->root
.type
== bfd_link_hash_warning
)
658 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
668 case R_RISCV_CALL_PLT
:
670 case R_RISCV_GOT_HI20
:
671 case R_RISCV_PCREL_HI20
:
672 /* Create the ifunc sections, iplt and ipltgot, for static
674 if (h
->type
== STT_GNU_IFUNC
675 && !_bfd_elf_create_ifunc_sections (htab
->elf
.dynobj
, info
))
683 /* It is referenced by a non-shared object. */
689 case R_RISCV_TLS_GD_HI20
:
690 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
691 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_GD
))
695 case R_RISCV_TLS_GOT_HI20
:
696 if (bfd_link_pic (info
))
697 info
->flags
|= DF_STATIC_TLS
;
698 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
699 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_IE
))
703 case R_RISCV_GOT_HI20
:
704 if (!riscv_elf_record_got_reference (abfd
, info
, h
, r_symndx
)
705 || !riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_NORMAL
))
710 case R_RISCV_CALL_PLT
:
711 /* These symbol requires a procedure linkage table entry. We
712 actually build the entry in adjust_dynamic_symbol,
713 because these might be a case of linking PIC code without
714 linking in any dynamic objects, in which case we don't
715 need to generate a procedure linkage table after all. */
717 /* If it is a local symbol, then we resolve it directly
718 without creating a PLT entry. */
723 h
->plt
.refcount
+= 1;
726 case R_RISCV_PCREL_HI20
:
728 && h
->type
== STT_GNU_IFUNC
)
731 h
->pointer_equality_needed
= 1;
733 /* We don't use the PCREL_HI20 in the data section,
734 so we always need the plt when it refers to
736 h
->plt
.refcount
+= 1;
742 case R_RISCV_RVC_BRANCH
:
743 case R_RISCV_RVC_JUMP
:
744 /* In shared libraries and pie, these relocs are known
746 if (bfd_link_pic (info
))
750 case R_RISCV_TPREL_HI20
:
751 if (!bfd_link_executable (info
))
752 return bad_static_reloc (abfd
, r_type
, h
);
754 riscv_elf_record_tls_type (abfd
, h
, r_symndx
, GOT_TLS_LE
);
758 if (bfd_link_pic (info
))
759 return bad_static_reloc (abfd
, r_type
, h
);
763 case R_RISCV_JUMP_SLOT
:
764 case R_RISCV_RELATIVE
:
772 && (!bfd_link_pic (info
)
773 || h
->type
== STT_GNU_IFUNC
))
775 /* This reloc might not bind locally. */
777 h
->pointer_equality_needed
= 1;
780 || (sec
->flags
& (SEC_CODE
| SEC_READONLY
)) != 0)
782 /* We may need a .plt entry if the symbol is a function
783 defined in a shared lib or is a function referenced
784 from the code or read-only section. */
785 h
->plt
.refcount
+= 1;
789 /* If we are creating a shared library, and this is a reloc
790 against a global symbol, or a non PC relative reloc
791 against a local symbol, then we need to copy the reloc
792 into the shared library. However, if we are linking with
793 -Bsymbolic, we do not need to copy a reloc against a
794 global symbol which is defined in an object we are
795 including in the link (i.e., DEF_REGULAR is set). At
796 this point we have not seen all the input files, so it is
797 possible that DEF_REGULAR is not set now but will be set
798 later (it is never cleared). In case of a weak definition,
799 DEF_REGULAR may be cleared later by a strong definition in
800 a shared library. We account for that possibility below by
801 storing information in the relocs_copied field of the hash
802 table entry. A similar situation occurs when creating
803 shared libraries and symbol visibility changes render the
806 If on the other hand, we are creating an executable, we
807 may need to keep relocations for symbols satisfied by a
808 dynamic library if we manage to avoid copy relocs for the
811 Generate dynamic pointer relocation against STT_GNU_IFUNC
812 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
813 reloc_howto_type
* r
= riscv_elf_rtype_to_howto (abfd
, r_type
);
815 if ((bfd_link_pic (info
)
816 && (sec
->flags
& SEC_ALLOC
) != 0
817 && ((r
!= NULL
&& !r
->pc_relative
)
820 || h
->root
.type
== bfd_link_hash_defweak
821 || !h
->def_regular
))))
822 || (!bfd_link_pic (info
)
823 && (sec
->flags
& SEC_ALLOC
) != 0
825 && (h
->root
.type
== bfd_link_hash_defweak
827 || (!bfd_link_pic (info
)
829 && h
->type
== STT_GNU_IFUNC
830 && (sec
->flags
& SEC_CODE
) == 0))
832 struct elf_dyn_relocs
*p
;
833 struct elf_dyn_relocs
**head
;
835 /* When creating a shared object, we must copy these
836 relocs into the output file. We create a reloc
837 section in dynobj and make room for the reloc. */
840 sreloc
= _bfd_elf_make_dynamic_reloc_section
841 (sec
, htab
->elf
.dynobj
, RISCV_ELF_LOG_WORD_BYTES
,
842 abfd
, /*rela?*/ TRUE
);
848 /* If this is a global symbol, we count the number of
849 relocations we need for this symbol. */
851 head
= &h
->dyn_relocs
;
854 /* Track dynamic relocs needed for local syms too.
855 We really need local syms available to do this
860 Elf_Internal_Sym
*isym
;
862 isym
= bfd_sym_from_r_symndx (&htab
->elf
.sym_cache
,
867 s
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
871 vpp
= &elf_section_data (s
)->local_dynrel
;
872 head
= (struct elf_dyn_relocs
**) vpp
;
876 if (p
== NULL
|| p
->sec
!= sec
)
878 size_t amt
= sizeof *p
;
879 p
= ((struct elf_dyn_relocs
*)
880 bfd_alloc (htab
->elf
.dynobj
, amt
));
891 p
->pc_count
+= r
== NULL
? 0 : r
->pc_relative
;
896 case R_RISCV_GNU_VTINHERIT
:
897 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
901 case R_RISCV_GNU_VTENTRY
:
902 if (!bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
915 riscv_elf_gc_mark_hook (asection
*sec
,
916 struct bfd_link_info
*info
,
917 Elf_Internal_Rela
*rel
,
918 struct elf_link_hash_entry
*h
,
919 Elf_Internal_Sym
*sym
)
922 switch (ELFNN_R_TYPE (rel
->r_info
))
924 case R_RISCV_GNU_VTINHERIT
:
925 case R_RISCV_GNU_VTENTRY
:
929 return _bfd_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
);
932 /* Adjust a symbol defined by a dynamic object and referenced by a
933 regular object. The current definition is in some section of the
934 dynamic object, but we're not including those sections. We have to
935 change the definition to something the rest of the link can
939 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info
*info
,
940 struct elf_link_hash_entry
*h
)
942 struct riscv_elf_link_hash_table
*htab
;
943 struct riscv_elf_link_hash_entry
* eh
;
947 htab
= riscv_elf_hash_table (info
);
948 BFD_ASSERT (htab
!= NULL
);
950 dynobj
= htab
->elf
.dynobj
;
952 /* Make sure we know what is going on here. */
953 BFD_ASSERT (dynobj
!= NULL
955 || h
->type
== STT_GNU_IFUNC
959 && !h
->def_regular
)));
961 /* If this is a function, put it in the procedure linkage table. We
962 will fill in the contents of the procedure linkage table later
963 (although we could actually do it here). */
964 if (h
->type
== STT_FUNC
|| h
->type
== STT_GNU_IFUNC
|| h
->needs_plt
)
966 if (h
->plt
.refcount
<= 0
967 || (h
->type
!= STT_GNU_IFUNC
968 && (SYMBOL_CALLS_LOCAL (info
, h
)
969 || (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
970 && h
->root
.type
== bfd_link_hash_undefweak
))))
972 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
973 input file, but the symbol was never referred to by a dynamic
974 object, or if all references were garbage collected. In such
975 a case, we don't actually need to build a PLT entry. */
976 h
->plt
.offset
= (bfd_vma
) -1;
983 h
->plt
.offset
= (bfd_vma
) -1;
985 /* If this is a weak symbol, and there is a real definition, the
986 processor independent code will have arranged for us to see the
987 real definition first, and we can just use the same value. */
990 struct elf_link_hash_entry
*def
= weakdef (h
);
991 BFD_ASSERT (def
->root
.type
== bfd_link_hash_defined
);
992 h
->root
.u
.def
.section
= def
->root
.u
.def
.section
;
993 h
->root
.u
.def
.value
= def
->root
.u
.def
.value
;
997 /* This is a reference to a symbol defined by a dynamic object which
998 is not a function. */
1000 /* If we are creating a shared library, we must presume that the
1001 only references to the symbol are via the global offset table.
1002 For such cases we need not do anything here; the relocations will
1003 be handled correctly by relocate_section. */
1004 if (bfd_link_pic (info
))
1007 /* If there are no references to this symbol that do not use the
1008 GOT, we don't need to generate a copy reloc. */
1009 if (!h
->non_got_ref
)
1012 /* If -z nocopyreloc was given, we won't generate them either. */
1013 if (info
->nocopyreloc
)
1019 /* If we don't find any dynamic relocs in read-only sections, then
1020 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1021 if (!_bfd_elf_readonly_dynrelocs (h
))
1027 /* We must allocate the symbol in our .dynbss section, which will
1028 become part of the .bss section of the executable. There will be
1029 an entry for this symbol in the .dynsym section. The dynamic
1030 object will contain position independent code, so all references
1031 from the dynamic object to this symbol will go through the global
1032 offset table. The dynamic linker will use the .dynsym entry to
1033 determine the address it must put in the global offset table, so
1034 both the dynamic object and the regular object will refer to the
1035 same memory location for the variable. */
1037 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1038 to copy the initial value out of the dynamic object and into the
1039 runtime process image. We need to remember the offset into the
1040 .rel.bss section we are going to use. */
1041 eh
= (struct riscv_elf_link_hash_entry
*) h
;
1042 if (eh
->tls_type
& ~GOT_NORMAL
)
1044 s
= htab
->sdyntdata
;
1045 srel
= htab
->elf
.srelbss
;
1047 else if ((h
->root
.u
.def
.section
->flags
& SEC_READONLY
) != 0)
1049 s
= htab
->elf
.sdynrelro
;
1050 srel
= htab
->elf
.sreldynrelro
;
1054 s
= htab
->elf
.sdynbss
;
1055 srel
= htab
->elf
.srelbss
;
1057 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0 && h
->size
!= 0)
1059 srel
->size
+= sizeof (ElfNN_External_Rela
);
1063 return _bfd_elf_adjust_dynamic_copy (info
, h
, s
);
1066 /* Allocate space in .plt, .got and associated reloc sections for
1070 allocate_dynrelocs (struct elf_link_hash_entry
*h
, void *inf
)
1072 struct bfd_link_info
*info
;
1073 struct riscv_elf_link_hash_table
*htab
;
1074 struct elf_dyn_relocs
*p
;
1076 if (h
->root
.type
== bfd_link_hash_indirect
)
1079 info
= (struct bfd_link_info
*) inf
;
1080 htab
= riscv_elf_hash_table (info
);
1081 BFD_ASSERT (htab
!= NULL
);
1083 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1084 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1085 if they are defined and referenced in a non-shared object. */
1086 if (h
->type
== STT_GNU_IFUNC
1089 else if (htab
->elf
.dynamic_sections_created
1090 && h
->plt
.refcount
> 0)
1092 /* Make sure this symbol is output as a dynamic symbol.
1093 Undefined weak syms won't yet be marked as dynamic. */
1094 if (h
->dynindx
== -1
1095 && !h
->forced_local
)
1097 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1101 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info
), h
))
1103 asection
*s
= htab
->elf
.splt
;
1106 s
->size
= PLT_HEADER_SIZE
;
1108 h
->plt
.offset
= s
->size
;
1110 /* Make room for this entry. */
1111 s
->size
+= PLT_ENTRY_SIZE
;
1113 /* We also need to make an entry in the .got.plt section. */
1114 htab
->elf
.sgotplt
->size
+= GOT_ENTRY_SIZE
;
1116 /* We also need to make an entry in the .rela.plt section. */
1117 htab
->elf
.srelplt
->size
+= sizeof (ElfNN_External_Rela
);
1119 /* If this symbol is not defined in a regular file, and we are
1120 not generating a shared library, then set the symbol to this
1121 location in the .plt. This is required to make function
1122 pointers compare as equal between the normal executable and
1123 the shared library. */
1124 if (! bfd_link_pic (info
)
1127 h
->root
.u
.def
.section
= s
;
1128 h
->root
.u
.def
.value
= h
->plt
.offset
;
1133 h
->plt
.offset
= (bfd_vma
) -1;
1139 h
->plt
.offset
= (bfd_vma
) -1;
1143 if (h
->got
.refcount
> 0)
1147 int tls_type
= riscv_elf_hash_entry (h
)->tls_type
;
1149 /* Make sure this symbol is output as a dynamic symbol.
1150 Undefined weak syms won't yet be marked as dynamic. */
1151 if (h
->dynindx
== -1
1152 && !h
->forced_local
)
1154 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1159 h
->got
.offset
= s
->size
;
1160 dyn
= htab
->elf
.dynamic_sections_created
;
1161 if (tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
1163 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1164 if (tls_type
& GOT_TLS_GD
)
1166 s
->size
+= 2 * RISCV_ELF_WORD_BYTES
;
1167 htab
->elf
.srelgot
->size
+= 2 * sizeof (ElfNN_External_Rela
);
1170 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1171 if (tls_type
& GOT_TLS_IE
)
1173 s
->size
+= RISCV_ELF_WORD_BYTES
;
1174 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1179 s
->size
+= RISCV_ELF_WORD_BYTES
;
1180 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, bfd_link_pic (info
), h
)
1181 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1182 htab
->elf
.srelgot
->size
+= sizeof (ElfNN_External_Rela
);
1186 h
->got
.offset
= (bfd_vma
) -1;
1188 if (h
->dyn_relocs
== NULL
)
1191 /* In the shared -Bsymbolic case, discard space allocated for
1192 dynamic pc-relative relocs against symbols which turn out to be
1193 defined in regular objects. For the normal shared case, discard
1194 space for pc-relative relocs that have become local due to symbol
1195 visibility changes. */
1197 if (bfd_link_pic (info
))
1199 if (SYMBOL_CALLS_LOCAL (info
, h
))
1201 struct elf_dyn_relocs
**pp
;
1203 for (pp
= &h
->dyn_relocs
; (p
= *pp
) != NULL
; )
1205 p
->count
-= p
->pc_count
;
1214 /* Also discard relocs on undefined weak syms with non-default
1216 if (h
->dyn_relocs
!= NULL
1217 && h
->root
.type
== bfd_link_hash_undefweak
)
1219 if (ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
1220 || UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
1221 h
->dyn_relocs
= NULL
;
1223 /* Make sure undefined weak symbols are output as a dynamic
1225 else if (h
->dynindx
== -1
1226 && !h
->forced_local
)
1228 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1235 /* For the non-shared case, discard space for relocs against
1236 symbols which turn out to need copy relocs or are not
1242 || (htab
->elf
.dynamic_sections_created
1243 && (h
->root
.type
== bfd_link_hash_undefweak
1244 || h
->root
.type
== bfd_link_hash_undefined
))))
1246 /* Make sure this symbol is output as a dynamic symbol.
1247 Undefined weak syms won't yet be marked as dynamic. */
1248 if (h
->dynindx
== -1
1249 && !h
->forced_local
)
1251 if (! bfd_elf_link_record_dynamic_symbol (info
, h
))
1255 /* If that succeeded, we know we'll be keeping all the
1257 if (h
->dynindx
!= -1)
1261 h
->dyn_relocs
= NULL
;
1266 /* Finally, allocate space. */
1267 for (p
= h
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1269 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1270 sreloc
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1276 /* Allocate space in .plt, .got and associated reloc sections for
1277 ifunc dynamic relocs. */
1280 allocate_ifunc_dynrelocs (struct elf_link_hash_entry
*h
,
1283 struct bfd_link_info
*info
;
1285 if (h
->root
.type
== bfd_link_hash_indirect
)
1288 if (h
->root
.type
== bfd_link_hash_warning
)
1289 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1291 info
= (struct bfd_link_info
*) inf
;
1293 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1294 here if it is defined and referenced in a non-shared object. */
1295 if (h
->type
== STT_GNU_IFUNC
1297 return _bfd_elf_allocate_ifunc_dyn_relocs (info
, h
,
1306 /* Allocate space in .plt, .got and associated reloc sections for
1307 local ifunc dynamic relocs. */
1310 allocate_local_ifunc_dynrelocs (void **slot
, void *inf
)
1312 struct elf_link_hash_entry
*h
1313 = (struct elf_link_hash_entry
*) *slot
;
1315 if (h
->type
!= STT_GNU_IFUNC
1319 || h
->root
.type
!= bfd_link_hash_defined
)
1322 return allocate_ifunc_dynrelocs (h
, inf
);
1326 riscv_elf_size_dynamic_sections (bfd
*output_bfd
, struct bfd_link_info
*info
)
1328 struct riscv_elf_link_hash_table
*htab
;
1333 htab
= riscv_elf_hash_table (info
);
1334 BFD_ASSERT (htab
!= NULL
);
1335 dynobj
= htab
->elf
.dynobj
;
1336 BFD_ASSERT (dynobj
!= NULL
);
1338 if (elf_hash_table (info
)->dynamic_sections_created
)
1340 /* Set the contents of the .interp section to the interpreter. */
1341 if (bfd_link_executable (info
) && !info
->nointerp
)
1343 s
= bfd_get_linker_section (dynobj
, ".interp");
1344 BFD_ASSERT (s
!= NULL
);
1345 s
->size
= strlen (ELFNN_DYNAMIC_INTERPRETER
) + 1;
1346 s
->contents
= (unsigned char *) ELFNN_DYNAMIC_INTERPRETER
;
1350 /* Set up .got offsets for local syms, and space for local dynamic
1352 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link
.next
)
1354 bfd_signed_vma
*local_got
;
1355 bfd_signed_vma
*end_local_got
;
1356 char *local_tls_type
;
1357 bfd_size_type locsymcount
;
1358 Elf_Internal_Shdr
*symtab_hdr
;
1361 if (! is_riscv_elf (ibfd
))
1364 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1366 struct elf_dyn_relocs
*p
;
1368 for (p
= elf_section_data (s
)->local_dynrel
; p
!= NULL
; p
= p
->next
)
1370 if (!bfd_is_abs_section (p
->sec
)
1371 && bfd_is_abs_section (p
->sec
->output_section
))
1373 /* Input section has been discarded, either because
1374 it is a copy of a linkonce section or due to
1375 linker script /DISCARD/, so we'll be discarding
1378 else if (p
->count
!= 0)
1380 srel
= elf_section_data (p
->sec
)->sreloc
;
1381 srel
->size
+= p
->count
* sizeof (ElfNN_External_Rela
);
1382 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1383 info
->flags
|= DF_TEXTREL
;
1388 local_got
= elf_local_got_refcounts (ibfd
);
1392 symtab_hdr
= &elf_symtab_hdr (ibfd
);
1393 locsymcount
= symtab_hdr
->sh_info
;
1394 end_local_got
= local_got
+ locsymcount
;
1395 local_tls_type
= _bfd_riscv_elf_local_got_tls_type (ibfd
);
1397 srel
= htab
->elf
.srelgot
;
1398 for (; local_got
< end_local_got
; ++local_got
, ++local_tls_type
)
1402 *local_got
= s
->size
;
1403 s
->size
+= RISCV_ELF_WORD_BYTES
;
1404 if (*local_tls_type
& GOT_TLS_GD
)
1405 s
->size
+= RISCV_ELF_WORD_BYTES
;
1406 if (bfd_link_pic (info
)
1407 || (*local_tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
)))
1408 srel
->size
+= sizeof (ElfNN_External_Rela
);
1411 *local_got
= (bfd_vma
) -1;
1415 /* Allocate .plt and .got entries and space dynamic relocs for
1417 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, info
);
1419 /* Allocate .plt and .got entries and space dynamic relocs for
1420 global ifunc symbols. */
1421 elf_link_hash_traverse (&htab
->elf
, allocate_ifunc_dynrelocs
, info
);
1423 /* Allocate .plt and .got entries and space dynamic relocs for
1424 local ifunc symbols. */
1425 htab_traverse (htab
->loc_hash_table
, allocate_local_ifunc_dynrelocs
, info
);
1427 if (htab
->elf
.sgotplt
)
1429 struct elf_link_hash_entry
*got
;
1430 got
= elf_link_hash_lookup (elf_hash_table (info
),
1431 "_GLOBAL_OFFSET_TABLE_",
1432 FALSE
, FALSE
, FALSE
);
1434 /* Don't allocate .got.plt section if there are no GOT nor PLT
1435 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1437 || !got
->ref_regular_nonweak
)
1438 && (htab
->elf
.sgotplt
->size
== GOTPLT_HEADER_SIZE
)
1439 && (htab
->elf
.splt
== NULL
1440 || htab
->elf
.splt
->size
== 0)
1441 && (htab
->elf
.sgot
== NULL
1442 || (htab
->elf
.sgot
->size
1443 == get_elf_backend_data (output_bfd
)->got_header_size
)))
1444 htab
->elf
.sgotplt
->size
= 0;
1447 /* The check_relocs and adjust_dynamic_symbol entry points have
1448 determined the sizes of the various dynamic sections. Allocate
1450 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1452 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1455 if (s
== htab
->elf
.splt
1456 || s
== htab
->elf
.sgot
1457 || s
== htab
->elf
.sgotplt
1458 || s
== htab
->elf
.iplt
1459 || s
== htab
->elf
.igotplt
1460 || s
== htab
->elf
.sdynbss
1461 || s
== htab
->elf
.sdynrelro
1462 || s
== htab
->sdyntdata
)
1464 /* Strip this section if we don't need it; see the
1467 else if (strncmp (s
->name
, ".rela", 5) == 0)
1471 /* We use the reloc_count field as a counter if we need
1472 to copy relocs into the output file. */
1478 /* It's not one of our sections. */
1484 /* If we don't need this section, strip it from the
1485 output file. This is mostly to handle .rela.bss and
1486 .rela.plt. We must create both sections in
1487 create_dynamic_sections, because they must be created
1488 before the linker maps input sections to output
1489 sections. The linker does that before
1490 adjust_dynamic_symbol is called, and it is that
1491 function which decides whether anything needs to go
1492 into these sections. */
1493 s
->flags
|= SEC_EXCLUDE
;
1497 if ((s
->flags
& SEC_HAS_CONTENTS
) == 0)
1500 /* Allocate memory for the section contents. Zero the memory
1501 for the benefit of .rela.plt, which has 4 unused entries
1502 at the beginning, and we don't want garbage. */
1503 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->size
);
1504 if (s
->contents
== NULL
)
1508 return _bfd_elf_add_dynamic_tags (output_bfd
, info
, TRUE
);
1512 #define DTP_OFFSET 0x800
1514 /* Return the relocation value for a TLS dtp-relative reloc. */
1517 dtpoff (struct bfd_link_info
*info
, bfd_vma address
)
1519 /* If tls_sec is NULL, we should have signalled an error already. */
1520 if (elf_hash_table (info
)->tls_sec
== NULL
)
1522 return address
- elf_hash_table (info
)->tls_sec
->vma
- DTP_OFFSET
;
1525 /* Return the relocation value for a static TLS tp-relative relocation. */
1528 tpoff (struct bfd_link_info
*info
, bfd_vma address
)
1530 /* If tls_sec is NULL, we should have signalled an error already. */
1531 if (elf_hash_table (info
)->tls_sec
== NULL
)
1533 return address
- elf_hash_table (info
)->tls_sec
->vma
- TP_OFFSET
;
1536 /* Return the global pointer's value, or 0 if it is not in use. */
1539 riscv_global_pointer_value (struct bfd_link_info
*info
)
1541 struct bfd_link_hash_entry
*h
;
1543 h
= bfd_link_hash_lookup (info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
, TRUE
);
1544 if (h
== NULL
|| h
->type
!= bfd_link_hash_defined
)
1547 return h
->u
.def
.value
+ sec_addr (h
->u
.def
.section
);
1550 /* Emplace a static relocation. */
1552 static bfd_reloc_status_type
1553 perform_relocation (const reloc_howto_type
*howto
,
1554 const Elf_Internal_Rela
*rel
,
1556 asection
*input_section
,
1560 if (howto
->pc_relative
)
1561 value
-= sec_addr (input_section
) + rel
->r_offset
;
1562 value
+= rel
->r_addend
;
1564 switch (ELFNN_R_TYPE (rel
->r_info
))
1567 case R_RISCV_TPREL_HI20
:
1568 case R_RISCV_PCREL_HI20
:
1569 case R_RISCV_GOT_HI20
:
1570 case R_RISCV_TLS_GOT_HI20
:
1571 case R_RISCV_TLS_GD_HI20
:
1572 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1573 return bfd_reloc_overflow
;
1574 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
));
1577 case R_RISCV_LO12_I
:
1578 case R_RISCV_GPREL_I
:
1579 case R_RISCV_TPREL_LO12_I
:
1580 case R_RISCV_TPREL_I
:
1581 case R_RISCV_PCREL_LO12_I
:
1582 value
= ENCODE_ITYPE_IMM (value
);
1585 case R_RISCV_LO12_S
:
1586 case R_RISCV_GPREL_S
:
1587 case R_RISCV_TPREL_LO12_S
:
1588 case R_RISCV_TPREL_S
:
1589 case R_RISCV_PCREL_LO12_S
:
1590 value
= ENCODE_STYPE_IMM (value
);
1594 case R_RISCV_CALL_PLT
:
1595 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
)))
1596 return bfd_reloc_overflow
;
1597 value
= ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value
))
1598 | (ENCODE_ITYPE_IMM (value
) << 32);
1602 if (!VALID_UJTYPE_IMM (value
))
1603 return bfd_reloc_overflow
;
1604 value
= ENCODE_UJTYPE_IMM (value
);
1607 case R_RISCV_BRANCH
:
1608 if (!VALID_SBTYPE_IMM (value
))
1609 return bfd_reloc_overflow
;
1610 value
= ENCODE_SBTYPE_IMM (value
);
1613 case R_RISCV_RVC_BRANCH
:
1614 if (!VALID_RVC_B_IMM (value
))
1615 return bfd_reloc_overflow
;
1616 value
= ENCODE_RVC_B_IMM (value
);
1619 case R_RISCV_RVC_JUMP
:
1620 if (!VALID_RVC_J_IMM (value
))
1621 return bfd_reloc_overflow
;
1622 value
= ENCODE_RVC_J_IMM (value
);
1625 case R_RISCV_RVC_LUI
:
1626 if (RISCV_CONST_HIGH_PART (value
) == 0)
1628 /* Linker relaxation can convert an address equal to or greater than
1629 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1630 valid immediate. We can fix this by converting it to a C.LI. */
1631 bfd_vma insn
= bfd_get (howto
->bitsize
, input_bfd
,
1632 contents
+ rel
->r_offset
);
1633 insn
= (insn
& ~MATCH_C_LUI
) | MATCH_C_LI
;
1634 bfd_put (howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1635 value
= ENCODE_RVC_IMM (0);
1637 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
)))
1638 return bfd_reloc_overflow
;
1640 value
= ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value
));
1658 case R_RISCV_32_PCREL
:
1659 case R_RISCV_TLS_DTPREL32
:
1660 case R_RISCV_TLS_DTPREL64
:
1663 case R_RISCV_DELETE
:
1664 return bfd_reloc_ok
;
1667 return bfd_reloc_notsupported
;
1670 bfd_vma word
= bfd_get (howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1671 word
= (word
& ~howto
->dst_mask
) | (value
& howto
->dst_mask
);
1672 bfd_put (howto
->bitsize
, input_bfd
, word
, contents
+ rel
->r_offset
);
1674 return bfd_reloc_ok
;
1677 /* Remember all PC-relative high-part relocs we've encountered to help us
1678 later resolve the corresponding low-part relocs. */
1684 } riscv_pcrel_hi_reloc
;
1686 typedef struct riscv_pcrel_lo_reloc
1688 asection
* input_section
;
1689 struct bfd_link_info
* info
;
1690 reloc_howto_type
* howto
;
1691 const Elf_Internal_Rela
* reloc
;
1694 bfd_byte
* contents
;
1695 struct riscv_pcrel_lo_reloc
* next
;
1696 } riscv_pcrel_lo_reloc
;
1701 riscv_pcrel_lo_reloc
*lo_relocs
;
1702 } riscv_pcrel_relocs
;
1705 riscv_pcrel_reloc_hash (const void *entry
)
1707 const riscv_pcrel_hi_reloc
*e
= entry
;
1708 return (hashval_t
)(e
->address
>> 2);
1712 riscv_pcrel_reloc_eq (const void *entry1
, const void *entry2
)
1714 const riscv_pcrel_hi_reloc
*e1
= entry1
, *e2
= entry2
;
1715 return e1
->address
== e2
->address
;
1719 riscv_init_pcrel_relocs (riscv_pcrel_relocs
*p
)
1722 p
->lo_relocs
= NULL
;
1723 p
->hi_relocs
= htab_create (1024, riscv_pcrel_reloc_hash
,
1724 riscv_pcrel_reloc_eq
, free
);
1725 return p
->hi_relocs
!= NULL
;
1729 riscv_free_pcrel_relocs (riscv_pcrel_relocs
*p
)
1731 riscv_pcrel_lo_reloc
*cur
= p
->lo_relocs
;
1735 riscv_pcrel_lo_reloc
*next
= cur
->next
;
1740 htab_delete (p
->hi_relocs
);
1744 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela
*rel
,
1745 struct bfd_link_info
*info
,
1749 const reloc_howto_type
*howto
,
1752 /* We may need to reference low addreses in PC-relative modes even when the
1753 * PC is far away from these addresses. For example, undefweak references
1754 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1755 * addresses that we can link PC-relative programs at, the linker can't
1756 * actually relocate references to those symbols. In order to allow these
1757 * programs to work we simply convert the PC-relative auipc sequences to
1758 * 0-relative lui sequences. */
1759 if (bfd_link_pic (info
))
1762 /* If it's possible to reference the symbol using auipc we do so, as that's
1763 * more in the spirit of the PC-relative relocations we're processing. */
1764 bfd_vma offset
= addr
- pc
;
1765 if (ARCH_SIZE
== 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset
)))
1768 /* If it's impossible to reference this with a LUI-based offset then don't
1769 * bother to convert it at all so users still see the PC-relative relocation
1770 * in the truncation message. */
1771 if (ARCH_SIZE
> 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr
)))
1774 rel
->r_info
= ELFNN_R_INFO(addr
, R_RISCV_HI20
);
1776 bfd_vma insn
= bfd_get(howto
->bitsize
, input_bfd
, contents
+ rel
->r_offset
);
1777 insn
= (insn
& ~MASK_AUIPC
) | MATCH_LUI
;
1778 bfd_put(howto
->bitsize
, input_bfd
, insn
, contents
+ rel
->r_offset
);
1783 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs
*p
, bfd_vma addr
,
1784 bfd_vma value
, bfd_boolean absolute
)
1786 bfd_vma offset
= absolute
? value
: value
- addr
;
1787 riscv_pcrel_hi_reloc entry
= {addr
, offset
};
1788 riscv_pcrel_hi_reloc
**slot
=
1789 (riscv_pcrel_hi_reloc
**) htab_find_slot (p
->hi_relocs
, &entry
, INSERT
);
1791 BFD_ASSERT (*slot
== NULL
);
1792 *slot
= (riscv_pcrel_hi_reloc
*) bfd_malloc (sizeof (riscv_pcrel_hi_reloc
));
1800 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs
*p
,
1801 asection
*input_section
,
1802 struct bfd_link_info
*info
,
1803 reloc_howto_type
*howto
,
1804 const Elf_Internal_Rela
*reloc
,
1809 riscv_pcrel_lo_reloc
*entry
;
1810 entry
= (riscv_pcrel_lo_reloc
*) bfd_malloc (sizeof (riscv_pcrel_lo_reloc
));
1813 *entry
= (riscv_pcrel_lo_reloc
) {input_section
, info
, howto
, reloc
, addr
,
1814 name
, contents
, p
->lo_relocs
};
1815 p
->lo_relocs
= entry
;
1820 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs
*p
)
1822 riscv_pcrel_lo_reloc
*r
;
1824 for (r
= p
->lo_relocs
; r
!= NULL
; r
= r
->next
)
1826 bfd
*input_bfd
= r
->input_section
->owner
;
1828 riscv_pcrel_hi_reloc search
= {r
->addr
, 0};
1829 riscv_pcrel_hi_reloc
*entry
= htab_find (p
->hi_relocs
, &search
);
1831 /* Check for overflow into bit 11 when adding reloc addend. */
1832 || (! (entry
->value
& 0x800)
1833 && ((entry
->value
+ r
->reloc
->r_addend
) & 0x800)))
1835 char *string
= (entry
== NULL
1836 ? "%pcrel_lo missing matching %pcrel_hi"
1837 : "%pcrel_lo overflow with an addend");
1838 (*r
->info
->callbacks
->reloc_dangerous
)
1839 (r
->info
, string
, input_bfd
, r
->input_section
, r
->reloc
->r_offset
);
1843 perform_relocation (r
->howto
, r
->reloc
, entry
->value
, r
->input_section
,
1844 input_bfd
, r
->contents
);
1850 /* Relocate a RISC-V ELF section.
1852 The RELOCATE_SECTION function is called by the new ELF backend linker
1853 to handle the relocations for a section.
1855 The relocs are always passed as Rela structures.
1857 This function is responsible for adjusting the section contents as
1858 necessary, and (if generating a relocatable output file) adjusting
1859 the reloc addend as necessary.
1861 This function does not have to worry about setting the reloc
1862 address or the reloc symbol index.
1864 LOCAL_SYMS is a pointer to the swapped in local symbols.
1866 LOCAL_SECTIONS is an array giving the section in the input file
1867 corresponding to the st_shndx field of each local symbol.
1869 The global hash table entry for the global symbols can be found
1870 via elf_sym_hashes (input_bfd).
1872 When generating relocatable output, this function must handle
1873 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1874 going to be the section symbol corresponding to the output
1875 section, which means that the addend must be adjusted
1879 riscv_elf_relocate_section (bfd
*output_bfd
,
1880 struct bfd_link_info
*info
,
1882 asection
*input_section
,
1884 Elf_Internal_Rela
*relocs
,
1885 Elf_Internal_Sym
*local_syms
,
1886 asection
**local_sections
)
1888 Elf_Internal_Rela
*rel
;
1889 Elf_Internal_Rela
*relend
;
1890 riscv_pcrel_relocs pcrel_relocs
;
1891 bfd_boolean ret
= FALSE
;
1892 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
1893 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (input_bfd
);
1894 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (input_bfd
);
1895 bfd_vma
*local_got_offsets
= elf_local_got_offsets (input_bfd
);
1896 bfd_boolean absolute
;
1898 if (!riscv_init_pcrel_relocs (&pcrel_relocs
))
1901 relend
= relocs
+ input_section
->reloc_count
;
1902 for (rel
= relocs
; rel
< relend
; rel
++)
1904 unsigned long r_symndx
;
1905 struct elf_link_hash_entry
*h
;
1906 Elf_Internal_Sym
*sym
;
1909 bfd_reloc_status_type r
= bfd_reloc_ok
;
1910 const char *name
= NULL
;
1911 bfd_vma off
, ie_off
;
1912 bfd_boolean unresolved_reloc
, is_ie
= FALSE
;
1913 bfd_vma pc
= sec_addr (input_section
) + rel
->r_offset
;
1914 int r_type
= ELFNN_R_TYPE (rel
->r_info
), tls_type
;
1915 reloc_howto_type
*howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
1916 const char *msg
= NULL
;
1917 char *msg_buf
= NULL
;
1918 bfd_boolean resolved_to_zero
;
1921 || r_type
== R_RISCV_GNU_VTINHERIT
|| r_type
== R_RISCV_GNU_VTENTRY
)
1924 /* This is a final link. */
1925 r_symndx
= ELFNN_R_SYM (rel
->r_info
);
1929 unresolved_reloc
= FALSE
;
1930 if (r_symndx
< symtab_hdr
->sh_info
)
1932 sym
= local_syms
+ r_symndx
;
1933 sec
= local_sections
[r_symndx
];
1934 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1936 /* Relocate against local STT_GNU_IFUNC symbol. */
1937 if (!bfd_link_relocatable (info
)
1938 && ELF_ST_TYPE (sym
->st_info
) == STT_GNU_IFUNC
)
1940 h
= riscv_elf_get_local_sym_hash (htab
, input_bfd
, rel
, FALSE
);
1944 /* Set STT_GNU_IFUNC symbol value. */
1945 h
->root
.u
.def
.value
= sym
->st_value
;
1946 h
->root
.u
.def
.section
= sec
;
1951 bfd_boolean warned
, ignored
;
1953 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1954 r_symndx
, symtab_hdr
, sym_hashes
,
1956 unresolved_reloc
, warned
, ignored
);
1959 /* To avoid generating warning messages about truncated
1960 relocations, set the relocation's address to be the same as
1961 the start of this section. */
1962 if (input_section
->output_section
!= NULL
)
1963 relocation
= input_section
->output_section
->vma
;
1969 if (sec
!= NULL
&& discarded_section (sec
))
1970 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1971 rel
, 1, relend
, howto
, 0, contents
);
1973 if (bfd_link_relocatable (info
))
1976 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1977 it here if it is defined in a non-shared object. */
1979 && h
->type
== STT_GNU_IFUNC
1982 asection
*plt
, *base_got
;
1984 if ((input_section
->flags
& SEC_ALLOC
) == 0)
1986 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
1987 STT_GNU_IFUNC symbol as STT_FUNC. */
1988 if (elf_section_type (input_section
) == SHT_NOTE
)
1991 /* Dynamic relocs are not propagated for SEC_DEBUGGING
1992 sections because such sections are not SEC_ALLOC and
1993 thus ld.so will not process them. */
1994 if ((input_section
->flags
& SEC_DEBUGGING
) != 0)
1999 else if (h
->plt
.offset
== (bfd_vma
) -1
2000 /* The following relocation may not need the .plt entries
2001 when all references to a STT_GNU_IFUNC symbols are done
2002 via GOT or static function pointers. */
2003 && r_type
!= R_RISCV_32
2004 && r_type
!= R_RISCV_64
2005 && r_type
!= R_RISCV_HI20
2006 && r_type
!= R_RISCV_GOT_HI20
2007 && r_type
!= R_RISCV_LO12_I
2008 && r_type
!= R_RISCV_LO12_S
)
2009 goto bad_ifunc_reloc
;
2011 /* STT_GNU_IFUNC symbol must go through PLT. */
2012 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2013 relocation
= plt
->output_section
->vma
2014 + plt
->output_offset
2021 if (rel
->r_addend
!= 0)
2023 if (h
->root
.root
.string
)
2024 name
= h
->root
.root
.string
;
2026 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2029 /* xgettext:c-format */
2030 (_("%pB: relocation %s against STT_GNU_IFUNC "
2031 "symbol `%s' has non-zero addend: %" PRId64
),
2032 input_bfd
, howto
->name
, name
, (int64_t) rel
->r_addend
);
2033 bfd_set_error (bfd_error_bad_value
);
2037 /* Generate dynamic relocation only when there is a non-GOT
2038 reference in a shared object or there is no PLT. */
2039 if ((bfd_link_pic (info
) && h
->non_got_ref
)
2040 || h
->plt
.offset
== (bfd_vma
) -1)
2042 Elf_Internal_Rela outrel
;
2045 /* Need a dynamic relocation to get the real function
2047 outrel
.r_offset
= _bfd_elf_section_offset (output_bfd
,
2051 if (outrel
.r_offset
== (bfd_vma
) -1
2052 || outrel
.r_offset
== (bfd_vma
) -2)
2055 outrel
.r_offset
+= input_section
->output_section
->vma
2056 + input_section
->output_offset
;
2058 if (h
->dynindx
== -1
2060 || bfd_link_executable (info
))
2062 info
->callbacks
->minfo
2063 (_("Local IFUNC function `%s' in %pB\n"),
2064 h
->root
.root
.string
,
2065 h
->root
.u
.def
.section
->owner
);
2067 /* This symbol is resolved locally. */
2068 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2069 outrel
.r_addend
= h
->root
.u
.def
.value
2070 + h
->root
.u
.def
.section
->output_section
->vma
2071 + h
->root
.u
.def
.section
->output_offset
;
2075 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2076 outrel
.r_addend
= 0;
2079 /* Dynamic relocations are stored in
2080 1. .rela.ifunc section in PIC object.
2081 2. .rela.got section in dynamic executable.
2082 3. .rela.iplt section in static executable. */
2083 if (bfd_link_pic (info
))
2084 sreloc
= htab
->elf
.irelifunc
;
2085 else if (htab
->elf
.splt
!= NULL
)
2086 sreloc
= htab
->elf
.srelgot
;
2088 sreloc
= htab
->elf
.irelplt
;
2090 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2092 /* If this reloc is against an external symbol, we
2093 do not want to fiddle with the addend. Otherwise,
2094 we need to include the symbol value so that it
2095 becomes an addend for the dynamic reloc. For an
2096 internal symbol, we have updated addend. */
2101 case R_RISCV_GOT_HI20
:
2102 base_got
= htab
->elf
.sgot
;
2103 off
= h
->got
.offset
;
2105 if (base_got
== NULL
)
2108 if (off
== (bfd_vma
) -1)
2112 /* We can't use h->got.offset here to save state, or
2113 even just remember the offset, as finish_dynamic_symbol
2114 would use that as offset into .got. */
2116 if (htab
->elf
.splt
!= NULL
)
2118 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
)
2120 off
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2121 base_got
= htab
->elf
.sgotplt
;
2125 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2126 off
= plt_idx
* GOT_ENTRY_SIZE
;
2127 base_got
= htab
->elf
.igotplt
;
2130 if (h
->dynindx
== -1
2134 /* This references the local definition. We must
2135 initialize this entry in the global offset table.
2136 Since the offset must always be a multiple of 8,
2137 we use the least significant bit to record
2138 whether we have initialized it already.
2140 When doing a dynamic link, we create a .rela.got
2141 relocation entry to initialize the value. This
2142 is done in the finish_dynamic_symbol routine. */
2147 bfd_put_NN (output_bfd
, relocation
,
2148 base_got
->contents
+ off
);
2149 /* Note that this is harmless for the case,
2150 as -1 | 1 still is -1. */
2156 relocation
= base_got
->output_section
->vma
2157 + base_got
->output_offset
+ off
;
2159 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2160 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2162 r
= bfd_reloc_notsupported
;
2163 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2165 r
= bfd_reloc_overflow
;
2169 case R_RISCV_CALL_PLT
:
2171 case R_RISCV_LO12_I
:
2172 case R_RISCV_LO12_S
:
2175 case R_RISCV_PCREL_HI20
:
2176 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2177 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2179 r
= bfd_reloc_notsupported
;
2180 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2182 r
= bfd_reloc_overflow
;
2187 if (h
->root
.root
.string
)
2188 name
= h
->root
.root
.string
;
2190 /* The entry of local ifunc is fake in global hash table,
2191 we should find the name by the original local symbol. */
2192 name
= bfd_elf_sym_name (input_bfd
, symtab_hdr
, sym
, NULL
);
2195 /* xgettext:c-format */
2196 (_("%pB: relocation %s against STT_GNU_IFUNC "
2197 "symbol `%s' isn't supported"), input_bfd
,
2199 bfd_set_error (bfd_error_bad_value
);
2206 name
= h
->root
.root
.string
;
2209 name
= (bfd_elf_string_from_elf_section
2210 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
2211 if (name
== NULL
|| *name
== '\0')
2212 name
= bfd_section_name (sec
);
2215 resolved_to_zero
= (h
!= NULL
2216 && UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
));
2222 case R_RISCV_TPREL_ADD
:
2224 case R_RISCV_JUMP_SLOT
:
2225 case R_RISCV_RELATIVE
:
2226 /* These require nothing of us at all. */
2230 case R_RISCV_BRANCH
:
2231 case R_RISCV_RVC_BRANCH
:
2232 case R_RISCV_RVC_LUI
:
2233 case R_RISCV_LO12_I
:
2234 case R_RISCV_LO12_S
:
2239 case R_RISCV_32_PCREL
:
2240 case R_RISCV_DELETE
:
2241 /* These require no special handling beyond perform_relocation. */
2244 case R_RISCV_GOT_HI20
:
2247 bfd_boolean dyn
, pic
;
2249 off
= h
->got
.offset
;
2250 BFD_ASSERT (off
!= (bfd_vma
) -1);
2251 dyn
= elf_hash_table (info
)->dynamic_sections_created
;
2252 pic
= bfd_link_pic (info
);
2254 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2255 || (pic
&& SYMBOL_REFERENCES_LOCAL (info
, h
)))
2257 /* This is actually a static link, or it is a
2258 -Bsymbolic link and the symbol is defined
2259 locally, or the symbol was forced to be local
2260 because of a version file. We must initialize
2261 this entry in the global offset table. Since the
2262 offset must always be a multiple of the word size,
2263 we use the least significant bit to record whether
2264 we have initialized it already.
2266 When doing a dynamic link, we create a .rela.got
2267 relocation entry to initialize the value. This
2268 is done in the finish_dynamic_symbol routine. */
2273 bfd_put_NN (output_bfd
, relocation
,
2274 htab
->elf
.sgot
->contents
+ off
);
2279 unresolved_reloc
= FALSE
;
2283 BFD_ASSERT (local_got_offsets
!= NULL
2284 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
2286 off
= local_got_offsets
[r_symndx
];
2288 /* The offset must always be a multiple of the word size.
2289 So, we can use the least significant bit to record
2290 whether we have already processed this entry. */
2295 if (bfd_link_pic (info
))
2298 Elf_Internal_Rela outrel
;
2300 /* We need to generate a R_RISCV_RELATIVE reloc
2301 for the dynamic linker. */
2302 s
= htab
->elf
.srelgot
;
2303 BFD_ASSERT (s
!= NULL
);
2305 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2307 ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2308 outrel
.r_addend
= relocation
;
2310 riscv_elf_append_rela (output_bfd
, s
, &outrel
);
2313 bfd_put_NN (output_bfd
, relocation
,
2314 htab
->elf
.sgot
->contents
+ off
);
2315 local_got_offsets
[r_symndx
] |= 1;
2318 relocation
= sec_addr (htab
->elf
.sgot
) + off
;
2319 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2326 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2327 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2329 r
= bfd_reloc_notsupported
;
2330 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2331 relocation
, absolute
))
2332 r
= bfd_reloc_overflow
;
2340 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2341 contents
+ rel
->r_offset
);
2342 relocation
= old_value
+ relocation
;
2352 bfd_vma old_value
= bfd_get (howto
->bitsize
, input_bfd
,
2353 contents
+ rel
->r_offset
);
2354 relocation
= old_value
- relocation
;
2359 case R_RISCV_CALL_PLT
:
2360 /* Handle a call to an undefined weak function. This won't be
2361 relaxed, so we have to handle it here. */
2362 if (h
!= NULL
&& h
->root
.type
== bfd_link_hash_undefweak
2363 && (!bfd_link_pic (info
) || h
->plt
.offset
== MINUS_ONE
))
2365 /* We can use x0 as the base register. */
2366 bfd_vma insn
= bfd_get_32 (input_bfd
,
2367 contents
+ rel
->r_offset
+ 4);
2368 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2369 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
+ 4);
2370 /* Set the relocation value so that we get 0 after the pc
2371 relative adjustment. */
2372 relocation
= sec_addr (input_section
) + rel
->r_offset
;
2377 case R_RISCV_RVC_JUMP
:
2378 /* This line has to match the check in _bfd_riscv_relax_section. */
2379 if (bfd_link_pic (info
) && h
!= NULL
&& h
->plt
.offset
!= MINUS_ONE
)
2381 /* Refer to the PLT entry. */
2382 relocation
= sec_addr (htab
->elf
.splt
) + h
->plt
.offset
;
2383 unresolved_reloc
= FALSE
;
2387 case R_RISCV_TPREL_HI20
:
2388 relocation
= tpoff (info
, relocation
);
2391 case R_RISCV_TPREL_LO12_I
:
2392 case R_RISCV_TPREL_LO12_S
:
2393 relocation
= tpoff (info
, relocation
);
2396 case R_RISCV_TPREL_I
:
2397 case R_RISCV_TPREL_S
:
2398 relocation
= tpoff (info
, relocation
);
2399 if (VALID_ITYPE_IMM (relocation
+ rel
->r_addend
))
2401 /* We can use tp as the base register. */
2402 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2403 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2404 insn
|= X_TP
<< OP_SH_RS1
;
2405 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2408 r
= bfd_reloc_overflow
;
2411 case R_RISCV_GPREL_I
:
2412 case R_RISCV_GPREL_S
:
2414 bfd_vma gp
= riscv_global_pointer_value (info
);
2415 bfd_boolean x0_base
= VALID_ITYPE_IMM (relocation
+ rel
->r_addend
);
2416 if (x0_base
|| VALID_ITYPE_IMM (relocation
+ rel
->r_addend
- gp
))
2418 /* We can use x0 or gp as the base register. */
2419 bfd_vma insn
= bfd_get_32 (input_bfd
, contents
+ rel
->r_offset
);
2420 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
2423 rel
->r_addend
-= gp
;
2424 insn
|= X_GP
<< OP_SH_RS1
;
2426 bfd_put_32 (input_bfd
, insn
, contents
+ rel
->r_offset
);
2429 r
= bfd_reloc_overflow
;
2433 case R_RISCV_PCREL_HI20
:
2434 absolute
= riscv_zero_pcrel_hi_reloc (rel
,
2441 r_type
= ELFNN_R_TYPE (rel
->r_info
);
2442 howto
= riscv_elf_rtype_to_howto (input_bfd
, r_type
);
2444 r
= bfd_reloc_notsupported
;
2445 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2446 relocation
+ rel
->r_addend
,
2448 r
= bfd_reloc_overflow
;
2451 case R_RISCV_PCREL_LO12_I
:
2452 case R_RISCV_PCREL_LO12_S
:
2453 /* We don't allow section symbols plus addends as the auipc address,
2454 because then riscv_relax_delete_bytes would have to search through
2455 all relocs to update these addends. This is also ambiguous, as
2456 we do allow offsets to be added to the target address, which are
2457 not to be used to find the auipc address. */
2458 if (((sym
!= NULL
&& (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
))
2459 || (h
!= NULL
&& h
->type
== STT_SECTION
))
2462 msg
= _("%pcrel_lo section symbol with an addend");
2463 r
= bfd_reloc_dangerous
;
2467 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs
, input_section
, info
,
2468 howto
, rel
, relocation
, name
,
2471 r
= bfd_reloc_overflow
;
2474 case R_RISCV_TLS_DTPREL32
:
2475 case R_RISCV_TLS_DTPREL64
:
2476 relocation
= dtpoff (info
, relocation
);
2481 if ((input_section
->flags
& SEC_ALLOC
) == 0)
2484 if ((bfd_link_pic (info
)
2486 || (ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2487 && !resolved_to_zero
)
2488 || h
->root
.type
!= bfd_link_hash_undefweak
)
2489 && (! howto
->pc_relative
2490 || !SYMBOL_CALLS_LOCAL (info
, h
)))
2491 || (!bfd_link_pic (info
)
2497 || h
->root
.type
== bfd_link_hash_undefweak
2498 || h
->root
.type
== bfd_link_hash_undefined
)))
2500 Elf_Internal_Rela outrel
;
2502 bfd_boolean skip_static_relocation
, skip_dynamic_relocation
;
2504 /* When generating a shared object, these relocations
2505 are copied into the output file to be resolved at run
2509 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2511 skip_static_relocation
= outrel
.r_offset
!= (bfd_vma
) -2;
2512 skip_dynamic_relocation
= outrel
.r_offset
>= (bfd_vma
) -2;
2513 outrel
.r_offset
+= sec_addr (input_section
);
2515 if (skip_dynamic_relocation
)
2516 memset (&outrel
, 0, sizeof outrel
);
2517 else if (h
!= NULL
&& h
->dynindx
!= -1
2518 && !(bfd_link_pic (info
)
2519 && SYMBOLIC_BIND (info
, h
)
2522 outrel
.r_info
= ELFNN_R_INFO (h
->dynindx
, r_type
);
2523 outrel
.r_addend
= rel
->r_addend
;
2527 outrel
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2528 outrel
.r_addend
= relocation
+ rel
->r_addend
;
2531 sreloc
= elf_section_data (input_section
)->sreloc
;
2532 riscv_elf_append_rela (output_bfd
, sreloc
, &outrel
);
2533 if (skip_static_relocation
)
2538 case R_RISCV_TLS_GOT_HI20
:
2542 case R_RISCV_TLS_GD_HI20
:
2545 off
= h
->got
.offset
;
2550 off
= local_got_offsets
[r_symndx
];
2551 local_got_offsets
[r_symndx
] |= 1;
2554 tls_type
= _bfd_riscv_elf_tls_type (input_bfd
, h
, r_symndx
);
2555 BFD_ASSERT (tls_type
& (GOT_TLS_IE
| GOT_TLS_GD
));
2556 /* If this symbol is referenced by both GD and IE TLS, the IE
2557 reference's GOT slot follows the GD reference's slots. */
2559 if ((tls_type
& GOT_TLS_GD
) && (tls_type
& GOT_TLS_IE
))
2560 ie_off
= 2 * GOT_ENTRY_SIZE
;
2566 Elf_Internal_Rela outrel
;
2568 bfd_boolean need_relocs
= FALSE
;
2570 if (htab
->elf
.srelgot
== NULL
)
2575 bfd_boolean dyn
, pic
;
2576 dyn
= htab
->elf
.dynamic_sections_created
;
2577 pic
= bfd_link_pic (info
);
2579 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, pic
, h
)
2580 && (!pic
|| !SYMBOL_REFERENCES_LOCAL (info
, h
)))
2584 /* The GOT entries have not been initialized yet. Do it
2585 now, and emit any relocations. */
2586 if ((bfd_link_pic (info
) || indx
!= 0)
2588 || ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
2589 || h
->root
.type
!= bfd_link_hash_undefweak
))
2592 if (tls_type
& GOT_TLS_GD
)
2596 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
) + off
;
2597 outrel
.r_addend
= 0;
2598 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPMODNN
);
2599 bfd_put_NN (output_bfd
, 0,
2600 htab
->elf
.sgot
->contents
+ off
);
2601 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2604 BFD_ASSERT (! unresolved_reloc
);
2605 bfd_put_NN (output_bfd
,
2606 dtpoff (info
, relocation
),
2607 (htab
->elf
.sgot
->contents
+ off
+
2608 RISCV_ELF_WORD_BYTES
));
2612 bfd_put_NN (output_bfd
, 0,
2613 (htab
->elf
.sgot
->contents
+ off
+
2614 RISCV_ELF_WORD_BYTES
));
2615 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_DTPRELNN
);
2616 outrel
.r_offset
+= RISCV_ELF_WORD_BYTES
;
2617 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2622 /* If we are not emitting relocations for a
2623 general dynamic reference, then we must be in a
2624 static link or an executable link with the
2625 symbol binding locally. Mark it as belonging
2626 to module 1, the executable. */
2627 bfd_put_NN (output_bfd
, 1,
2628 htab
->elf
.sgot
->contents
+ off
);
2629 bfd_put_NN (output_bfd
,
2630 dtpoff (info
, relocation
),
2631 (htab
->elf
.sgot
->contents
+ off
+
2632 RISCV_ELF_WORD_BYTES
));
2636 if (tls_type
& GOT_TLS_IE
)
2640 bfd_put_NN (output_bfd
, 0,
2641 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2642 outrel
.r_offset
= sec_addr (htab
->elf
.sgot
)
2644 outrel
.r_addend
= 0;
2646 outrel
.r_addend
= tpoff (info
, relocation
);
2647 outrel
.r_info
= ELFNN_R_INFO (indx
, R_RISCV_TLS_TPRELNN
);
2648 riscv_elf_append_rela (output_bfd
, htab
->elf
.srelgot
, &outrel
);
2652 bfd_put_NN (output_bfd
, tpoff (info
, relocation
),
2653 htab
->elf
.sgot
->contents
+ off
+ ie_off
);
2658 BFD_ASSERT (off
< (bfd_vma
) -2);
2659 relocation
= sec_addr (htab
->elf
.sgot
) + off
+ (is_ie
? ie_off
: 0);
2660 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs
, pc
,
2662 r
= bfd_reloc_overflow
;
2663 unresolved_reloc
= FALSE
;
2667 r
= bfd_reloc_notsupported
;
2670 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2671 because such sections are not SEC_ALLOC and thus ld.so will
2672 not process them. */
2673 if (unresolved_reloc
2674 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
2676 && _bfd_elf_section_offset (output_bfd
, info
, input_section
,
2677 rel
->r_offset
) != (bfd_vma
) -1)
2682 case R_RISCV_RVC_JUMP
:
2683 if (asprintf (&msg_buf
,
2684 _("%%X%%P: relocation %s against `%s' can "
2685 "not be used when making a shared object; "
2686 "recompile with -fPIC\n"),
2688 h
->root
.root
.string
) == -1)
2693 if (asprintf (&msg_buf
,
2694 _("%%X%%P: unresolvable %s relocation against "
2697 h
->root
.root
.string
) == -1)
2703 r
= bfd_reloc_notsupported
;
2707 if (r
== bfd_reloc_ok
)
2708 r
= perform_relocation (howto
, rel
, relocation
, input_section
,
2709 input_bfd
, contents
);
2711 /* We should have already detected the error and set message before.
2712 If the error message isn't set since the linker runs out of memory
2713 or we don't set it before, then we should set the default message
2714 with the "internal error" string here. */
2720 case bfd_reloc_overflow
:
2721 info
->callbacks
->reloc_overflow
2722 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
2723 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
2726 case bfd_reloc_undefined
:
2727 info
->callbacks
->undefined_symbol
2728 (info
, name
, input_bfd
, input_section
, rel
->r_offset
,
2732 case bfd_reloc_outofrange
:
2734 msg
= _("%X%P: internal error: out of range error\n");
2737 case bfd_reloc_notsupported
:
2739 msg
= _("%X%P: internal error: unsupported relocation error\n");
2742 case bfd_reloc_dangerous
:
2743 /* The error message should already be set. */
2745 msg
= _("dangerous relocation error");
2746 info
->callbacks
->reloc_dangerous
2747 (info
, msg
, input_bfd
, input_section
, rel
->r_offset
);
2751 msg
= _("%X%P: internal error: unknown error\n");
2755 /* Do not report error message for the dangerous relocation again. */
2756 if (msg
&& r
!= bfd_reloc_dangerous
)
2757 info
->callbacks
->einfo (msg
);
2759 /* Free the unused `msg_buf`. */
2762 /* We already reported the error via a callback, so don't try to report
2763 it again by returning false. That leads to spurious errors. */
2768 ret
= riscv_resolve_pcrel_lo_relocs (&pcrel_relocs
);
2770 riscv_free_pcrel_relocs (&pcrel_relocs
);
2774 /* Finish up dynamic symbol handling. We set the contents of various
2775 dynamic sections here. */
2778 riscv_elf_finish_dynamic_symbol (bfd
*output_bfd
,
2779 struct bfd_link_info
*info
,
2780 struct elf_link_hash_entry
*h
,
2781 Elf_Internal_Sym
*sym
)
2783 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
2784 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
2786 if (h
->plt
.offset
!= (bfd_vma
) -1)
2788 /* We've decided to create a PLT entry for this symbol. */
2790 bfd_vma i
, header_address
, plt_idx
, got_offset
, got_address
;
2791 uint32_t plt_entry
[PLT_ENTRY_INSNS
];
2792 Elf_Internal_Rela rela
;
2793 asection
*plt
, *gotplt
, *relplt
;
2795 /* When building a static executable, use .iplt, .igot.plt and
2796 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2797 if (htab
->elf
.splt
!= NULL
)
2799 plt
= htab
->elf
.splt
;
2800 gotplt
= htab
->elf
.sgotplt
;
2801 relplt
= htab
->elf
.srelplt
;
2805 plt
= htab
->elf
.iplt
;
2806 gotplt
= htab
->elf
.igotplt
;
2807 relplt
= htab
->elf
.irelplt
;
2810 /* This symbol has an entry in the procedure linkage table. Set
2812 if ((h
->dynindx
== -1
2813 && !((h
->forced_local
|| bfd_link_executable (info
))
2815 && h
->type
== STT_GNU_IFUNC
))
2821 /* Calculate the address of the PLT header. */
2822 header_address
= sec_addr (plt
);
2824 /* Calculate the index of the entry and the offset of .got.plt entry.
2825 For static executables, we don't reserve anything. */
2826 if (plt
== htab
->elf
.splt
)
2828 plt_idx
= (h
->plt
.offset
- PLT_HEADER_SIZE
) / PLT_ENTRY_SIZE
;
2829 got_offset
= GOTPLT_HEADER_SIZE
+ (plt_idx
* GOT_ENTRY_SIZE
);
2833 plt_idx
= h
->plt
.offset
/ PLT_ENTRY_SIZE
;
2834 got_offset
= plt_idx
* GOT_ENTRY_SIZE
;
2837 /* Calculate the address of the .got.plt entry. */
2838 got_address
= sec_addr (gotplt
) + got_offset
;
2840 /* Find out where the .plt entry should go. */
2841 loc
= plt
->contents
+ h
->plt
.offset
;
2843 /* Fill in the PLT entry itself. */
2844 if (! riscv_make_plt_entry (output_bfd
, got_address
,
2845 header_address
+ h
->plt
.offset
,
2849 for (i
= 0; i
< PLT_ENTRY_INSNS
; i
++)
2850 bfd_put_32 (output_bfd
, plt_entry
[i
], loc
+ 4*i
);
2852 /* Fill in the initial value of the .got.plt entry. */
2853 loc
= gotplt
->contents
+ (got_address
- sec_addr (gotplt
));
2854 bfd_put_NN (output_bfd
, sec_addr (plt
), loc
);
2856 rela
.r_offset
= got_address
;
2858 if (h
->dynindx
== -1
2859 || ((bfd_link_executable (info
)
2860 || ELF_ST_VISIBILITY (h
->other
) != STV_DEFAULT
)
2862 && h
->type
== STT_GNU_IFUNC
))
2864 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2865 h
->root
.root
.string
,
2866 h
->root
.u
.def
.section
->owner
);
2868 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2869 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2870 asection
*sec
= h
->root
.u
.def
.section
;
2871 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2872 rela
.r_addend
= h
->root
.u
.def
.value
2873 + sec
->output_section
->vma
2874 + sec
->output_offset
;
2878 /* Fill in the entry in the .rela.plt section. */
2879 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_JUMP_SLOT
);
2883 loc
= relplt
->contents
+ plt_idx
* sizeof (ElfNN_External_Rela
);
2884 bed
->s
->swap_reloca_out (output_bfd
, &rela
, loc
);
2886 if (!h
->def_regular
)
2888 /* Mark the symbol as undefined, rather than as defined in
2889 the .plt section. Leave the value alone. */
2890 sym
->st_shndx
= SHN_UNDEF
;
2891 /* If the symbol is weak, we do need to clear the value.
2892 Otherwise, the PLT entry would provide a definition for
2893 the symbol even if the symbol wasn't defined anywhere,
2894 and so the symbol would never be NULL. */
2895 if (!h
->ref_regular_nonweak
)
2900 if (h
->got
.offset
!= (bfd_vma
) -1
2901 && !(riscv_elf_hash_entry (h
)->tls_type
& (GOT_TLS_GD
| GOT_TLS_IE
))
2902 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info
, h
))
2906 Elf_Internal_Rela rela
;
2908 /* This symbol has an entry in the GOT. Set it up. */
2910 sgot
= htab
->elf
.sgot
;
2911 srela
= htab
->elf
.srelgot
;
2912 BFD_ASSERT (sgot
!= NULL
&& srela
!= NULL
);
2914 rela
.r_offset
= sec_addr (sgot
) + (h
->got
.offset
&~ (bfd_vma
) 1);
2916 /* Handle the ifunc symbol in GOT entry. */
2918 && h
->type
== STT_GNU_IFUNC
)
2920 if (h
->plt
.offset
== (bfd_vma
) -1)
2922 /* STT_GNU_IFUNC is referenced without PLT. */
2923 if (htab
->elf
.splt
== NULL
)
2925 /* use .rel[a].iplt section to store .got relocations
2926 in static executable. */
2927 srela
= htab
->elf
.irelplt
;
2929 if (SYMBOL_REFERENCES_LOCAL (info
, h
))
2931 info
->callbacks
->minfo (_("Local IFUNC function `%s' in %pB\n"),
2932 h
->root
.root
.string
,
2933 h
->root
.u
.def
.section
->owner
);
2935 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_IRELATIVE
);
2936 rela
.r_addend
= (h
->root
.u
.def
.value
2937 + h
->root
.u
.def
.section
->output_section
->vma
2938 + h
->root
.u
.def
.section
->output_offset
);
2942 /* Generate R_RISCV_NN. */
2943 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2944 BFD_ASSERT (h
->dynindx
!= -1);
2945 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2949 else if (bfd_link_pic (info
))
2951 /* Generate R_RISCV_NN. */
2952 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2953 BFD_ASSERT (h
->dynindx
!= -1);
2954 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2961 if (!h
->pointer_equality_needed
)
2964 /* For non-shared object, we can't use .got.plt, which
2965 contains the real function address if we need pointer
2966 equality. We load the GOT entry with the PLT entry. */
2967 plt
= htab
->elf
.splt
? htab
->elf
.splt
: htab
->elf
.iplt
;
2968 bfd_put_NN (output_bfd
, (plt
->output_section
->vma
2969 + plt
->output_offset
2971 htab
->elf
.sgot
->contents
2972 + (h
->got
.offset
& ~(bfd_vma
) 1));
2976 /* If this is a local symbol reference, we just want to emit a RELATIVE
2977 reloc. This can happen if it is a -Bsymbolic link, or a pie link, or
2978 the symbol was forced to be local because of a version file.
2979 The entry in the global offset table will already have been
2980 initialized in the relocate_section function. */
2981 else if (bfd_link_pic (info
)
2982 && SYMBOL_REFERENCES_LOCAL (info
, h
))
2984 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2985 asection
*sec
= h
->root
.u
.def
.section
;
2986 rela
.r_info
= ELFNN_R_INFO (0, R_RISCV_RELATIVE
);
2987 rela
.r_addend
= (h
->root
.u
.def
.value
2988 + sec
->output_section
->vma
2989 + sec
->output_offset
);
2993 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2994 BFD_ASSERT (h
->dynindx
!= -1);
2995 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_NN
);
2999 bfd_put_NN (output_bfd
, 0,
3000 sgot
->contents
+ (h
->got
.offset
& ~(bfd_vma
) 1));
3001 riscv_elf_append_rela (output_bfd
, srela
, &rela
);
3006 Elf_Internal_Rela rela
;
3009 /* This symbols needs a copy reloc. Set it up. */
3010 BFD_ASSERT (h
->dynindx
!= -1);
3012 rela
.r_offset
= sec_addr (h
->root
.u
.def
.section
) + h
->root
.u
.def
.value
;
3013 rela
.r_info
= ELFNN_R_INFO (h
->dynindx
, R_RISCV_COPY
);
3015 if (h
->root
.u
.def
.section
== htab
->elf
.sdynrelro
)
3016 s
= htab
->elf
.sreldynrelro
;
3018 s
= htab
->elf
.srelbss
;
3019 riscv_elf_append_rela (output_bfd
, s
, &rela
);
3022 /* Mark some specially defined symbols as absolute. */
3023 if (h
== htab
->elf
.hdynamic
3024 || (h
== htab
->elf
.hgot
|| h
== htab
->elf
.hplt
))
3025 sym
->st_shndx
= SHN_ABS
;
3030 /* Finish up local dynamic symbol handling. We set the contents of
3031 various dynamic sections here. */
3034 riscv_elf_finish_local_dynamic_symbol (void **slot
, void *inf
)
3036 struct elf_link_hash_entry
*h
= (struct elf_link_hash_entry
*) *slot
;
3037 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
3039 return riscv_elf_finish_dynamic_symbol (info
->output_bfd
, info
, h
, NULL
);
3042 /* Finish up the dynamic sections. */
3045 riscv_finish_dyn (bfd
*output_bfd
, struct bfd_link_info
*info
,
3046 bfd
*dynobj
, asection
*sdyn
)
3048 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
3049 const struct elf_backend_data
*bed
= get_elf_backend_data (output_bfd
);
3050 size_t dynsize
= bed
->s
->sizeof_dyn
;
3051 bfd_byte
*dyncon
, *dynconend
;
3053 dynconend
= sdyn
->contents
+ sdyn
->size
;
3054 for (dyncon
= sdyn
->contents
; dyncon
< dynconend
; dyncon
+= dynsize
)
3056 Elf_Internal_Dyn dyn
;
3059 bed
->s
->swap_dyn_in (dynobj
, dyncon
, &dyn
);
3064 s
= htab
->elf
.sgotplt
;
3065 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3068 s
= htab
->elf
.srelplt
;
3069 dyn
.d_un
.d_ptr
= s
->output_section
->vma
+ s
->output_offset
;
3072 s
= htab
->elf
.srelplt
;
3073 dyn
.d_un
.d_val
= s
->size
;
3079 bed
->s
->swap_dyn_out (output_bfd
, &dyn
, dyncon
);
3085 riscv_elf_finish_dynamic_sections (bfd
*output_bfd
,
3086 struct bfd_link_info
*info
)
3090 struct riscv_elf_link_hash_table
*htab
;
3092 htab
= riscv_elf_hash_table (info
);
3093 BFD_ASSERT (htab
!= NULL
);
3094 dynobj
= htab
->elf
.dynobj
;
3096 sdyn
= bfd_get_linker_section (dynobj
, ".dynamic");
3098 if (elf_hash_table (info
)->dynamic_sections_created
)
3103 splt
= htab
->elf
.splt
;
3104 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
3106 ret
= riscv_finish_dyn (output_bfd
, info
, dynobj
, sdyn
);
3111 /* Fill in the head and tail entries in the procedure linkage table. */
3115 uint32_t plt_header
[PLT_HEADER_INSNS
];
3116 ret
= riscv_make_plt_header (output_bfd
,
3117 sec_addr (htab
->elf
.sgotplt
),
3118 sec_addr (splt
), plt_header
);
3122 for (i
= 0; i
< PLT_HEADER_INSNS
; i
++)
3123 bfd_put_32 (output_bfd
, plt_header
[i
], splt
->contents
+ 4*i
);
3125 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
3130 if (htab
->elf
.sgotplt
)
3132 asection
*output_section
= htab
->elf
.sgotplt
->output_section
;
3134 if (bfd_is_abs_section (output_section
))
3136 (*_bfd_error_handler
)
3137 (_("discarded output section: `%pA'"), htab
->elf
.sgotplt
);
3141 if (htab
->elf
.sgotplt
->size
> 0)
3143 /* Write the first two entries in .got.plt, needed for the dynamic
3145 bfd_put_NN (output_bfd
, (bfd_vma
) -1, htab
->elf
.sgotplt
->contents
);
3146 bfd_put_NN (output_bfd
, (bfd_vma
) 0,
3147 htab
->elf
.sgotplt
->contents
+ GOT_ENTRY_SIZE
);
3150 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3155 asection
*output_section
= htab
->elf
.sgot
->output_section
;
3157 if (htab
->elf
.sgot
->size
> 0)
3159 /* Set the first entry in the global offset table to the address of
3160 the dynamic section. */
3161 bfd_vma val
= sdyn
? sec_addr (sdyn
) : 0;
3162 bfd_put_NN (output_bfd
, val
, htab
->elf
.sgot
->contents
);
3165 elf_section_data (output_section
)->this_hdr
.sh_entsize
= GOT_ENTRY_SIZE
;
3168 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3169 htab_traverse (htab
->loc_hash_table
,
3170 riscv_elf_finish_local_dynamic_symbol
,
3176 /* Return address for Ith PLT stub in section PLT, for relocation REL
3177 or (bfd_vma) -1 if it should not be included. */
3180 riscv_elf_plt_sym_val (bfd_vma i
, const asection
*plt
,
3181 const arelent
*rel ATTRIBUTE_UNUSED
)
3183 return plt
->vma
+ PLT_HEADER_SIZE
+ i
* PLT_ENTRY_SIZE
;
3186 static enum elf_reloc_type_class
3187 riscv_reloc_type_class (const struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
3188 const asection
*rel_sec ATTRIBUTE_UNUSED
,
3189 const Elf_Internal_Rela
*rela
)
3191 switch (ELFNN_R_TYPE (rela
->r_info
))
3193 case R_RISCV_RELATIVE
:
3194 return reloc_class_relative
;
3195 case R_RISCV_JUMP_SLOT
:
3196 return reloc_class_plt
;
3198 return reloc_class_copy
;
3200 return reloc_class_normal
;
3204 /* Given the ELF header flags in FLAGS, it returns a string that describes the
3208 riscv_float_abi_string (flagword flags
)
3210 switch (flags
& EF_RISCV_FLOAT_ABI
)
3212 case EF_RISCV_FLOAT_ABI_SOFT
:
3213 return "soft-float";
3215 case EF_RISCV_FLOAT_ABI_SINGLE
:
3216 return "single-float";
3218 case EF_RISCV_FLOAT_ABI_DOUBLE
:
3219 return "double-float";
3221 case EF_RISCV_FLOAT_ABI_QUAD
:
3222 return "quad-float";
3229 /* The information of architecture attribute. */
3230 static riscv_subset_list_t in_subsets
;
3231 static riscv_subset_list_t out_subsets
;
3232 static riscv_subset_list_t merged_subsets
;
3234 /* Predicator for standard extension. */
3237 riscv_std_ext_p (const char *name
)
3239 return (strlen (name
) == 1) && (name
[0] != 'x') && (name
[0] != 's');
3242 /* Check if the versions are compatible. */
3245 riscv_version_mismatch (bfd
*ibfd
,
3246 struct riscv_subset_t
*in
,
3247 struct riscv_subset_t
*out
)
3249 if (in
== NULL
|| out
== NULL
)
3252 /* Since there are no version conflicts for now, we just report
3253 warning when the versions are mis-matched. */
3254 if (in
->major_version
!= out
->major_version
3255 || in
->minor_version
!= out
->minor_version
)
3258 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
3259 "extension, the output version is %d.%d"),
3265 out
->minor_version
);
3267 /* Update the output ISA versions to the newest ones. */
3268 if ((in
->major_version
> out
->major_version
)
3269 || (in
->major_version
== out
->major_version
3270 && in
->minor_version
> out
->minor_version
))
3272 out
->major_version
= in
->major_version
;
3273 out
->minor_version
= in
->minor_version
;
3280 /* Return true if subset is 'i' or 'e'. */
3283 riscv_i_or_e_p (bfd
*ibfd
,
3285 struct riscv_subset_t
*subset
)
3287 if ((strcasecmp (subset
->name
, "e") != 0)
3288 && (strcasecmp (subset
->name
, "i") != 0))
3291 (_("error: %pB: corrupted ISA string '%s'. "
3292 "First letter should be 'i' or 'e' but got '%s'"),
3293 ibfd
, arch
, subset
->name
);
3299 /* Merge standard extensions.
3302 Return FALSE if failed to merge.
3306 `in_arch`: Raw arch string for input object.
3307 `out_arch`: Raw arch string for output object.
3308 `pin`: subset list for input object, and it'll skip all merged subset after
3310 `pout`: Like `pin`, but for output object. */
3313 riscv_merge_std_ext (bfd
*ibfd
,
3314 const char *in_arch
,
3315 const char *out_arch
,
3316 struct riscv_subset_t
**pin
,
3317 struct riscv_subset_t
**pout
)
3319 const char *standard_exts
= riscv_supported_std_ext ();
3321 struct riscv_subset_t
*in
= *pin
;
3322 struct riscv_subset_t
*out
= *pout
;
3324 /* First letter should be 'i' or 'e'. */
3325 if (!riscv_i_or_e_p (ibfd
, in_arch
, in
))
3328 if (!riscv_i_or_e_p (ibfd
, out_arch
, out
))
3331 if (strcasecmp (in
->name
, out
->name
) != 0)
3333 /* TODO: We might allow merge 'i' with 'e'. */
3335 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
3336 ibfd
, in
->name
, out
->name
);
3339 else if (!riscv_version_mismatch (ibfd
, in
, out
))
3342 riscv_add_subset (&merged_subsets
,
3343 out
->name
, out
->major_version
, out
->minor_version
);
3348 /* Handle standard extension first. */
3349 for (p
= standard_exts
; *p
; ++p
)
3351 char find_ext
[2] = {*p
, '\0'};
3352 struct riscv_subset_t
*find_in
=
3353 riscv_lookup_subset (&in_subsets
, find_ext
);
3354 struct riscv_subset_t
*find_out
=
3355 riscv_lookup_subset (&out_subsets
, find_ext
);
3357 if (find_in
== NULL
&& find_out
== NULL
)
3360 if (!riscv_version_mismatch (ibfd
, find_in
, find_out
))
3363 struct riscv_subset_t
*merged
= find_out
? find_out
: find_in
;
3364 riscv_add_subset (&merged_subsets
, merged
->name
,
3365 merged
->major_version
, merged
->minor_version
);
3368 /* Skip all standard extensions. */
3369 while ((in
!= NULL
) && riscv_std_ext_p (in
->name
)) in
= in
->next
;
3370 while ((out
!= NULL
) && riscv_std_ext_p (out
->name
)) out
= out
->next
;
3378 /* If C is a prefix class, then return the EXT string without the prefix.
3379 Otherwise return the entire EXT string. */
3382 riscv_skip_prefix (const char *ext
, riscv_isa_ext_class_t c
)
3386 case RV_ISA_CLASS_X
: return &ext
[1];
3387 case RV_ISA_CLASS_S
: return &ext
[1];
3388 case RV_ISA_CLASS_Z
: return &ext
[1];
3389 default: return ext
;
3393 /* Compare prefixed extension names canonically. */
3396 riscv_prefix_cmp (const char *a
, const char *b
)
3398 riscv_isa_ext_class_t ca
= riscv_get_prefix_class (a
);
3399 riscv_isa_ext_class_t cb
= riscv_get_prefix_class (b
);
3401 /* Extension name without prefix */
3402 const char *anp
= riscv_skip_prefix (a
, ca
);
3403 const char *bnp
= riscv_skip_prefix (b
, cb
);
3406 return strcasecmp (anp
, bnp
);
3408 return (int)ca
- (int)cb
;
3411 /* Merge multi letter extensions. PIN is a pointer to the head of the input
3412 object subset list. Likewise for POUT and the output object. Return TRUE
3413 on success and FALSE when a conflict is found. */
3416 riscv_merge_multi_letter_ext (bfd
*ibfd
,
3417 riscv_subset_t
**pin
,
3418 riscv_subset_t
**pout
)
3420 riscv_subset_t
*in
= *pin
;
3421 riscv_subset_t
*out
= *pout
;
3422 riscv_subset_t
*tail
;
3428 cmp
= riscv_prefix_cmp (in
->name
, out
->name
);
3432 /* `in' comes before `out', append `in' and increment. */
3433 riscv_add_subset (&merged_subsets
, in
->name
, in
->major_version
,
3439 /* `out' comes before `in', append `out' and increment. */
3440 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3441 out
->minor_version
);
3446 /* Both present, check version and increment both. */
3447 if (!riscv_version_mismatch (ibfd
, in
, out
))
3450 riscv_add_subset (&merged_subsets
, out
->name
, out
->major_version
,
3451 out
->minor_version
);
3458 /* If we're here, either `in' or `out' is running longer than
3459 the other. So, we need to append the corresponding tail. */
3460 tail
= in
? in
: out
;
3464 riscv_add_subset (&merged_subsets
, tail
->name
, tail
->major_version
,
3465 tail
->minor_version
);
3473 /* Merge Tag_RISCV_arch attribute. */
3476 riscv_merge_arch_attr_info (bfd
*ibfd
, char *in_arch
, char *out_arch
)
3478 riscv_subset_t
*in
, *out
;
3479 char *merged_arch_str
;
3481 unsigned xlen_in
, xlen_out
;
3482 merged_subsets
.head
= NULL
;
3483 merged_subsets
.tail
= NULL
;
3485 riscv_parse_subset_t rpe_in
;
3486 riscv_parse_subset_t rpe_out
;
3488 /* Only assembler needs to check the default version of ISA, so just set
3489 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
3490 rpe_in
.subset_list
= &in_subsets
;
3491 rpe_in
.error_handler
= _bfd_error_handler
;
3492 rpe_in
.xlen
= &xlen_in
;
3493 rpe_in
.get_default_version
= NULL
;
3495 rpe_out
.subset_list
= &out_subsets
;
3496 rpe_out
.error_handler
= _bfd_error_handler
;
3497 rpe_out
.xlen
= &xlen_out
;
3498 rpe_out
.get_default_version
= NULL
;
3500 if (in_arch
== NULL
&& out_arch
== NULL
)
3503 if (in_arch
== NULL
&& out_arch
!= NULL
)
3506 if (in_arch
!= NULL
&& out_arch
== NULL
)
3509 /* Parse subset from arch string. */
3510 if (!riscv_parse_subset (&rpe_in
, in_arch
))
3513 if (!riscv_parse_subset (&rpe_out
, out_arch
))
3516 /* Checking XLEN. */
3517 if (xlen_out
!= xlen_in
)
3520 (_("error: %pB: ISA string of input (%s) doesn't match "
3521 "output (%s)"), ibfd
, in_arch
, out_arch
);
3525 /* Merge subset list. */
3526 in
= in_subsets
.head
;
3527 out
= out_subsets
.head
;
3529 /* Merge standard extension. */
3530 if (!riscv_merge_std_ext (ibfd
, in_arch
, out_arch
, &in
, &out
))
3533 /* Merge all non-single letter extensions with single call. */
3534 if (!riscv_merge_multi_letter_ext (ibfd
, &in
, &out
))
3537 if (xlen_in
!= xlen_out
)
3540 (_("error: %pB: XLEN of input (%u) doesn't match "
3541 "output (%u)"), ibfd
, xlen_in
, xlen_out
);
3545 if (xlen_in
!= ARCH_SIZE
)
3548 (_("error: %pB: unsupported XLEN (%u), you might be "
3549 "using wrong emulation"), ibfd
, xlen_in
);
3553 merged_arch_str
= riscv_arch_str (ARCH_SIZE
, &merged_subsets
);
3555 /* Release the subset lists. */
3556 riscv_release_subset_list (&in_subsets
);
3557 riscv_release_subset_list (&out_subsets
);
3558 riscv_release_subset_list (&merged_subsets
);
3560 return merged_arch_str
;
3563 /* Merge object attributes from IBFD into output_bfd of INFO.
3564 Raise an error if there are conflicting attributes. */
3567 riscv_merge_attributes (bfd
*ibfd
, struct bfd_link_info
*info
)
3569 bfd
*obfd
= info
->output_bfd
;
3570 obj_attribute
*in_attr
;
3571 obj_attribute
*out_attr
;
3572 bfd_boolean result
= TRUE
;
3573 bfd_boolean priv_attrs_merged
= FALSE
;
3574 const char *sec_name
= get_elf_backend_data (ibfd
)->obj_attrs_section
;
3577 /* Skip linker created files. */
3578 if (ibfd
->flags
& BFD_LINKER_CREATED
)
3581 /* Skip any input that doesn't have an attribute section.
3582 This enables to link object files without attribute section with
3584 if (bfd_get_section_by_name (ibfd
, sec_name
) == NULL
)
3587 if (!elf_known_obj_attributes_proc (obfd
)[0].i
)
3589 /* This is the first object. Copy the attributes. */
3590 _bfd_elf_copy_obj_attributes (ibfd
, obfd
);
3592 out_attr
= elf_known_obj_attributes_proc (obfd
);
3594 /* Use the Tag_null value to indicate the attributes have been
3601 in_attr
= elf_known_obj_attributes_proc (ibfd
);
3602 out_attr
= elf_known_obj_attributes_proc (obfd
);
3604 for (i
= LEAST_KNOWN_OBJ_ATTRIBUTE
; i
< NUM_KNOWN_OBJ_ATTRIBUTES
; i
++)
3608 case Tag_RISCV_arch
:
3609 if (!out_attr
[Tag_RISCV_arch
].s
)
3610 out_attr
[Tag_RISCV_arch
].s
= in_attr
[Tag_RISCV_arch
].s
;
3611 else if (in_attr
[Tag_RISCV_arch
].s
3612 && out_attr
[Tag_RISCV_arch
].s
)
3614 /* Check arch compatible. */
3616 riscv_merge_arch_attr_info (ibfd
,
3617 in_attr
[Tag_RISCV_arch
].s
,
3618 out_attr
[Tag_RISCV_arch
].s
);
3619 if (merged_arch
== NULL
)
3622 out_attr
[Tag_RISCV_arch
].s
= "";
3625 out_attr
[Tag_RISCV_arch
].s
= merged_arch
;
3629 case Tag_RISCV_priv_spec
:
3630 case Tag_RISCV_priv_spec_minor
:
3631 case Tag_RISCV_priv_spec_revision
:
3632 /* If we have handled the priv attributes, then skip it. */
3633 if (!priv_attrs_merged
)
3635 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
3636 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
3637 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
3638 enum riscv_priv_spec_class in_priv_spec
;
3639 enum riscv_priv_spec_class out_priv_spec
;
3641 /* Get the priv spec class from elf attribute numbers. */
3642 riscv_get_priv_spec_class_from_numbers (in_attr
[Tag_a
].i
,
3646 riscv_get_priv_spec_class_from_numbers (out_attr
[Tag_a
].i
,
3651 /* Allow to link the object without the priv specs. */
3652 if (out_priv_spec
== PRIV_SPEC_CLASS_NONE
)
3654 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3655 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3656 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3658 else if (in_priv_spec
!= PRIV_SPEC_CLASS_NONE
3659 && in_priv_spec
!= out_priv_spec
)
3662 (_("warning: %pB use privilege spec version %u.%u.%u but "
3663 "the output use version %u.%u.%u"),
3672 /* The priv spec v1.9.1 can not be linked with other spec
3673 versions since the conflicts. We plan to drop the
3674 v1.9.1 in a year or two, so this confict should be
3675 removed in the future. */
3676 if (in_priv_spec
== PRIV_SPEC_CLASS_1P9P1
3677 || out_priv_spec
== PRIV_SPEC_CLASS_1P9P1
)
3680 (_("warning: privilege spec version 1.9.1 can not be "
3681 "linked with other spec versions"));
3684 /* Update the output priv spec to the newest one. */
3685 if (in_priv_spec
> out_priv_spec
)
3687 out_attr
[Tag_a
].i
= in_attr
[Tag_a
].i
;
3688 out_attr
[Tag_b
].i
= in_attr
[Tag_b
].i
;
3689 out_attr
[Tag_c
].i
= in_attr
[Tag_c
].i
;
3692 priv_attrs_merged
= TRUE
;
3696 case Tag_RISCV_unaligned_access
:
3697 out_attr
[i
].i
|= in_attr
[i
].i
;
3700 case Tag_RISCV_stack_align
:
3701 if (out_attr
[i
].i
== 0)
3702 out_attr
[i
].i
= in_attr
[i
].i
;
3703 else if (in_attr
[i
].i
!= 0
3704 && out_attr
[i
].i
!= 0
3705 && out_attr
[i
].i
!= in_attr
[i
].i
)
3708 (_("error: %pB use %u-byte stack aligned but the output "
3709 "use %u-byte stack aligned"),
3710 ibfd
, in_attr
[i
].i
, out_attr
[i
].i
);
3716 result
&= _bfd_elf_merge_unknown_attribute_low (ibfd
, obfd
, i
);
3719 /* If out_attr was copied from in_attr then it won't have a type yet. */
3720 if (in_attr
[i
].type
&& !out_attr
[i
].type
)
3721 out_attr
[i
].type
= in_attr
[i
].type
;
3724 /* Merge Tag_compatibility attributes and any common GNU ones. */
3725 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3728 /* Check for any attributes not known on RISC-V. */
3729 result
&= _bfd_elf_merge_unknown_attribute_list (ibfd
, obfd
);
3734 /* Merge backend specific data from an object file to the output
3735 object file when linking. */
3738 _bfd_riscv_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
3740 bfd
*obfd
= info
->output_bfd
;
3741 flagword new_flags
, old_flags
;
3743 if (!is_riscv_elf (ibfd
) || !is_riscv_elf (obfd
))
3746 if (strcmp (bfd_get_target (ibfd
), bfd_get_target (obfd
)) != 0)
3748 (*_bfd_error_handler
)
3749 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
3750 " target emulation `%s' does not match `%s'"),
3751 ibfd
, bfd_get_target (ibfd
), bfd_get_target (obfd
));
3755 if (!_bfd_elf_merge_object_attributes (ibfd
, info
))
3758 if (!riscv_merge_attributes (ibfd
, info
))
3761 new_flags
= elf_elfheader (ibfd
)->e_flags
;
3762 old_flags
= elf_elfheader (obfd
)->e_flags
;
3764 if (! elf_flags_init (obfd
))
3766 elf_flags_init (obfd
) = TRUE
;
3767 elf_elfheader (obfd
)->e_flags
= new_flags
;
3771 /* Check to see if the input BFD actually contains any sections. If not,
3772 its flags may not have been initialized either, but it cannot actually
3773 cause any incompatibility. Do not short-circuit dynamic objects; their
3774 section list may be emptied by elf_link_add_object_symbols.
3776 Also check to see if there are no code sections in the input. In this
3777 case, there is no need to check for code specific flags. */
3778 if (!(ibfd
->flags
& DYNAMIC
))
3780 bfd_boolean null_input_bfd
= TRUE
;
3781 bfd_boolean only_data_sections
= TRUE
;
3784 for (sec
= ibfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
3786 if ((bfd_section_flags (sec
)
3787 & (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3788 == (SEC_LOAD
| SEC_CODE
| SEC_HAS_CONTENTS
))
3789 only_data_sections
= FALSE
;
3791 null_input_bfd
= FALSE
;
3795 if (null_input_bfd
|| only_data_sections
)
3799 /* Disallow linking different float ABIs. */
3800 if ((old_flags
^ new_flags
) & EF_RISCV_FLOAT_ABI
)
3802 (*_bfd_error_handler
)
3803 (_("%pB: can't link %s modules with %s modules"), ibfd
,
3804 riscv_float_abi_string (new_flags
),
3805 riscv_float_abi_string (old_flags
));
3809 /* Disallow linking RVE and non-RVE. */
3810 if ((old_flags
^ new_flags
) & EF_RISCV_RVE
)
3812 (*_bfd_error_handler
)
3813 (_("%pB: can't link RVE with other target"), ibfd
);
3817 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3818 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_RISCV_RVC
;
3823 bfd_set_error (bfd_error_bad_value
);
3827 /* Delete some bytes from a section while relaxing. */
3830 riscv_relax_delete_bytes (bfd
*abfd
, asection
*sec
, bfd_vma addr
, size_t count
,
3831 struct bfd_link_info
*link_info
)
3833 unsigned int i
, symcount
;
3834 bfd_vma toaddr
= sec
->size
;
3835 struct elf_link_hash_entry
**sym_hashes
= elf_sym_hashes (abfd
);
3836 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3837 unsigned int sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3838 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
3839 bfd_byte
*contents
= data
->this_hdr
.contents
;
3841 /* Actually delete the bytes. */
3843 memmove (contents
+ addr
, contents
+ addr
+ count
, toaddr
- addr
- count
);
3845 /* Adjust the location of all of the relocs. Note that we need not
3846 adjust the addends, since all PC-relative references must be against
3847 symbols, which we will adjust below. */
3848 for (i
= 0; i
< sec
->reloc_count
; i
++)
3849 if (data
->relocs
[i
].r_offset
> addr
&& data
->relocs
[i
].r_offset
< toaddr
)
3850 data
->relocs
[i
].r_offset
-= count
;
3852 /* Adjust the local symbols defined in this section. */
3853 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
3855 Elf_Internal_Sym
*sym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ i
;
3856 if (sym
->st_shndx
== sec_shndx
)
3858 /* If the symbol is in the range of memory we just moved, we
3859 have to adjust its value. */
3860 if (sym
->st_value
> addr
&& sym
->st_value
<= toaddr
)
3861 sym
->st_value
-= count
;
3863 /* If the symbol *spans* the bytes we just deleted (i.e. its
3864 *end* is in the moved bytes but its *start* isn't), then we
3865 must adjust its size.
3867 This test needs to use the original value of st_value, otherwise
3868 we might accidentally decrease size when deleting bytes right
3869 before the symbol. But since deleted relocs can't span across
3870 symbols, we can't have both a st_value and a st_size decrease,
3871 so it is simpler to just use an else. */
3872 else if (sym
->st_value
<= addr
3873 && sym
->st_value
+ sym
->st_size
> addr
3874 && sym
->st_value
+ sym
->st_size
<= toaddr
)
3875 sym
->st_size
-= count
;
3879 /* Now adjust the global symbols defined in this section. */
3880 symcount
= ((symtab_hdr
->sh_size
/ sizeof (ElfNN_External_Sym
))
3881 - symtab_hdr
->sh_info
);
3883 for (i
= 0; i
< symcount
; i
++)
3885 struct elf_link_hash_entry
*sym_hash
= sym_hashes
[i
];
3887 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3888 containing the definition of __wrap_SYMBOL, includes a direct
3889 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3890 the same symbol (which is __wrap_SYMBOL), but still exist as two
3891 different symbols in 'sym_hashes', we don't want to adjust
3892 the global symbol __wrap_SYMBOL twice. */
3893 /* The same problem occurs with symbols that are versioned_hidden, as
3894 foo becomes an alias for foo@BAR, and hence they need the same
3896 if (link_info
->wrap_hash
!= NULL
3897 || sym_hash
->versioned
== versioned_hidden
)
3899 struct elf_link_hash_entry
**cur_sym_hashes
;
3901 /* Loop only over the symbols which have already been checked. */
3902 for (cur_sym_hashes
= sym_hashes
; cur_sym_hashes
< &sym_hashes
[i
];
3905 /* If the current symbol is identical to 'sym_hash', that means
3906 the symbol was already adjusted (or at least checked). */
3907 if (*cur_sym_hashes
== sym_hash
)
3910 /* Don't adjust the symbol again. */
3911 if (cur_sym_hashes
< &sym_hashes
[i
])
3915 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3916 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3917 && sym_hash
->root
.u
.def
.section
== sec
)
3919 /* As above, adjust the value if needed. */
3920 if (sym_hash
->root
.u
.def
.value
> addr
3921 && sym_hash
->root
.u
.def
.value
<= toaddr
)
3922 sym_hash
->root
.u
.def
.value
-= count
;
3924 /* As above, adjust the size if needed. */
3925 else if (sym_hash
->root
.u
.def
.value
<= addr
3926 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
3927 && sym_hash
->root
.u
.def
.value
+ sym_hash
->size
<= toaddr
)
3928 sym_hash
->size
-= count
;
3935 /* A second format for recording PC-relative hi relocations. This stores the
3936 information required to relax them to GP-relative addresses. */
3938 typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc
;
3939 struct riscv_pcgp_hi_reloc
3946 bfd_boolean undefined_weak
;
3947 riscv_pcgp_hi_reloc
*next
;
3950 typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc
;
3951 struct riscv_pcgp_lo_reloc
3954 riscv_pcgp_lo_reloc
*next
;
3959 riscv_pcgp_hi_reloc
*hi
;
3960 riscv_pcgp_lo_reloc
*lo
;
3961 } riscv_pcgp_relocs
;
3963 /* Initialize the pcgp reloc info in P. */
3966 riscv_init_pcgp_relocs (riscv_pcgp_relocs
*p
)
3973 /* Free the pcgp reloc info in P. */
3976 riscv_free_pcgp_relocs (riscv_pcgp_relocs
*p
,
3977 bfd
*abfd ATTRIBUTE_UNUSED
,
3978 asection
*sec ATTRIBUTE_UNUSED
)
3980 riscv_pcgp_hi_reloc
*c
;
3981 riscv_pcgp_lo_reloc
*l
;
3983 for (c
= p
->hi
; c
!= NULL
;)
3985 riscv_pcgp_hi_reloc
*next
= c
->next
;
3990 for (l
= p
->lo
; l
!= NULL
;)
3992 riscv_pcgp_lo_reloc
*next
= l
->next
;
3998 /* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3999 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4000 relax the corresponding lo part reloc. */
4003 riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
,
4004 bfd_vma hi_addend
, bfd_vma hi_addr
,
4005 unsigned hi_sym
, asection
*sym_sec
,
4006 bfd_boolean undefined_weak
)
4008 riscv_pcgp_hi_reloc
*new = bfd_malloc (sizeof(*new));
4011 new->hi_sec_off
= hi_sec_off
;
4012 new->hi_addend
= hi_addend
;
4013 new->hi_addr
= hi_addr
;
4014 new->hi_sym
= hi_sym
;
4015 new->sym_sec
= sym_sec
;
4016 new->undefined_weak
= undefined_weak
;
4022 /* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4023 This is used by a lo part reloc to find the corresponding hi part reloc. */
4025 static riscv_pcgp_hi_reloc
*
4026 riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4028 riscv_pcgp_hi_reloc
*c
;
4030 for (c
= p
->hi
; c
!= NULL
; c
= c
->next
)
4031 if (c
->hi_sec_off
== hi_sec_off
)
4036 /* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4037 This is used to record relocs that can't be relaxed. */
4040 riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4042 riscv_pcgp_lo_reloc
*new = bfd_malloc (sizeof(*new));
4045 new->hi_sec_off
= hi_sec_off
;
4051 /* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4052 This is used by a hi part reloc to find the corresponding lo part reloc. */
4055 riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs
*p
, bfd_vma hi_sec_off
)
4057 riscv_pcgp_lo_reloc
*c
;
4059 for (c
= p
->lo
; c
!= NULL
; c
= c
->next
)
4060 if (c
->hi_sec_off
== hi_sec_off
)
4065 typedef bfd_boolean (*relax_func_t
) (bfd
*, asection
*, asection
*,
4066 struct bfd_link_info
*,
4067 Elf_Internal_Rela
*,
4068 bfd_vma
, bfd_vma
, bfd_vma
, bfd_boolean
*,
4069 riscv_pcgp_relocs
*,
4070 bfd_boolean undefined_weak
);
4072 /* Relax AUIPC + JALR into JAL. */
4075 _bfd_riscv_relax_call (bfd
*abfd
, asection
*sec
, asection
*sym_sec
,
4076 struct bfd_link_info
*link_info
,
4077 Elf_Internal_Rela
*rel
,
4079 bfd_vma max_alignment
,
4080 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4082 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4083 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
4085 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4086 bfd_vma foff
= symval
- (sec_addr (sec
) + rel
->r_offset
);
4087 bfd_boolean near_zero
= (symval
+ RISCV_IMM_REACH
/2) < RISCV_IMM_REACH
;
4088 bfd_vma auipc
, jalr
;
4089 int rd
, r_type
, len
= 4, rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4091 /* If the call crosses section boundaries, an alignment directive could
4092 cause the PC-relative offset to later increase, so we need to add in the
4093 max alignment of any section inclusive from the call to the target.
4094 Otherwise, we only need to use the alignment of the current section. */
4095 if (VALID_UJTYPE_IMM (foff
))
4097 if (sym_sec
->output_section
== sec
->output_section
4098 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4099 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4100 foff
+= ((bfd_signed_vma
) foff
< 0 ? -max_alignment
: max_alignment
);
4103 /* See if this function call can be shortened. */
4104 if (!VALID_UJTYPE_IMM (foff
) && !(!bfd_link_pic (link_info
) && near_zero
))
4107 /* Shorten the function call. */
4108 BFD_ASSERT (rel
->r_offset
+ 8 <= sec
->size
);
4110 auipc
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
4111 jalr
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
+ 4);
4112 rd
= (jalr
>> OP_SH_RD
) & OP_MASK_RD
;
4113 rvc
= rvc
&& VALID_RVC_J_IMM (foff
);
4115 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4116 rvc
= rvc
&& (rd
== 0 || (rd
== X_RA
&& ARCH_SIZE
== 32));
4120 /* Relax to C.J[AL] rd, addr. */
4121 r_type
= R_RISCV_RVC_JUMP
;
4122 auipc
= rd
== 0 ? MATCH_C_J
: MATCH_C_JAL
;
4125 else if (VALID_UJTYPE_IMM (foff
))
4127 /* Relax to JAL rd, addr. */
4128 r_type
= R_RISCV_JAL
;
4129 auipc
= MATCH_JAL
| (rd
<< OP_SH_RD
);
4131 else /* near_zero */
4133 /* Relax to JALR rd, x0, addr. */
4134 r_type
= R_RISCV_LO12_I
;
4135 auipc
= MATCH_JALR
| (rd
<< OP_SH_RD
);
4138 /* Replace the R_RISCV_CALL reloc. */
4139 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), r_type
);
4140 /* Replace the AUIPC. */
4141 bfd_put (8 * len
, abfd
, auipc
, contents
+ rel
->r_offset
);
4143 /* Delete unnecessary JALR. */
4145 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ len
, 8 - len
,
4149 /* Traverse all output sections and return the max alignment. */
4152 _bfd_riscv_get_max_alignment (asection
*sec
)
4154 unsigned int max_alignment_power
= 0;
4157 for (o
= sec
->output_section
->owner
->sections
; o
!= NULL
; o
= o
->next
)
4159 if (o
->alignment_power
> max_alignment_power
)
4160 max_alignment_power
= o
->alignment_power
;
4163 return (bfd_vma
) 1 << max_alignment_power
;
4166 /* Relax non-PIC global variable references. */
4169 _bfd_riscv_relax_lui (bfd
*abfd
,
4172 struct bfd_link_info
*link_info
,
4173 Elf_Internal_Rela
*rel
,
4175 bfd_vma max_alignment
,
4176 bfd_vma reserve_size
,
4178 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4179 bfd_boolean undefined_weak
)
4181 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4182 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4183 int use_rvc
= elf_elfheader (abfd
)->e_flags
& EF_RISCV_RVC
;
4185 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4189 /* If gp and the symbol are in the same output section, which is not the
4190 abs section, then consider only that output section's alignment. */
4191 struct bfd_link_hash_entry
*h
=
4192 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
4194 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4195 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4196 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4199 /* Is the reference in range of x0 or gp?
4200 Valid gp range conservatively because of alignment issue. */
4202 || (VALID_ITYPE_IMM (symval
)
4204 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4206 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4208 unsigned sym
= ELFNN_R_SYM (rel
->r_info
);
4209 switch (ELFNN_R_TYPE (rel
->r_info
))
4211 case R_RISCV_LO12_I
:
4214 /* Change the RS1 to zero. */
4215 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
4216 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4217 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
4220 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4223 case R_RISCV_LO12_S
:
4226 /* Change the RS1 to zero. */
4227 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
4228 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4229 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
4232 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4236 /* We can delete the unnecessary LUI and reloc. */
4237 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4239 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4,
4247 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
4248 account for this assuming page alignment at worst. In the presence of
4249 RELRO segment the linker aligns it by one page size, therefore sections
4250 after the segment can be moved more than one page. */
4253 && ELFNN_R_TYPE (rel
->r_info
) == R_RISCV_HI20
4254 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
))
4255 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval
)
4256 + (link_info
->relro
? 2 * ELF_MAXPAGESIZE
4257 : ELF_MAXPAGESIZE
)))
4259 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
4260 bfd_vma lui
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
4261 unsigned rd
= ((unsigned)lui
>> OP_SH_RD
) & OP_MASK_RD
;
4262 if (rd
== 0 || rd
== X_SP
)
4265 lui
= (lui
& (OP_MASK_RD
<< OP_SH_RD
)) | MATCH_C_LUI
;
4266 bfd_put_32 (abfd
, lui
, contents
+ rel
->r_offset
);
4268 /* Replace the R_RISCV_HI20 reloc. */
4269 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_RVC_LUI
);
4272 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ 2, 2,
4279 /* Relax non-PIC TLS references. */
4282 _bfd_riscv_relax_tls_le (bfd
*abfd
,
4284 asection
*sym_sec ATTRIBUTE_UNUSED
,
4285 struct bfd_link_info
*link_info
,
4286 Elf_Internal_Rela
*rel
,
4288 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4289 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4291 riscv_pcgp_relocs
*prcel_relocs ATTRIBUTE_UNUSED
,
4292 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
4294 /* See if this symbol is in range of tp. */
4295 if (RISCV_CONST_HIGH_PART (tpoff (link_info
, symval
)) != 0)
4298 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4299 switch (ELFNN_R_TYPE (rel
->r_info
))
4301 case R_RISCV_TPREL_LO12_I
:
4302 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_I
);
4305 case R_RISCV_TPREL_LO12_S
:
4306 rel
->r_info
= ELFNN_R_INFO (ELFNN_R_SYM (rel
->r_info
), R_RISCV_TPREL_S
);
4309 case R_RISCV_TPREL_HI20
:
4310 case R_RISCV_TPREL_ADD
:
4311 /* We can delete the unnecessary instruction and reloc. */
4312 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4314 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
, 4, link_info
);
4321 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
4324 _bfd_riscv_relax_align (bfd
*abfd
, asection
*sec
,
4326 struct bfd_link_info
*link_info
,
4327 Elf_Internal_Rela
*rel
,
4329 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4330 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4331 bfd_boolean
*again ATTRIBUTE_UNUSED
,
4332 riscv_pcgp_relocs
*pcrel_relocs ATTRIBUTE_UNUSED
,
4333 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
4335 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4336 bfd_vma alignment
= 1, pos
;
4337 while (alignment
<= rel
->r_addend
)
4340 symval
-= rel
->r_addend
;
4341 bfd_vma aligned_addr
= ((symval
- 1) & ~(alignment
- 1)) + alignment
;
4342 bfd_vma nop_bytes
= aligned_addr
- symval
;
4344 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4345 sec
->sec_flg0
= TRUE
;
4347 /* Make sure there are enough NOPs to actually achieve the alignment. */
4348 if (rel
->r_addend
< nop_bytes
)
4351 (_("%pB(%pA+%#" PRIx64
"): %" PRId64
" bytes required for alignment "
4352 "to %" PRId64
"-byte boundary, but only %" PRId64
" present"),
4353 abfd
, sym_sec
, (uint64_t) rel
->r_offset
,
4354 (int64_t) nop_bytes
, (int64_t) alignment
, (int64_t) rel
->r_addend
);
4355 bfd_set_error (bfd_error_bad_value
);
4359 /* Delete the reloc. */
4360 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_NONE
);
4362 /* If the number of NOPs is already correct, there's nothing to do. */
4363 if (nop_bytes
== rel
->r_addend
)
4366 /* Write as many RISC-V NOPs as we need. */
4367 for (pos
= 0; pos
< (nop_bytes
& -4); pos
+= 4)
4368 bfd_put_32 (abfd
, RISCV_NOP
, contents
+ rel
->r_offset
+ pos
);
4370 /* Write a final RVC NOP if need be. */
4371 if (nop_bytes
% 4 != 0)
4372 bfd_put_16 (abfd
, RVC_NOP
, contents
+ rel
->r_offset
+ pos
);
4374 /* Delete the excess bytes. */
4375 return riscv_relax_delete_bytes (abfd
, sec
, rel
->r_offset
+ nop_bytes
,
4376 rel
->r_addend
- nop_bytes
, link_info
);
4379 /* Relax PC-relative references to GP-relative references. */
4382 _bfd_riscv_relax_pc (bfd
*abfd ATTRIBUTE_UNUSED
,
4385 struct bfd_link_info
*link_info
,
4386 Elf_Internal_Rela
*rel
,
4388 bfd_vma max_alignment
,
4389 bfd_vma reserve_size
,
4390 bfd_boolean
*again ATTRIBUTE_UNUSED
,
4391 riscv_pcgp_relocs
*pcgp_relocs
,
4392 bfd_boolean undefined_weak
)
4394 bfd_byte
*contents
= elf_section_data (sec
)->this_hdr
.contents
;
4395 bfd_vma gp
= riscv_global_pointer_value (link_info
);
4397 BFD_ASSERT (rel
->r_offset
+ 4 <= sec
->size
);
4399 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4400 * actual target address. */
4401 riscv_pcgp_hi_reloc hi_reloc
;
4402 memset (&hi_reloc
, 0, sizeof (hi_reloc
));
4403 switch (ELFNN_R_TYPE (rel
->r_info
))
4405 case R_RISCV_PCREL_LO12_I
:
4406 case R_RISCV_PCREL_LO12_S
:
4408 /* If the %lo has an addend, it isn't for the label pointing at the
4409 hi part instruction, but rather for the symbol pointed at by the
4410 hi part instruction. So we must subtract it here for the lookup.
4411 It is still used below in the final symbol address. */
4412 bfd_vma hi_sec_off
= symval
- sec_addr (sym_sec
) - rel
->r_addend
;
4413 riscv_pcgp_hi_reloc
*hi
= riscv_find_pcgp_hi_reloc (pcgp_relocs
,
4417 riscv_record_pcgp_lo_reloc (pcgp_relocs
, hi_sec_off
);
4422 symval
= hi_reloc
.hi_addr
;
4423 sym_sec
= hi_reloc
.sym_sec
;
4425 /* We can not know whether the undefined weak symbol is referenced
4426 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4427 we have to record the 'undefined_weak' flag when handling the
4428 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4429 undefined_weak
= hi_reloc
.undefined_weak
;
4433 case R_RISCV_PCREL_HI20
:
4434 /* Mergeable symbols and code might later move out of range. */
4435 if (! undefined_weak
4436 && sym_sec
->flags
& (SEC_MERGE
| SEC_CODE
))
4439 /* If the cooresponding lo relocation has already been seen then it's not
4440 * safe to relax this relocation. */
4441 if (riscv_find_pcgp_lo_reloc (pcgp_relocs
, rel
->r_offset
))
4452 /* If gp and the symbol are in the same output section, which is not the
4453 abs section, then consider only that output section's alignment. */
4454 struct bfd_link_hash_entry
*h
=
4455 bfd_link_hash_lookup (link_info
->hash
, RISCV_GP_SYMBOL
, FALSE
, FALSE
,
4457 if (h
->u
.def
.section
->output_section
== sym_sec
->output_section
4458 && sym_sec
->output_section
!= bfd_abs_section_ptr
)
4459 max_alignment
= (bfd_vma
) 1 << sym_sec
->output_section
->alignment_power
;
4462 /* Is the reference in range of x0 or gp?
4463 Valid gp range conservatively because of alignment issue. */
4465 || (VALID_ITYPE_IMM (symval
)
4467 && VALID_ITYPE_IMM (symval
- gp
+ max_alignment
+ reserve_size
))
4469 && VALID_ITYPE_IMM (symval
- gp
- max_alignment
- reserve_size
))))
4471 unsigned sym
= hi_reloc
.hi_sym
;
4472 switch (ELFNN_R_TYPE (rel
->r_info
))
4474 case R_RISCV_PCREL_LO12_I
:
4477 /* Change the RS1 to zero, and then modify the relocation
4478 type to R_RISCV_LO12_I. */
4479 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
4480 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4481 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
4482 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_I
);
4483 rel
->r_addend
= hi_reloc
.hi_addend
;
4487 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_I
);
4488 rel
->r_addend
+= hi_reloc
.hi_addend
;
4492 case R_RISCV_PCREL_LO12_S
:
4495 /* Change the RS1 to zero, and then modify the relocation
4496 type to R_RISCV_LO12_S. */
4497 bfd_vma insn
= bfd_get_32 (abfd
, contents
+ rel
->r_offset
);
4498 insn
&= ~(OP_MASK_RS1
<< OP_SH_RS1
);
4499 bfd_put_32 (abfd
, insn
, contents
+ rel
->r_offset
);
4500 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_LO12_S
);
4501 rel
->r_addend
= hi_reloc
.hi_addend
;
4505 rel
->r_info
= ELFNN_R_INFO (sym
, R_RISCV_GPREL_S
);
4506 rel
->r_addend
+= hi_reloc
.hi_addend
;
4510 case R_RISCV_PCREL_HI20
:
4511 riscv_record_pcgp_hi_reloc (pcgp_relocs
,
4515 ELFNN_R_SYM(rel
->r_info
),
4518 /* We can delete the unnecessary AUIPC and reloc. */
4519 rel
->r_info
= ELFNN_R_INFO (0, R_RISCV_DELETE
);
4531 /* Relax PC-relative references to GP-relative references. */
4534 _bfd_riscv_relax_delete (bfd
*abfd
,
4536 asection
*sym_sec ATTRIBUTE_UNUSED
,
4537 struct bfd_link_info
*link_info
,
4538 Elf_Internal_Rela
*rel
,
4539 bfd_vma symval ATTRIBUTE_UNUSED
,
4540 bfd_vma max_alignment ATTRIBUTE_UNUSED
,
4541 bfd_vma reserve_size ATTRIBUTE_UNUSED
,
4542 bfd_boolean
*again ATTRIBUTE_UNUSED
,
4543 riscv_pcgp_relocs
*pcgp_relocs ATTRIBUTE_UNUSED
,
4544 bfd_boolean undefined_weak ATTRIBUTE_UNUSED
)
4546 if (!riscv_relax_delete_bytes(abfd
, sec
, rel
->r_offset
, rel
->r_addend
,
4549 rel
->r_info
= ELFNN_R_INFO(0, R_RISCV_NONE
);
4553 /* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
4554 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
4555 disabled, handles code alignment directives. */
4558 _bfd_riscv_relax_section (bfd
*abfd
, asection
*sec
,
4559 struct bfd_link_info
*info
,
4562 Elf_Internal_Shdr
*symtab_hdr
= &elf_symtab_hdr (abfd
);
4563 struct riscv_elf_link_hash_table
*htab
= riscv_elf_hash_table (info
);
4564 struct bfd_elf_section_data
*data
= elf_section_data (sec
);
4565 Elf_Internal_Rela
*relocs
;
4566 bfd_boolean ret
= FALSE
;
4568 bfd_vma max_alignment
, reserve_size
= 0;
4569 riscv_pcgp_relocs pcgp_relocs
;
4573 if (bfd_link_relocatable (info
)
4575 || (sec
->flags
& SEC_RELOC
) == 0
4576 || sec
->reloc_count
== 0
4577 || (info
->disable_target_specific_optimizations
4578 && info
->relax_pass
== 0))
4581 riscv_init_pcgp_relocs (&pcgp_relocs
);
4583 /* Read this BFD's relocs if we haven't done so already. */
4585 relocs
= data
->relocs
;
4586 else if (!(relocs
= _bfd_elf_link_read_relocs (abfd
, sec
, NULL
, NULL
,
4587 info
->keep_memory
)))
4592 max_alignment
= htab
->max_alignment
;
4593 if (max_alignment
== (bfd_vma
) -1)
4595 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4596 htab
->max_alignment
= max_alignment
;
4600 max_alignment
= _bfd_riscv_get_max_alignment (sec
);
4602 /* Examine and consider relaxing each reloc. */
4603 for (i
= 0; i
< sec
->reloc_count
; i
++)
4606 Elf_Internal_Rela
*rel
= relocs
+ i
;
4607 relax_func_t relax_func
;
4608 int type
= ELFNN_R_TYPE (rel
->r_info
);
4611 bfd_boolean undefined_weak
= FALSE
;
4614 if (info
->relax_pass
== 0)
4616 if (type
== R_RISCV_CALL
|| type
== R_RISCV_CALL_PLT
)
4617 relax_func
= _bfd_riscv_relax_call
;
4618 else if (type
== R_RISCV_HI20
4619 || type
== R_RISCV_LO12_I
4620 || type
== R_RISCV_LO12_S
)
4621 relax_func
= _bfd_riscv_relax_lui
;
4622 else if (!bfd_link_pic(info
)
4623 && (type
== R_RISCV_PCREL_HI20
4624 || type
== R_RISCV_PCREL_LO12_I
4625 || type
== R_RISCV_PCREL_LO12_S
))
4626 relax_func
= _bfd_riscv_relax_pc
;
4627 else if (type
== R_RISCV_TPREL_HI20
4628 || type
== R_RISCV_TPREL_ADD
4629 || type
== R_RISCV_TPREL_LO12_I
4630 || type
== R_RISCV_TPREL_LO12_S
)
4631 relax_func
= _bfd_riscv_relax_tls_le
;
4635 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4636 if (i
== sec
->reloc_count
- 1
4637 || ELFNN_R_TYPE ((rel
+ 1)->r_info
) != R_RISCV_RELAX
4638 || rel
->r_offset
!= (rel
+ 1)->r_offset
)
4641 /* Skip over the R_RISCV_RELAX. */
4644 else if (info
->relax_pass
== 1 && type
== R_RISCV_DELETE
)
4645 relax_func
= _bfd_riscv_relax_delete
;
4646 else if (info
->relax_pass
== 2 && type
== R_RISCV_ALIGN
)
4647 relax_func
= _bfd_riscv_relax_align
;
4651 data
->relocs
= relocs
;
4653 /* Read this BFD's contents if we haven't done so already. */
4654 if (!data
->this_hdr
.contents
4655 && !bfd_malloc_and_get_section (abfd
, sec
, &data
->this_hdr
.contents
))
4658 /* Read this BFD's symbols if we haven't done so already. */
4659 if (symtab_hdr
->sh_info
!= 0
4660 && !symtab_hdr
->contents
4661 && !(symtab_hdr
->contents
=
4662 (unsigned char *) bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
4663 symtab_hdr
->sh_info
,
4664 0, NULL
, NULL
, NULL
)))
4667 /* Get the value of the symbol referred to by the reloc. */
4668 if (ELFNN_R_SYM (rel
->r_info
) < symtab_hdr
->sh_info
)
4670 /* A local symbol. */
4671 Elf_Internal_Sym
*isym
= ((Elf_Internal_Sym
*) symtab_hdr
->contents
4672 + ELFNN_R_SYM (rel
->r_info
));
4673 reserve_size
= (isym
->st_size
- rel
->r_addend
) > isym
->st_size
4674 ? 0 : isym
->st_size
- rel
->r_addend
;
4676 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4677 a fake global symbol entry for this, so deal with the local ifunc
4679 if (ELF_ST_TYPE (isym
->st_info
) == STT_GNU_IFUNC
)
4682 if (isym
->st_shndx
== SHN_UNDEF
)
4683 sym_sec
= sec
, symval
= rel
->r_offset
;
4686 BFD_ASSERT (isym
->st_shndx
< elf_numsections (abfd
));
4687 sym_sec
= elf_elfsections (abfd
)[isym
->st_shndx
]->bfd_section
;
4689 /* The purpose of this code is unknown. It breaks linker scripts
4690 for embedded development that place sections at address zero.
4691 This code is believed to be unnecessary. Disabling it but not
4692 yet removing it, in case something breaks. */
4693 if (sec_addr (sym_sec
) == 0)
4696 symval
= isym
->st_value
;
4698 symtype
= ELF_ST_TYPE (isym
->st_info
);
4703 struct elf_link_hash_entry
*h
;
4705 indx
= ELFNN_R_SYM (rel
->r_info
) - symtab_hdr
->sh_info
;
4706 h
= elf_sym_hashes (abfd
)[indx
];
4708 while (h
->root
.type
== bfd_link_hash_indirect
4709 || h
->root
.type
== bfd_link_hash_warning
)
4710 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
4712 /* Disable the relaxation for ifunc. */
4713 if (h
!= NULL
&& h
->type
== STT_GNU_IFUNC
)
4716 if (h
->root
.type
== bfd_link_hash_undefweak
4717 && (relax_func
== _bfd_riscv_relax_lui
4718 || relax_func
== _bfd_riscv_relax_pc
))
4720 /* For the lui and auipc relaxations, since the symbol
4721 value of an undefined weak symbol is always be zero,
4722 we can optimize the patterns into a single LI/MV/ADDI
4725 Note that, creating shared libraries and pie output may
4726 break the rule above. Fortunately, since we do not relax
4727 pc relocs when creating shared libraries and pie output,
4728 and the absolute address access for R_RISCV_HI20 isn't
4729 allowed when "-fPIC" is set, the problem of creating shared
4730 libraries can not happen currently. Once we support the
4731 auipc relaxations when creating shared libraries, then we will
4732 need the more rigorous checking for this optimization. */
4733 undefined_weak
= TRUE
;
4736 /* This line has to match the check in riscv_elf_relocate_section
4737 in the R_RISCV_CALL[_PLT] case. */
4738 if (bfd_link_pic (info
) && h
->plt
.offset
!= MINUS_ONE
)
4740 sym_sec
= htab
->elf
.splt
;
4741 symval
= h
->plt
.offset
;
4743 else if (undefined_weak
)
4746 sym_sec
= bfd_und_section_ptr
;
4748 else if ((h
->root
.type
== bfd_link_hash_defined
4749 || h
->root
.type
== bfd_link_hash_defweak
)
4750 && h
->root
.u
.def
.section
!= NULL
4751 && h
->root
.u
.def
.section
->output_section
!= NULL
)
4753 symval
= h
->root
.u
.def
.value
;
4754 sym_sec
= h
->root
.u
.def
.section
;
4759 if (h
->type
!= STT_FUNC
)
4761 (h
->size
- rel
->r_addend
) > h
->size
? 0 : h
->size
- rel
->r_addend
;
4765 if (sym_sec
->sec_info_type
== SEC_INFO_TYPE_MERGE
4766 && (sym_sec
->flags
& SEC_MERGE
))
4768 /* At this stage in linking, no SEC_MERGE symbol has been
4769 adjusted, so all references to such symbols need to be
4770 passed through _bfd_merged_section_offset. (Later, in
4771 relocate_section, all SEC_MERGE symbols *except* for
4772 section symbols have been adjusted.)
4774 gas may reduce relocations against symbols in SEC_MERGE
4775 sections to a relocation against the section symbol when
4776 the original addend was zero. When the reloc is against
4777 a section symbol we should include the addend in the
4778 offset passed to _bfd_merged_section_offset, since the
4779 location of interest is the original symbol. On the
4780 other hand, an access to "sym+addend" where "sym" is not
4781 a section symbol should not include the addend; Such an
4782 access is presumed to be an offset from "sym"; The
4783 location of interest is just "sym". */
4784 if (symtype
== STT_SECTION
)
4785 symval
+= rel
->r_addend
;
4787 symval
= _bfd_merged_section_offset (abfd
, &sym_sec
,
4788 elf_section_data (sym_sec
)->sec_info
,
4791 if (symtype
!= STT_SECTION
)
4792 symval
+= rel
->r_addend
;
4795 symval
+= rel
->r_addend
;
4797 symval
+= sec_addr (sym_sec
);
4799 if (!relax_func (abfd
, sec
, sym_sec
, info
, rel
, symval
,
4800 max_alignment
, reserve_size
, again
,
4801 &pcgp_relocs
, undefined_weak
))
4808 if (relocs
!= data
->relocs
)
4810 riscv_free_pcgp_relocs(&pcgp_relocs
, abfd
, sec
);
4816 # define PRSTATUS_SIZE 204
4817 # define PRSTATUS_OFFSET_PR_CURSIG 12
4818 # define PRSTATUS_OFFSET_PR_PID 24
4819 # define PRSTATUS_OFFSET_PR_REG 72
4820 # define ELF_GREGSET_T_SIZE 128
4821 # define PRPSINFO_SIZE 128
4822 # define PRPSINFO_OFFSET_PR_PID 16
4823 # define PRPSINFO_OFFSET_PR_FNAME 32
4824 # define PRPSINFO_OFFSET_PR_PSARGS 48
4826 # define PRSTATUS_SIZE 376
4827 # define PRSTATUS_OFFSET_PR_CURSIG 12
4828 # define PRSTATUS_OFFSET_PR_PID 32
4829 # define PRSTATUS_OFFSET_PR_REG 112
4830 # define ELF_GREGSET_T_SIZE 256
4831 # define PRPSINFO_SIZE 136
4832 # define PRPSINFO_OFFSET_PR_PID 24
4833 # define PRPSINFO_OFFSET_PR_FNAME 40
4834 # define PRPSINFO_OFFSET_PR_PSARGS 56
4837 /* Support for core dump NOTE sections. */
4840 riscv_elf_grok_prstatus (bfd
*abfd
, Elf_Internal_Note
*note
)
4842 switch (note
->descsz
)
4847 case PRSTATUS_SIZE
: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4849 elf_tdata (abfd
)->core
->signal
4850 = bfd_get_16 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_CURSIG
);
4853 elf_tdata (abfd
)->core
->lwpid
4854 = bfd_get_32 (abfd
, note
->descdata
+ PRSTATUS_OFFSET_PR_PID
);
4858 /* Make a ".reg/999" section. */
4859 return _bfd_elfcore_make_pseudosection (abfd
, ".reg", ELF_GREGSET_T_SIZE
,
4860 note
->descpos
+ PRSTATUS_OFFSET_PR_REG
);
4864 riscv_elf_grok_psinfo (bfd
*abfd
, Elf_Internal_Note
*note
)
4866 switch (note
->descsz
)
4871 case PRPSINFO_SIZE
: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4873 elf_tdata (abfd
)->core
->pid
4874 = bfd_get_32 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PID
);
4877 elf_tdata (abfd
)->core
->program
= _bfd_elfcore_strndup
4878 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_FNAME
, 16);
4881 elf_tdata (abfd
)->core
->command
= _bfd_elfcore_strndup
4882 (abfd
, note
->descdata
+ PRPSINFO_OFFSET_PR_PSARGS
, 80);
4886 /* Note that for some reason, a spurious space is tacked
4887 onto the end of the args in some (at least one anyway)
4888 implementations, so strip it off if it exists. */
4891 char *command
= elf_tdata (abfd
)->core
->command
;
4892 int n
= strlen (command
);
4894 if (0 < n
&& command
[n
- 1] == ' ')
4895 command
[n
- 1] = '\0';
4901 /* Set the right mach type. */
4903 riscv_elf_object_p (bfd
*abfd
)
4905 /* There are only two mach types in RISCV currently. */
4906 if (strcmp (abfd
->xvec
->name
, "elf32-littleriscv") == 0)
4907 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv32
);
4909 bfd_default_set_arch_mach (abfd
, bfd_arch_riscv
, bfd_mach_riscv64
);
4914 /* Determine whether an object attribute tag takes an integer, a
4918 riscv_elf_obj_attrs_arg_type (int tag
)
4920 return (tag
& 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL
: ATTR_TYPE_FLAG_INT_VAL
;
4923 #define TARGET_LITTLE_SYM riscv_elfNN_vec
4924 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
4926 #define elf_backend_reloc_type_class riscv_reloc_type_class
4928 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4929 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4930 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4931 #define bfd_elfNN_bfd_merge_private_bfd_data \
4932 _bfd_riscv_elf_merge_private_bfd_data
4934 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4935 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4936 #define elf_backend_check_relocs riscv_elf_check_relocs
4937 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4938 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4939 #define elf_backend_relocate_section riscv_elf_relocate_section
4940 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4941 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4942 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
4943 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
4944 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4945 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
4946 #define elf_backend_object_p riscv_elf_object_p
4947 #define elf_info_to_howto_rel NULL
4948 #define elf_info_to_howto riscv_info_to_howto_rela
4949 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
4950 #define bfd_elfNN_mkobject elfNN_riscv_mkobject
4952 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4954 #define elf_backend_can_gc_sections 1
4955 #define elf_backend_can_refcount 1
4956 #define elf_backend_want_got_plt 1
4957 #define elf_backend_plt_readonly 1
4958 #define elf_backend_plt_alignment 4
4959 #define elf_backend_want_plt_sym 1
4960 #define elf_backend_got_header_size (ARCH_SIZE / 8)
4961 #define elf_backend_want_dynrelro 1
4962 #define elf_backend_rela_normal 1
4963 #define elf_backend_default_execstack 0
4965 #undef elf_backend_obj_attrs_vendor
4966 #define elf_backend_obj_attrs_vendor "riscv"
4967 #undef elf_backend_obj_attrs_arg_type
4968 #define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
4969 #undef elf_backend_obj_attrs_section_type
4970 #define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
4971 #undef elf_backend_obj_attrs_section
4972 #define elf_backend_obj_attrs_section ".riscv.attributes"
4974 #include "elfNN-target.h"