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