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