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