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