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