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