]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfnn-riscv.c
Fix the BFD library's find_nearest_line feature to produce consistent results.
[thirdparty/binutils-gdb.git] / bfd / elfnn-riscv.c
CommitLineData
e23eba97 1/* RISC-V-specific support for NN-bit ELF.
d87bef3a 2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
e23eba97
NC
3
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on TILE-Gx and MIPS targets.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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/>. */
22
23/* This file handles RISC-V ELF targets. */
24
25#include "sysdep.h"
26#include "bfd.h"
27#include "libbfd.h"
28#include "bfdlink.h"
29#include "genlink.h"
30#include "elf-bfd.h"
31#include "elfxx-riscv.h"
32#include "elf/riscv.h"
33#include "opcode/riscv.h"
02dd9d25 34#include "objalloc.h"
e23eba97 35
fbc09e7a 36#include <limits.h>
fbc09e7a
MC
37#ifndef CHAR_BIT
38#define CHAR_BIT 8
39#endif
40
ff6f4d9b
PD
41/* Internal relocations used exclusively by the relaxation pass. */
42#define R_RISCV_DELETE (R_RISCV_max + 1)
43
e23eba97
NC
44#define ARCH_SIZE NN
45
46#define MINUS_ONE ((bfd_vma)0 - 1)
47
48#define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
49
50#define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
51
52/* The name of the dynamic interpreter. This is put in the .interp
53 section. */
54
55#define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
56#define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
57
58#define ELF_ARCH bfd_arch_riscv
59#define ELF_TARGET_ID RISCV_ELF_DATA
60#define ELF_MACHINE_CODE EM_RISCV
61#define ELF_MAXPAGESIZE 0x1000
62#define ELF_COMMONPAGESIZE 0x1000
63
fbc95f1e
KC
64#define RISCV_ATTRIBUTES_SECTION_NAME ".riscv.attributes"
65
e23eba97
NC
66/* RISC-V ELF linker hash entry. */
67
68struct riscv_elf_link_hash_entry
69{
70 struct elf_link_hash_entry elf;
71
1942a048
NC
72#define GOT_UNKNOWN 0
73#define GOT_NORMAL 1
74#define GOT_TLS_GD 2
75#define GOT_TLS_IE 4
76#define GOT_TLS_LE 8
e23eba97
NC
77 char tls_type;
78};
79
80#define riscv_elf_hash_entry(ent) \
1942a048 81 ((struct riscv_elf_link_hash_entry *) (ent))
e23eba97
NC
82
83struct _bfd_riscv_elf_obj_tdata
84{
85 struct elf_obj_tdata root;
86
87 /* tls_type for each local got entry. */
88 char *local_got_tls_type;
89};
90
91#define _bfd_riscv_elf_tdata(abfd) \
92 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
93
94#define _bfd_riscv_elf_local_got_tls_type(abfd) \
95 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
96
97#define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
98 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
99 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
100
101#define is_riscv_elf(bfd) \
102 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
103 && elf_tdata (bfd) != NULL \
104 && elf_object_id (bfd) == RISCV_ELF_DATA)
105
0a1b45a2 106static bool
fc46e8bd
NC
107elfNN_riscv_mkobject (bfd *abfd)
108{
109 return bfd_elf_allocate_object (abfd,
110 sizeof (struct _bfd_riscv_elf_obj_tdata),
111 RISCV_ELF_DATA);
112}
113
e23eba97
NC
114#include "elf/common.h"
115#include "elf/internal.h"
116
117struct riscv_elf_link_hash_table
118{
119 struct elf_link_hash_table elf;
120
121 /* Short-cuts to get to dynamic linker sections. */
e23eba97
NC
122 asection *sdyntdata;
123
fc3c5343
L
124 /* The max alignment of output sections. */
125 bfd_vma max_alignment;
02dd9d25
NC
126
127 /* Used by local STT_GNU_IFUNC symbols. */
128 htab_t loc_hash_table;
129 void * loc_hash_memory;
51a8a7c2
NC
130
131 /* The index of the last unused .rel.iplt slot. */
132 bfd_vma last_iplt_index;
ebdcad3f 133
ef9d2565
NC
134 /* The data segment phase, don't relax the section
135 when it is exp_seg_relro_adjust. */
136 int *data_segment_phase;
8155b853
NC
137
138 /* Relocations for variant CC symbols may be present. */
139 int variant_cc;
e23eba97
NC
140};
141
fbc09e7a 142/* Instruction access functions. */
fbc09e7a
MC
143#define riscv_get_insn(bits, ptr) \
144 ((bits) == 16 ? bfd_getl16 (ptr) \
145 : (bits) == 32 ? bfd_getl32 (ptr) \
146 : (bits) == 64 ? bfd_getl64 (ptr) \
147 : (abort (), (bfd_vma) - 1))
148#define riscv_put_insn(bits, val, ptr) \
149 ((bits) == 16 ? bfd_putl16 (val, ptr) \
150 : (bits) == 32 ? bfd_putl32 (val, ptr) \
151 : (bits) == 64 ? bfd_putl64 (val, ptr) \
152 : (abort (), (void) 0))
e23eba97
NC
153
154/* Get the RISC-V ELF linker hash table from a link_info structure. */
155#define riscv_elf_hash_table(p) \
0f55320b
AM
156 ((is_elf_hash_table ((p)->hash) \
157 && elf_hash_table_id (elf_hash_table (p)) == RISCV_ELF_DATA) \
158 ? (struct riscv_elf_link_hash_table *) (p)->hash : NULL)
e23eba97 159
0a1b45a2 160static bool
0aa13fee 161riscv_info_to_howto_rela (bfd *abfd,
e23eba97
NC
162 arelent *cache_ptr,
163 Elf_Internal_Rela *dst)
164{
0aa13fee 165 cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, ELFNN_R_TYPE (dst->r_info));
f3185997 166 return cache_ptr->howto != NULL;
e23eba97
NC
167}
168
169static void
170riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
171{
172 const struct elf_backend_data *bed;
173 bfd_byte *loc;
174
175 bed = get_elf_backend_data (abfd);
176 loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
177 bed->s->swap_reloca_out (abfd, rel, loc);
178}
179
fbc09e7a
MC
180/* Return true if a relocation is modifying an instruction. */
181
0a1b45a2 182static bool
fbc09e7a
MC
183riscv_is_insn_reloc (const reloc_howto_type *howto)
184{
185 /* Heuristic: A multibyte destination with a nontrivial mask
186 is an instruction */
187 return (howto->bitsize > 8
188 && howto->dst_mask != 0
189 && ~(howto->dst_mask | (howto->bitsize < sizeof(bfd_vma) * CHAR_BIT
190 ? (MINUS_ONE << howto->bitsize) : (bfd_vma)0)) != 0);
191}
192
e23eba97 193/* PLT/GOT stuff. */
e23eba97
NC
194#define PLT_HEADER_INSNS 8
195#define PLT_ENTRY_INSNS 4
196#define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
197#define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
e23eba97 198#define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
02dd9d25
NC
199/* Reserve two entries of GOTPLT for ld.so, one is used for PLT resolver,
200 the other is used for link map. Other targets also reserve one more
201 entry used for runtime profile? */
e23eba97
NC
202#define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
203
204#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
205
e23eba97
NC
206#if ARCH_SIZE == 32
207# define MATCH_LREG MATCH_LW
208#else
209# define MATCH_LREG MATCH_LD
210#endif
211
212/* Generate a PLT header. */
213
0a1b45a2 214static bool
5ef23793
JW
215riscv_make_plt_header (bfd *output_bfd, bfd_vma gotplt_addr, bfd_vma addr,
216 uint32_t *entry)
e23eba97
NC
217{
218 bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
219 bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
220
5ef23793
JW
221 /* RVE has no t3 register, so this won't work, and is not supported. */
222 if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
223 {
224 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
225 output_bfd);
0a1b45a2 226 return false;
5ef23793
JW
227 }
228
e23eba97 229 /* auipc t2, %hi(.got.plt)
07d6d2b8 230 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
e23eba97
NC
231 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
232 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
233 addi t0, t2, %lo(.got.plt) # &.got.plt
234 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
07d6d2b8 235 l[w|d] t0, PTRSIZE(t0) # link map
dcd709e0 236 jr t3 */
e23eba97
NC
237
238 entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
239 entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
240 entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
1174d920 241 entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, (uint32_t) -(PLT_HEADER_SIZE + 12));
e23eba97
NC
242 entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
243 entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
244 entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
245 entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
5ef23793 246
0a1b45a2 247 return true;
e23eba97
NC
248}
249
250/* Generate a PLT entry. */
251
0a1b45a2 252static bool
5ef23793
JW
253riscv_make_plt_entry (bfd *output_bfd, bfd_vma got, bfd_vma addr,
254 uint32_t *entry)
e23eba97 255{
5ef23793
JW
256 /* RVE has no t3 register, so this won't work, and is not supported. */
257 if (elf_elfheader (output_bfd)->e_flags & EF_RISCV_RVE)
258 {
259 _bfd_error_handler (_("%pB: warning: RVE PLT generation not supported"),
260 output_bfd);
0a1b45a2 261 return false;
5ef23793
JW
262 }
263
e23eba97
NC
264 /* auipc t3, %hi(.got.plt entry)
265 l[w|d] t3, %lo(.got.plt entry)(t3)
266 jalr t1, t3
dcd709e0 267 nop */
e23eba97
NC
268
269 entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
1d65abb5 270 entry[1] = RISCV_ITYPE (LREG, X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
e23eba97
NC
271 entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
272 entry[3] = RISCV_NOP;
5ef23793 273
0a1b45a2 274 return true;
e23eba97
NC
275}
276
277/* Create an entry in an RISC-V ELF linker hash table. */
278
279static struct bfd_hash_entry *
280link_hash_newfunc (struct bfd_hash_entry *entry,
281 struct bfd_hash_table *table, const char *string)
282{
283 /* Allocate the structure if it has not already been allocated by a
284 subclass. */
285 if (entry == NULL)
286 {
287 entry =
288 bfd_hash_allocate (table,
289 sizeof (struct riscv_elf_link_hash_entry));
290 if (entry == NULL)
291 return entry;
292 }
293
294 /* Call the allocation method of the superclass. */
295 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
296 if (entry != NULL)
297 {
298 struct riscv_elf_link_hash_entry *eh;
299
300 eh = (struct riscv_elf_link_hash_entry *) entry;
e23eba97
NC
301 eh->tls_type = GOT_UNKNOWN;
302 }
303
304 return entry;
305}
306
02dd9d25 307/* Compute a hash of a local hash entry. We use elf_link_hash_entry
dcd709e0
NC
308 for local symbol so that we can handle local STT_GNU_IFUNC symbols
309 as global symbol. We reuse indx and dynstr_index for local symbol
310 hash since they aren't used by global symbols in this backend. */
02dd9d25
NC
311
312static hashval_t
313riscv_elf_local_htab_hash (const void *ptr)
314{
315 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) ptr;
316 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
317}
318
319/* Compare local hash entries. */
320
321static int
322riscv_elf_local_htab_eq (const void *ptr1, const void *ptr2)
323{
324 struct elf_link_hash_entry *h1 = (struct elf_link_hash_entry *) ptr1;
325 struct elf_link_hash_entry *h2 = (struct elf_link_hash_entry *) ptr2;
326
327 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
328}
329
330/* Find and/or create a hash entry for local symbol. */
331
332static struct elf_link_hash_entry *
333riscv_elf_get_local_sym_hash (struct riscv_elf_link_hash_table *htab,
334 bfd *abfd, const Elf_Internal_Rela *rel,
0a1b45a2 335 bool create)
02dd9d25
NC
336{
337 struct riscv_elf_link_hash_entry eh, *ret;
338 asection *sec = abfd->sections;
339 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
340 ELFNN_R_SYM (rel->r_info));
341 void **slot;
342
343 eh.elf.indx = sec->id;
344 eh.elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
345 slot = htab_find_slot_with_hash (htab->loc_hash_table, &eh, h,
346 create ? INSERT : NO_INSERT);
347
348 if (!slot)
349 return NULL;
350
351 if (*slot)
352 {
353 ret = (struct riscv_elf_link_hash_entry *) *slot;
354 return &ret->elf;
355 }
356
357 ret = (struct riscv_elf_link_hash_entry *)
358 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
359 sizeof (struct riscv_elf_link_hash_entry));
360 if (ret)
361 {
362 memset (ret, 0, sizeof (*ret));
363 ret->elf.indx = sec->id;
364 ret->elf.dynstr_index = ELFNN_R_SYM (rel->r_info);
365 ret->elf.dynindx = -1;
366 *slot = ret;
367 }
368 return &ret->elf;
369}
370
371/* Destroy a RISC-V elf linker hash table. */
372
373static void
374riscv_elf_link_hash_table_free (bfd *obfd)
375{
376 struct riscv_elf_link_hash_table *ret
377 = (struct riscv_elf_link_hash_table *) obfd->link.hash;
378
379 if (ret->loc_hash_table)
380 htab_delete (ret->loc_hash_table);
381 if (ret->loc_hash_memory)
382 objalloc_free ((struct objalloc *) ret->loc_hash_memory);
383
384 _bfd_elf_link_hash_table_free (obfd);
385}
386
e23eba97
NC
387/* Create a RISC-V ELF linker hash table. */
388
389static struct bfd_link_hash_table *
390riscv_elf_link_hash_table_create (bfd *abfd)
391{
392 struct riscv_elf_link_hash_table *ret;
986f0783 393 size_t amt = sizeof (struct riscv_elf_link_hash_table);
e23eba97
NC
394
395 ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
396 if (ret == NULL)
397 return NULL;
398
399 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
400 sizeof (struct riscv_elf_link_hash_entry),
401 RISCV_ELF_DATA))
402 {
403 free (ret);
404 return NULL;
405 }
406
fc3c5343 407 ret->max_alignment = (bfd_vma) -1;
02dd9d25
NC
408
409 /* Create hash table for local ifunc. */
410 ret->loc_hash_table = htab_try_create (1024,
411 riscv_elf_local_htab_hash,
412 riscv_elf_local_htab_eq,
413 NULL);
414 ret->loc_hash_memory = objalloc_create ();
415 if (!ret->loc_hash_table || !ret->loc_hash_memory)
416 {
417 riscv_elf_link_hash_table_free (abfd);
418 return NULL;
419 }
420 ret->elf.root.hash_table_free = riscv_elf_link_hash_table_free;
421
e23eba97
NC
422 return &ret->elf.root;
423}
424
425/* Create the .got section. */
426
0a1b45a2 427static bool
e23eba97
NC
428riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
429{
430 flagword flags;
431 asection *s, *s_got;
432 struct elf_link_hash_entry *h;
433 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
434 struct elf_link_hash_table *htab = elf_hash_table (info);
435
436 /* This function may be called more than once. */
ce558b89 437 if (htab->sgot != NULL)
0a1b45a2 438 return true;
e23eba97
NC
439
440 flags = bed->dynamic_sec_flags;
441
442 s = bfd_make_section_anyway_with_flags (abfd,
443 (bed->rela_plts_and_copies_p
444 ? ".rela.got" : ".rel.got"),
445 (bed->dynamic_sec_flags
446 | SEC_READONLY));
447 if (s == NULL
fd361982 448 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 449 return false;
e23eba97
NC
450 htab->srelgot = s;
451
452 s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
453 if (s == NULL
fd361982 454 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 455 return false;
e23eba97
NC
456 htab->sgot = s;
457
458 /* The first bit of the global offset table is the header. */
459 s->size += bed->got_header_size;
460
461 if (bed->want_got_plt)
462 {
463 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
464 if (s == NULL
fd361982 465 || !bfd_set_section_alignment (s, bed->s->log_file_align))
0a1b45a2 466 return false;
e23eba97
NC
467 htab->sgotplt = s;
468
469 /* Reserve room for the header. */
470 s->size += GOTPLT_HEADER_SIZE;
471 }
472
473 if (bed->want_got_sym)
474 {
475 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
476 section. We don't do this in the linker script because we don't want
477 to define the symbol if we are not creating a global offset
478 table. */
479 h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
480 "_GLOBAL_OFFSET_TABLE_");
481 elf_hash_table (info)->hgot = h;
482 if (h == NULL)
0a1b45a2 483 return false;
e23eba97
NC
484 }
485
0a1b45a2 486 return true;
e23eba97
NC
487}
488
489/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
490 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
491 hash table. */
492
0a1b45a2 493static bool
e23eba97
NC
494riscv_elf_create_dynamic_sections (bfd *dynobj,
495 struct bfd_link_info *info)
496{
497 struct riscv_elf_link_hash_table *htab;
498
499 htab = riscv_elf_hash_table (info);
500 BFD_ASSERT (htab != NULL);
501
502 if (!riscv_elf_create_got_section (dynobj, info))
0a1b45a2 503 return false;
e23eba97
NC
504
505 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
0a1b45a2 506 return false;
e23eba97 507
e23eba97
NC
508 if (!bfd_link_pic (info))
509 {
3e7bd7f2
JW
510 /* Technically, this section doesn't have contents. It is used as the
511 target of TLS copy relocs, to copy TLS data from shared libraries into
512 the executable. However, if we don't mark it as loadable, then it
513 matches the IS_TBSS test in ldlang.c, and there is no run-time address
514 space allocated for it even though it has SEC_ALLOC. That test is
515 correct for .tbss, but not correct for this section. There is also
516 a second problem that having a section with no contents can only work
517 if it comes after all sections with contents in the same segment,
518 but the linker script does not guarantee that. This is just mixed in
519 with other .tdata.* sections. We can fix both problems by lying and
520 saying that there are contents. This section is expected to be small
521 so this should not cause a significant extra program startup cost. */
e23eba97
NC
522 htab->sdyntdata =
523 bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
13755f40 524 (SEC_ALLOC | SEC_THREAD_LOCAL
3e7bd7f2
JW
525 | SEC_LOAD | SEC_DATA
526 | SEC_HAS_CONTENTS
13755f40 527 | SEC_LINKER_CREATED));
e23eba97
NC
528 }
529
9d19e4fd
AM
530 if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
531 || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
e23eba97
NC
532 abort ();
533
0a1b45a2 534 return true;
e23eba97
NC
535}
536
537/* Copy the extra info we tack onto an elf_link_hash_entry. */
538
539static void
540riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
541 struct elf_link_hash_entry *dir,
542 struct elf_link_hash_entry *ind)
543{
544 struct riscv_elf_link_hash_entry *edir, *eind;
545
546 edir = (struct riscv_elf_link_hash_entry *) dir;
547 eind = (struct riscv_elf_link_hash_entry *) ind;
548
e23eba97
NC
549 if (ind->root.type == bfd_link_hash_indirect
550 && dir->got.refcount <= 0)
551 {
552 edir->tls_type = eind->tls_type;
553 eind->tls_type = GOT_UNKNOWN;
554 }
555 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
556}
557
0a1b45a2 558static bool
e23eba97
NC
559riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
560 unsigned long symndx, char tls_type)
561{
562 char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
563
564 *new_tls_type |= tls_type;
565 if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
566 {
567 (*_bfd_error_handler)
871b3ab2 568 (_("%pB: `%s' accessed both as normal and thread local symbol"),
e23eba97 569 abfd, h ? h->root.root.string : "<local>");
0a1b45a2 570 return false;
e23eba97 571 }
0a1b45a2 572 return true;
e23eba97
NC
573}
574
0a1b45a2 575static bool
e23eba97
NC
576riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
577 struct elf_link_hash_entry *h, long symndx)
578{
579 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
580 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
581
582 if (htab->elf.sgot == NULL)
583 {
584 if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
0a1b45a2 585 return false;
e23eba97
NC
586 }
587
588 if (h != NULL)
589 {
590 h->got.refcount += 1;
0a1b45a2 591 return true;
e23eba97
NC
592 }
593
594 /* This is a global offset table entry for a local symbol. */
595 if (elf_local_got_refcounts (abfd) == NULL)
596 {
597 bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
598 if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
0a1b45a2 599 return false;
e23eba97
NC
600 _bfd_riscv_elf_local_got_tls_type (abfd)
601 = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
602 }
603 elf_local_got_refcounts (abfd) [symndx] += 1;
604
0a1b45a2 605 return true;
e23eba97
NC
606}
607
0a1b45a2 608static bool
e23eba97
NC
609bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
610{
f3185997
NC
611 reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
612
02dd9d25
NC
613 /* We propably can improve the information to tell users that they
614 should be recompile the code with -fPIC or -fPIE, just like what
615 x86 does. */
e23eba97 616 (*_bfd_error_handler)
871b3ab2 617 (_("%pB: relocation %s against `%s' can not be used when making a shared "
e23eba97 618 "object; recompile with -fPIC"),
f3185997
NC
619 abfd, r ? r->name : _("<unknown>"),
620 h != NULL ? h->root.root.string : "a local symbol");
e23eba97 621 bfd_set_error (bfd_error_bad_value);
0a1b45a2 622 return false;
e23eba97 623}
dcd709e0 624
e23eba97
NC
625/* Look through the relocs for a section during the first phase, and
626 allocate space in the global offset table or procedure linkage
627 table. */
628
0a1b45a2 629static bool
e23eba97
NC
630riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
631 asection *sec, const Elf_Internal_Rela *relocs)
632{
633 struct riscv_elf_link_hash_table *htab;
634 Elf_Internal_Shdr *symtab_hdr;
635 struct elf_link_hash_entry **sym_hashes;
636 const Elf_Internal_Rela *rel;
637 asection *sreloc = NULL;
638
639 if (bfd_link_relocatable (info))
0a1b45a2 640 return true;
e23eba97
NC
641
642 htab = riscv_elf_hash_table (info);
643 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
644 sym_hashes = elf_sym_hashes (abfd);
645
646 if (htab->elf.dynobj == NULL)
647 htab->elf.dynobj = abfd;
648
649 for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
650 {
651 unsigned int r_type;
d42c267e 652 unsigned int r_symndx;
e23eba97
NC
653 struct elf_link_hash_entry *h;
654
655 r_symndx = ELFNN_R_SYM (rel->r_info);
656 r_type = ELFNN_R_TYPE (rel->r_info);
657
658 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
659 {
871b3ab2 660 (*_bfd_error_handler) (_("%pB: bad symbol index: %d"),
e23eba97 661 abfd, r_symndx);
0a1b45a2 662 return false;
e23eba97
NC
663 }
664
665 if (r_symndx < symtab_hdr->sh_info)
02dd9d25
NC
666 {
667 /* A local symbol. */
668 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
669 abfd, r_symndx);
670 if (isym == NULL)
0a1b45a2 671 return false;
02dd9d25
NC
672
673 /* Check relocation against local STT_GNU_IFUNC symbol. */
674 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
675 {
0a1b45a2 676 h = riscv_elf_get_local_sym_hash (htab, abfd, rel, true);
02dd9d25 677 if (h == NULL)
0a1b45a2 678 return false;
02dd9d25
NC
679
680 /* Fake STT_GNU_IFUNC global symbol. */
681 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
682 isym, NULL);
683 h->type = STT_GNU_IFUNC;
684 h->def_regular = 1;
685 h->ref_regular = 1;
686 h->forced_local = 1;
687 h->root.type = bfd_link_hash_defined;
688 }
689 else
690 h = NULL;
691 }
e23eba97
NC
692 else
693 {
694 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
695 while (h->root.type == bfd_link_hash_indirect
696 || h->root.type == bfd_link_hash_warning)
697 h = (struct elf_link_hash_entry *) h->root.u.i.link;
e23eba97
NC
698 }
699
02dd9d25
NC
700 if (h != NULL)
701 {
702 switch (r_type)
703 {
704 case R_RISCV_32:
705 case R_RISCV_64:
706 case R_RISCV_CALL:
707 case R_RISCV_CALL_PLT:
708 case R_RISCV_HI20:
709 case R_RISCV_GOT_HI20:
710 case R_RISCV_PCREL_HI20:
711 /* Create the ifunc sections, iplt and ipltgot, for static
712 executables. */
713 if (h->type == STT_GNU_IFUNC
714 && !_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
0a1b45a2 715 return false;
02dd9d25
NC
716 break;
717
718 default:
719 break;
720 }
721
722 /* It is referenced by a non-shared object. */
723 h->ref_regular = 1;
724 }
725
e23eba97
NC
726 switch (r_type)
727 {
728 case R_RISCV_TLS_GD_HI20:
729 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
730 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
0a1b45a2 731 return false;
e23eba97
NC
732 break;
733
734 case R_RISCV_TLS_GOT_HI20:
735 if (bfd_link_pic (info))
736 info->flags |= DF_STATIC_TLS;
737 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
738 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
0a1b45a2 739 return false;
e23eba97
NC
740 break;
741
742 case R_RISCV_GOT_HI20:
743 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
744 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
0a1b45a2 745 return false;
e23eba97
NC
746 break;
747
3b1450b3 748 case R_RISCV_CALL:
e23eba97 749 case R_RISCV_CALL_PLT:
dcd709e0
NC
750 /* These symbol requires a procedure linkage table entry.
751 We actually build the entry in adjust_dynamic_symbol,
3b1450b3 752 because these might be a case of linking PIC code without
e23eba97
NC
753 linking in any dynamic objects, in which case we don't
754 need to generate a procedure linkage table after all. */
755
3b1450b3
NC
756 /* If it is a local symbol, then we resolve it directly
757 without creating a PLT entry. */
758 if (h == NULL)
759 continue;
760
761 h->needs_plt = 1;
762 h->plt.refcount += 1;
e23eba97
NC
763 break;
764
02dd9d25
NC
765 case R_RISCV_PCREL_HI20:
766 if (h != NULL
767 && h->type == STT_GNU_IFUNC)
768 {
769 h->non_got_ref = 1;
770 h->pointer_equality_needed = 1;
771
772 /* We don't use the PCREL_HI20 in the data section,
773 so we always need the plt when it refers to
774 ifunc symbol. */
775 h->plt.refcount += 1;
776 }
777 /* Fall through. */
778
e23eba97
NC
779 case R_RISCV_JAL:
780 case R_RISCV_BRANCH:
781 case R_RISCV_RVC_BRANCH:
782 case R_RISCV_RVC_JUMP:
02dd9d25
NC
783 /* In shared libraries and pie, these relocs are known
784 to bind locally. */
e23eba97
NC
785 if (bfd_link_pic (info))
786 break;
787 goto static_reloc;
788
789 case R_RISCV_TPREL_HI20:
790 if (!bfd_link_executable (info))
791 return bad_static_reloc (abfd, r_type, h);
792 if (h != NULL)
793 riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
794 goto static_reloc;
795
796 case R_RISCV_HI20:
797 if (bfd_link_pic (info))
798 return bad_static_reloc (abfd, r_type, h);
799 /* Fall through. */
800
801 case R_RISCV_COPY:
802 case R_RISCV_JUMP_SLOT:
803 case R_RISCV_RELATIVE:
804 case R_RISCV_64:
805 case R_RISCV_32:
806 /* Fall through. */
807
808 static_reloc:
e23eba97 809
02dd9d25
NC
810 if (h != NULL
811 && (!bfd_link_pic (info)
812 || h->type == STT_GNU_IFUNC))
e23eba97 813 {
02dd9d25
NC
814 /* This reloc might not bind locally. */
815 h->non_got_ref = 1;
816 h->pointer_equality_needed = 1;
817
818 if (!h->def_regular
819 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
820 {
821 /* We may need a .plt entry if the symbol is a function
822 defined in a shared lib or is a function referenced
823 from the code or read-only section. */
824 h->plt.refcount += 1;
825 }
e23eba97
NC
826 }
827
828 /* If we are creating a shared library, and this is a reloc
829 against a global symbol, or a non PC relative reloc
830 against a local symbol, then we need to copy the reloc
831 into the shared library. However, if we are linking with
832 -Bsymbolic, we do not need to copy a reloc against a
833 global symbol which is defined in an object we are
834 including in the link (i.e., DEF_REGULAR is set). At
835 this point we have not seen all the input files, so it is
836 possible that DEF_REGULAR is not set now but will be set
837 later (it is never cleared). In case of a weak definition,
838 DEF_REGULAR may be cleared later by a strong definition in
839 a shared library. We account for that possibility below by
840 storing information in the relocs_copied field of the hash
841 table entry. A similar situation occurs when creating
842 shared libraries and symbol visibility changes render the
843 symbol local.
844
845 If on the other hand, we are creating an executable, we
846 may need to keep relocations for symbols satisfied by a
847 dynamic library if we manage to avoid copy relocs for the
02dd9d25
NC
848 symbol.
849
850 Generate dynamic pointer relocation against STT_GNU_IFUNC
851 symbol in the non-code section (R_RISCV_32/R_RISCV_64). */
f3185997
NC
852 reloc_howto_type * r = riscv_elf_rtype_to_howto (abfd, r_type);
853
e23eba97
NC
854 if ((bfd_link_pic (info)
855 && (sec->flags & SEC_ALLOC) != 0
02dd9d25 856 && ((r != NULL && !r->pc_relative)
e23eba97 857 || (h != NULL
02dd9d25 858 && (!info->symbolic
e23eba97
NC
859 || h->root.type == bfd_link_hash_defweak
860 || !h->def_regular))))
861 || (!bfd_link_pic (info)
862 && (sec->flags & SEC_ALLOC) != 0
863 && h != NULL
864 && (h->root.type == bfd_link_hash_defweak
02dd9d25
NC
865 || !h->def_regular))
866 || (!bfd_link_pic (info)
867 && h != NULL
868 && h->type == STT_GNU_IFUNC
869 && (sec->flags & SEC_CODE) == 0))
e23eba97 870 {
3bf083ed
AM
871 struct elf_dyn_relocs *p;
872 struct elf_dyn_relocs **head;
e23eba97
NC
873
874 /* When creating a shared object, we must copy these
875 relocs into the output file. We create a reloc
876 section in dynobj and make room for the reloc. */
877 if (sreloc == NULL)
878 {
879 sreloc = _bfd_elf_make_dynamic_reloc_section
880 (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
0a1b45a2 881 abfd, /*rela?*/ true);
e23eba97
NC
882
883 if (sreloc == NULL)
0a1b45a2 884 return false;
e23eba97
NC
885 }
886
887 /* If this is a global symbol, we count the number of
888 relocations we need for this symbol. */
889 if (h != NULL)
190eb1dd 890 head = &h->dyn_relocs;
e23eba97
NC
891 else
892 {
893 /* Track dynamic relocs needed for local syms too.
894 We really need local syms available to do this
895 easily. Oh well. */
896
897 asection *s;
898 void *vpp;
899 Elf_Internal_Sym *isym;
900
f1dfbfdb 901 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
e23eba97
NC
902 abfd, r_symndx);
903 if (isym == NULL)
0a1b45a2 904 return false;
e23eba97
NC
905
906 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
907 if (s == NULL)
908 s = sec;
909
910 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 911 head = (struct elf_dyn_relocs **) vpp;
e23eba97
NC
912 }
913
914 p = *head;
915 if (p == NULL || p->sec != sec)
916 {
986f0783 917 size_t amt = sizeof *p;
3bf083ed 918 p = ((struct elf_dyn_relocs *)
e23eba97
NC
919 bfd_alloc (htab->elf.dynobj, amt));
920 if (p == NULL)
0a1b45a2 921 return false;
e23eba97
NC
922 p->next = *head;
923 *head = p;
924 p->sec = sec;
925 p->count = 0;
926 p->pc_count = 0;
927 }
928
929 p->count += 1;
f3185997 930 p->pc_count += r == NULL ? 0 : r->pc_relative;
e23eba97
NC
931 }
932
933 break;
934
e23eba97
NC
935 default:
936 break;
937 }
938 }
939
0a1b45a2 940 return true;
e23eba97
NC
941}
942
e23eba97
NC
943/* Adjust a symbol defined by a dynamic object and referenced by a
944 regular object. The current definition is in some section of the
945 dynamic object, but we're not including those sections. We have to
946 change the definition to something the rest of the link can
947 understand. */
948
0a1b45a2 949static bool
e23eba97
NC
950riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
951 struct elf_link_hash_entry *h)
952{
953 struct riscv_elf_link_hash_table *htab;
954 struct riscv_elf_link_hash_entry * eh;
e23eba97 955 bfd *dynobj;
5474d94f 956 asection *s, *srel;
e23eba97
NC
957
958 htab = riscv_elf_hash_table (info);
959 BFD_ASSERT (htab != NULL);
960
961 dynobj = htab->elf.dynobj;
962
963 /* Make sure we know what is going on here. */
964 BFD_ASSERT (dynobj != NULL
965 && (h->needs_plt
966 || h->type == STT_GNU_IFUNC
60d67dc8 967 || h->is_weakalias
e23eba97
NC
968 || (h->def_dynamic
969 && h->ref_regular
970 && !h->def_regular)));
971
972 /* If this is a function, put it in the procedure linkage table. We
973 will fill in the contents of the procedure linkage table later
974 (although we could actually do it here). */
975 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
976 {
977 if (h->plt.refcount <= 0
02dd9d25
NC
978 || (h->type != STT_GNU_IFUNC
979 && (SYMBOL_CALLS_LOCAL (info, h)
980 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
981 && h->root.type == bfd_link_hash_undefweak))))
e23eba97
NC
982 {
983 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
984 input file, but the symbol was never referred to by a dynamic
985 object, or if all references were garbage collected. In such
986 a case, we don't actually need to build a PLT entry. */
987 h->plt.offset = (bfd_vma) -1;
988 h->needs_plt = 0;
989 }
990
0a1b45a2 991 return true;
e23eba97
NC
992 }
993 else
994 h->plt.offset = (bfd_vma) -1;
995
996 /* If this is a weak symbol, and there is a real definition, the
997 processor independent code will have arranged for us to see the
998 real definition first, and we can just use the same value. */
60d67dc8 999 if (h->is_weakalias)
e23eba97 1000 {
60d67dc8
AM
1001 struct elf_link_hash_entry *def = weakdef (h);
1002 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1003 h->root.u.def.section = def->root.u.def.section;
1004 h->root.u.def.value = def->root.u.def.value;
0a1b45a2 1005 return true;
e23eba97
NC
1006 }
1007
1008 /* This is a reference to a symbol defined by a dynamic object which
1009 is not a function. */
1010
1011 /* If we are creating a shared library, we must presume that the
1012 only references to the symbol are via the global offset table.
1013 For such cases we need not do anything here; the relocations will
1014 be handled correctly by relocate_section. */
1015 if (bfd_link_pic (info))
0a1b45a2 1016 return true;
e23eba97
NC
1017
1018 /* If there are no references to this symbol that do not use the
1019 GOT, we don't need to generate a copy reloc. */
1020 if (!h->non_got_ref)
0a1b45a2 1021 return true;
e23eba97
NC
1022
1023 /* If -z nocopyreloc was given, we won't generate them either. */
1024 if (info->nocopyreloc)
1025 {
1026 h->non_got_ref = 0;
0a1b45a2 1027 return true;
e23eba97
NC
1028 }
1029
3bf083ed 1030 /* If we don't find any dynamic relocs in read-only sections, then
e23eba97 1031 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5dbc8b37 1032 if (!_bfd_elf_readonly_dynrelocs (h))
e23eba97
NC
1033 {
1034 h->non_got_ref = 0;
0a1b45a2 1035 return true;
e23eba97
NC
1036 }
1037
1038 /* We must allocate the symbol in our .dynbss section, which will
1039 become part of the .bss section of the executable. There will be
1040 an entry for this symbol in the .dynsym section. The dynamic
1041 object will contain position independent code, so all references
1042 from the dynamic object to this symbol will go through the global
1043 offset table. The dynamic linker will use the .dynsym entry to
1044 determine the address it must put in the global offset table, so
1045 both the dynamic object and the regular object will refer to the
1046 same memory location for the variable. */
1047
1048 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1049 to copy the initial value out of the dynamic object and into the
1050 runtime process image. We need to remember the offset into the
1051 .rel.bss section we are going to use. */
3bf083ed 1052 eh = (struct riscv_elf_link_hash_entry *) h;
3df5cd13
AW
1053 if (eh->tls_type & ~GOT_NORMAL)
1054 {
1055 s = htab->sdyntdata;
1056 srel = htab->elf.srelbss;
1057 }
1058 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
5474d94f
AM
1059 {
1060 s = htab->elf.sdynrelro;
1061 srel = htab->elf.sreldynrelro;
1062 }
1063 else
1064 {
1065 s = htab->elf.sdynbss;
1066 srel = htab->elf.srelbss;
1067 }
e23eba97
NC
1068 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
1069 {
5474d94f 1070 srel->size += sizeof (ElfNN_External_Rela);
e23eba97
NC
1071 h->needs_copy = 1;
1072 }
1073
5474d94f 1074 return _bfd_elf_adjust_dynamic_copy (info, h, s);
e23eba97
NC
1075}
1076
1077/* Allocate space in .plt, .got and associated reloc sections for
1078 dynamic relocs. */
1079
0a1b45a2 1080static bool
e23eba97
NC
1081allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1082{
1083 struct bfd_link_info *info;
1084 struct riscv_elf_link_hash_table *htab;
3bf083ed 1085 struct elf_dyn_relocs *p;
e23eba97
NC
1086
1087 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 1088 return true;
e23eba97
NC
1089
1090 info = (struct bfd_link_info *) inf;
1091 htab = riscv_elf_hash_table (info);
1092 BFD_ASSERT (htab != NULL);
1093
18b98722
NC
1094 /* When we are generating pde, make sure gp symbol is output as a
1095 dynamic symbol. Then ld.so can set the gp register earlier, before
1096 resolving the ifunc. */
1097 if (!bfd_link_pic (info)
1098 && htab->elf.dynamic_sections_created
1099 && strcmp (h->root.root.string, RISCV_GP_SYMBOL) == 0
1100 && !bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 1101 return false;
18b98722 1102
02dd9d25
NC
1103 /* Since STT_GNU_IFUNC symbols must go through PLT, we handle them
1104 in the allocate_ifunc_dynrelocs and allocate_local_ifunc_dynrelocs,
1105 if they are defined and referenced in a non-shared object. */
1106 if (h->type == STT_GNU_IFUNC
1107 && h->def_regular)
0a1b45a2 1108 return true;
02dd9d25
NC
1109 else if (htab->elf.dynamic_sections_created
1110 && h->plt.refcount > 0)
e23eba97
NC
1111 {
1112 /* Make sure this symbol is output as a dynamic symbol.
1113 Undefined weak syms won't yet be marked as dynamic. */
1114 if (h->dynindx == -1
1115 && !h->forced_local)
1116 {
1117 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 1118 return false;
e23eba97
NC
1119 }
1120
1121 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
1122 {
1123 asection *s = htab->elf.splt;
1124
1125 if (s->size == 0)
1126 s->size = PLT_HEADER_SIZE;
1127
1128 h->plt.offset = s->size;
1129
1130 /* Make room for this entry. */
1131 s->size += PLT_ENTRY_SIZE;
1132
1133 /* We also need to make an entry in the .got.plt section. */
1134 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1135
1136 /* We also need to make an entry in the .rela.plt section. */
1137 htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
1138
1139 /* If this symbol is not defined in a regular file, and we are
1140 not generating a shared library, then set the symbol to this
1141 location in the .plt. This is required to make function
1142 pointers compare as equal between the normal executable and
1143 the shared library. */
1144 if (! bfd_link_pic (info)
1145 && !h->def_regular)
1146 {
1147 h->root.u.def.section = s;
1148 h->root.u.def.value = h->plt.offset;
1149 }
8155b853
NC
1150
1151 /* If the symbol has STO_RISCV_VARIANT_CC flag, then raise the
1152 variant_cc flag of riscv_elf_link_hash_table. */
1153 if (h->other & STO_RISCV_VARIANT_CC)
1154 htab->variant_cc = 1;
e23eba97
NC
1155 }
1156 else
1157 {
1158 h->plt.offset = (bfd_vma) -1;
1159 h->needs_plt = 0;
1160 }
1161 }
1162 else
1163 {
1164 h->plt.offset = (bfd_vma) -1;
1165 h->needs_plt = 0;
1166 }
1167
1168 if (h->got.refcount > 0)
1169 {
1170 asection *s;
0a1b45a2 1171 bool dyn;
e23eba97
NC
1172 int tls_type = riscv_elf_hash_entry (h)->tls_type;
1173
1174 /* Make sure this symbol is output as a dynamic symbol.
1175 Undefined weak syms won't yet be marked as dynamic. */
1176 if (h->dynindx == -1
1177 && !h->forced_local)
1178 {
1179 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 1180 return false;
e23eba97
NC
1181 }
1182
1183 s = htab->elf.sgot;
1184 h->got.offset = s->size;
1185 dyn = htab->elf.dynamic_sections_created;
1186 if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
1187 {
1188 /* TLS_GD needs two dynamic relocs and two GOT slots. */
1189 if (tls_type & GOT_TLS_GD)
1190 {
1191 s->size += 2 * RISCV_ELF_WORD_BYTES;
1192 htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
1193 }
1194
1195 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1196 if (tls_type & GOT_TLS_IE)
1197 {
1198 s->size += RISCV_ELF_WORD_BYTES;
1199 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1200 }
1201 }
1202 else
1203 {
1204 s->size += RISCV_ELF_WORD_BYTES;
6487709f
JW
1205 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1206 && ! UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
e23eba97
NC
1207 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1208 }
1209 }
1210 else
1211 h->got.offset = (bfd_vma) -1;
1212
190eb1dd 1213 if (h->dyn_relocs == NULL)
0a1b45a2 1214 return true;
e23eba97
NC
1215
1216 /* In the shared -Bsymbolic case, discard space allocated for
1217 dynamic pc-relative relocs against symbols which turn out to be
1218 defined in regular objects. For the normal shared case, discard
1219 space for pc-relative relocs that have become local due to symbol
1220 visibility changes. */
1221
1222 if (bfd_link_pic (info))
1223 {
1224 if (SYMBOL_CALLS_LOCAL (info, h))
1225 {
3bf083ed 1226 struct elf_dyn_relocs **pp;
e23eba97 1227
190eb1dd 1228 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
e23eba97
NC
1229 {
1230 p->count -= p->pc_count;
1231 p->pc_count = 0;
1232 if (p->count == 0)
1233 *pp = p->next;
1234 else
1235 pp = &p->next;
1236 }
1237 }
1238
1239 /* Also discard relocs on undefined weak syms with non-default
1240 visibility. */
190eb1dd 1241 if (h->dyn_relocs != NULL
e23eba97
NC
1242 && h->root.type == bfd_link_hash_undefweak)
1243 {
6487709f
JW
1244 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1245 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
190eb1dd 1246 h->dyn_relocs = NULL;
e23eba97
NC
1247
1248 /* Make sure undefined weak symbols are output as a dynamic
1249 symbol in PIEs. */
1250 else if (h->dynindx == -1
1251 && !h->forced_local)
1252 {
1253 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 1254 return false;
e23eba97
NC
1255 }
1256 }
1257 }
1258 else
1259 {
1260 /* For the non-shared case, discard space for relocs against
1261 symbols which turn out to need copy relocs or are not
1262 dynamic. */
1263
1264 if (!h->non_got_ref
1265 && ((h->def_dynamic
1266 && !h->def_regular)
1267 || (htab->elf.dynamic_sections_created
1268 && (h->root.type == bfd_link_hash_undefweak
1269 || h->root.type == bfd_link_hash_undefined))))
1270 {
1271 /* Make sure this symbol is output as a dynamic symbol.
1272 Undefined weak syms won't yet be marked as dynamic. */
1273 if (h->dynindx == -1
1274 && !h->forced_local)
1275 {
1276 if (! bfd_elf_link_record_dynamic_symbol (info, h))
0a1b45a2 1277 return false;
e23eba97
NC
1278 }
1279
1280 /* If that succeeded, we know we'll be keeping all the
1281 relocs. */
1282 if (h->dynindx != -1)
1283 goto keep;
1284 }
1285
190eb1dd 1286 h->dyn_relocs = NULL;
e23eba97
NC
1287
1288 keep: ;
1289 }
1290
1291 /* Finally, allocate space. */
190eb1dd 1292 for (p = h->dyn_relocs; p != NULL; p = p->next)
e23eba97
NC
1293 {
1294 asection *sreloc = elf_section_data (p->sec)->sreloc;
1295 sreloc->size += p->count * sizeof (ElfNN_External_Rela);
1296 }
1297
0a1b45a2 1298 return true;
e23eba97
NC
1299}
1300
02dd9d25
NC
1301/* Allocate space in .plt, .got and associated reloc sections for
1302 ifunc dynamic relocs. */
1303
0a1b45a2 1304static bool
02dd9d25
NC
1305allocate_ifunc_dynrelocs (struct elf_link_hash_entry *h,
1306 void *inf)
1307{
1308 struct bfd_link_info *info;
1309
1310 if (h->root.type == bfd_link_hash_indirect)
0a1b45a2 1311 return true;
02dd9d25
NC
1312
1313 if (h->root.type == bfd_link_hash_warning)
1314 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1315
1316 info = (struct bfd_link_info *) inf;
1317
1318 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1319 here if it is defined and referenced in a non-shared object. */
1320 if (h->type == STT_GNU_IFUNC
1321 && h->def_regular)
1322 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1323 &h->dyn_relocs,
1324 PLT_ENTRY_SIZE,
1325 PLT_HEADER_SIZE,
1326 GOT_ENTRY_SIZE,
0a1b45a2
AM
1327 true);
1328 return true;
02dd9d25
NC
1329}
1330
1331/* Allocate space in .plt, .got and associated reloc sections for
1332 local ifunc dynamic relocs. */
1333
1201fda6 1334static int
02dd9d25
NC
1335allocate_local_ifunc_dynrelocs (void **slot, void *inf)
1336{
1337 struct elf_link_hash_entry *h
1338 = (struct elf_link_hash_entry *) *slot;
1339
1340 if (h->type != STT_GNU_IFUNC
1341 || !h->def_regular
1342 || !h->ref_regular
1343 || !h->forced_local
1344 || h->root.type != bfd_link_hash_defined)
1345 abort ();
1346
1347 return allocate_ifunc_dynrelocs (h, inf);
1348}
1349
0a1b45a2 1350static bool
e23eba97
NC
1351riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1352{
1353 struct riscv_elf_link_hash_table *htab;
1354 bfd *dynobj;
1355 asection *s;
1356 bfd *ibfd;
1357
1358 htab = riscv_elf_hash_table (info);
1359 BFD_ASSERT (htab != NULL);
1360 dynobj = htab->elf.dynobj;
1361 BFD_ASSERT (dynobj != NULL);
1362
1363 if (elf_hash_table (info)->dynamic_sections_created)
1364 {
1365 /* Set the contents of the .interp section to the interpreter. */
1366 if (bfd_link_executable (info) && !info->nointerp)
1367 {
1368 s = bfd_get_linker_section (dynobj, ".interp");
1369 BFD_ASSERT (s != NULL);
1370 s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
1371 s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
1372 }
1373 }
1374
1375 /* Set up .got offsets for local syms, and space for local dynamic
1376 relocs. */
1377 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1378 {
1379 bfd_signed_vma *local_got;
1380 bfd_signed_vma *end_local_got;
1381 char *local_tls_type;
1382 bfd_size_type locsymcount;
1383 Elf_Internal_Shdr *symtab_hdr;
1384 asection *srel;
1385
1386 if (! is_riscv_elf (ibfd))
1387 continue;
1388
1389 for (s = ibfd->sections; s != NULL; s = s->next)
1390 {
3bf083ed 1391 struct elf_dyn_relocs *p;
e23eba97
NC
1392
1393 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1394 {
1395 if (!bfd_is_abs_section (p->sec)
1396 && bfd_is_abs_section (p->sec->output_section))
1397 {
1398 /* Input section has been discarded, either because
1399 it is a copy of a linkonce section or due to
1400 linker script /DISCARD/, so we'll be discarding
1401 the relocs too. */
1402 }
1403 else if (p->count != 0)
1404 {
1405 srel = elf_section_data (p->sec)->sreloc;
1406 srel->size += p->count * sizeof (ElfNN_External_Rela);
1407 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1408 info->flags |= DF_TEXTREL;
1409 }
1410 }
1411 }
1412
1413 local_got = elf_local_got_refcounts (ibfd);
1414 if (!local_got)
1415 continue;
1416
1417 symtab_hdr = &elf_symtab_hdr (ibfd);
1418 locsymcount = symtab_hdr->sh_info;
1419 end_local_got = local_got + locsymcount;
1420 local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
1421 s = htab->elf.sgot;
1422 srel = htab->elf.srelgot;
1423 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1424 {
1425 if (*local_got > 0)
1426 {
1427 *local_got = s->size;
1428 s->size += RISCV_ELF_WORD_BYTES;
1429 if (*local_tls_type & GOT_TLS_GD)
1430 s->size += RISCV_ELF_WORD_BYTES;
1431 if (bfd_link_pic (info)
1432 || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
1433 srel->size += sizeof (ElfNN_External_Rela);
1434 }
1435 else
1436 *local_got = (bfd_vma) -1;
1437 }
1438 }
1439
02dd9d25
NC
1440 /* Allocate .plt and .got entries and space dynamic relocs for
1441 global symbols. */
e23eba97
NC
1442 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1443
02dd9d25
NC
1444 /* Allocate .plt and .got entries and space dynamic relocs for
1445 global ifunc symbols. */
1446 elf_link_hash_traverse (&htab->elf, allocate_ifunc_dynrelocs, info);
1447
1448 /* Allocate .plt and .got entries and space dynamic relocs for
1449 local ifunc symbols. */
1450 htab_traverse (htab->loc_hash_table, allocate_local_ifunc_dynrelocs, info);
1451
51a8a7c2
NC
1452 /* Used to resolve the dynamic relocs overwite problems when
1453 generating static executable. */
1454 if (htab->elf.irelplt)
1455 htab->last_iplt_index = htab->elf.irelplt->reloc_count - 1;
1456
e23eba97
NC
1457 if (htab->elf.sgotplt)
1458 {
1459 struct elf_link_hash_entry *got;
1460 got = elf_link_hash_lookup (elf_hash_table (info),
1461 "_GLOBAL_OFFSET_TABLE_",
0a1b45a2 1462 false, false, false);
e23eba97
NC
1463
1464 /* Don't allocate .got.plt section if there are no GOT nor PLT
1465 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1466 if ((got == NULL
1467 || !got->ref_regular_nonweak)
1468 && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
1469 && (htab->elf.splt == NULL
1470 || htab->elf.splt->size == 0)
1471 && (htab->elf.sgot == NULL
1472 || (htab->elf.sgot->size
1473 == get_elf_backend_data (output_bfd)->got_header_size)))
1474 htab->elf.sgotplt->size = 0;
1475 }
1476
1477 /* The check_relocs and adjust_dynamic_symbol entry points have
1478 determined the sizes of the various dynamic sections. Allocate
1479 memory for them. */
1480 for (s = dynobj->sections; s != NULL; s = s->next)
1481 {
1482 if ((s->flags & SEC_LINKER_CREATED) == 0)
1483 continue;
1484
1485 if (s == htab->elf.splt
1486 || s == htab->elf.sgot
1487 || s == htab->elf.sgotplt
02dd9d25
NC
1488 || s == htab->elf.iplt
1489 || s == htab->elf.igotplt
5474d94f 1490 || s == htab->elf.sdynbss
3e1b4df8
JW
1491 || s == htab->elf.sdynrelro
1492 || s == htab->sdyntdata)
e23eba97
NC
1493 {
1494 /* Strip this section if we don't need it; see the
1495 comment below. */
1496 }
3f3328b8 1497 else if (startswith (s->name, ".rela"))
e23eba97
NC
1498 {
1499 if (s->size != 0)
1500 {
1501 /* We use the reloc_count field as a counter if we need
1502 to copy relocs into the output file. */
1503 s->reloc_count = 0;
1504 }
1505 }
1506 else
1507 {
1508 /* It's not one of our sections. */
1509 continue;
1510 }
1511
1512 if (s->size == 0)
1513 {
1514 /* If we don't need this section, strip it from the
1515 output file. This is mostly to handle .rela.bss and
1516 .rela.plt. We must create both sections in
1517 create_dynamic_sections, because they must be created
1518 before the linker maps input sections to output
1519 sections. The linker does that before
1520 adjust_dynamic_symbol is called, and it is that
1521 function which decides whether anything needs to go
1522 into these sections. */
1523 s->flags |= SEC_EXCLUDE;
1524 continue;
1525 }
1526
1527 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1528 continue;
1529
1530 /* Allocate memory for the section contents. Zero the memory
1531 for the benefit of .rela.plt, which has 4 unused entries
1532 at the beginning, and we don't want garbage. */
1533 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1534 if (s->contents == NULL)
0a1b45a2 1535 return false;
e23eba97
NC
1536 }
1537
8155b853
NC
1538 /* Add dynamic entries. */
1539 if (elf_hash_table (info)->dynamic_sections_created)
1540 {
1541 if (!_bfd_elf_add_dynamic_tags (output_bfd, info, true))
1542 return false;
1543
1544 if (htab->variant_cc
1545 && !_bfd_elf_add_dynamic_entry (info, DT_RISCV_VARIANT_CC, 0))
1546 return false;
1547 }
1548
1549 return true;
e23eba97
NC
1550}
1551
1552#define TP_OFFSET 0
1553#define DTP_OFFSET 0x800
1554
1555/* Return the relocation value for a TLS dtp-relative reloc. */
1556
1557static bfd_vma
1558dtpoff (struct bfd_link_info *info, bfd_vma address)
1559{
1560 /* If tls_sec is NULL, we should have signalled an error already. */
1561 if (elf_hash_table (info)->tls_sec == NULL)
1562 return 0;
1563 return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1564}
1565
1566/* Return the relocation value for a static TLS tp-relative relocation. */
1567
1568static bfd_vma
1569tpoff (struct bfd_link_info *info, bfd_vma address)
1570{
1571 /* If tls_sec is NULL, we should have signalled an error already. */
1572 if (elf_hash_table (info)->tls_sec == NULL)
1573 return 0;
1574 return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1575}
1576
1577/* Return the global pointer's value, or 0 if it is not in use. */
1578
1579static bfd_vma
1580riscv_global_pointer_value (struct bfd_link_info *info)
1581{
1582 struct bfd_link_hash_entry *h;
1583
0a1b45a2 1584 h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, false, false, true);
e23eba97
NC
1585 if (h == NULL || h->type != bfd_link_hash_defined)
1586 return 0;
1587
1588 return h->u.def.value + sec_addr (h->u.def.section);
1589}
1590
1591/* Emplace a static relocation. */
1592
1593static bfd_reloc_status_type
1594perform_relocation (const reloc_howto_type *howto,
1595 const Elf_Internal_Rela *rel,
1596 bfd_vma value,
1597 asection *input_section,
1598 bfd *input_bfd,
1599 bfd_byte *contents)
1600{
1601 if (howto->pc_relative)
1602 value -= sec_addr (input_section) + rel->r_offset;
1603 value += rel->r_addend;
1604
1605 switch (ELFNN_R_TYPE (rel->r_info))
1606 {
1607 case R_RISCV_HI20:
1608 case R_RISCV_TPREL_HI20:
1609 case R_RISCV_PCREL_HI20:
1610 case R_RISCV_GOT_HI20:
1611 case R_RISCV_TLS_GOT_HI20:
1612 case R_RISCV_TLS_GD_HI20:
1613 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1614 return bfd_reloc_overflow;
1615 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1616 break;
1617
1618 case R_RISCV_LO12_I:
1619 case R_RISCV_GPREL_I:
1620 case R_RISCV_TPREL_LO12_I:
45f76423 1621 case R_RISCV_TPREL_I:
e23eba97
NC
1622 case R_RISCV_PCREL_LO12_I:
1623 value = ENCODE_ITYPE_IMM (value);
1624 break;
1625
1626 case R_RISCV_LO12_S:
1627 case R_RISCV_GPREL_S:
1628 case R_RISCV_TPREL_LO12_S:
45f76423 1629 case R_RISCV_TPREL_S:
e23eba97
NC
1630 case R_RISCV_PCREL_LO12_S:
1631 value = ENCODE_STYPE_IMM (value);
1632 break;
1633
1634 case R_RISCV_CALL:
1635 case R_RISCV_CALL_PLT:
1636 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1637 return bfd_reloc_overflow;
1638 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1639 | (ENCODE_ITYPE_IMM (value) << 32);
1640 break;
1641
1642 case R_RISCV_JAL:
5a9f5403 1643 if (!VALID_JTYPE_IMM (value))
e23eba97 1644 return bfd_reloc_overflow;
5a9f5403 1645 value = ENCODE_JTYPE_IMM (value);
e23eba97
NC
1646 break;
1647
1648 case R_RISCV_BRANCH:
5a9f5403 1649 if (!VALID_BTYPE_IMM (value))
e23eba97 1650 return bfd_reloc_overflow;
5a9f5403 1651 value = ENCODE_BTYPE_IMM (value);
e23eba97
NC
1652 break;
1653
1654 case R_RISCV_RVC_BRANCH:
5a9f5403 1655 if (!VALID_CBTYPE_IMM (value))
e23eba97 1656 return bfd_reloc_overflow;
5a9f5403 1657 value = ENCODE_CBTYPE_IMM (value);
e23eba97
NC
1658 break;
1659
1660 case R_RISCV_RVC_JUMP:
5a9f5403 1661 if (!VALID_CJTYPE_IMM (value))
e23eba97 1662 return bfd_reloc_overflow;
5a9f5403 1663 value = ENCODE_CJTYPE_IMM (value);
e23eba97
NC
1664 break;
1665
1666 case R_RISCV_RVC_LUI:
080a4883
JW
1667 if (RISCV_CONST_HIGH_PART (value) == 0)
1668 {
1669 /* Linker relaxation can convert an address equal to or greater than
1670 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1671 valid immediate. We can fix this by converting it to a C.LI. */
fbc09e7a
MC
1672 bfd_vma insn = riscv_get_insn (howto->bitsize,
1673 contents + rel->r_offset);
080a4883 1674 insn = (insn & ~MATCH_C_LUI) | MATCH_C_LI;
fbc09e7a 1675 riscv_put_insn (howto->bitsize, insn, contents + rel->r_offset);
5a9f5403 1676 value = ENCODE_CITYPE_IMM (0);
080a4883 1677 }
5a9f5403 1678 else if (!VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
e23eba97 1679 return bfd_reloc_overflow;
080a4883 1680 else
5a9f5403 1681 value = ENCODE_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (value));
e23eba97
NC
1682 break;
1683
1684 case R_RISCV_32:
1685 case R_RISCV_64:
1686 case R_RISCV_ADD8:
1687 case R_RISCV_ADD16:
1688 case R_RISCV_ADD32:
1689 case R_RISCV_ADD64:
45f76423 1690 case R_RISCV_SUB6:
e23eba97
NC
1691 case R_RISCV_SUB8:
1692 case R_RISCV_SUB16:
1693 case R_RISCV_SUB32:
1694 case R_RISCV_SUB64:
45f76423
AW
1695 case R_RISCV_SET6:
1696 case R_RISCV_SET8:
1697 case R_RISCV_SET16:
1698 case R_RISCV_SET32:
a6cbf936 1699 case R_RISCV_32_PCREL:
e23eba97
NC
1700 case R_RISCV_TLS_DTPREL32:
1701 case R_RISCV_TLS_DTPREL64:
1702 break;
1703
ff6f4d9b
PD
1704 case R_RISCV_DELETE:
1705 return bfd_reloc_ok;
1706
e23eba97
NC
1707 default:
1708 return bfd_reloc_notsupported;
1709 }
1710
fbc09e7a
MC
1711 bfd_vma word;
1712 if (riscv_is_insn_reloc (howto))
1713 word = riscv_get_insn (howto->bitsize, contents + rel->r_offset);
1714 else
1715 word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
e23eba97 1716 word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
fbc09e7a
MC
1717 if (riscv_is_insn_reloc (howto))
1718 riscv_put_insn (howto->bitsize, word, contents + rel->r_offset);
1719 else
1720 bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
e23eba97
NC
1721
1722 return bfd_reloc_ok;
1723}
1724
1725/* Remember all PC-relative high-part relocs we've encountered to help us
1726 later resolve the corresponding low-part relocs. */
1727
1728typedef struct
1729{
50331d64 1730 /* PC value. */
e23eba97 1731 bfd_vma address;
50331d64 1732 /* Relocation value with addend. */
e23eba97 1733 bfd_vma value;
50331d64
NC
1734 /* Original reloc type. */
1735 int type;
e23eba97
NC
1736} riscv_pcrel_hi_reloc;
1737
1738typedef struct riscv_pcrel_lo_reloc
1739{
50331d64
NC
1740 /* PC value of auipc. */
1741 bfd_vma address;
1742 /* Internal relocation. */
1743 const Elf_Internal_Rela *reloc;
1744 /* Record the following information helps to resolve the %pcrel
1745 which cross different input section. For now we build a hash
1746 for pcrel at the start of riscv_elf_relocate_section, and then
1747 free the hash at the end. But riscv_elf_relocate_section only
1748 handles an input section at a time, so that means we can only
1749 resolve the %pcrel_hi and %pcrel_lo which are in the same input
1750 section. Otherwise, we will report dangerous relocation errors
1751 for those %pcrel which are not in the same input section. */
1942a048
NC
1752 asection *input_section;
1753 struct bfd_link_info *info;
1754 reloc_howto_type *howto;
1942a048 1755 bfd_byte *contents;
50331d64 1756 /* The next riscv_pcrel_lo_reloc. */
1942a048 1757 struct riscv_pcrel_lo_reloc *next;
e23eba97
NC
1758} riscv_pcrel_lo_reloc;
1759
1760typedef struct
1761{
50331d64 1762 /* Hash table for riscv_pcrel_hi_reloc. */
e23eba97 1763 htab_t hi_relocs;
50331d64 1764 /* Linked list for riscv_pcrel_lo_reloc. */
e23eba97
NC
1765 riscv_pcrel_lo_reloc *lo_relocs;
1766} riscv_pcrel_relocs;
1767
1768static hashval_t
1769riscv_pcrel_reloc_hash (const void *entry)
1770{
1771 const riscv_pcrel_hi_reloc *e = entry;
1772 return (hashval_t)(e->address >> 2);
1773}
1774
1201fda6 1775static int
e23eba97
NC
1776riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
1777{
1778 const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
1779 return e1->address == e2->address;
1780}
1781
0a1b45a2 1782static bool
e23eba97
NC
1783riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
1784{
e23eba97
NC
1785 p->lo_relocs = NULL;
1786 p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
1787 riscv_pcrel_reloc_eq, free);
1788 return p->hi_relocs != NULL;
1789}
1790
1791static void
1792riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
1793{
1794 riscv_pcrel_lo_reloc *cur = p->lo_relocs;
1795
1796 while (cur != NULL)
1797 {
1798 riscv_pcrel_lo_reloc *next = cur->next;
1799 free (cur);
1800 cur = next;
1801 }
1802
1803 htab_delete (p->hi_relocs);
1804}
1805
0a1b45a2 1806static bool
b1308d2c
PD
1807riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
1808 struct bfd_link_info *info,
1809 bfd_vma pc,
1810 bfd_vma addr,
1811 bfd_byte *contents,
50331d64 1812 const reloc_howto_type *howto)
e23eba97 1813{
b1308d2c 1814 /* We may need to reference low addreses in PC-relative modes even when the
dcd709e0
NC
1815 PC is far away from these addresses. For example, undefweak references
1816 need to produce the address 0 when linked. As 0 is far from the arbitrary
1817 addresses that we can link PC-relative programs at, the linker can't
1818 actually relocate references to those symbols. In order to allow these
1819 programs to work we simply convert the PC-relative auipc sequences to
1820 0-relative lui sequences. */
b1308d2c 1821 if (bfd_link_pic (info))
0a1b45a2 1822 return false;
b1308d2c
PD
1823
1824 /* If it's possible to reference the symbol using auipc we do so, as that's
dcd709e0 1825 more in the spirit of the PC-relative relocations we're processing. */
b1308d2c
PD
1826 bfd_vma offset = addr - pc;
1827 if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
0a1b45a2 1828 return false;
b1308d2c
PD
1829
1830 /* If it's impossible to reference this with a LUI-based offset then don't
dcd709e0
NC
1831 bother to convert it at all so users still see the PC-relative relocation
1832 in the truncation message. */
b1308d2c 1833 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr)))
0a1b45a2 1834 return false;
b1308d2c 1835
1942a048 1836 rel->r_info = ELFNN_R_INFO (addr, R_RISCV_HI20);
b1308d2c 1837
1942a048 1838 bfd_vma insn = riscv_get_insn (howto->bitsize, contents + rel->r_offset);
b1308d2c 1839 insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
1942a048 1840 riscv_put_insn (howto->bitsize, insn, contents + rel->r_offset);
0a1b45a2 1841 return true;
b1308d2c
PD
1842}
1843
0a1b45a2 1844static bool
50331d64
NC
1845riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p,
1846 bfd_vma addr,
1847 bfd_vma value,
1848 int type,
1849 bool absolute)
b1308d2c
PD
1850{
1851 bfd_vma offset = absolute ? value : value - addr;
50331d64 1852 riscv_pcrel_hi_reloc entry = {addr, offset, type};
e23eba97
NC
1853 riscv_pcrel_hi_reloc **slot =
1854 (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
1855
1856 BFD_ASSERT (*slot == NULL);
1857 *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
1858 if (*slot == NULL)
0a1b45a2 1859 return false;
e23eba97 1860 **slot = entry;
0a1b45a2 1861 return true;
e23eba97
NC
1862}
1863
0a1b45a2 1864static bool
e23eba97 1865riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
50331d64
NC
1866 bfd_vma addr,
1867 const Elf_Internal_Rela *reloc,
e23eba97
NC
1868 asection *input_section,
1869 struct bfd_link_info *info,
1870 reloc_howto_type *howto,
e23eba97
NC
1871 bfd_byte *contents)
1872{
1873 riscv_pcrel_lo_reloc *entry;
1874 entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
1875 if (entry == NULL)
0a1b45a2 1876 return false;
50331d64
NC
1877 *entry = (riscv_pcrel_lo_reloc) {addr, reloc, input_section, info,
1878 howto, contents, p->lo_relocs};
e23eba97 1879 p->lo_relocs = entry;
0a1b45a2 1880 return true;
e23eba97
NC
1881}
1882
0a1b45a2 1883static bool
e23eba97
NC
1884riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
1885{
1886 riscv_pcrel_lo_reloc *r;
1887
1888 for (r = p->lo_relocs; r != NULL; r = r->next)
1889 {
1890 bfd *input_bfd = r->input_section->owner;
1891
50331d64 1892 riscv_pcrel_hi_reloc search = {r->address, 0, 0};
e23eba97 1893 riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
50331d64
NC
1894 /* There may be a risk if the %pcrel_lo with addend refers to
1895 an IFUNC symbol. The %pcrel_hi has been relocated to plt,
1896 so the corresponding %pcrel_lo with addend looks wrong. */
1897 char *string = NULL;
1898 if (entry == NULL)
1899 string = _("%pcrel_lo missing matching %pcrel_hi");
1900 else if (entry->type == R_RISCV_GOT_HI20
1901 && r->reloc->r_addend != 0)
1902 string = _("%pcrel_lo with addend isn't allowed for R_RISCV_GOT_HI20");
1903 else if (RISCV_CONST_HIGH_PART (entry->value)
1904 != RISCV_CONST_HIGH_PART (entry->value + r->reloc->r_addend))
07d6d2b8 1905 {
50331d64
NC
1906 /* Check the overflow when adding reloc addend. */
1907 if (asprintf (&string,
1908 _("%%pcrel_lo overflow with an addend, the "
1909 "value of %%pcrel_hi is 0x%" PRIx64 " without "
1910 "any addend, but may be 0x%" PRIx64 " after "
1911 "adding the %%pcrel_lo addend"),
1912 (int64_t) RISCV_CONST_HIGH_PART (entry->value),
1913 (int64_t) RISCV_CONST_HIGH_PART
75f03fa7
NC
1914 (entry->value + r->reloc->r_addend)) == -1)
1915 string = _("%pcrel_lo overflow with an addend");
50331d64 1916 }
75f03fa7 1917
50331d64
NC
1918 if (string != NULL)
1919 {
551703cf
JW
1920 (*r->info->callbacks->reloc_dangerous)
1921 (r->info, string, input_bfd, r->input_section, r->reloc->r_offset);
0a1b45a2 1922 return true;
07d6d2b8 1923 }
e23eba97
NC
1924
1925 perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
1926 input_bfd, r->contents);
1927 }
1928
0a1b45a2 1929 return true;
e23eba97
NC
1930}
1931
1932/* Relocate a RISC-V ELF section.
1933
1934 The RELOCATE_SECTION function is called by the new ELF backend linker
1935 to handle the relocations for a section.
1936
1937 The relocs are always passed as Rela structures.
1938
1939 This function is responsible for adjusting the section contents as
1940 necessary, and (if generating a relocatable output file) adjusting
1941 the reloc addend as necessary.
1942
1943 This function does not have to worry about setting the reloc
1944 address or the reloc symbol index.
1945
1946 LOCAL_SYMS is a pointer to the swapped in local symbols.
1947
1948 LOCAL_SECTIONS is an array giving the section in the input file
1949 corresponding to the st_shndx field of each local symbol.
1950
1951 The global hash table entry for the global symbols can be found
1952 via elf_sym_hashes (input_bfd).
1953
1954 When generating relocatable output, this function must handle
1955 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1956 going to be the section symbol corresponding to the output
1957 section, which means that the addend must be adjusted
1958 accordingly. */
1959
0f684201 1960static int
e23eba97
NC
1961riscv_elf_relocate_section (bfd *output_bfd,
1962 struct bfd_link_info *info,
1963 bfd *input_bfd,
1964 asection *input_section,
1965 bfd_byte *contents,
1966 Elf_Internal_Rela *relocs,
1967 Elf_Internal_Sym *local_syms,
1968 asection **local_sections)
1969{
1970 Elf_Internal_Rela *rel;
1971 Elf_Internal_Rela *relend;
1972 riscv_pcrel_relocs pcrel_relocs;
0a1b45a2 1973 bool ret = false;
e23eba97
NC
1974 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
1975 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1976 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1977 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
0a1b45a2 1978 bool absolute;
e23eba97
NC
1979
1980 if (!riscv_init_pcrel_relocs (&pcrel_relocs))
0a1b45a2 1981 return false;
e23eba97
NC
1982
1983 relend = relocs + input_section->reloc_count;
1984 for (rel = relocs; rel < relend; rel++)
1985 {
1986 unsigned long r_symndx;
1987 struct elf_link_hash_entry *h;
1988 Elf_Internal_Sym *sym;
1989 asection *sec;
1990 bfd_vma relocation;
1991 bfd_reloc_status_type r = bfd_reloc_ok;
02dd9d25 1992 const char *name = NULL;
e23eba97 1993 bfd_vma off, ie_off;
0a1b45a2 1994 bool unresolved_reloc, is_ie = false;
e23eba97
NC
1995 bfd_vma pc = sec_addr (input_section) + rel->r_offset;
1996 int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
0aa13fee 1997 reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
e23eba97 1998 const char *msg = NULL;
330a6637 1999 char *msg_buf = NULL;
0a1b45a2 2000 bool resolved_to_zero;
e23eba97 2001
45359528 2002 if (howto == NULL)
e23eba97
NC
2003 continue;
2004
2005 /* This is a final link. */
2006 r_symndx = ELFNN_R_SYM (rel->r_info);
2007 h = NULL;
2008 sym = NULL;
2009 sec = NULL;
0a1b45a2 2010 unresolved_reloc = false;
e23eba97
NC
2011 if (r_symndx < symtab_hdr->sh_info)
2012 {
2013 sym = local_syms + r_symndx;
2014 sec = local_sections[r_symndx];
2015 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
02dd9d25
NC
2016
2017 /* Relocate against local STT_GNU_IFUNC symbol. */
2018 if (!bfd_link_relocatable (info)
2019 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2020 {
0a1b45a2 2021 h = riscv_elf_get_local_sym_hash (htab, input_bfd, rel, false);
02dd9d25
NC
2022 if (h == NULL)
2023 abort ();
2024
2025 /* Set STT_GNU_IFUNC symbol value. */
2026 h->root.u.def.value = sym->st_value;
2027 h->root.u.def.section = sec;
2028 }
e23eba97
NC
2029 }
2030 else
2031 {
0a1b45a2 2032 bool warned, ignored;
e23eba97
NC
2033
2034 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2035 r_symndx, symtab_hdr, sym_hashes,
2036 h, sec, relocation,
2037 unresolved_reloc, warned, ignored);
2038 if (warned)
2039 {
2040 /* To avoid generating warning messages about truncated
2041 relocations, set the relocation's address to be the same as
2042 the start of this section. */
2043 if (input_section->output_section != NULL)
2044 relocation = input_section->output_section->vma;
2045 else
2046 relocation = 0;
2047 }
2048 }
2049
2050 if (sec != NULL && discarded_section (sec))
2051 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2052 rel, 1, relend, howto, 0, contents);
2053
2054 if (bfd_link_relocatable (info))
2055 continue;
2056
02dd9d25
NC
2057 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2058 it here if it is defined in a non-shared object. */
2059 if (h != NULL
2060 && h->type == STT_GNU_IFUNC
2061 && h->def_regular)
2062 {
2063 asection *plt, *base_got;
2064
2065 if ((input_section->flags & SEC_ALLOC) == 0)
2066 {
2067 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2068 STT_GNU_IFUNC symbol as STT_FUNC. */
2069 if (elf_section_type (input_section) == SHT_NOTE)
2070 goto skip_ifunc;
2071
2072 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2073 sections because such sections are not SEC_ALLOC and
2074 thus ld.so will not process them. */
2075 if ((input_section->flags & SEC_DEBUGGING) != 0)
2076 continue;
2077
2078 abort ();
2079 }
2080 else if (h->plt.offset == (bfd_vma) -1
2081 /* The following relocation may not need the .plt entries
2082 when all references to a STT_GNU_IFUNC symbols are done
2083 via GOT or static function pointers. */
2084 && r_type != R_RISCV_32
2085 && r_type != R_RISCV_64
2086 && r_type != R_RISCV_HI20
2087 && r_type != R_RISCV_GOT_HI20
2088 && r_type != R_RISCV_LO12_I
2089 && r_type != R_RISCV_LO12_S)
2090 goto bad_ifunc_reloc;
2091
2092 /* STT_GNU_IFUNC symbol must go through PLT. */
2093 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2094 relocation = plt->output_section->vma
2095 + plt->output_offset
2096 + h->plt.offset;
2097
2098 switch (r_type)
2099 {
2100 case R_RISCV_32:
2101 case R_RISCV_64:
2102 if (rel->r_addend != 0)
2103 {
2104 if (h->root.root.string)
2105 name = h->root.root.string;
2106 else
2107 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
2108
2109 _bfd_error_handler
2110 /* xgettext:c-format */
2111 (_("%pB: relocation %s against STT_GNU_IFUNC "
2112 "symbol `%s' has non-zero addend: %" PRId64),
2113 input_bfd, howto->name, name, (int64_t) rel->r_addend);
2114 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2115 return false;
02dd9d25
NC
2116 }
2117
2118 /* Generate dynamic relocation only when there is a non-GOT
2119 reference in a shared object or there is no PLT. */
2120 if ((bfd_link_pic (info) && h->non_got_ref)
2121 || h->plt.offset == (bfd_vma) -1)
2122 {
2123 Elf_Internal_Rela outrel;
2124 asection *sreloc;
2125
2126 /* Need a dynamic relocation to get the real function
2127 address. */
2128 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2129 info,
2130 input_section,
2131 rel->r_offset);
2132 if (outrel.r_offset == (bfd_vma) -1
2133 || outrel.r_offset == (bfd_vma) -2)
2134 abort ();
2135
2136 outrel.r_offset += input_section->output_section->vma
2137 + input_section->output_offset;
2138
2139 if (h->dynindx == -1
2140 || h->forced_local
2141 || bfd_link_executable (info))
2142 {
2143 info->callbacks->minfo
2144 (_("Local IFUNC function `%s' in %pB\n"),
2145 h->root.root.string,
2146 h->root.u.def.section->owner);
2147
2148 /* This symbol is resolved locally. */
2149 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
2150 outrel.r_addend = h->root.u.def.value
2151 + h->root.u.def.section->output_section->vma
2152 + h->root.u.def.section->output_offset;
2153 }
2154 else
2155 {
2156 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2157 outrel.r_addend = 0;
2158 }
2159
2160 /* Dynamic relocations are stored in
2161 1. .rela.ifunc section in PIC object.
2162 2. .rela.got section in dynamic executable.
2163 3. .rela.iplt section in static executable. */
2164 if (bfd_link_pic (info))
2165 sreloc = htab->elf.irelifunc;
2166 else if (htab->elf.splt != NULL)
2167 sreloc = htab->elf.srelgot;
2168 else
2169 sreloc = htab->elf.irelplt;
2170
2171 riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2172
2173 /* If this reloc is against an external symbol, we
2174 do not want to fiddle with the addend. Otherwise,
2175 we need to include the symbol value so that it
2176 becomes an addend for the dynamic reloc. For an
2177 internal symbol, we have updated addend. */
2178 continue;
2179 }
2180 goto do_relocation;
2181
2182 case R_RISCV_GOT_HI20:
2183 base_got = htab->elf.sgot;
2184 off = h->got.offset;
2185
2186 if (base_got == NULL)
2187 abort ();
2188
2189 if (off == (bfd_vma) -1)
2190 {
2191 bfd_vma plt_idx;
2192
2193 /* We can't use h->got.offset here to save state, or
2194 even just remember the offset, as finish_dynamic_symbol
2195 would use that as offset into .got. */
2196
2197 if (htab->elf.splt != NULL)
2198 {
2199 plt_idx = (h->plt.offset - PLT_HEADER_SIZE)
2200 / PLT_ENTRY_SIZE;
2201 off = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
2202 base_got = htab->elf.sgotplt;
2203 }
2204 else
2205 {
2206 plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
2207 off = plt_idx * GOT_ENTRY_SIZE;
2208 base_got = htab->elf.igotplt;
2209 }
2210
2211 if (h->dynindx == -1
2212 || h->forced_local
2213 || info->symbolic)
2214 {
2215 /* This references the local definition. We must
2216 initialize this entry in the global offset table.
2217 Since the offset must always be a multiple of 8,
2218 we use the least significant bit to record
2219 whether we have initialized it already.
2220
2221 When doing a dynamic link, we create a .rela.got
2222 relocation entry to initialize the value. This
2223 is done in the finish_dynamic_symbol routine. */
2224 if ((off & 1) != 0)
2225 off &= ~1;
2226 else
2227 {
2228 bfd_put_NN (output_bfd, relocation,
2229 base_got->contents + off);
2230 /* Note that this is harmless for the case,
2231 as -1 | 1 still is -1. */
2232 h->got.offset |= 1;
2233 }
2234 }
2235 }
2236
2237 relocation = base_got->output_section->vma
2238 + base_got->output_offset + off;
2239
50331d64
NC
2240 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2241 relocation, r_type,
2242 false))
02dd9d25
NC
2243 r = bfd_reloc_overflow;
2244 goto do_relocation;
2245
2246 case R_RISCV_CALL:
2247 case R_RISCV_CALL_PLT:
2248 case R_RISCV_HI20:
2249 case R_RISCV_LO12_I:
2250 case R_RISCV_LO12_S:
2251 goto do_relocation;
2252
2253 case R_RISCV_PCREL_HI20:
50331d64
NC
2254 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2255 relocation, r_type,
2256 false))
02dd9d25
NC
2257 r = bfd_reloc_overflow;
2258 goto do_relocation;
2259
2260 default:
1942a048 2261 bad_ifunc_reloc:
02dd9d25
NC
2262 if (h->root.root.string)
2263 name = h->root.root.string;
2264 else
2265 /* The entry of local ifunc is fake in global hash table,
2266 we should find the name by the original local symbol. */
2267 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
2268
2269 _bfd_error_handler
2270 /* xgettext:c-format */
2271 (_("%pB: relocation %s against STT_GNU_IFUNC "
2272 "symbol `%s' isn't supported"), input_bfd,
2273 howto->name, name);
2274 bfd_set_error (bfd_error_bad_value);
0a1b45a2 2275 return false;
02dd9d25
NC
2276 }
2277 }
2278
1942a048 2279 skip_ifunc:
e23eba97
NC
2280 if (h != NULL)
2281 name = h->root.root.string;
2282 else
2283 {
2284 name = (bfd_elf_string_from_elf_section
2285 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2286 if (name == NULL || *name == '\0')
fd361982 2287 name = bfd_section_name (sec);
e23eba97
NC
2288 }
2289
6487709f
JW
2290 resolved_to_zero = (h != NULL
2291 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
2292
e23eba97
NC
2293 switch (r_type)
2294 {
2295 case R_RISCV_NONE:
45f76423 2296 case R_RISCV_RELAX:
e23eba97
NC
2297 case R_RISCV_TPREL_ADD:
2298 case R_RISCV_COPY:
2299 case R_RISCV_JUMP_SLOT:
2300 case R_RISCV_RELATIVE:
2301 /* These require nothing of us at all. */
2302 continue;
2303
2304 case R_RISCV_HI20:
2305 case R_RISCV_BRANCH:
2306 case R_RISCV_RVC_BRANCH:
2307 case R_RISCV_RVC_LUI:
2308 case R_RISCV_LO12_I:
2309 case R_RISCV_LO12_S:
45f76423
AW
2310 case R_RISCV_SET6:
2311 case R_RISCV_SET8:
2312 case R_RISCV_SET16:
2313 case R_RISCV_SET32:
a6cbf936 2314 case R_RISCV_32_PCREL:
ff6f4d9b 2315 case R_RISCV_DELETE:
e23eba97
NC
2316 /* These require no special handling beyond perform_relocation. */
2317 break;
2318
2319 case R_RISCV_GOT_HI20:
2320 if (h != NULL)
2321 {
0a1b45a2 2322 bool dyn, pic;
e23eba97
NC
2323
2324 off = h->got.offset;
2325 BFD_ASSERT (off != (bfd_vma) -1);
2326 dyn = elf_hash_table (info)->dynamic_sections_created;
2327 pic = bfd_link_pic (info);
2328
2329 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
2330 || (pic && SYMBOL_REFERENCES_LOCAL (info, h)))
2331 {
2332 /* This is actually a static link, or it is a
2333 -Bsymbolic link and the symbol is defined
2334 locally, or the symbol was forced to be local
2335 because of a version file. We must initialize
2336 this entry in the global offset table. Since the
2337 offset must always be a multiple of the word size,
2338 we use the least significant bit to record whether
2339 we have initialized it already.
2340
2341 When doing a dynamic link, we create a .rela.got
2342 relocation entry to initialize the value. This
2343 is done in the finish_dynamic_symbol routine. */
2344 if ((off & 1) != 0)
2345 off &= ~1;
2346 else
2347 {
2348 bfd_put_NN (output_bfd, relocation,
2349 htab->elf.sgot->contents + off);
2350 h->got.offset |= 1;
2351 }
2352 }
2353 else
0a1b45a2 2354 unresolved_reloc = false;
e23eba97
NC
2355 }
2356 else
2357 {
2358 BFD_ASSERT (local_got_offsets != NULL
2359 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2360
2361 off = local_got_offsets[r_symndx];
2362
2363 /* The offset must always be a multiple of the word size.
2364 So, we can use the least significant bit to record
2365 whether we have already processed this entry. */
2366 if ((off & 1) != 0)
2367 off &= ~1;
2368 else
2369 {
2370 if (bfd_link_pic (info))
2371 {
2372 asection *s;
2373 Elf_Internal_Rela outrel;
2374
2375 /* We need to generate a R_RISCV_RELATIVE reloc
2376 for the dynamic linker. */
2377 s = htab->elf.srelgot;
2378 BFD_ASSERT (s != NULL);
2379
2380 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2381 outrel.r_info =
2382 ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2383 outrel.r_addend = relocation;
2384 relocation = 0;
2385 riscv_elf_append_rela (output_bfd, s, &outrel);
2386 }
2387
2388 bfd_put_NN (output_bfd, relocation,
2389 htab->elf.sgot->contents + off);
2390 local_got_offsets[r_symndx] |= 1;
2391 }
2392 }
50331d64
NC
2393
2394 if (rel->r_addend != 0)
2395 {
2396 msg = _("The addend isn't allowed for R_RISCV_GOT_HI20");
2397 r = bfd_reloc_dangerous;
2398 }
2399 else
2400 {
2401 /* Address of got entry. */
2402 relocation = sec_addr (htab->elf.sgot) + off;
2403 absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc,
2404 relocation, contents,
2405 howto);
2406 /* Update howto if relocation is changed. */
2407 howto = riscv_elf_rtype_to_howto (input_bfd,
2408 ELFNN_R_TYPE (rel->r_info));
2409 if (howto == NULL)
2410 r = bfd_reloc_notsupported;
2411 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2412 relocation, r_type,
2413 absolute))
2414 r = bfd_reloc_overflow;
2415 }
e23eba97
NC
2416 break;
2417
2418 case R_RISCV_ADD8:
2419 case R_RISCV_ADD16:
2420 case R_RISCV_ADD32:
2421 case R_RISCV_ADD64:
2422 {
2423 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2424 contents + rel->r_offset);
2425 relocation = old_value + relocation;
2426 }
2427 break;
2428
45f76423 2429 case R_RISCV_SUB6:
06f0a892
XZ
2430 {
2431 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2432 contents + rel->r_offset);
2433 relocation = (old_value & ~howto->dst_mask)
2434 | (((old_value & howto->dst_mask) - relocation)
2435 & howto->dst_mask);
2436 }
2437 break;
2438
e23eba97
NC
2439 case R_RISCV_SUB8:
2440 case R_RISCV_SUB16:
2441 case R_RISCV_SUB32:
2442 case R_RISCV_SUB64:
2443 {
2444 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2445 contents + rel->r_offset);
2446 relocation = old_value - relocation;
2447 }
2448 break;
2449
e23eba97 2450 case R_RISCV_CALL:
85f78364 2451 case R_RISCV_CALL_PLT:
cf7a5066
JW
2452 /* Handle a call to an undefined weak function. This won't be
2453 relaxed, so we have to handle it here. */
2454 if (h != NULL && h->root.type == bfd_link_hash_undefweak
85f78364 2455 && (!bfd_link_pic (info) || h->plt.offset == MINUS_ONE))
cf7a5066
JW
2456 {
2457 /* We can use x0 as the base register. */
fbc09e7a 2458 bfd_vma insn = bfd_getl32 (contents + rel->r_offset + 4);
cf7a5066 2459 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
fbc09e7a 2460 bfd_putl32 (insn, contents + rel->r_offset + 4);
cf7a5066
JW
2461 /* Set the relocation value so that we get 0 after the pc
2462 relative adjustment. */
2463 relocation = sec_addr (input_section) + rel->r_offset;
2464 }
2465 /* Fall through. */
2466
e23eba97
NC
2467 case R_RISCV_JAL:
2468 case R_RISCV_RVC_JUMP:
ecb915b4 2469 if (bfd_link_pic (info) && h != NULL)
e23eba97 2470 {
ecb915b4
NC
2471 if (h->plt.offset != MINUS_ONE)
2472 {
2473 /* Refer to the PLT entry. This check has to match the
2474 check in _bfd_riscv_relax_section. */
2475 relocation = sec_addr (htab->elf.splt) + h->plt.offset;
2476 unresolved_reloc = false;
2477 }
2478 else if (!SYMBOL_REFERENCES_LOCAL (info, h)
2479 && (input_section->flags & SEC_ALLOC) != 0
2480 && (input_section->flags & SEC_READONLY) != 0
2481 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
2482 {
2483 /* PR 28509, when generating the shared object, these
2484 referenced symbols may bind externally, which means
2485 they will be exported to the dynamic symbol table,
2486 and are preemptible by default. These symbols cannot
2487 be referenced by the non-pic relocations, like
2488 R_RISCV_JAL and R_RISCV_RVC_JUMP relocations.
2489
2490 However, consider that linker may relax the R_RISCV_CALL
2491 relocations to R_RISCV_JAL or R_RISCV_RVC_JUMP, if
2492 these relocations are relocated to the plt entries,
2493 then we won't report error for them.
2494
2495 Perhaps we also need the similar checks for the
2496 R_RISCV_BRANCH and R_RISCV_RVC_BRANCH relocations. */
2497 if (asprintf (&msg_buf,
2498 _("%%X%%P: relocation %s against `%s' which "
2499 "may bind externally can not be used when "
2500 "making a shared object; recompile "
2501 "with -fPIC\n"),
2502 howto->name, h->root.root.string) == -1)
2503 msg_buf = NULL;
2504 msg = msg_buf;
2505 r = bfd_reloc_notsupported;
2506 }
e23eba97
NC
2507 }
2508 break;
2509
2510 case R_RISCV_TPREL_HI20:
2511 relocation = tpoff (info, relocation);
2512 break;
2513
2514 case R_RISCV_TPREL_LO12_I:
2515 case R_RISCV_TPREL_LO12_S:
45f76423
AW
2516 relocation = tpoff (info, relocation);
2517 break;
2518
2519 case R_RISCV_TPREL_I:
2520 case R_RISCV_TPREL_S:
e23eba97
NC
2521 relocation = tpoff (info, relocation);
2522 if (VALID_ITYPE_IMM (relocation + rel->r_addend))
2523 {
2524 /* We can use tp as the base register. */
fbc09e7a 2525 bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
e23eba97
NC
2526 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2527 insn |= X_TP << OP_SH_RS1;
fbc09e7a 2528 bfd_putl32 (insn, contents + rel->r_offset);
e23eba97 2529 }
45f76423
AW
2530 else
2531 r = bfd_reloc_overflow;
e23eba97
NC
2532 break;
2533
2534 case R_RISCV_GPREL_I:
2535 case R_RISCV_GPREL_S:
2536 {
2537 bfd_vma gp = riscv_global_pointer_value (info);
0a1b45a2 2538 bool x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
e23eba97
NC
2539 if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
2540 {
2541 /* We can use x0 or gp as the base register. */
fbc09e7a 2542 bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
e23eba97
NC
2543 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2544 if (!x0_base)
2545 {
2546 rel->r_addend -= gp;
2547 insn |= X_GP << OP_SH_RS1;
2548 }
fbc09e7a 2549 bfd_putl32 (insn, contents + rel->r_offset);
e23eba97
NC
2550 }
2551 else
2552 r = bfd_reloc_overflow;
2553 break;
2554 }
2555
2556 case R_RISCV_PCREL_HI20:
50331d64
NC
2557 absolute = riscv_zero_pcrel_hi_reloc (rel, info, pc, relocation,
2558 contents, howto);
2559 /* Update howto if relocation is changed. */
2560 howto = riscv_elf_rtype_to_howto (input_bfd,
2561 ELFNN_R_TYPE (rel->r_info));
f3185997
NC
2562 if (howto == NULL)
2563 r = bfd_reloc_notsupported;
2564 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2565 relocation + rel->r_addend,
50331d64 2566 r_type, absolute))
e23eba97
NC
2567 r = bfd_reloc_overflow;
2568 break;
2569
2570 case R_RISCV_PCREL_LO12_I:
2571 case R_RISCV_PCREL_LO12_S:
551703cf
JW
2572 /* We don't allow section symbols plus addends as the auipc address,
2573 because then riscv_relax_delete_bytes would have to search through
2574 all relocs to update these addends. This is also ambiguous, as
2575 we do allow offsets to be added to the target address, which are
2576 not to be used to find the auipc address. */
a9f5a551
JW
2577 if (((sym != NULL && (ELF_ST_TYPE (sym->st_info) == STT_SECTION))
2578 || (h != NULL && h->type == STT_SECTION))
2579 && rel->r_addend)
2a0d9853 2580 {
330a6637 2581 msg = _("%pcrel_lo section symbol with an addend");
2a0d9853
JW
2582 r = bfd_reloc_dangerous;
2583 break;
2584 }
2585
50331d64
NC
2586 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, relocation, rel,
2587 input_section, info, howto,
e23eba97
NC
2588 contents))
2589 continue;
2590 r = bfd_reloc_overflow;
2591 break;
2592
2593 case R_RISCV_TLS_DTPREL32:
2594 case R_RISCV_TLS_DTPREL64:
2595 relocation = dtpoff (info, relocation);
2596 break;
2597
2598 case R_RISCV_32:
2599 case R_RISCV_64:
2600 if ((input_section->flags & SEC_ALLOC) == 0)
2601 break;
2602
2603 if ((bfd_link_pic (info)
2604 && (h == NULL
6487709f
JW
2605 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2606 && !resolved_to_zero)
e23eba97 2607 || h->root.type != bfd_link_hash_undefweak)
1942a048 2608 && (!howto->pc_relative
e23eba97
NC
2609 || !SYMBOL_CALLS_LOCAL (info, h)))
2610 || (!bfd_link_pic (info)
2611 && h != NULL
2612 && h->dynindx != -1
2613 && !h->non_got_ref
2614 && ((h->def_dynamic
2615 && !h->def_regular)
2616 || h->root.type == bfd_link_hash_undefweak
2617 || h->root.type == bfd_link_hash_undefined)))
2618 {
2619 Elf_Internal_Rela outrel;
02dd9d25 2620 asection *sreloc;
0a1b45a2 2621 bool skip_static_relocation, skip_dynamic_relocation;
e23eba97
NC
2622
2623 /* When generating a shared object, these relocations
2624 are copied into the output file to be resolved at run
2625 time. */
2626
2627 outrel.r_offset =
2628 _bfd_elf_section_offset (output_bfd, info, input_section,
2629 rel->r_offset);
2630 skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
2631 skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
2632 outrel.r_offset += sec_addr (input_section);
2633
2634 if (skip_dynamic_relocation)
2635 memset (&outrel, 0, sizeof outrel);
2636 else if (h != NULL && h->dynindx != -1
2637 && !(bfd_link_pic (info)
2638 && SYMBOLIC_BIND (info, h)
2639 && h->def_regular))
2640 {
2641 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2642 outrel.r_addend = rel->r_addend;
2643 }
2644 else
2645 {
2646 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2647 outrel.r_addend = relocation + rel->r_addend;
2648 }
2649
02dd9d25 2650 sreloc = elf_section_data (input_section)->sreloc;
e23eba97
NC
2651 riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2652 if (skip_static_relocation)
2653 continue;
2654 }
2655 break;
2656
2657 case R_RISCV_TLS_GOT_HI20:
0a1b45a2 2658 is_ie = true;
e23eba97
NC
2659 /* Fall through. */
2660
2661 case R_RISCV_TLS_GD_HI20:
2662 if (h != NULL)
2663 {
2664 off = h->got.offset;
2665 h->got.offset |= 1;
2666 }
2667 else
2668 {
2669 off = local_got_offsets[r_symndx];
2670 local_got_offsets[r_symndx] |= 1;
2671 }
2672
2673 tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
2674 BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
2675 /* If this symbol is referenced by both GD and IE TLS, the IE
2676 reference's GOT slot follows the GD reference's slots. */
2677 ie_off = 0;
2678 if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
2679 ie_off = 2 * GOT_ENTRY_SIZE;
2680
2681 if ((off & 1) != 0)
2682 off &= ~1;
2683 else
2684 {
2685 Elf_Internal_Rela outrel;
2686 int indx = 0;
0a1b45a2 2687 bool need_relocs = false;
e23eba97
NC
2688
2689 if (htab->elf.srelgot == NULL)
2690 abort ();
2691
2692 if (h != NULL)
2693 {
0a1b45a2 2694 bool dyn, pic;
e23eba97
NC
2695 dyn = htab->elf.dynamic_sections_created;
2696 pic = bfd_link_pic (info);
2697
2698 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
2699 && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
2700 indx = h->dynindx;
2701 }
2702
2703 /* The GOT entries have not been initialized yet. Do it
07d6d2b8 2704 now, and emit any relocations. */
e23eba97
NC
2705 if ((bfd_link_pic (info) || indx != 0)
2706 && (h == NULL
2707 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2708 || h->root.type != bfd_link_hash_undefweak))
0a1b45a2 2709 need_relocs = true;
e23eba97
NC
2710
2711 if (tls_type & GOT_TLS_GD)
2712 {
2713 if (need_relocs)
2714 {
2715 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2716 outrel.r_addend = 0;
2717 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
2718 bfd_put_NN (output_bfd, 0,
2719 htab->elf.sgot->contents + off);
2720 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2721 if (indx == 0)
2722 {
2723 BFD_ASSERT (! unresolved_reloc);
2724 bfd_put_NN (output_bfd,
2725 dtpoff (info, relocation),
1942a048
NC
2726 (htab->elf.sgot->contents
2727 + off + RISCV_ELF_WORD_BYTES));
e23eba97
NC
2728 }
2729 else
2730 {
2731 bfd_put_NN (output_bfd, 0,
1942a048
NC
2732 (htab->elf.sgot->contents
2733 + off + RISCV_ELF_WORD_BYTES));
e23eba97
NC
2734 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
2735 outrel.r_offset += RISCV_ELF_WORD_BYTES;
2736 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2737 }
2738 }
2739 else
2740 {
2741 /* If we are not emitting relocations for a
2742 general dynamic reference, then we must be in a
2743 static link or an executable link with the
2744 symbol binding locally. Mark it as belonging
2745 to module 1, the executable. */
2746 bfd_put_NN (output_bfd, 1,
2747 htab->elf.sgot->contents + off);
2748 bfd_put_NN (output_bfd,
2749 dtpoff (info, relocation),
1942a048
NC
2750 (htab->elf.sgot->contents
2751 + off + RISCV_ELF_WORD_BYTES));
e23eba97
NC
2752 }
2753 }
2754
2755 if (tls_type & GOT_TLS_IE)
2756 {
2757 if (need_relocs)
2758 {
2759 bfd_put_NN (output_bfd, 0,
2760 htab->elf.sgot->contents + off + ie_off);
2761 outrel.r_offset = sec_addr (htab->elf.sgot)
1942a048 2762 + off + ie_off;
e23eba97
NC
2763 outrel.r_addend = 0;
2764 if (indx == 0)
2765 outrel.r_addend = tpoff (info, relocation);
2766 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
2767 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2768 }
2769 else
2770 {
2771 bfd_put_NN (output_bfd, tpoff (info, relocation),
2772 htab->elf.sgot->contents + off + ie_off);
2773 }
2774 }
2775 }
2776
2777 BFD_ASSERT (off < (bfd_vma) -2);
2778 relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
b1308d2c 2779 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
50331d64
NC
2780 relocation, r_type,
2781 false))
e23eba97 2782 r = bfd_reloc_overflow;
0a1b45a2 2783 unresolved_reloc = false;
e23eba97
NC
2784 break;
2785
2786 default:
2787 r = bfd_reloc_notsupported;
2788 }
2789
2790 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2791 because such sections are not SEC_ALLOC and thus ld.so will
2792 not process them. */
2793 if (unresolved_reloc
2794 && !((input_section->flags & SEC_DEBUGGING) != 0
2795 && h->def_dynamic)
2796 && _bfd_elf_section_offset (output_bfd, info, input_section,
2797 rel->r_offset) != (bfd_vma) -1)
2798 {
ecb915b4
NC
2799 if (asprintf (&msg_buf,
2800 _("%%X%%P: unresolvable %s relocation against "
2801 "symbol `%s'\n"),
2802 howto->name,
2803 h->root.root.string) == -1)
2804 msg_buf = NULL;
330a6637
JW
2805 msg = msg_buf;
2806 r = bfd_reloc_notsupported;
e23eba97
NC
2807 }
2808
02dd9d25 2809 do_relocation:
e23eba97
NC
2810 if (r == bfd_reloc_ok)
2811 r = perform_relocation (howto, rel, relocation, input_section,
2812 input_bfd, contents);
2813
330a6637
JW
2814 /* We should have already detected the error and set message before.
2815 If the error message isn't set since the linker runs out of memory
2816 or we don't set it before, then we should set the default message
2817 with the "internal error" string here. */
e23eba97
NC
2818 switch (r)
2819 {
2820 case bfd_reloc_ok:
2821 continue;
2822
2823 case bfd_reloc_overflow:
2824 info->callbacks->reloc_overflow
2825 (info, (h ? &h->root : NULL), name, howto->name,
2826 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2827 break;
2828
2829 case bfd_reloc_undefined:
2830 info->callbacks->undefined_symbol
2831 (info, name, input_bfd, input_section, rel->r_offset,
0a1b45a2 2832 true);
e23eba97
NC
2833 break;
2834
2835 case bfd_reloc_outofrange:
330a6637
JW
2836 if (msg == NULL)
2837 msg = _("%X%P: internal error: out of range error\n");
e23eba97
NC
2838 break;
2839
2840 case bfd_reloc_notsupported:
330a6637
JW
2841 if (msg == NULL)
2842 msg = _("%X%P: internal error: unsupported relocation error\n");
e23eba97
NC
2843 break;
2844
2845 case bfd_reloc_dangerous:
330a6637
JW
2846 /* The error message should already be set. */
2847 if (msg == NULL)
2848 msg = _("dangerous relocation error");
2a0d9853 2849 info->callbacks->reloc_dangerous
330a6637 2850 (info, msg, input_bfd, input_section, rel->r_offset);
e23eba97
NC
2851 break;
2852
2853 default:
2a0d9853 2854 msg = _("%X%P: internal error: unknown error\n");
e23eba97
NC
2855 break;
2856 }
2857
330a6637
JW
2858 /* Do not report error message for the dangerous relocation again. */
2859 if (msg && r != bfd_reloc_dangerous)
2a0d9853
JW
2860 info->callbacks->einfo (msg);
2861
c9594989
AM
2862 /* Free the unused `msg_buf`. */
2863 free (msg_buf);
330a6637 2864
3f48fe4a
JW
2865 /* We already reported the error via a callback, so don't try to report
2866 it again by returning false. That leads to spurious errors. */
0a1b45a2 2867 ret = true;
e23eba97
NC
2868 goto out;
2869 }
2870
2871 ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
dc1e8a47 2872 out:
e23eba97
NC
2873 riscv_free_pcrel_relocs (&pcrel_relocs);
2874 return ret;
2875}
2876
2877/* Finish up dynamic symbol handling. We set the contents of various
2878 dynamic sections here. */
2879
0a1b45a2 2880static bool
e23eba97
NC
2881riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
2882 struct bfd_link_info *info,
2883 struct elf_link_hash_entry *h,
2884 Elf_Internal_Sym *sym)
2885{
2886 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2887 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2888
2889 if (h->plt.offset != (bfd_vma) -1)
2890 {
2891 /* We've decided to create a PLT entry for this symbol. */
2892 bfd_byte *loc;
02dd9d25 2893 bfd_vma i, header_address, plt_idx, got_offset, got_address;
e23eba97
NC
2894 uint32_t plt_entry[PLT_ENTRY_INSNS];
2895 Elf_Internal_Rela rela;
02dd9d25
NC
2896 asection *plt, *gotplt, *relplt;
2897
2898 /* When building a static executable, use .iplt, .igot.plt and
2899 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2900 if (htab->elf.splt != NULL)
2901 {
2902 plt = htab->elf.splt;
2903 gotplt = htab->elf.sgotplt;
2904 relplt = htab->elf.srelplt;
2905 }
2906 else
2907 {
2908 plt = htab->elf.iplt;
2909 gotplt = htab->elf.igotplt;
2910 relplt = htab->elf.irelplt;
2911 }
2912
2913 /* This symbol has an entry in the procedure linkage table. Set
2914 it up. */
2915 if ((h->dynindx == -1
2916 && !((h->forced_local || bfd_link_executable (info))
2917 && h->def_regular
2918 && h->type == STT_GNU_IFUNC))
2919 || plt == NULL
2920 || gotplt == NULL
2921 || relplt == NULL)
0a1b45a2 2922 return false;
e23eba97
NC
2923
2924 /* Calculate the address of the PLT header. */
02dd9d25 2925 header_address = sec_addr (plt);
e23eba97 2926
02dd9d25
NC
2927 /* Calculate the index of the entry and the offset of .got.plt entry.
2928 For static executables, we don't reserve anything. */
2929 if (plt == htab->elf.splt)
2930 {
2931 plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2932 got_offset = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
2933 }
2934 else
2935 {
2936 plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
2937 got_offset = plt_idx * GOT_ENTRY_SIZE;
2938 }
e23eba97
NC
2939
2940 /* Calculate the address of the .got.plt entry. */
02dd9d25 2941 got_address = sec_addr (gotplt) + got_offset;
e23eba97
NC
2942
2943 /* Find out where the .plt entry should go. */
02dd9d25 2944 loc = plt->contents + h->plt.offset;
e23eba97
NC
2945
2946 /* Fill in the PLT entry itself. */
5ef23793
JW
2947 if (! riscv_make_plt_entry (output_bfd, got_address,
2948 header_address + h->plt.offset,
2949 plt_entry))
0a1b45a2 2950 return false;
5ef23793 2951
e23eba97 2952 for (i = 0; i < PLT_ENTRY_INSNS; i++)
fbc09e7a 2953 bfd_putl32 (plt_entry[i], loc + 4*i);
e23eba97
NC
2954
2955 /* Fill in the initial value of the .got.plt entry. */
02dd9d25
NC
2956 loc = gotplt->contents + (got_address - sec_addr (gotplt));
2957 bfd_put_NN (output_bfd, sec_addr (plt), loc);
e23eba97 2958
e23eba97 2959 rela.r_offset = got_address;
e23eba97 2960
02dd9d25
NC
2961 if (h->dynindx == -1
2962 || ((bfd_link_executable (info)
2963 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2964 && h->def_regular
2965 && h->type == STT_GNU_IFUNC))
2966 {
2967 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2968 h->root.root.string,
2969 h->root.u.def.section->owner);
2970
2971 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2972 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2973 asection *sec = h->root.u.def.section;
2974 rela.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
2975 rela.r_addend = h->root.u.def.value
2976 + sec->output_section->vma
2977 + sec->output_offset;
2978 }
2979 else
2980 {
2981 /* Fill in the entry in the .rela.plt section. */
2982 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
2983 rela.r_addend = 0;
2984 }
2985
2986 loc = relplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
e23eba97
NC
2987 bed->s->swap_reloca_out (output_bfd, &rela, loc);
2988
2989 if (!h->def_regular)
2990 {
2991 /* Mark the symbol as undefined, rather than as defined in
2992 the .plt section. Leave the value alone. */
2993 sym->st_shndx = SHN_UNDEF;
2994 /* If the symbol is weak, we do need to clear the value.
2995 Otherwise, the PLT entry would provide a definition for
2996 the symbol even if the symbol wasn't defined anywhere,
2997 and so the symbol would never be NULL. */
2998 if (!h->ref_regular_nonweak)
2999 sym->st_value = 0;
3000 }
3001 }
3002
3003 if (h->got.offset != (bfd_vma) -1
6487709f
JW
3004 && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))
3005 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
e23eba97
NC
3006 {
3007 asection *sgot;
3008 asection *srela;
3009 Elf_Internal_Rela rela;
0a1b45a2 3010 bool use_elf_append_rela = true;
e23eba97
NC
3011
3012 /* This symbol has an entry in the GOT. Set it up. */
3013
3014 sgot = htab->elf.sgot;
3015 srela = htab->elf.srelgot;
3016 BFD_ASSERT (sgot != NULL && srela != NULL);
3017
3018 rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
3019
02dd9d25
NC
3020 /* Handle the ifunc symbol in GOT entry. */
3021 if (h->def_regular
3022 && h->type == STT_GNU_IFUNC)
3023 {
3024 if (h->plt.offset == (bfd_vma) -1)
3025 {
3026 /* STT_GNU_IFUNC is referenced without PLT. */
51a8a7c2 3027
02dd9d25
NC
3028 if (htab->elf.splt == NULL)
3029 {
51a8a7c2 3030 /* Use .rela.iplt section to store .got relocations
02dd9d25
NC
3031 in static executable. */
3032 srela = htab->elf.irelplt;
51a8a7c2
NC
3033
3034 /* Do not use riscv_elf_append_rela to add dynamic
3035 relocs. */
0a1b45a2 3036 use_elf_append_rela = false;
02dd9d25 3037 }
51a8a7c2 3038
02dd9d25
NC
3039 if (SYMBOL_REFERENCES_LOCAL (info, h))
3040 {
3041 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
3042 h->root.root.string,
3043 h->root.u.def.section->owner);
3044
3045 rela.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
3046 rela.r_addend = (h->root.u.def.value
3047 + h->root.u.def.section->output_section->vma
3048 + h->root.u.def.section->output_offset);
3049 }
3050 else
3051 {
3052 /* Generate R_RISCV_NN. */
1942a048 3053 BFD_ASSERT ((h->got.offset & 1) == 0);
02dd9d25
NC
3054 BFD_ASSERT (h->dynindx != -1);
3055 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
3056 rela.r_addend = 0;
3057 }
3058 }
3059 else if (bfd_link_pic (info))
3060 {
3061 /* Generate R_RISCV_NN. */
1942a048 3062 BFD_ASSERT ((h->got.offset & 1) == 0);
02dd9d25
NC
3063 BFD_ASSERT (h->dynindx != -1);
3064 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
3065 rela.r_addend = 0;
3066 }
3067 else
3068 {
3069 asection *plt;
3070
3071 if (!h->pointer_equality_needed)
3072 abort ();
3073
3074 /* For non-shared object, we can't use .got.plt, which
3075 contains the real function address if we need pointer
3076 equality. We load the GOT entry with the PLT entry. */
3077 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3078 bfd_put_NN (output_bfd, (plt->output_section->vma
3079 + plt->output_offset
3080 + h->plt.offset),
3081 htab->elf.sgot->contents
3082 + (h->got.offset & ~(bfd_vma) 1));
0a1b45a2 3083 return true;
02dd9d25
NC
3084 }
3085 }
02dd9d25
NC
3086 else if (bfd_link_pic (info)
3087 && SYMBOL_REFERENCES_LOCAL (info, h))
e23eba97 3088 {
51a8a7c2
NC
3089 /* If this is a local symbol reference, we just want to emit
3090 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
3091 or a pie link, or the symbol was forced to be local because
3092 of a version file. The entry in the global offset table will
3093 already have been initialized in the relocate_section function. */
1942a048 3094 BFD_ASSERT ((h->got.offset & 1) != 0);
e23eba97
NC
3095 asection *sec = h->root.u.def.section;
3096 rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
3097 rela.r_addend = (h->root.u.def.value
3098 + sec->output_section->vma
3099 + sec->output_offset);
3100 }
3101 else
3102 {
1942a048 3103 BFD_ASSERT ((h->got.offset & 1) == 0);
e23eba97
NC
3104 BFD_ASSERT (h->dynindx != -1);
3105 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
3106 rela.r_addend = 0;
3107 }
3108
3109 bfd_put_NN (output_bfd, 0,
3110 sgot->contents + (h->got.offset & ~(bfd_vma) 1));
51a8a7c2
NC
3111
3112 if (use_elf_append_rela)
3113 riscv_elf_append_rela (output_bfd, srela, &rela);
3114 else
3115 {
3116 /* Use riscv_elf_append_rela to add the dynamic relocs into
3117 .rela.iplt may cause the overwrite problems. Since we insert
3118 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3119 but the riscv_elf_append_rela adds the relocs to the place
3120 that are calculated from the reloc_index (in seqential).
3121
3122 One solution is that add these dynamic relocs (GOT IFUNC)
3123 from the last of .rela.iplt section. */
3124 bfd_vma iplt_idx = htab->last_iplt_index--;
3125 bfd_byte *loc = srela->contents
3126 + iplt_idx * sizeof (ElfNN_External_Rela);
3127 bed->s->swap_reloca_out (output_bfd, &rela, loc);
3128 }
e23eba97
NC
3129 }
3130
3131 if (h->needs_copy)
3132 {
3133 Elf_Internal_Rela rela;
5474d94f 3134 asection *s;
e23eba97
NC
3135
3136 /* This symbols needs a copy reloc. Set it up. */
3137 BFD_ASSERT (h->dynindx != -1);
3138
3139 rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
3140 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
3141 rela.r_addend = 0;
afbf7e8e 3142 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
3143 s = htab->elf.sreldynrelro;
3144 else
3145 s = htab->elf.srelbss;
3146 riscv_elf_append_rela (output_bfd, s, &rela);
e23eba97
NC
3147 }
3148
3149 /* Mark some specially defined symbols as absolute. */
3150 if (h == htab->elf.hdynamic
3151 || (h == htab->elf.hgot || h == htab->elf.hplt))
3152 sym->st_shndx = SHN_ABS;
3153
0a1b45a2 3154 return true;
e23eba97
NC
3155}
3156
02dd9d25
NC
3157/* Finish up local dynamic symbol handling. We set the contents of
3158 various dynamic sections here. */
3159
1201fda6 3160static int
02dd9d25
NC
3161riscv_elf_finish_local_dynamic_symbol (void **slot, void *inf)
3162{
3163 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
3164 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3165
3166 return riscv_elf_finish_dynamic_symbol (info->output_bfd, info, h, NULL);
3167}
3168
e23eba97
NC
3169/* Finish up the dynamic sections. */
3170
0a1b45a2 3171static bool
e23eba97
NC
3172riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
3173 bfd *dynobj, asection *sdyn)
3174{
3175 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
3176 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
3177 size_t dynsize = bed->s->sizeof_dyn;
3178 bfd_byte *dyncon, *dynconend;
3179
3180 dynconend = sdyn->contents + sdyn->size;
3181 for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
3182 {
3183 Elf_Internal_Dyn dyn;
3184 asection *s;
3185
3186 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
3187
3188 switch (dyn.d_tag)
3189 {
3190 case DT_PLTGOT:
3191 s = htab->elf.sgotplt;
3192 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3193 break;
3194 case DT_JMPREL:
3195 s = htab->elf.srelplt;
3196 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3197 break;
3198 case DT_PLTRELSZ:
3199 s = htab->elf.srelplt;
3200 dyn.d_un.d_val = s->size;
3201 break;
3202 default:
3203 continue;
3204 }
3205
3206 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3207 }
0a1b45a2 3208 return true;
e23eba97
NC
3209}
3210
0a1b45a2 3211static bool
e23eba97
NC
3212riscv_elf_finish_dynamic_sections (bfd *output_bfd,
3213 struct bfd_link_info *info)
3214{
3215 bfd *dynobj;
3216 asection *sdyn;
3217 struct riscv_elf_link_hash_table *htab;
3218
3219 htab = riscv_elf_hash_table (info);
3220 BFD_ASSERT (htab != NULL);
3221 dynobj = htab->elf.dynobj;
3222
3223 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3224
3225 if (elf_hash_table (info)->dynamic_sections_created)
3226 {
3227 asection *splt;
0a1b45a2 3228 bool ret;
e23eba97
NC
3229
3230 splt = htab->elf.splt;
3231 BFD_ASSERT (splt != NULL && sdyn != NULL);
3232
3233 ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
3234
535b785f 3235 if (!ret)
e23eba97
NC
3236 return ret;
3237
3238 /* Fill in the head and tail entries in the procedure linkage table. */
3239 if (splt->size > 0)
3240 {
3241 int i;
3242 uint32_t plt_header[PLT_HEADER_INSNS];
5ef23793
JW
3243 ret = riscv_make_plt_header (output_bfd,
3244 sec_addr (htab->elf.sgotplt),
3245 sec_addr (splt), plt_header);
3246 if (!ret)
3247 return ret;
e23eba97
NC
3248
3249 for (i = 0; i < PLT_HEADER_INSNS; i++)
fbc09e7a 3250 bfd_putl32 (plt_header[i], splt->contents + 4*i);
e23eba97 3251
cc162427
AW
3252 elf_section_data (splt->output_section)->this_hdr.sh_entsize
3253 = PLT_ENTRY_SIZE;
3254 }
e23eba97
NC
3255 }
3256
3257 if (htab->elf.sgotplt)
3258 {
3259 asection *output_section = htab->elf.sgotplt->output_section;
3260
3261 if (bfd_is_abs_section (output_section))
3262 {
3263 (*_bfd_error_handler)
871b3ab2 3264 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
0a1b45a2 3265 return false;
e23eba97
NC
3266 }
3267
3268 if (htab->elf.sgotplt->size > 0)
3269 {
3270 /* Write the first two entries in .got.plt, needed for the dynamic
3271 linker. */
3272 bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
3273 bfd_put_NN (output_bfd, (bfd_vma) 0,
3274 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
3275 }
3276
3277 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3278 }
3279
3280 if (htab->elf.sgot)
3281 {
3282 asection *output_section = htab->elf.sgot->output_section;
3283
3284 if (htab->elf.sgot->size > 0)
3285 {
3286 /* Set the first entry in the global offset table to the address of
3287 the dynamic section. */
3288 bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
3289 bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
3290 }
3291
3292 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3293 }
3294
02dd9d25
NC
3295 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3296 htab_traverse (htab->loc_hash_table,
3297 riscv_elf_finish_local_dynamic_symbol,
3298 info);
3299
0a1b45a2 3300 return true;
e23eba97
NC
3301}
3302
3303/* Return address for Ith PLT stub in section PLT, for relocation REL
3304 or (bfd_vma) -1 if it should not be included. */
3305
3306static bfd_vma
3307riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
3308 const arelent *rel ATTRIBUTE_UNUSED)
3309{
3310 return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
3311}
3312
3313static enum elf_reloc_type_class
3314riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3315 const asection *rel_sec ATTRIBUTE_UNUSED,
3316 const Elf_Internal_Rela *rela)
3317{
3318 switch (ELFNN_R_TYPE (rela->r_info))
3319 {
3320 case R_RISCV_RELATIVE:
3321 return reloc_class_relative;
3322 case R_RISCV_JUMP_SLOT:
3323 return reloc_class_plt;
3324 case R_RISCV_COPY:
3325 return reloc_class_copy;
3326 default:
3327 return reloc_class_normal;
3328 }
3329}
3330
0242af40
JW
3331/* Given the ELF header flags in FLAGS, it returns a string that describes the
3332 float ABI. */
3333
3334static const char *
3335riscv_float_abi_string (flagword flags)
3336{
3337 switch (flags & EF_RISCV_FLOAT_ABI)
3338 {
3339 case EF_RISCV_FLOAT_ABI_SOFT:
3340 return "soft-float";
3341 break;
3342 case EF_RISCV_FLOAT_ABI_SINGLE:
3343 return "single-float";
3344 break;
3345 case EF_RISCV_FLOAT_ABI_DOUBLE:
3346 return "double-float";
3347 break;
3348 case EF_RISCV_FLOAT_ABI_QUAD:
3349 return "quad-float";
3350 break;
3351 default:
3352 abort ();
3353 }
3354}
3355
dcd709e0 3356/* The information of architecture elf attributes. */
7d7a7d7c
JW
3357static riscv_subset_list_t in_subsets;
3358static riscv_subset_list_t out_subsets;
3359static riscv_subset_list_t merged_subsets;
3360
3361/* Predicator for standard extension. */
3362
0a1b45a2 3363static bool
7d7a7d7c
JW
3364riscv_std_ext_p (const char *name)
3365{
3366 return (strlen (name) == 1) && (name[0] != 'x') && (name[0] != 's');
3367}
3368
87fdd7ac
PD
3369/* Update the output subset's version to match the input when the input
3370 subset's version is newer. */
7d7a7d7c 3371
87fdd7ac
PD
3372static void
3373riscv_update_subset_version (struct riscv_subset_t *in,
3374 struct riscv_subset_t *out)
7d7a7d7c 3375{
32f0ce4d 3376 if (in == NULL || out == NULL)
87fdd7ac
PD
3377 return;
3378
3379 /* Update the output ISA versions to the newest ones, but otherwise don't
3380 provide any errors or warnings about mis-matched ISA versions as it's
3381 generally too tricky to check for these at link time. */
3382 if ((in->major_version > out->major_version)
3383 || (in->major_version == out->major_version
3384 && in->minor_version > out->minor_version)
3385 || (out->major_version == RISCV_UNKNOWN_VERSION))
32f0ce4d 3386 {
87fdd7ac
PD
3387 out->major_version = in->major_version;
3388 out->minor_version = in->minor_version;
32f0ce4d 3389 }
7d7a7d7c
JW
3390}
3391
3392/* Return true if subset is 'i' or 'e'. */
3393
0a1b45a2 3394static bool
7d7a7d7c
JW
3395riscv_i_or_e_p (bfd *ibfd,
3396 const char *arch,
3397 struct riscv_subset_t *subset)
3398{
3399 if ((strcasecmp (subset->name, "e") != 0)
3400 && (strcasecmp (subset->name, "i") != 0))
3401 {
3402 _bfd_error_handler
9184ef8a
NC
3403 (_("error: %pB: corrupted ISA string '%s'. "
3404 "First letter should be 'i' or 'e' but got '%s'"),
7d7a7d7c 3405 ibfd, arch, subset->name);
0a1b45a2 3406 return false;
7d7a7d7c 3407 }
0a1b45a2 3408 return true;
7d7a7d7c
JW
3409}
3410
3411/* Merge standard extensions.
3412
3413 Return Value:
3414 Return FALSE if failed to merge.
3415
3416 Arguments:
3417 `bfd`: bfd handler.
dcd709e0
NC
3418 `in_arch`: Raw ISA string for input object.
3419 `out_arch`: Raw ISA string for output object.
3420 `pin`: Subset list for input object.
3421 `pout`: Subset list for output object. */
7d7a7d7c 3422
0a1b45a2 3423static bool
7d7a7d7c
JW
3424riscv_merge_std_ext (bfd *ibfd,
3425 const char *in_arch,
3426 const char *out_arch,
3427 struct riscv_subset_t **pin,
3428 struct riscv_subset_t **pout)
3429{
c341f467 3430 const char *standard_exts = "mafdqlcbjtpvnh";
7d7a7d7c
JW
3431 const char *p;
3432 struct riscv_subset_t *in = *pin;
3433 struct riscv_subset_t *out = *pout;
3434
3435 /* First letter should be 'i' or 'e'. */
3436 if (!riscv_i_or_e_p (ibfd, in_arch, in))
0a1b45a2 3437 return false;
7d7a7d7c
JW
3438
3439 if (!riscv_i_or_e_p (ibfd, out_arch, out))
0a1b45a2 3440 return false;
7d7a7d7c 3441
8f595e9b 3442 if (strcasecmp (in->name, out->name) != 0)
7d7a7d7c
JW
3443 {
3444 /* TODO: We might allow merge 'i' with 'e'. */
3445 _bfd_error_handler
9184ef8a 3446 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
7d7a7d7c 3447 ibfd, in->name, out->name);
0a1b45a2 3448 return false;
7d7a7d7c 3449 }
87fdd7ac
PD
3450
3451 riscv_update_subset_version(in, out);
3452 riscv_add_subset (&merged_subsets,
3453 out->name, out->major_version, out->minor_version);
7d7a7d7c
JW
3454
3455 in = in->next;
3456 out = out->next;
3457
3458 /* Handle standard extension first. */
3459 for (p = standard_exts; *p; ++p)
3460 {
dfe92496 3461 struct riscv_subset_t *ext_in, *ext_out, *ext_merged;
7d7a7d7c 3462 char find_ext[2] = {*p, '\0'};
0a1b45a2 3463 bool find_in, find_out;
7d7a7d7c 3464
dfe92496
NC
3465 find_in = riscv_lookup_subset (&in_subsets, find_ext, &ext_in);
3466 find_out = riscv_lookup_subset (&out_subsets, find_ext, &ext_out);
3467
3468 if (!find_in && !find_out)
7d7a7d7c
JW
3469 continue;
3470
87fdd7ac
PD
3471 if (find_in && find_out)
3472 riscv_update_subset_version(ext_in, ext_out);
7d7a7d7c 3473
dfe92496
NC
3474 ext_merged = find_out ? ext_out : ext_in;
3475 riscv_add_subset (&merged_subsets, ext_merged->name,
3476 ext_merged->major_version, ext_merged->minor_version);
7d7a7d7c
JW
3477 }
3478
3479 /* Skip all standard extensions. */
3480 while ((in != NULL) && riscv_std_ext_p (in->name)) in = in->next;
3481 while ((out != NULL) && riscv_std_ext_p (out->name)) out = out->next;
3482
3483 *pin = in;
3484 *pout = out;
3485
0a1b45a2 3486 return true;
7d7a7d7c
JW
3487}
3488
403d1bd9
JW
3489/* Merge multi letter extensions. PIN is a pointer to the head of the input
3490 object subset list. Likewise for POUT and the output object. Return TRUE
3491 on success and FALSE when a conflict is found. */
7d7a7d7c 3492
0a1b45a2 3493static bool
87fdd7ac 3494riscv_merge_multi_letter_ext (riscv_subset_t **pin,
403d1bd9 3495 riscv_subset_t **pout)
7d7a7d7c
JW
3496{
3497 riscv_subset_t *in = *pin;
3498 riscv_subset_t *out = *pout;
403d1bd9 3499 riscv_subset_t *tail;
7d7a7d7c 3500
403d1bd9 3501 int cmp;
7d7a7d7c 3502
403d1bd9 3503 while (in && out)
7d7a7d7c 3504 {
4c0e540e 3505 cmp = riscv_compare_subsets (in->name, out->name);
403d1bd9
JW
3506
3507 if (cmp < 0)
3508 {
3509 /* `in' comes before `out', append `in' and increment. */
3510 riscv_add_subset (&merged_subsets, in->name, in->major_version,
3511 in->minor_version);
3512 in = in->next;
3513 }
3514 else if (cmp > 0)
3515 {
3516 /* `out' comes before `in', append `out' and increment. */
3517 riscv_add_subset (&merged_subsets, out->name, out->major_version,
3518 out->minor_version);
3519 out = out->next;
3520 }
3521 else
7d7a7d7c 3522 {
403d1bd9 3523 /* Both present, check version and increment both. */
87fdd7ac 3524 riscv_update_subset_version (in, out);
403d1bd9
JW
3525
3526 riscv_add_subset (&merged_subsets, out->name, out->major_version,
3527 out->minor_version);
3528 out = out->next;
3529 in = in->next;
7d7a7d7c 3530 }
7d7a7d7c
JW
3531 }
3532
1942a048
NC
3533 if (in || out)
3534 {
3535 /* If we're here, either `in' or `out' is running longer than
3536 the other. So, we need to append the corresponding tail. */
3537 tail = in ? in : out;
3538 while (tail)
3539 {
3540 riscv_add_subset (&merged_subsets, tail->name, tail->major_version,
3541 tail->minor_version);
3542 tail = tail->next;
3543 }
3544 }
403d1bd9 3545
0a1b45a2 3546 return true;
7d7a7d7c
JW
3547}
3548
3549/* Merge Tag_RISCV_arch attribute. */
3550
3551static char *
3552riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
3553{
3554 riscv_subset_t *in, *out;
3555 char *merged_arch_str;
3556
3557 unsigned xlen_in, xlen_out;
3558 merged_subsets.head = NULL;
3559 merged_subsets.tail = NULL;
3560
f786c359
NC
3561 riscv_parse_subset_t riscv_rps_ld_in =
3562 {&in_subsets, _bfd_error_handler, &xlen_in, NULL, false};
3563 riscv_parse_subset_t riscv_rps_ld_out =
3564 {&out_subsets, _bfd_error_handler, &xlen_out, NULL, false};
7d7a7d7c
JW
3565
3566 if (in_arch == NULL && out_arch == NULL)
3567 return NULL;
7d7a7d7c
JW
3568 if (in_arch == NULL && out_arch != NULL)
3569 return out_arch;
7d7a7d7c
JW
3570 if (in_arch != NULL && out_arch == NULL)
3571 return in_arch;
3572
dcd709e0 3573 /* Parse subset from ISA string. */
f786c359 3574 if (!riscv_parse_subset (&riscv_rps_ld_in, in_arch))
7d7a7d7c 3575 return NULL;
f786c359 3576 if (!riscv_parse_subset (&riscv_rps_ld_out, out_arch))
7d7a7d7c
JW
3577 return NULL;
3578
3579 /* Checking XLEN. */
3580 if (xlen_out != xlen_in)
3581 {
3582 _bfd_error_handler
3583 (_("error: %pB: ISA string of input (%s) doesn't match "
9184ef8a 3584 "output (%s)"), ibfd, in_arch, out_arch);
7d7a7d7c
JW
3585 return NULL;
3586 }
3587
3588 /* Merge subset list. */
3589 in = in_subsets.head;
3590 out = out_subsets.head;
3591
3592 /* Merge standard extension. */
3593 if (!riscv_merge_std_ext (ibfd, in_arch, out_arch, &in, &out))
3594 return NULL;
403d1bd9
JW
3595
3596 /* Merge all non-single letter extensions with single call. */
87fdd7ac 3597 if (!riscv_merge_multi_letter_ext (&in, &out))
7d7a7d7c
JW
3598 return NULL;
3599
3600 if (xlen_in != xlen_out)
3601 {
3602 _bfd_error_handler
3603 (_("error: %pB: XLEN of input (%u) doesn't match "
9184ef8a 3604 "output (%u)"), ibfd, xlen_in, xlen_out);
7d7a7d7c
JW
3605 return NULL;
3606 }
3607
3608 if (xlen_in != ARCH_SIZE)
3609 {
3610 _bfd_error_handler
9184ef8a
NC
3611 (_("error: %pB: unsupported XLEN (%u), you might be "
3612 "using wrong emulation"), ibfd, xlen_in);
7d7a7d7c
JW
3613 return NULL;
3614 }
3615
3616 merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets);
3617
3618 /* Release the subset lists. */
3619 riscv_release_subset_list (&in_subsets);
3620 riscv_release_subset_list (&out_subsets);
3621 riscv_release_subset_list (&merged_subsets);
3622
3623 return merged_arch_str;
3624}
3625
3626/* Merge object attributes from IBFD into output_bfd of INFO.
3627 Raise an error if there are conflicting attributes. */
3628
0a1b45a2 3629static bool
7d7a7d7c
JW
3630riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
3631{
3632 bfd *obfd = info->output_bfd;
3633 obj_attribute *in_attr;
3634 obj_attribute *out_attr;
0a1b45a2
AM
3635 bool result = true;
3636 bool priv_attrs_merged = false;
7d7a7d7c
JW
3637 const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
3638 unsigned int i;
3639
3640 /* Skip linker created files. */
3641 if (ibfd->flags & BFD_LINKER_CREATED)
0a1b45a2 3642 return true;
7d7a7d7c
JW
3643
3644 /* Skip any input that doesn't have an attribute section.
3645 This enables to link object files without attribute section with
3646 any others. */
3647 if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
0a1b45a2 3648 return true;
7d7a7d7c
JW
3649
3650 if (!elf_known_obj_attributes_proc (obfd)[0].i)
3651 {
3652 /* This is the first object. Copy the attributes. */
3653 _bfd_elf_copy_obj_attributes (ibfd, obfd);
3654
3655 out_attr = elf_known_obj_attributes_proc (obfd);
3656
3657 /* Use the Tag_null value to indicate the attributes have been
3658 initialized. */
3659 out_attr[0].i = 1;
3660
0a1b45a2 3661 return true;
7d7a7d7c
JW
3662 }
3663
3664 in_attr = elf_known_obj_attributes_proc (ibfd);
3665 out_attr = elf_known_obj_attributes_proc (obfd);
3666
3667 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
3668 {
3669 switch (i)
3670 {
3671 case Tag_RISCV_arch:
3672 if (!out_attr[Tag_RISCV_arch].s)
3673 out_attr[Tag_RISCV_arch].s = in_attr[Tag_RISCV_arch].s;
3674 else if (in_attr[Tag_RISCV_arch].s
3675 && out_attr[Tag_RISCV_arch].s)
3676 {
dcd709e0 3677 /* Check compatible. */
7d7a7d7c
JW
3678 char *merged_arch =
3679 riscv_merge_arch_attr_info (ibfd,
3680 in_attr[Tag_RISCV_arch].s,
3681 out_attr[Tag_RISCV_arch].s);
3682 if (merged_arch == NULL)
3683 {
0a1b45a2 3684 result = false;
7d7a7d7c
JW
3685 out_attr[Tag_RISCV_arch].s = "";
3686 }
3687 else
3688 out_attr[Tag_RISCV_arch].s = merged_arch;
3689 }
3690 break;
41285764 3691
7d7a7d7c
JW
3692 case Tag_RISCV_priv_spec:
3693 case Tag_RISCV_priv_spec_minor:
3694 case Tag_RISCV_priv_spec_revision:
dcd709e0 3695 /* If we have handled the privileged elf attributes, then skip it. */
cbd7581f 3696 if (!priv_attrs_merged)
41285764 3697 {
cbd7581f
NC
3698 unsigned int Tag_a = Tag_RISCV_priv_spec;
3699 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
3700 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
3d73d29e
NC
3701 enum riscv_spec_class in_priv_spec = PRIV_SPEC_CLASS_NONE;
3702 enum riscv_spec_class out_priv_spec = PRIV_SPEC_CLASS_NONE;
39ff0b81 3703
dcd709e0 3704 /* Get the privileged spec class from elf attributes. */
39ff0b81
NC
3705 riscv_get_priv_spec_class_from_numbers (in_attr[Tag_a].i,
3706 in_attr[Tag_b].i,
3707 in_attr[Tag_c].i,
3708 &in_priv_spec);
3709 riscv_get_priv_spec_class_from_numbers (out_attr[Tag_a].i,
3710 out_attr[Tag_b].i,
3711 out_attr[Tag_c].i,
3712 &out_priv_spec);
cbd7581f 3713
dcd709e0 3714 /* Allow to link the object without the privileged specs. */
39ff0b81 3715 if (out_priv_spec == PRIV_SPEC_CLASS_NONE)
cbd7581f
NC
3716 {
3717 out_attr[Tag_a].i = in_attr[Tag_a].i;
3718 out_attr[Tag_b].i = in_attr[Tag_b].i;
3719 out_attr[Tag_c].i = in_attr[Tag_c].i;
3720 }
39ff0b81
NC
3721 else if (in_priv_spec != PRIV_SPEC_CLASS_NONE
3722 && in_priv_spec != out_priv_spec)
cbd7581f
NC
3723 {
3724 _bfd_error_handler
b800637e 3725 (_("warning: %pB use privileged spec version %u.%u.%u but "
9184ef8a 3726 "the output use version %u.%u.%u"),
cbd7581f
NC
3727 ibfd,
3728 in_attr[Tag_a].i,
3729 in_attr[Tag_b].i,
3730 in_attr[Tag_c].i,
3731 out_attr[Tag_a].i,
3732 out_attr[Tag_b].i,
3733 out_attr[Tag_c].i);
39ff0b81 3734
dcd709e0
NC
3735 /* The privileged spec v1.9.1 can not be linked with others
3736 since the conflicts, so we plan to drop it in a year or
3737 two. */
39ff0b81
NC
3738 if (in_priv_spec == PRIV_SPEC_CLASS_1P9P1
3739 || out_priv_spec == PRIV_SPEC_CLASS_1P9P1)
3740 {
3741 _bfd_error_handler
b800637e 3742 (_("warning: privileged spec version 1.9.1 can not be "
9184ef8a 3743 "linked with other spec versions"));
39ff0b81
NC
3744 }
3745
dcd709e0 3746 /* Update the output privileged spec to the newest one. */
39ff0b81
NC
3747 if (in_priv_spec > out_priv_spec)
3748 {
3749 out_attr[Tag_a].i = in_attr[Tag_a].i;
3750 out_attr[Tag_b].i = in_attr[Tag_b].i;
3751 out_attr[Tag_c].i = in_attr[Tag_c].i;
3752 }
cbd7581f 3753 }
0a1b45a2 3754 priv_attrs_merged = true;
7d7a7d7c
JW
3755 }
3756 break;
41285764 3757
7d7a7d7c
JW
3758 case Tag_RISCV_unaligned_access:
3759 out_attr[i].i |= in_attr[i].i;
3760 break;
41285764 3761
7d7a7d7c
JW
3762 case Tag_RISCV_stack_align:
3763 if (out_attr[i].i == 0)
3764 out_attr[i].i = in_attr[i].i;
3765 else if (in_attr[i].i != 0
3766 && out_attr[i].i != 0
3767 && out_attr[i].i != in_attr[i].i)
3768 {
3769 _bfd_error_handler
3770 (_("error: %pB use %u-byte stack aligned but the output "
9184ef8a 3771 "use %u-byte stack aligned"),
7d7a7d7c 3772 ibfd, in_attr[i].i, out_attr[i].i);
0a1b45a2 3773 result = false;
7d7a7d7c
JW
3774 }
3775 break;
41285764 3776
7d7a7d7c
JW
3777 default:
3778 result &= _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
3779 }
3780
3781 /* If out_attr was copied from in_attr then it won't have a type yet. */
3782 if (in_attr[i].type && !out_attr[i].type)
3783 out_attr[i].type = in_attr[i].type;
3784 }
3785
3786 /* Merge Tag_compatibility attributes and any common GNU ones. */
3787 if (!_bfd_elf_merge_object_attributes (ibfd, info))
0a1b45a2 3788 return false;
7d7a7d7c
JW
3789
3790 /* Check for any attributes not known on RISC-V. */
3791 result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
3792
3793 return result;
3794}
3795
e23eba97
NC
3796/* Merge backend specific data from an object file to the output
3797 object file when linking. */
3798
0a1b45a2 3799static bool
e23eba97
NC
3800_bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3801{
3802 bfd *obfd = info->output_bfd;
87f98bac 3803 flagword new_flags, old_flags;
e23eba97
NC
3804
3805 if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
0a1b45a2 3806 return true;
e23eba97
NC
3807
3808 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
3809 {
3810 (*_bfd_error_handler)
871b3ab2 3811 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
96b0927d
PD
3812 " target emulation `%s' does not match `%s'"),
3813 ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
0a1b45a2 3814 return false;
e23eba97
NC
3815 }
3816
3817 if (!_bfd_elf_merge_object_attributes (ibfd, info))
0a1b45a2 3818 return false;
e23eba97 3819
7d7a7d7c 3820 if (!riscv_merge_attributes (ibfd, info))
0a1b45a2 3821 return false;
7d7a7d7c 3822
87f98bac
JW
3823 /* Check to see if the input BFD actually contains any sections. If not,
3824 its flags may not have been initialized either, but it cannot actually
3825 cause any incompatibility. Do not short-circuit dynamic objects; their
3826 section list may be emptied by elf_link_add_object_symbols.
3827
3828 Also check to see if there are no code sections in the input. In this
3829 case, there is no need to check for code specific flags. */
3830 if (!(ibfd->flags & DYNAMIC))
3831 {
0a1b45a2
AM
3832 bool null_input_bfd = true;
3833 bool only_data_sections = true;
87f98bac
JW
3834 asection *sec;
3835
3836 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3837 {
0a1b45a2 3838 null_input_bfd = false;
0d6aab77 3839
fd361982 3840 if ((bfd_section_flags (sec)
87f98bac
JW
3841 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3842 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
0d6aab77 3843 {
0a1b45a2 3844 only_data_sections = false;
0d6aab77
NC
3845 break;
3846 }
87f98bac
JW
3847 }
3848
3849 if (null_input_bfd || only_data_sections)
0a1b45a2 3850 return true;
87f98bac
JW
3851 }
3852
0d6aab77
NC
3853 new_flags = elf_elfheader (ibfd)->e_flags;
3854 old_flags = elf_elfheader (obfd)->e_flags;
3855
3856 if (!elf_flags_init (obfd))
3857 {
0a1b45a2 3858 elf_flags_init (obfd) = true;
0d6aab77 3859 elf_elfheader (obfd)->e_flags = new_flags;
0a1b45a2 3860 return true;
0d6aab77
NC
3861 }
3862
2922d21d
AW
3863 /* Disallow linking different float ABIs. */
3864 if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
e23eba97
NC
3865 {
3866 (*_bfd_error_handler)
0242af40
JW
3867 (_("%pB: can't link %s modules with %s modules"), ibfd,
3868 riscv_float_abi_string (new_flags),
3869 riscv_float_abi_string (old_flags));
e23eba97
NC
3870 goto fail;
3871 }
3872
7f999549
JW
3873 /* Disallow linking RVE and non-RVE. */
3874 if ((old_flags ^ new_flags) & EF_RISCV_RVE)
3875 {
3876 (*_bfd_error_handler)
3877 (_("%pB: can't link RVE with other target"), ibfd);
3878 goto fail;
3879 }
3880
e23eba97
NC
3881 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3882 elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
3883
96462b01
S
3884 /* Allow linking TSO and non-TSO, and keep the TSO flag. */
3885 elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_TSO;
3886
0a1b45a2 3887 return true;
e23eba97 3888
dc1e8a47 3889 fail:
e23eba97 3890 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3891 return false;
e23eba97
NC
3892}
3893
9d06997a
PD
3894/* A second format for recording PC-relative hi relocations. This stores the
3895 information required to relax them to GP-relative addresses. */
3896
3897typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc;
3898struct riscv_pcgp_hi_reloc
3899{
3900 bfd_vma hi_sec_off;
3901 bfd_vma hi_addend;
3902 bfd_vma hi_addr;
3903 unsigned hi_sym;
3904 asection *sym_sec;
0a1b45a2 3905 bool undefined_weak;
9d06997a
PD
3906 riscv_pcgp_hi_reloc *next;
3907};
3908
3909typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc;
3910struct riscv_pcgp_lo_reloc
3911{
3912 bfd_vma hi_sec_off;
3913 riscv_pcgp_lo_reloc *next;
3914};
3915
3916typedef struct
3917{
3918 riscv_pcgp_hi_reloc *hi;
3919 riscv_pcgp_lo_reloc *lo;
3920} riscv_pcgp_relocs;
3921
5f9aecea
JW
3922/* Initialize the pcgp reloc info in P. */
3923
0a1b45a2 3924static bool
9d06997a
PD
3925riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
3926{
3927 p->hi = NULL;
3928 p->lo = NULL;
0a1b45a2 3929 return true;
9d06997a
PD
3930}
3931
5f9aecea
JW
3932/* Free the pcgp reloc info in P. */
3933
9d06997a
PD
3934static void
3935riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
3936 bfd *abfd ATTRIBUTE_UNUSED,
3937 asection *sec ATTRIBUTE_UNUSED)
3938{
3939 riscv_pcgp_hi_reloc *c;
3940 riscv_pcgp_lo_reloc *l;
3941
1942a048 3942 for (c = p->hi; c != NULL; )
9d06997a
PD
3943 {
3944 riscv_pcgp_hi_reloc *next = c->next;
3945 free (c);
3946 c = next;
3947 }
3948
1942a048 3949 for (l = p->lo; l != NULL; )
9d06997a
PD
3950 {
3951 riscv_pcgp_lo_reloc *next = l->next;
3952 free (l);
3953 l = next;
3954 }
3955}
3956
5f9aecea
JW
3957/* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
3958 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
3959 relax the corresponding lo part reloc. */
3960
0a1b45a2 3961static bool
9d06997a
PD
3962riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
3963 bfd_vma hi_addend, bfd_vma hi_addr,
9d1da81b 3964 unsigned hi_sym, asection *sym_sec,
0a1b45a2 3965 bool undefined_weak)
9d06997a 3966{
1942a048 3967 riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof (*new));
9d06997a 3968 if (!new)
0a1b45a2 3969 return false;
9d06997a
PD
3970 new->hi_sec_off = hi_sec_off;
3971 new->hi_addend = hi_addend;
3972 new->hi_addr = hi_addr;
3973 new->hi_sym = hi_sym;
3974 new->sym_sec = sym_sec;
9d1da81b 3975 new->undefined_weak = undefined_weak;
9d06997a
PD
3976 new->next = p->hi;
3977 p->hi = new;
0a1b45a2 3978 return true;
9d06997a
PD
3979}
3980
5f9aecea
JW
3981/* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
3982 This is used by a lo part reloc to find the corresponding hi part reloc. */
3983
9d06997a 3984static riscv_pcgp_hi_reloc *
1942a048 3985riscv_find_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
9d06997a
PD
3986{
3987 riscv_pcgp_hi_reloc *c;
3988
3989 for (c = p->hi; c != NULL; c = c->next)
3990 if (c->hi_sec_off == hi_sec_off)
3991 return c;
3992 return NULL;
3993}
3994
5f9aecea
JW
3995/* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
3996 This is used to record relocs that can't be relaxed. */
9d06997a 3997
0a1b45a2 3998static bool
9d06997a
PD
3999riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4000{
1942a048 4001 riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof (*new));
9d06997a 4002 if (!new)
0a1b45a2 4003 return false;
9d06997a
PD
4004 new->hi_sec_off = hi_sec_off;
4005 new->next = p->lo;
4006 p->lo = new;
0a1b45a2 4007 return true;
9d06997a
PD
4008}
4009
5f9aecea
JW
4010/* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4011 This is used by a hi part reloc to find the corresponding lo part reloc. */
4012
0a1b45a2 4013static bool
9d06997a
PD
4014riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4015{
4016 riscv_pcgp_lo_reloc *c;
4017
4018 for (c = p->lo; c != NULL; c = c->next)
4019 if (c->hi_sec_off == hi_sec_off)
0a1b45a2
AM
4020 return true;
4021 return false;
9d06997a
PD
4022}
4023
9abcdc10
LR
4024static void
4025riscv_update_pcgp_relocs (riscv_pcgp_relocs *p, asection *deleted_sec,
4026 bfd_vma deleted_addr, size_t deleted_count)
4027{
4028 /* Bytes have already been deleted and toaddr should match the old section
4029 size for our checks, so adjust it here. */
4030 bfd_vma toaddr = deleted_sec->size + deleted_count;
4031 riscv_pcgp_lo_reloc *l;
4032 riscv_pcgp_hi_reloc *h;
4033
4034 /* Update section offsets of corresponding pcrel_hi relocs for the pcrel_lo
4035 entries where they occur after the deleted bytes. */
4036 for (l = p->lo; l != NULL; l = l->next)
4037 if (l->hi_sec_off > deleted_addr
4038 && l->hi_sec_off < toaddr)
4039 l->hi_sec_off -= deleted_count;
4040
4041 /* Update both section offsets, and symbol values of pcrel_hi relocs where
4042 these values occur after the deleted bytes. */
4043 for (h = p->hi; h != NULL; h = h->next)
4044 {
4045 if (h->hi_sec_off > deleted_addr
4046 && h->hi_sec_off < toaddr)
4047 h->hi_sec_off -= deleted_count;
4048 if (h->sym_sec == deleted_sec
4049 && h->hi_addr > deleted_addr
4050 && h->hi_addr < toaddr)
4051 h->hi_addr -= deleted_count;
4052 }
4053}
4054
43025f01 4055/* Delete some bytes, adjust relcocations and symbol table from a section. */
9abcdc10
LR
4056
4057static bool
43025f01
PN
4058_riscv_relax_delete_bytes (bfd *abfd,
4059 asection *sec,
4060 bfd_vma addr,
4061 size_t count,
4062 struct bfd_link_info *link_info,
4063 riscv_pcgp_relocs *p,
4064 bfd_vma delete_total,
4065 bfd_vma toaddr)
9abcdc10
LR
4066{
4067 unsigned int i, symcount;
9abcdc10
LR
4068 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
4069 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4070 unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4071 struct bfd_elf_section_data *data = elf_section_data (sec);
4072 bfd_byte *contents = data->this_hdr.contents;
43025f01 4073 size_t bytes_to_move = toaddr - addr - count;
9abcdc10
LR
4074
4075 /* Actually delete the bytes. */
4076 sec->size -= count;
43025f01
PN
4077 memmove (contents + addr, contents + addr + count + delete_total, bytes_to_move);
4078
4079 /* Still adjust relocations and symbols in non-linear times. */
4080 toaddr = sec->size + count;
9abcdc10
LR
4081
4082 /* Adjust the location of all of the relocs. Note that we need not
4083 adjust the addends, since all PC-relative references must be against
4084 symbols, which we will adjust below. */
4085 for (i = 0; i < sec->reloc_count; i++)
4086 if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
4087 data->relocs[i].r_offset -= count;
4088
4089 /* Adjust the hi_sec_off, and the hi_addr of any entries in the pcgp relocs
4090 table for which these values occur after the deleted bytes. */
4091 if (p)
4092 riscv_update_pcgp_relocs (p, sec, addr, count);
4093
4094 /* Adjust the local symbols defined in this section. */
4095 for (i = 0; i < symtab_hdr->sh_info; i++)
4096 {
4097 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
4098 if (sym->st_shndx == sec_shndx)
4099 {
4100 /* If the symbol is in the range of memory we just moved, we
4101 have to adjust its value. */
4102 if (sym->st_value > addr && sym->st_value <= toaddr)
4103 sym->st_value -= count;
4104
4105 /* If the symbol *spans* the bytes we just deleted (i.e. its
4106 *end* is in the moved bytes but its *start* isn't), then we
4107 must adjust its size.
4108
4109 This test needs to use the original value of st_value, otherwise
4110 we might accidentally decrease size when deleting bytes right
4111 before the symbol. But since deleted relocs can't span across
4112 symbols, we can't have both a st_value and a st_size decrease,
4113 so it is simpler to just use an else. */
4114 else if (sym->st_value <= addr
4115 && sym->st_value + sym->st_size > addr
4116 && sym->st_value + sym->st_size <= toaddr)
4117 sym->st_size -= count;
4118 }
4119 }
4120
4121 /* Now adjust the global symbols defined in this section. */
4122 symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
4123 - symtab_hdr->sh_info);
4124
4125 for (i = 0; i < symcount; i++)
4126 {
4127 struct elf_link_hash_entry *sym_hash = sym_hashes[i];
4128
4129 /* The '--wrap SYMBOL' option is causing a pain when the object file,
4130 containing the definition of __wrap_SYMBOL, includes a direct
4131 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
4132 the same symbol (which is __wrap_SYMBOL), but still exist as two
4133 different symbols in 'sym_hashes', we don't want to adjust
4134 the global symbol __wrap_SYMBOL twice.
4135
4136 The same problem occurs with symbols that are versioned_hidden, as
4137 foo becomes an alias for foo@BAR, and hence they need the same
4138 treatment. */
4139 if (link_info->wrap_hash != NULL
4140 || sym_hash->versioned != unversioned)
4141 {
4142 struct elf_link_hash_entry **cur_sym_hashes;
4143
4144 /* Loop only over the symbols which have already been checked. */
4145 for (cur_sym_hashes = sym_hashes; cur_sym_hashes < &sym_hashes[i];
4146 cur_sym_hashes++)
4147 {
4148 /* If the current symbol is identical to 'sym_hash', that means
4149 the symbol was already adjusted (or at least checked). */
4150 if (*cur_sym_hashes == sym_hash)
4151 break;
4152 }
4153 /* Don't adjust the symbol again. */
4154 if (cur_sym_hashes < &sym_hashes[i])
4155 continue;
4156 }
4157
4158 if ((sym_hash->root.type == bfd_link_hash_defined
4159 || sym_hash->root.type == bfd_link_hash_defweak)
4160 && sym_hash->root.u.def.section == sec)
4161 {
4162 /* As above, adjust the value if needed. */
4163 if (sym_hash->root.u.def.value > addr
4164 && sym_hash->root.u.def.value <= toaddr)
4165 sym_hash->root.u.def.value -= count;
4166
4167 /* As above, adjust the size if needed. */
4168 else if (sym_hash->root.u.def.value <= addr
4169 && sym_hash->root.u.def.value + sym_hash->size > addr
4170 && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
4171 sym_hash->size -= count;
4172 }
4173 }
4174
4175 return true;
4176}
4177
43025f01
PN
4178typedef bool (*relax_delete_t) (bfd *, asection *,
4179 bfd_vma, size_t,
4180 struct bfd_link_info *,
4181 riscv_pcgp_relocs *,
4182 Elf_Internal_Rela *);
4183
4184static relax_delete_t riscv_relax_delete_bytes;
4185
4186/* Do not delete some bytes from a section while relaxing.
4187 Just mark the deleted bytes as R_RISCV_DELETE. */
4188
4189static bool
4190_riscv_relax_delete_piecewise (bfd *abfd ATTRIBUTE_UNUSED,
4191 asection *sec ATTRIBUTE_UNUSED,
4192 bfd_vma addr,
4193 size_t count,
4194 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
4195 riscv_pcgp_relocs *p ATTRIBUTE_UNUSED,
4196 Elf_Internal_Rela *rel)
4197{
4198 if (rel == NULL)
4199 return false;
4200 rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE);
4201 rel->r_offset = addr;
4202 rel->r_addend = count;
4203 return true;
4204}
4205
4206/* Delete some bytes from a section while relaxing. */
4207
4208static bool
4209_riscv_relax_delete_immediate (bfd *abfd,
4210 asection *sec,
4211 bfd_vma addr,
4212 size_t count,
4213 struct bfd_link_info *link_info,
4214 riscv_pcgp_relocs *p,
4215 Elf_Internal_Rela *rel)
4216{
4217 if (rel != NULL)
4218 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
4219 return _riscv_relax_delete_bytes (abfd, sec, addr, count,
4220 link_info, p, 0, sec->size);
4221}
4222
4223/* Delete the bytes for R_RISCV_DELETE relocs. */
4224
4225static bool
4226riscv_relax_resolve_delete_relocs (bfd *abfd,
4227 asection *sec,
4228 struct bfd_link_info *link_info,
4229 Elf_Internal_Rela *relocs)
4230{
4231 bfd_vma delete_total = 0;
4232 unsigned int i;
4233
4234 for (i = 0; i < sec->reloc_count; i++)
4235 {
4236 Elf_Internal_Rela *rel = relocs + i;
4237 if (ELFNN_R_TYPE (rel->r_info) != R_RISCV_DELETE)
4238 continue;
4239
4240 /* Find the next R_RISCV_DELETE reloc if possible. */
4241 Elf_Internal_Rela *rel_next = NULL;
4242 unsigned int start = rel - relocs;
4243 for (i = start; i < sec->reloc_count; i++)
4244 {
4245 /* Since we only replace existing relocs and don't add new relocs, the
4246 relocs are in sequential order. We can skip the relocs prior to this
4247 one, making this search linear time. */
4248 rel_next = relocs + i;
4249 if (ELFNN_R_TYPE ((rel_next)->r_info) == R_RISCV_DELETE
4250 && (rel_next)->r_offset > rel->r_offset)
f52fb009
NC
4251 {
4252 BFD_ASSERT (rel_next - rel > 0);
4253 break;
4254 }
43025f01
PN
4255 else
4256 rel_next = NULL;
4257 }
4258
4259 bfd_vma toaddr = rel_next == NULL ? sec->size : rel_next->r_offset;
4260 if (!_riscv_relax_delete_bytes (abfd, sec, rel->r_offset, rel->r_addend,
4261 link_info, NULL, delete_total, toaddr))
4262 return false;
4263
4264 delete_total += rel->r_addend;
4265 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
4266
4267 /* Skip ahead to the next delete reloc. */
f52fb009
NC
4268 i = rel_next != NULL ? (unsigned int) (rel_next - relocs - 1)
4269 : sec->reloc_count;
43025f01
PN
4270 }
4271
4272 return true;
4273}
4274
0a1b45a2
AM
4275typedef bool (*relax_func_t) (bfd *, asection *, asection *,
4276 struct bfd_link_info *,
4277 Elf_Internal_Rela *,
4278 bfd_vma, bfd_vma, bfd_vma, bool *,
4279 riscv_pcgp_relocs *,
4280 bool undefined_weak);
45f76423 4281
e23eba97
NC
4282/* Relax AUIPC + JALR into JAL. */
4283
0a1b45a2 4284static bool
e23eba97
NC
4285_bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
4286 struct bfd_link_info *link_info,
4287 Elf_Internal_Rela *rel,
4288 bfd_vma symval,
45f76423
AW
4289 bfd_vma max_alignment,
4290 bfd_vma reserve_size ATTRIBUTE_UNUSED,
0a1b45a2 4291 bool *again,
9abcdc10 4292 riscv_pcgp_relocs *pcgp_relocs,
0a1b45a2 4293 bool undefined_weak ATTRIBUTE_UNUSED)
e23eba97
NC
4294{
4295 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1174d920 4296 bfd_vma foff = symval - (sec_addr (sec) + rel->r_offset);
0a1b45a2 4297 bool near_zero = (symval + RISCV_IMM_REACH / 2) < RISCV_IMM_REACH;
e23eba97
NC
4298 bfd_vma auipc, jalr;
4299 int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4300
4301 /* If the call crosses section boundaries, an alignment directive could
c6261a00
JW
4302 cause the PC-relative offset to later increase, so we need to add in the
4303 max alignment of any section inclusive from the call to the target.
4304 Otherwise, we only need to use the alignment of the current section. */
5a9f5403 4305 if (VALID_JTYPE_IMM (foff))
c6261a00
JW
4306 {
4307 if (sym_sec->output_section == sec->output_section
4308 && sym_sec->output_section != bfd_abs_section_ptr)
4309 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
1174d920 4310 foff += ((bfd_signed_vma) foff < 0 ? -max_alignment : max_alignment);
c6261a00 4311 }
e23eba97
NC
4312
4313 /* See if this function call can be shortened. */
5a9f5403 4314 if (!VALID_JTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
0a1b45a2 4315 return true;
e23eba97
NC
4316
4317 /* Shorten the function call. */
4318 BFD_ASSERT (rel->r_offset + 8 <= sec->size);
4319
fbc09e7a
MC
4320 auipc = bfd_getl32 (contents + rel->r_offset);
4321 jalr = bfd_getl32 (contents + rel->r_offset + 4);
e23eba97 4322 rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
5a9f5403 4323 rvc = rvc && VALID_CJTYPE_IMM (foff);
e23eba97 4324
ae2b14c7
JW
4325 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4326 rvc = rvc && (rd == 0 || (rd == X_RA && ARCH_SIZE == 32));
4327
4328 if (rvc)
e23eba97
NC
4329 {
4330 /* Relax to C.J[AL] rd, addr. */
4331 r_type = R_RISCV_RVC_JUMP;
4332 auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
4333 len = 2;
4334 }
5a9f5403 4335 else if (VALID_JTYPE_IMM (foff))
e23eba97
NC
4336 {
4337 /* Relax to JAL rd, addr. */
4338 r_type = R_RISCV_JAL;
4339 auipc = MATCH_JAL | (rd << OP_SH_RD);
4340 }
dcd709e0 4341 else
e23eba97 4342 {
dcd709e0 4343 /* Near zero, relax to JALR rd, x0, addr. */
e23eba97
NC
4344 r_type = R_RISCV_LO12_I;
4345 auipc = MATCH_JALR | (rd << OP_SH_RD);
4346 }
4347
4348 /* Replace the R_RISCV_CALL reloc. */
4349 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
4350 /* Replace the AUIPC. */
fbc09e7a 4351 riscv_put_insn (8 * len, auipc, contents + rel->r_offset);
e23eba97 4352
43025f01 4353 /* Delete unnecessary JALR and reuse the R_RISCV_RELAX reloc. */
0a1b45a2 4354 *again = true;
7f02625e 4355 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len,
43025f01 4356 link_info, pcgp_relocs, rel + 1);
e23eba97
NC
4357}
4358
4359/* Traverse all output sections and return the max alignment. */
4360
1d61f794 4361static bfd_vma
e23eba97
NC
4362_bfd_riscv_get_max_alignment (asection *sec)
4363{
4364 unsigned int max_alignment_power = 0;
4365 asection *o;
4366
4367 for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
4368 {
4369 if (o->alignment_power > max_alignment_power)
4370 max_alignment_power = o->alignment_power;
4371 }
4372
1d61f794 4373 return (bfd_vma) 1 << max_alignment_power;
e23eba97
NC
4374}
4375
dcd709e0 4376/* Relax non-PIC global variable references to GP-relative references. */
e23eba97 4377
0a1b45a2 4378static bool
e23eba97
NC
4379_bfd_riscv_relax_lui (bfd *abfd,
4380 asection *sec,
4381 asection *sym_sec,
4382 struct bfd_link_info *link_info,
4383 Elf_Internal_Rela *rel,
4384 bfd_vma symval,
45f76423
AW
4385 bfd_vma max_alignment,
4386 bfd_vma reserve_size,
0a1b45a2 4387 bool *again,
9abcdc10 4388 riscv_pcgp_relocs *pcgp_relocs,
0a1b45a2 4389 bool undefined_weak)
e23eba97
NC
4390{
4391 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4392 bfd_vma gp = riscv_global_pointer_value (link_info);
4393 int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4394
e23eba97
NC
4395 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
4396
d0f744f9
AW
4397 if (gp)
4398 {
507685a3
JW
4399 /* If gp and the symbol are in the same output section, which is not the
4400 abs section, then consider only that output section's alignment. */
d0f744f9 4401 struct bfd_link_hash_entry *h =
0a1b45a2
AM
4402 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, false, false,
4403 true);
507685a3
JW
4404 if (h->u.def.section->output_section == sym_sec->output_section
4405 && sym_sec->output_section != bfd_abs_section_ptr)
d0f744f9
AW
4406 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
4407 }
4408
e23eba97
NC
4409 /* Is the reference in range of x0 or gp?
4410 Valid gp range conservatively because of alignment issue. */
9d1da81b
JW
4411 if (undefined_weak
4412 || (VALID_ITYPE_IMM (symval)
4413 || (symval >= gp
4414 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
4415 || (symval < gp
4416 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
e23eba97
NC
4417 {
4418 unsigned sym = ELFNN_R_SYM (rel->r_info);
4419 switch (ELFNN_R_TYPE (rel->r_info))
4420 {
4421 case R_RISCV_LO12_I:
9d1da81b
JW
4422 if (undefined_weak)
4423 {
4424 /* Change the RS1 to zero. */
fbc09e7a 4425 bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
9d1da81b 4426 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
fbc09e7a 4427 bfd_putl32 (insn, contents + rel->r_offset);
9d1da81b
JW
4428 }
4429 else
4430 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
0a1b45a2 4431 return true;
e23eba97
NC
4432
4433 case R_RISCV_LO12_S:
9d1da81b
JW
4434 if (undefined_weak)
4435 {
4436 /* Change the RS1 to zero. */
fbc09e7a 4437 bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
9d1da81b 4438 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
fbc09e7a 4439 bfd_putl32 (insn, contents + rel->r_offset);
9d1da81b
JW
4440 }
4441 else
4442 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
0a1b45a2 4443 return true;
e23eba97
NC
4444
4445 case R_RISCV_HI20:
43025f01 4446 /* Delete unnecessary LUI and reuse the reloc. */
0a1b45a2 4447 *again = true;
7f02625e 4448 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4,
43025f01 4449 link_info, pcgp_relocs, rel);
e23eba97
NC
4450
4451 default:
4452 abort ();
4453 }
4454 }
4455
4456 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
0f52d45a
JW
4457 account for this assuming page alignment at worst. In the presence of
4458 RELRO segment the linker aligns it by one page size, therefore sections
4459 after the segment can be moved more than one page. */
4460
e23eba97
NC
4461 if (use_rvc
4462 && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
5a9f5403
NC
4463 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
4464 && VALID_CITYPE_LUI_IMM (RISCV_CONST_HIGH_PART (symval)
0f52d45a
JW
4465 + (link_info->relro ? 2 * ELF_MAXPAGESIZE
4466 : ELF_MAXPAGESIZE)))
e23eba97 4467 {
3342be5d 4468 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
fbc09e7a 4469 bfd_vma lui = bfd_getl32 (contents + rel->r_offset);
3342be5d
AW
4470 unsigned rd = ((unsigned)lui >> OP_SH_RD) & OP_MASK_RD;
4471 if (rd == 0 || rd == X_SP)
0a1b45a2 4472 return true;
e23eba97
NC
4473
4474 lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
fbc09e7a 4475 bfd_putl32 (lui, contents + rel->r_offset);
e23eba97
NC
4476
4477 /* Replace the R_RISCV_HI20 reloc. */
4478 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
4479
43025f01 4480 /* Delete extra bytes and reuse the R_RISCV_RELAX reloc. */
0a1b45a2 4481 *again = true;
7f02625e 4482 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2,
43025f01 4483 link_info, pcgp_relocs, rel + 1);
e23eba97
NC
4484 }
4485
0a1b45a2 4486 return true;
e23eba97
NC
4487}
4488
dcd709e0 4489/* Relax non-PIC TLS references to TP-relative references. */
e23eba97 4490
0a1b45a2 4491static bool
e23eba97
NC
4492_bfd_riscv_relax_tls_le (bfd *abfd,
4493 asection *sec,
4494 asection *sym_sec ATTRIBUTE_UNUSED,
4495 struct bfd_link_info *link_info,
4496 Elf_Internal_Rela *rel,
4497 bfd_vma symval,
45f76423
AW
4498 bfd_vma max_alignment ATTRIBUTE_UNUSED,
4499 bfd_vma reserve_size ATTRIBUTE_UNUSED,
0a1b45a2 4500 bool *again,
9abcdc10 4501 riscv_pcgp_relocs *pcgp_relocs,
0a1b45a2 4502 bool undefined_weak ATTRIBUTE_UNUSED)
e23eba97
NC
4503{
4504 /* See if this symbol is in range of tp. */
4505 if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
0a1b45a2 4506 return true;
e23eba97 4507
e23eba97 4508 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
45f76423
AW
4509 switch (ELFNN_R_TYPE (rel->r_info))
4510 {
4511 case R_RISCV_TPREL_LO12_I:
4512 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_I);
0a1b45a2 4513 return true;
e23eba97 4514
45f76423
AW
4515 case R_RISCV_TPREL_LO12_S:
4516 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_S);
0a1b45a2 4517 return true;
45f76423
AW
4518
4519 case R_RISCV_TPREL_HI20:
4520 case R_RISCV_TPREL_ADD:
43025f01 4521 /* Delete unnecessary instruction and reuse the reloc. */
0a1b45a2 4522 *again = true;
9abcdc10 4523 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info,
43025f01 4524 pcgp_relocs, rel);
45f76423
AW
4525
4526 default:
4527 abort ();
4528 }
e23eba97
NC
4529}
4530
ebdcad3f
NC
4531/* Implement R_RISCV_ALIGN by deleting excess alignment NOPs.
4532 Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
e23eba97 4533
0a1b45a2 4534static bool
e23eba97 4535_bfd_riscv_relax_align (bfd *abfd, asection *sec,
9eb7b0ac 4536 asection *sym_sec,
7f02625e 4537 struct bfd_link_info *link_info,
e23eba97
NC
4538 Elf_Internal_Rela *rel,
4539 bfd_vma symval,
45f76423
AW
4540 bfd_vma max_alignment ATTRIBUTE_UNUSED,
4541 bfd_vma reserve_size ATTRIBUTE_UNUSED,
0a1b45a2 4542 bool *again ATTRIBUTE_UNUSED,
9abcdc10 4543 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
0a1b45a2 4544 bool undefined_weak ATTRIBUTE_UNUSED)
e23eba97
NC
4545{
4546 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4547 bfd_vma alignment = 1, pos;
4548 while (alignment <= rel->r_addend)
4549 alignment *= 2;
4550
4551 symval -= rel->r_addend;
4552 bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
4553 bfd_vma nop_bytes = aligned_addr - symval;
4554
9abcdc10
LR
4555 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4556 sec->sec_flg0 = true;
4557
e23eba97
NC
4558 /* Make sure there are enough NOPs to actually achieve the alignment. */
4559 if (rel->r_addend < nop_bytes)
9eb7b0ac 4560 {
f2b740ac
AM
4561 _bfd_error_handler
4562 (_("%pB(%pA+%#" PRIx64 "): %" PRId64 " bytes required for alignment "
4563 "to %" PRId64 "-byte boundary, but only %" PRId64 " present"),
4564 abfd, sym_sec, (uint64_t) rel->r_offset,
4565 (int64_t) nop_bytes, (int64_t) alignment, (int64_t) rel->r_addend);
9eb7b0ac 4566 bfd_set_error (bfd_error_bad_value);
0a1b45a2 4567 return false;
9eb7b0ac 4568 }
e23eba97
NC
4569
4570 /* Delete the reloc. */
4571 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
4572
4573 /* If the number of NOPs is already correct, there's nothing to do. */
4574 if (nop_bytes == rel->r_addend)
0a1b45a2 4575 return true;
e23eba97
NC
4576
4577 /* Write as many RISC-V NOPs as we need. */
4578 for (pos = 0; pos < (nop_bytes & -4); pos += 4)
fbc09e7a 4579 bfd_putl32 (RISCV_NOP, contents + rel->r_offset + pos);
e23eba97
NC
4580
4581 /* Write a final RVC NOP if need be. */
4582 if (nop_bytes % 4 != 0)
fbc09e7a 4583 bfd_putl16 (RVC_NOP, contents + rel->r_offset + pos);
e23eba97 4584
43025f01 4585 /* Delete excess bytes. */
e23eba97 4586 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
9abcdc10 4587 rel->r_addend - nop_bytes, link_info,
43025f01 4588 NULL, NULL);
e23eba97
NC
4589}
4590
ff6f4d9b
PD
4591/* Relax PC-relative references to GP-relative references. */
4592
0a1b45a2 4593static bool
1942a048
NC
4594_bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
4595 asection *sec,
4596 asection *sym_sec,
4597 struct bfd_link_info *link_info,
4598 Elf_Internal_Rela *rel,
4599 bfd_vma symval,
4600 bfd_vma max_alignment,
4601 bfd_vma reserve_size,
845652b7 4602 bool *again,
1942a048 4603 riscv_pcgp_relocs *pcgp_relocs,
0a1b45a2 4604 bool undefined_weak)
9d06997a 4605{
9d1da81b 4606 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
9d06997a
PD
4607 bfd_vma gp = riscv_global_pointer_value (link_info);
4608
4609 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
4610
4611 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
dcd709e0 4612 actual target address. */
e65b1a78
MR
4613 riscv_pcgp_hi_reloc hi_reloc;
4614 memset (&hi_reloc, 0, sizeof (hi_reloc));
9d06997a
PD
4615 switch (ELFNN_R_TYPE (rel->r_info))
4616 {
4617 case R_RISCV_PCREL_LO12_I:
4618 case R_RISCV_PCREL_LO12_S:
4619 {
a05f27b6
JW
4620 /* If the %lo has an addend, it isn't for the label pointing at the
4621 hi part instruction, but rather for the symbol pointed at by the
4622 hi part instruction. So we must subtract it here for the lookup.
4623 It is still used below in the final symbol address. */
4624 bfd_vma hi_sec_off = symval - sec_addr (sym_sec) - rel->r_addend;
9d06997a 4625 riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
a05f27b6 4626 hi_sec_off);
9d06997a
PD
4627 if (hi == NULL)
4628 {
a05f27b6 4629 riscv_record_pcgp_lo_reloc (pcgp_relocs, hi_sec_off);
0a1b45a2 4630 return true;
9d06997a
PD
4631 }
4632
4633 hi_reloc = *hi;
4634 symval = hi_reloc.hi_addr;
4635 sym_sec = hi_reloc.sym_sec;
9d1da81b
JW
4636
4637 /* We can not know whether the undefined weak symbol is referenced
4638 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4639 we have to record the 'undefined_weak' flag when handling the
4640 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4641 undefined_weak = hi_reloc.undefined_weak;
9d06997a
PD
4642 }
4643 break;
4644
4645 case R_RISCV_PCREL_HI20:
4646 /* Mergeable symbols and code might later move out of range. */
9d1da81b
JW
4647 if (! undefined_weak
4648 && sym_sec->flags & (SEC_MERGE | SEC_CODE))
0a1b45a2 4649 return true;
9d06997a
PD
4650
4651 /* If the cooresponding lo relocation has already been seen then it's not
dcd709e0 4652 safe to relax this relocation. */
9d06997a 4653 if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
0a1b45a2 4654 return true;
9d06997a
PD
4655
4656 break;
4657
4658 default:
4659 abort ();
4660 }
4661
4662 if (gp)
4663 {
507685a3
JW
4664 /* If gp and the symbol are in the same output section, which is not the
4665 abs section, then consider only that output section's alignment. */
9d06997a 4666 struct bfd_link_hash_entry *h =
0a1b45a2
AM
4667 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, false, false,
4668 true);
507685a3
JW
4669 if (h->u.def.section->output_section == sym_sec->output_section
4670 && sym_sec->output_section != bfd_abs_section_ptr)
9d06997a
PD
4671 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
4672 }
4673
4674 /* Is the reference in range of x0 or gp?
4675 Valid gp range conservatively because of alignment issue. */
9d1da81b
JW
4676 if (undefined_weak
4677 || (VALID_ITYPE_IMM (symval)
4678 || (symval >= gp
4679 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
4680 || (symval < gp
4681 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
9d06997a
PD
4682 {
4683 unsigned sym = hi_reloc.hi_sym;
4684 switch (ELFNN_R_TYPE (rel->r_info))
4685 {
4686 case R_RISCV_PCREL_LO12_I:
9d1da81b
JW
4687 if (undefined_weak)
4688 {
4689 /* Change the RS1 to zero, and then modify the relocation
4690 type to R_RISCV_LO12_I. */
fbc09e7a 4691 bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
9d1da81b 4692 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
fbc09e7a 4693 bfd_putl32 (insn, contents + rel->r_offset);
9d1da81b
JW
4694 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_I);
4695 rel->r_addend = hi_reloc.hi_addend;
4696 }
4697 else
4698 {
4699 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
4700 rel->r_addend += hi_reloc.hi_addend;
4701 }
0a1b45a2 4702 return true;
9d06997a
PD
4703
4704 case R_RISCV_PCREL_LO12_S:
9d1da81b
JW
4705 if (undefined_weak)
4706 {
4707 /* Change the RS1 to zero, and then modify the relocation
4708 type to R_RISCV_LO12_S. */
fbc09e7a 4709 bfd_vma insn = bfd_getl32 (contents + rel->r_offset);
9d1da81b 4710 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
fbc09e7a 4711 bfd_putl32 (insn, contents + rel->r_offset);
9d1da81b
JW
4712 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_S);
4713 rel->r_addend = hi_reloc.hi_addend;
4714 }
4715 else
4716 {
4717 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
4718 rel->r_addend += hi_reloc.hi_addend;
4719 }
0a1b45a2 4720 return true;
9d06997a
PD
4721
4722 case R_RISCV_PCREL_HI20:
07d6d2b8 4723 riscv_record_pcgp_hi_reloc (pcgp_relocs,
9d06997a
PD
4724 rel->r_offset,
4725 rel->r_addend,
4726 symval,
4727 ELFNN_R_SYM(rel->r_info),
9d1da81b
JW
4728 sym_sec,
4729 undefined_weak);
43025f01 4730 /* Delete unnecessary AUIPC and reuse the reloc. */
845652b7 4731 *again = true;
43025f01
PN
4732 riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info,
4733 pcgp_relocs, rel);
0a1b45a2 4734 return true;
9d06997a
PD
4735
4736 default:
4737 abort ();
4738 }
4739 }
4740
0a1b45a2 4741 return true;
9d06997a
PD
4742}
4743
ef9d2565
NC
4744/* Called by after_allocation to set the information of data segment
4745 before relaxing. */
4746
4747void
4748bfd_elfNN_riscv_set_data_segment_info (struct bfd_link_info *info,
4749 int *data_segment_phase)
4750{
4751 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
4752 htab->data_segment_phase = data_segment_phase;
4753}
4754
dcd709e0
NC
4755/* Relax a section.
4756
779b2502
TO
4757 Pass 0: Shortens code sequences for LUI/CALL/TPREL/PCREL relocs and
4758 deletes the obsolete bytes.
4759 Pass 1: Which cannot be disabled, handles code alignment directives. */
e23eba97 4760
0a1b45a2 4761static bool
e23eba97
NC
4762_bfd_riscv_relax_section (bfd *abfd, asection *sec,
4763 struct bfd_link_info *info,
0a1b45a2 4764 bool *again)
e23eba97
NC
4765{
4766 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
4767 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
4768 struct bfd_elf_section_data *data = elf_section_data (sec);
4769 Elf_Internal_Rela *relocs;
0a1b45a2 4770 bool ret = false;
e23eba97 4771 unsigned int i;
45f76423 4772 bfd_vma max_alignment, reserve_size = 0;
9d06997a 4773 riscv_pcgp_relocs pcgp_relocs;
e23eba97 4774
0a1b45a2 4775 *again = false;
e23eba97
NC
4776
4777 if (bfd_link_relocatable (info)
9abcdc10 4778 || sec->sec_flg0
e23eba97 4779 || sec->reloc_count == 0
3a574cce
AM
4780 || (sec->flags & SEC_RELOC) == 0
4781 || (sec->flags & SEC_HAS_CONTENTS) == 0
e23eba97 4782 || (info->disable_target_specific_optimizations
9abcdc10 4783 && info->relax_pass == 0)
ef9d2565
NC
4784 /* The exp_seg_relro_adjust is enum phase_enum (0x4),
4785 and defined in ld/ldexp.h. */
4786 || *(htab->data_segment_phase) == 4)
0a1b45a2 4787 return true;
e23eba97 4788
9d06997a
PD
4789 riscv_init_pcgp_relocs (&pcgp_relocs);
4790
e23eba97
NC
4791 /* Read this BFD's relocs if we haven't done so already. */
4792 if (data->relocs)
4793 relocs = data->relocs;
4794 else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
4795 info->keep_memory)))
4796 goto fail;
4797
fc3c5343
L
4798 if (htab)
4799 {
4800 max_alignment = htab->max_alignment;
4801 if (max_alignment == (bfd_vma) -1)
4802 {
4803 max_alignment = _bfd_riscv_get_max_alignment (sec);
4804 htab->max_alignment = max_alignment;
4805 }
4806 }
4807 else
4808 max_alignment = _bfd_riscv_get_max_alignment (sec);
e23eba97
NC
4809
4810 /* Examine and consider relaxing each reloc. */
4811 for (i = 0; i < sec->reloc_count; i++)
4812 {
4813 asection *sym_sec;
4814 Elf_Internal_Rela *rel = relocs + i;
45f76423 4815 relax_func_t relax_func;
e23eba97
NC
4816 int type = ELFNN_R_TYPE (rel->r_info);
4817 bfd_vma symval;
04b865dc 4818 char symtype;
0a1b45a2 4819 bool undefined_weak = false;
e23eba97 4820
ff6f4d9b 4821 relax_func = NULL;
43025f01 4822 riscv_relax_delete_bytes = NULL;
e23eba97
NC
4823 if (info->relax_pass == 0)
4824 {
abd20cb6
NC
4825 if (type == R_RISCV_CALL
4826 || type == R_RISCV_CALL_PLT)
e23eba97
NC
4827 relax_func = _bfd_riscv_relax_call;
4828 else if (type == R_RISCV_HI20
4829 || type == R_RISCV_LO12_I
4830 || type == R_RISCV_LO12_S)
4831 relax_func = _bfd_riscv_relax_lui;
45f76423
AW
4832 else if (type == R_RISCV_TPREL_HI20
4833 || type == R_RISCV_TPREL_ADD
4834 || type == R_RISCV_TPREL_LO12_I
4835 || type == R_RISCV_TPREL_LO12_S)
e23eba97 4836 relax_func = _bfd_riscv_relax_tls_le;
9abcdc10
LR
4837 else if (!bfd_link_pic (info)
4838 && (type == R_RISCV_PCREL_HI20
4839 || type == R_RISCV_PCREL_LO12_I
4840 || type == R_RISCV_PCREL_LO12_S))
4841 relax_func = _bfd_riscv_relax_pc;
45f76423
AW
4842 else
4843 continue;
43025f01 4844 riscv_relax_delete_bytes = _riscv_relax_delete_piecewise;
45f76423
AW
4845
4846 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4847 if (i == sec->reloc_count - 1
4848 || ELFNN_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
4849 || rel->r_offset != (rel + 1)->r_offset)
4850 continue;
4851
4852 /* Skip over the R_RISCV_RELAX. */
4853 i++;
e23eba97 4854 }
43025f01
PN
4855 else if (info->relax_pass == 1 && type == R_RISCV_ALIGN)
4856 {
4857 relax_func = _bfd_riscv_relax_align;
4858 riscv_relax_delete_bytes = _riscv_relax_delete_immediate;
4859 }
9abcdc10
LR
4860 else
4861 continue;
e23eba97
NC
4862
4863 data->relocs = relocs;
4864
4865 /* Read this BFD's contents if we haven't done so already. */
4866 if (!data->this_hdr.contents
4867 && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
4868 goto fail;
4869
4870 /* Read this BFD's symbols if we haven't done so already. */
4871 if (symtab_hdr->sh_info != 0
4872 && !symtab_hdr->contents
4873 && !(symtab_hdr->contents =
4874 (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
4875 symtab_hdr->sh_info,
4876 0, NULL, NULL, NULL)))
4877 goto fail;
4878
4879 /* Get the value of the symbol referred to by the reloc. */
4880 if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
4881 {
4882 /* A local symbol. */
4883 Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
4884 + ELFNN_R_SYM (rel->r_info));
45f76423
AW
4885 reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
4886 ? 0 : isym->st_size - rel->r_addend;
e23eba97 4887
02dd9d25
NC
4888 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4889 a fake global symbol entry for this, so deal with the local ifunc
4890 as a global. */
4891 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4892 continue;
4893
e23eba97 4894 if (isym->st_shndx == SHN_UNDEF)
04b865dc 4895 sym_sec = sec, symval = rel->r_offset;
e23eba97
NC
4896 else
4897 {
4898 BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
4899 sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
09ca4b9d
JW
4900#if 0
4901 /* The purpose of this code is unknown. It breaks linker scripts
4902 for embedded development that place sections at address zero.
4903 This code is believed to be unnecessary. Disabling it but not
4904 yet removing it, in case something breaks. */
e23eba97
NC
4905 if (sec_addr (sym_sec) == 0)
4906 continue;
09ca4b9d 4907#endif
04b865dc 4908 symval = isym->st_value;
e23eba97 4909 }
04b865dc 4910 symtype = ELF_ST_TYPE (isym->st_info);
e23eba97
NC
4911 }
4912 else
4913 {
4914 unsigned long indx;
4915 struct elf_link_hash_entry *h;
4916
4917 indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
4918 h = elf_sym_hashes (abfd)[indx];
4919
4920 while (h->root.type == bfd_link_hash_indirect
4921 || h->root.type == bfd_link_hash_warning)
4922 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4923
02dd9d25
NC
4924 /* Disable the relaxation for ifunc. */
4925 if (h != NULL && h->type == STT_GNU_IFUNC)
4926 continue;
4927
9d1da81b
JW
4928 if (h->root.type == bfd_link_hash_undefweak
4929 && (relax_func == _bfd_riscv_relax_lui
4930 || relax_func == _bfd_riscv_relax_pc))
4931 {
4932 /* For the lui and auipc relaxations, since the symbol
4933 value of an undefined weak symbol is always be zero,
4934 we can optimize the patterns into a single LI/MV/ADDI
4935 instruction.
4936
4937 Note that, creating shared libraries and pie output may
4938 break the rule above. Fortunately, since we do not relax
4939 pc relocs when creating shared libraries and pie output,
4940 and the absolute address access for R_RISCV_HI20 isn't
4941 allowed when "-fPIC" is set, the problem of creating shared
4942 libraries can not happen currently. Once we support the
4943 auipc relaxations when creating shared libraries, then we will
4944 need the more rigorous checking for this optimization. */
0a1b45a2 4945 undefined_weak = true;
9d1da81b
JW
4946 }
4947
85f78364
JW
4948 /* This line has to match the check in riscv_elf_relocate_section
4949 in the R_RISCV_CALL[_PLT] case. */
4950 if (bfd_link_pic (info) && h->plt.offset != MINUS_ONE)
04b865dc
JW
4951 {
4952 sym_sec = htab->elf.splt;
4953 symval = h->plt.offset;
4954 }
9d1da81b
JW
4955 else if (undefined_weak)
4956 {
4957 symval = 0;
4958 sym_sec = bfd_und_section_ptr;
4959 }
a2714d6c
AM
4960 else if ((h->root.type == bfd_link_hash_defined
4961 || h->root.type == bfd_link_hash_defweak)
4962 && h->root.u.def.section != NULL
4963 && h->root.u.def.section->output_section != NULL)
04b865dc
JW
4964 {
4965 symval = h->root.u.def.value;
4966 sym_sec = h->root.u.def.section;
4967 }
a2714d6c
AM
4968 else
4969 continue;
e23eba97 4970
45f76423
AW
4971 if (h->type != STT_FUNC)
4972 reserve_size =
4973 (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
04b865dc 4974 symtype = h->type;
e23eba97
NC
4975 }
4976
04b865dc
JW
4977 if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE
4978 && (sym_sec->flags & SEC_MERGE))
4979 {
4980 /* At this stage in linking, no SEC_MERGE symbol has been
4981 adjusted, so all references to such symbols need to be
4982 passed through _bfd_merged_section_offset. (Later, in
4983 relocate_section, all SEC_MERGE symbols *except* for
4984 section symbols have been adjusted.)
4985
4986 gas may reduce relocations against symbols in SEC_MERGE
4987 sections to a relocation against the section symbol when
4988 the original addend was zero. When the reloc is against
4989 a section symbol we should include the addend in the
4990 offset passed to _bfd_merged_section_offset, since the
4991 location of interest is the original symbol. On the
4992 other hand, an access to "sym+addend" where "sym" is not
4993 a section symbol should not include the addend; Such an
4994 access is presumed to be an offset from "sym"; The
4995 location of interest is just "sym". */
4996 if (symtype == STT_SECTION)
4997 symval += rel->r_addend;
4998
4999 symval = _bfd_merged_section_offset (abfd, &sym_sec,
5000 elf_section_data (sym_sec)->sec_info,
5001 symval);
5002
5003 if (symtype != STT_SECTION)
5004 symval += rel->r_addend;
5005 }
5006 else
5007 symval += rel->r_addend;
5008
5009 symval += sec_addr (sym_sec);
e23eba97
NC
5010
5011 if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
9d06997a 5012 max_alignment, reserve_size, again,
9d1da81b 5013 &pcgp_relocs, undefined_weak))
e23eba97
NC
5014 goto fail;
5015 }
5016
43025f01
PN
5017 /* Resolve R_RISCV_DELETE relocations. */
5018 if (!riscv_relax_resolve_delete_relocs (abfd, sec, info, relocs))
5019 goto fail;
5020
0a1b45a2 5021 ret = true;
e23eba97 5022
dc1e8a47 5023 fail:
e23eba97
NC
5024 if (relocs != data->relocs)
5025 free (relocs);
1942a048 5026 riscv_free_pcgp_relocs (&pcgp_relocs, abfd, sec);
e23eba97
NC
5027
5028 return ret;
5029}
5030
5031#if ARCH_SIZE == 32
79b8e8ab 5032# define PRSTATUS_SIZE 204
e23eba97
NC
5033# define PRSTATUS_OFFSET_PR_CURSIG 12
5034# define PRSTATUS_OFFSET_PR_PID 24
5035# define PRSTATUS_OFFSET_PR_REG 72
5036# define ELF_GREGSET_T_SIZE 128
5037# define PRPSINFO_SIZE 128
5038# define PRPSINFO_OFFSET_PR_PID 16
5039# define PRPSINFO_OFFSET_PR_FNAME 32
5040# define PRPSINFO_OFFSET_PR_PSARGS 48
0897bb7d
AB
5041# define PRPSINFO_PR_FNAME_LENGTH 16
5042# define PRPSINFO_PR_PSARGS_LENGTH 80
e23eba97
NC
5043#else
5044# define PRSTATUS_SIZE 376
5045# define PRSTATUS_OFFSET_PR_CURSIG 12
5046# define PRSTATUS_OFFSET_PR_PID 32
5047# define PRSTATUS_OFFSET_PR_REG 112
5048# define ELF_GREGSET_T_SIZE 256
5049# define PRPSINFO_SIZE 136
5050# define PRPSINFO_OFFSET_PR_PID 24
5051# define PRPSINFO_OFFSET_PR_FNAME 40
5052# define PRPSINFO_OFFSET_PR_PSARGS 56
0897bb7d
AB
5053# define PRPSINFO_PR_FNAME_LENGTH 16
5054# define PRPSINFO_PR_PSARGS_LENGTH 80
e23eba97
NC
5055#endif
5056
0897bb7d
AB
5057/* Write PRSTATUS and PRPSINFO note into core file. This will be called
5058 before the generic code in elf.c. By checking the compiler defines we
5059 only perform any action here if the generic code would otherwise not be
5060 able to help us. The intention is that bare metal core dumps (where the
5061 prstatus_t and/or prpsinfo_t might not be available) will use this code,
5062 while non bare metal tools will use the generic elf code. */
5063
5064static char *
5065riscv_write_core_note (bfd *abfd ATTRIBUTE_UNUSED,
5066 char *buf ATTRIBUTE_UNUSED,
5067 int *bufsiz ATTRIBUTE_UNUSED,
5068 int note_type ATTRIBUTE_UNUSED, ...)
5069{
5070 switch (note_type)
5071 {
5072 default:
5073 return NULL;
5074
5075#if !defined (HAVE_PRPSINFO_T)
5076 case NT_PRPSINFO:
5077 {
5078 char data[PRPSINFO_SIZE] ATTRIBUTE_NONSTRING;
5079 va_list ap;
5080
5081 va_start (ap, note_type);
5082 memset (data, 0, sizeof (data));
5083 strncpy (data + PRPSINFO_OFFSET_PR_FNAME, va_arg (ap, const char *),
5084 PRPSINFO_PR_FNAME_LENGTH);
5085#if GCC_VERSION == 8000 || GCC_VERSION == 8001
5086 DIAGNOSTIC_PUSH;
5087 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
5088 -Wstringop-truncation:
5089 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
5090 */
5091 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
5092#endif
5093 strncpy (data + PRPSINFO_OFFSET_PR_PSARGS, va_arg (ap, const char *),
5094 PRPSINFO_PR_PSARGS_LENGTH);
5095#if GCC_VERSION == 8000 || GCC_VERSION == 8001
5096 DIAGNOSTIC_POP;
5097#endif
5098 va_end (ap);
5099 return elfcore_write_note (abfd, buf, bufsiz,
5100 "CORE", note_type, data, sizeof (data));
5101 }
5102#endif /* !HAVE_PRPSINFO_T */
5103
5104#if !defined (HAVE_PRSTATUS_T)
5105 case NT_PRSTATUS:
5106 {
5107 char data[PRSTATUS_SIZE];
5108 va_list ap;
5109 long pid;
5110 int cursig;
5111 const void *greg;
5112
5113 va_start (ap, note_type);
5114 memset (data, 0, sizeof(data));
5115 pid = va_arg (ap, long);
5116 bfd_put_32 (abfd, pid, data + PRSTATUS_OFFSET_PR_PID);
5117 cursig = va_arg (ap, int);
5118 bfd_put_16 (abfd, cursig, data + PRSTATUS_OFFSET_PR_CURSIG);
5119 greg = va_arg (ap, const void *);
5120 memcpy (data + PRSTATUS_OFFSET_PR_REG, greg,
5121 PRSTATUS_SIZE - PRSTATUS_OFFSET_PR_REG - ARCH_SIZE / 8);
5122 va_end (ap);
5123 return elfcore_write_note (abfd, buf, bufsiz,
5124 "CORE", note_type, data, sizeof (data));
5125 }
5126#endif /* !HAVE_PRSTATUS_T */
5127 }
5128}
5129
e23eba97
NC
5130/* Support for core dump NOTE sections. */
5131
0a1b45a2 5132static bool
e23eba97
NC
5133riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5134{
5135 switch (note->descsz)
5136 {
5137 default:
0a1b45a2 5138 return false;
e23eba97 5139
dcd709e0 5140 case PRSTATUS_SIZE: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
e23eba97
NC
5141 /* pr_cursig */
5142 elf_tdata (abfd)->core->signal
5143 = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
5144
5145 /* pr_pid */
5146 elf_tdata (abfd)->core->lwpid
5147 = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
5148 break;
5149 }
5150
5151 /* Make a ".reg/999" section. */
5152 return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
5153 note->descpos + PRSTATUS_OFFSET_PR_REG);
5154}
5155
0a1b45a2 5156static bool
e23eba97
NC
5157riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5158{
5159 switch (note->descsz)
5160 {
5161 default:
0a1b45a2 5162 return false;
e23eba97
NC
5163
5164 case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
5165 /* pr_pid */
5166 elf_tdata (abfd)->core->pid
5167 = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
5168
5169 /* pr_fname */
5170 elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
0897bb7d
AB
5171 (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME,
5172 PRPSINFO_PR_FNAME_LENGTH);
e23eba97
NC
5173
5174 /* pr_psargs */
5175 elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
0897bb7d
AB
5176 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS,
5177 PRPSINFO_PR_PSARGS_LENGTH);
e23eba97
NC
5178 break;
5179 }
5180
5181 /* Note that for some reason, a spurious space is tacked
5182 onto the end of the args in some (at least one anyway)
5183 implementations, so strip it off if it exists. */
5184
5185 {
5186 char *command = elf_tdata (abfd)->core->command;
5187 int n = strlen (command);
5188
5189 if (0 < n && command[n - 1] == ' ')
5190 command[n - 1] = '\0';
5191 }
5192
0a1b45a2 5193 return true;
e23eba97
NC
5194}
5195
640d6bfd 5196/* Set the right mach type. */
dcd709e0 5197
0a1b45a2 5198static bool
640d6bfd
KLC
5199riscv_elf_object_p (bfd *abfd)
5200{
5201 /* There are only two mach types in RISCV currently. */
fbc09e7a
MC
5202 if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0
5203 || strcmp (abfd->xvec->name, "elf32-bigriscv") == 0)
640d6bfd
KLC
5204 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
5205 else
5206 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
5207
0a1b45a2 5208 return true;
640d6bfd
KLC
5209}
5210
2dc8dd17
JW
5211/* Determine whether an object attribute tag takes an integer, a
5212 string or both. */
5213
5214static int
5215riscv_elf_obj_attrs_arg_type (int tag)
5216{
5217 return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
5218}
e23eba97 5219
9b9b1092
NC
5220/* Do not choose mapping symbols as a function name. */
5221
5222static bfd_size_type
5223riscv_maybe_function_sym (const asymbol *sym,
5224 asection *sec,
5225 bfd_vma *code_off)
5226{
5227 if (sym->flags & BSF_LOCAL
5228 && riscv_elf_is_mapping_symbols (sym->name))
5229 return 0;
5230
5231 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
5232}
5233
5234/* Treat the following cases as target special symbols, they are
5235 usually omitted. */
ddfe525f
NC
5236
5237static bool
5238riscv_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
5239{
9b9b1092
NC
5240 /* PR27584, local and empty symbols. Since they are usually
5241 generated for pcrel relocations. */
ddfe525f 5242 return (!strcmp (sym->name, "")
9b9b1092
NC
5243 || _bfd_elf_is_local_label_name (abfd, sym->name)
5244 /* PR27916, mapping symbols. */
5245 || riscv_elf_is_mapping_symbols (sym->name));
ddfe525f
NC
5246}
5247
fbc95f1e
KC
5248static int
5249riscv_elf_additional_program_headers (bfd *abfd,
5250 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5251{
fbc95f1e
KC
5252 int ret = 0;
5253
5254 /* See if we need a PT_RISCV_ATTRIBUTES segment. */
5255 if (bfd_get_section_by_name (abfd, RISCV_ATTRIBUTES_SECTION_NAME))
5256 ++ret;
5257
5258 return ret;
5259}
5260
5261static bool
5262riscv_elf_modify_segment_map (bfd *abfd,
70a59063 5263 struct bfd_link_info *info ATTRIBUTE_UNUSED)
fbc95f1e
KC
5264{
5265 asection *s;
5266 struct elf_segment_map *m, **pm;
5267 size_t amt;
5268
5269 /* If there is a .riscv.attributes section, we need a PT_RISCV_ATTRIBUTES
5270 segment. */
5271 s = bfd_get_section_by_name (abfd, RISCV_ATTRIBUTES_SECTION_NAME);
5272 if (s != NULL)
5273 {
5274 for (m = elf_seg_map (abfd); m != NULL; m = m->next)
5275 if (m->p_type == PT_RISCV_ATTRIBUTES)
5276 break;
5277 /* If there is already a PT_RISCV_ATTRIBUTES header, avoid adding
5278 another. */
5279 if (m == NULL)
5280 {
5281 amt = sizeof (*m);
5282 m = bfd_zalloc (abfd, amt);
5283 if (m == NULL)
5284 return false;
5285
5286 m->p_type = PT_RISCV_ATTRIBUTES;
5287 m->count = 1;
5288 m->sections[0] = s;
5289
5290 /* We want to put it after the PHDR and INTERP segments. */
5291 pm = &elf_seg_map (abfd);
5292 while (*pm != NULL
5293 && ((*pm)->p_type == PT_PHDR
5294 || (*pm)->p_type == PT_INTERP))
5295 pm = &(*pm)->next;
5296
5297 m->next = *pm;
5298 *pm = m;
5299 }
5300 }
5301
5302 return true;
5303}
5304
8155b853
NC
5305/* Merge non-visibility st_other attributes. */
5306
5307static void
5308riscv_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5309 unsigned int st_other,
5310 bool definition ATTRIBUTE_UNUSED,
5311 bool dynamic ATTRIBUTE_UNUSED)
5312{
5313 unsigned int isym_sto = st_other & ~ELF_ST_VISIBILITY (-1);
5314 unsigned int h_sto = h->other & ~ELF_ST_VISIBILITY (-1);
5315
5316 if (isym_sto == h_sto)
5317 return;
5318
5319 if (isym_sto & ~STO_RISCV_VARIANT_CC)
5320 _bfd_error_handler (_("unknown attribute for symbol `%s': 0x%02x"),
5321 h->root.root.string, isym_sto);
5322
5323 if (isym_sto & STO_RISCV_VARIANT_CC)
5324 h->other |= STO_RISCV_VARIANT_CC;
5325}
5326
1942a048
NC
5327#define TARGET_LITTLE_SYM riscv_elfNN_vec
5328#define TARGET_LITTLE_NAME "elfNN-littleriscv"
5329#define TARGET_BIG_SYM riscv_elfNN_be_vec
5330#define TARGET_BIG_NAME "elfNN-bigriscv"
e23eba97 5331
1942a048 5332#define elf_backend_reloc_type_class riscv_reloc_type_class
e23eba97 5333
1942a048
NC
5334#define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
5335#define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
5336#define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
e23eba97
NC
5337#define bfd_elfNN_bfd_merge_private_bfd_data \
5338 _bfd_riscv_elf_merge_private_bfd_data
ddfe525f 5339#define bfd_elfNN_bfd_is_target_special_symbol riscv_elf_is_target_special_symbol
e23eba97 5340
1942a048
NC
5341#define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
5342#define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
5343#define elf_backend_check_relocs riscv_elf_check_relocs
5344#define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
5345#define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
5346#define elf_backend_relocate_section riscv_elf_relocate_section
5347#define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
5348#define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
1942a048
NC
5349#define elf_backend_plt_sym_val riscv_elf_plt_sym_val
5350#define elf_backend_grok_prstatus riscv_elf_grok_prstatus
5351#define elf_backend_grok_psinfo riscv_elf_grok_psinfo
5352#define elf_backend_object_p riscv_elf_object_p
0897bb7d 5353#define elf_backend_write_core_note riscv_write_core_note
9b9b1092 5354#define elf_backend_maybe_function_sym riscv_maybe_function_sym
1942a048
NC
5355#define elf_info_to_howto_rel NULL
5356#define elf_info_to_howto riscv_info_to_howto_rela
5357#define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
5358#define bfd_elfNN_mkobject elfNN_riscv_mkobject
fbc95f1e
KC
5359#define elf_backend_additional_program_headers \
5360 riscv_elf_additional_program_headers
5361#define elf_backend_modify_segment_map riscv_elf_modify_segment_map
8155b853 5362#define elf_backend_merge_symbol_attribute riscv_elf_merge_symbol_attribute
1942a048
NC
5363
5364#define elf_backend_init_index_section _bfd_elf_init_1_index_section
5365
5366#define elf_backend_can_gc_sections 1
5367#define elf_backend_can_refcount 1
5368#define elf_backend_want_got_plt 1
5369#define elf_backend_plt_readonly 1
5370#define elf_backend_plt_alignment 4
5371#define elf_backend_want_plt_sym 1
5372#define elf_backend_got_header_size (ARCH_SIZE / 8)
5373#define elf_backend_want_dynrelro 1
5374#define elf_backend_rela_normal 1
5375#define elf_backend_default_execstack 0
e23eba97 5376
2dc8dd17 5377#undef elf_backend_obj_attrs_vendor
1942a048 5378#define elf_backend_obj_attrs_vendor "riscv"
2dc8dd17 5379#undef elf_backend_obj_attrs_arg_type
1942a048 5380#define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
2dc8dd17 5381#undef elf_backend_obj_attrs_section_type
1942a048 5382#define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
2dc8dd17 5383#undef elf_backend_obj_attrs_section
fbc95f1e 5384#define elf_backend_obj_attrs_section RISCV_ATTRIBUTES_SECTION_NAME
2dc8dd17 5385
e23eba97 5386#include "elfNN-target.h"