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