]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elfnn-riscv.c
Use htab->c64_rel more, do not use GOT_CAP
[thirdparty/binutils-gdb.git] / bfd / elfnn-riscv.c
CommitLineData
e23eba97 1/* RISC-V-specific support for NN-bit ELF.
b3adc24a 2 Copyright (C) 2011-2020 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. */
f938669f 424 s->size += bed->got_header_size (info);
e23eba97
NC
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
f938669f
SP
1451 == get_elf_backend_data (output_bfd)->
1452 got_header_size (info))))
e23eba97
NC
1453 htab->elf.sgotplt->size = 0;
1454 }
1455
1456 /* The check_relocs and adjust_dynamic_symbol entry points have
1457 determined the sizes of the various dynamic sections. Allocate
1458 memory for them. */
1459 for (s = dynobj->sections; s != NULL; s = s->next)
1460 {
1461 if ((s->flags & SEC_LINKER_CREATED) == 0)
1462 continue;
1463
1464 if (s == htab->elf.splt
1465 || s == htab->elf.sgot
1466 || s == htab->elf.sgotplt
02dd9d25
NC
1467 || s == htab->elf.iplt
1468 || s == htab->elf.igotplt
5474d94f 1469 || s == htab->elf.sdynbss
3e1b4df8
JW
1470 || s == htab->elf.sdynrelro
1471 || s == htab->sdyntdata)
e23eba97
NC
1472 {
1473 /* Strip this section if we don't need it; see the
1474 comment below. */
1475 }
1476 else if (strncmp (s->name, ".rela", 5) == 0)
1477 {
1478 if (s->size != 0)
1479 {
1480 /* We use the reloc_count field as a counter if we need
1481 to copy relocs into the output file. */
1482 s->reloc_count = 0;
1483 }
1484 }
1485 else
1486 {
1487 /* It's not one of our sections. */
1488 continue;
1489 }
1490
1491 if (s->size == 0)
1492 {
1493 /* If we don't need this section, strip it from the
1494 output file. This is mostly to handle .rela.bss and
1495 .rela.plt. We must create both sections in
1496 create_dynamic_sections, because they must be created
1497 before the linker maps input sections to output
1498 sections. The linker does that before
1499 adjust_dynamic_symbol is called, and it is that
1500 function which decides whether anything needs to go
1501 into these sections. */
1502 s->flags |= SEC_EXCLUDE;
1503 continue;
1504 }
1505
1506 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1507 continue;
1508
1509 /* Allocate memory for the section contents. Zero the memory
1510 for the benefit of .rela.plt, which has 4 unused entries
1511 at the beginning, and we don't want garbage. */
1512 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1513 if (s->contents == NULL)
1514 return FALSE;
1515 }
1516
3084d7a2 1517 return _bfd_elf_add_dynamic_tags (output_bfd, info, TRUE);
e23eba97
NC
1518}
1519
1520#define TP_OFFSET 0
1521#define DTP_OFFSET 0x800
1522
1523/* Return the relocation value for a TLS dtp-relative reloc. */
1524
1525static bfd_vma
1526dtpoff (struct bfd_link_info *info, bfd_vma address)
1527{
1528 /* If tls_sec is NULL, we should have signalled an error already. */
1529 if (elf_hash_table (info)->tls_sec == NULL)
1530 return 0;
1531 return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1532}
1533
1534/* Return the relocation value for a static TLS tp-relative relocation. */
1535
1536static bfd_vma
1537tpoff (struct bfd_link_info *info, bfd_vma address)
1538{
1539 /* If tls_sec is NULL, we should have signalled an error already. */
1540 if (elf_hash_table (info)->tls_sec == NULL)
1541 return 0;
1542 return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1543}
1544
1545/* Return the global pointer's value, or 0 if it is not in use. */
1546
1547static bfd_vma
1548riscv_global_pointer_value (struct bfd_link_info *info)
1549{
1550 struct bfd_link_hash_entry *h;
1551
b5292032 1552 h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, FALSE, FALSE, TRUE);
e23eba97
NC
1553 if (h == NULL || h->type != bfd_link_hash_defined)
1554 return 0;
1555
1556 return h->u.def.value + sec_addr (h->u.def.section);
1557}
1558
1559/* Emplace a static relocation. */
1560
1561static bfd_reloc_status_type
1562perform_relocation (const reloc_howto_type *howto,
1563 const Elf_Internal_Rela *rel,
1564 bfd_vma value,
1565 asection *input_section,
1566 bfd *input_bfd,
1567 bfd_byte *contents)
1568{
1569 if (howto->pc_relative)
1570 value -= sec_addr (input_section) + rel->r_offset;
1571 value += rel->r_addend;
1572
1573 switch (ELFNN_R_TYPE (rel->r_info))
1574 {
1575 case R_RISCV_HI20:
1576 case R_RISCV_TPREL_HI20:
1577 case R_RISCV_PCREL_HI20:
1578 case R_RISCV_GOT_HI20:
1579 case R_RISCV_TLS_GOT_HI20:
1580 case R_RISCV_TLS_GD_HI20:
1581 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1582 return bfd_reloc_overflow;
1583 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1584 break;
1585
1586 case R_RISCV_LO12_I:
1587 case R_RISCV_GPREL_I:
1588 case R_RISCV_TPREL_LO12_I:
45f76423 1589 case R_RISCV_TPREL_I:
e23eba97
NC
1590 case R_RISCV_PCREL_LO12_I:
1591 value = ENCODE_ITYPE_IMM (value);
1592 break;
1593
1594 case R_RISCV_LO12_S:
1595 case R_RISCV_GPREL_S:
1596 case R_RISCV_TPREL_LO12_S:
45f76423 1597 case R_RISCV_TPREL_S:
e23eba97
NC
1598 case R_RISCV_PCREL_LO12_S:
1599 value = ENCODE_STYPE_IMM (value);
1600 break;
1601
1602 case R_RISCV_CALL:
1603 case R_RISCV_CALL_PLT:
1604 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1605 return bfd_reloc_overflow;
1606 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1607 | (ENCODE_ITYPE_IMM (value) << 32);
1608 break;
1609
1610 case R_RISCV_JAL:
1611 if (!VALID_UJTYPE_IMM (value))
1612 return bfd_reloc_overflow;
1613 value = ENCODE_UJTYPE_IMM (value);
1614 break;
1615
1616 case R_RISCV_BRANCH:
1617 if (!VALID_SBTYPE_IMM (value))
1618 return bfd_reloc_overflow;
1619 value = ENCODE_SBTYPE_IMM (value);
1620 break;
1621
1622 case R_RISCV_RVC_BRANCH:
1623 if (!VALID_RVC_B_IMM (value))
1624 return bfd_reloc_overflow;
1625 value = ENCODE_RVC_B_IMM (value);
1626 break;
1627
1628 case R_RISCV_RVC_JUMP:
1629 if (!VALID_RVC_J_IMM (value))
1630 return bfd_reloc_overflow;
1631 value = ENCODE_RVC_J_IMM (value);
1632 break;
1633
1634 case R_RISCV_RVC_LUI:
080a4883
JW
1635 if (RISCV_CONST_HIGH_PART (value) == 0)
1636 {
1637 /* Linker relaxation can convert an address equal to or greater than
1638 0x800 to slightly below 0x800. C.LUI does not accept zero as a
1639 valid immediate. We can fix this by converting it to a C.LI. */
1640 bfd_vma insn = bfd_get (howto->bitsize, input_bfd,
1641 contents + rel->r_offset);
1642 insn = (insn & ~MATCH_C_LUI) | MATCH_C_LI;
1643 bfd_put (howto->bitsize, input_bfd, insn, contents + rel->r_offset);
1644 value = ENCODE_RVC_IMM (0);
1645 }
1646 else if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
e23eba97 1647 return bfd_reloc_overflow;
080a4883
JW
1648 else
1649 value = ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value));
e23eba97
NC
1650 break;
1651
1652 case R_RISCV_32:
1653 case R_RISCV_64:
1654 case R_RISCV_ADD8:
1655 case R_RISCV_ADD16:
1656 case R_RISCV_ADD32:
1657 case R_RISCV_ADD64:
45f76423 1658 case R_RISCV_SUB6:
e23eba97
NC
1659 case R_RISCV_SUB8:
1660 case R_RISCV_SUB16:
1661 case R_RISCV_SUB32:
1662 case R_RISCV_SUB64:
45f76423
AW
1663 case R_RISCV_SET6:
1664 case R_RISCV_SET8:
1665 case R_RISCV_SET16:
1666 case R_RISCV_SET32:
a6cbf936 1667 case R_RISCV_32_PCREL:
e23eba97
NC
1668 case R_RISCV_TLS_DTPREL32:
1669 case R_RISCV_TLS_DTPREL64:
1670 break;
1671
ff6f4d9b
PD
1672 case R_RISCV_DELETE:
1673 return bfd_reloc_ok;
1674
e23eba97
NC
1675 default:
1676 return bfd_reloc_notsupported;
1677 }
1678
1679 bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
1680 word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
1681 bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
1682
1683 return bfd_reloc_ok;
1684}
1685
1686/* Remember all PC-relative high-part relocs we've encountered to help us
1687 later resolve the corresponding low-part relocs. */
1688
1689typedef struct
1690{
1691 bfd_vma address;
1692 bfd_vma value;
1693} riscv_pcrel_hi_reloc;
1694
1695typedef struct riscv_pcrel_lo_reloc
1696{
07d6d2b8
AM
1697 asection * input_section;
1698 struct bfd_link_info * info;
1699 reloc_howto_type * howto;
1700 const Elf_Internal_Rela * reloc;
1701 bfd_vma addr;
1702 const char * name;
1703 bfd_byte * contents;
1704 struct riscv_pcrel_lo_reloc * next;
e23eba97
NC
1705} riscv_pcrel_lo_reloc;
1706
1707typedef struct
1708{
1709 htab_t hi_relocs;
1710 riscv_pcrel_lo_reloc *lo_relocs;
1711} riscv_pcrel_relocs;
1712
1713static hashval_t
1714riscv_pcrel_reloc_hash (const void *entry)
1715{
1716 const riscv_pcrel_hi_reloc *e = entry;
1717 return (hashval_t)(e->address >> 2);
1718}
1719
1720static bfd_boolean
1721riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
1722{
1723 const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
1724 return e1->address == e2->address;
1725}
1726
1727static bfd_boolean
1728riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
1729{
1730
1731 p->lo_relocs = NULL;
1732 p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
1733 riscv_pcrel_reloc_eq, free);
1734 return p->hi_relocs != NULL;
1735}
1736
1737static void
1738riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
1739{
1740 riscv_pcrel_lo_reloc *cur = p->lo_relocs;
1741
1742 while (cur != NULL)
1743 {
1744 riscv_pcrel_lo_reloc *next = cur->next;
1745 free (cur);
1746 cur = next;
1747 }
1748
1749 htab_delete (p->hi_relocs);
1750}
1751
1752static bfd_boolean
b1308d2c
PD
1753riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
1754 struct bfd_link_info *info,
1755 bfd_vma pc,
1756 bfd_vma addr,
1757 bfd_byte *contents,
1758 const reloc_howto_type *howto,
1759 bfd *input_bfd)
e23eba97 1760{
b1308d2c
PD
1761 /* We may need to reference low addreses in PC-relative modes even when the
1762 * PC is far away from these addresses. For example, undefweak references
1763 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1764 * addresses that we can link PC-relative programs at, the linker can't
1765 * actually relocate references to those symbols. In order to allow these
1766 * programs to work we simply convert the PC-relative auipc sequences to
1767 * 0-relative lui sequences. */
1768 if (bfd_link_pic (info))
1769 return FALSE;
1770
1771 /* If it's possible to reference the symbol using auipc we do so, as that's
1772 * more in the spirit of the PC-relative relocations we're processing. */
1773 bfd_vma offset = addr - pc;
1774 if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
1775 return FALSE;
1776
1777 /* If it's impossible to reference this with a LUI-based offset then don't
1778 * bother to convert it at all so users still see the PC-relative relocation
1779 * in the truncation message. */
1780 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr)))
1781 return FALSE;
1782
1783 rel->r_info = ELFNN_R_INFO(addr, R_RISCV_HI20);
1784
1785 bfd_vma insn = bfd_get(howto->bitsize, input_bfd, contents + rel->r_offset);
1786 insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
1787 bfd_put(howto->bitsize, input_bfd, insn, contents + rel->r_offset);
1788 return TRUE;
1789}
1790
1791static bfd_boolean
1792riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr,
1793 bfd_vma value, bfd_boolean absolute)
1794{
1795 bfd_vma offset = absolute ? value : value - addr;
1796 riscv_pcrel_hi_reloc entry = {addr, offset};
e23eba97
NC
1797 riscv_pcrel_hi_reloc **slot =
1798 (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
1799
1800 BFD_ASSERT (*slot == NULL);
1801 *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
1802 if (*slot == NULL)
1803 return FALSE;
1804 **slot = entry;
1805 return TRUE;
1806}
1807
1808static bfd_boolean
1809riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
1810 asection *input_section,
1811 struct bfd_link_info *info,
1812 reloc_howto_type *howto,
1813 const Elf_Internal_Rela *reloc,
1814 bfd_vma addr,
1815 const char *name,
1816 bfd_byte *contents)
1817{
1818 riscv_pcrel_lo_reloc *entry;
1819 entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
1820 if (entry == NULL)
1821 return FALSE;
1822 *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr,
1823 name, contents, p->lo_relocs};
1824 p->lo_relocs = entry;
1825 return TRUE;
1826}
1827
1828static bfd_boolean
1829riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
1830{
1831 riscv_pcrel_lo_reloc *r;
1832
1833 for (r = p->lo_relocs; r != NULL; r = r->next)
1834 {
1835 bfd *input_bfd = r->input_section->owner;
1836
1837 riscv_pcrel_hi_reloc search = {r->addr, 0};
1838 riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
551703cf
JW
1839 if (entry == NULL
1840 /* Check for overflow into bit 11 when adding reloc addend. */
1841 || (! (entry->value & 0x800)
1842 && ((entry->value + r->reloc->r_addend) & 0x800)))
07d6d2b8 1843 {
551703cf
JW
1844 char *string = (entry == NULL
1845 ? "%pcrel_lo missing matching %pcrel_hi"
1846 : "%pcrel_lo overflow with an addend");
1847 (*r->info->callbacks->reloc_dangerous)
1848 (r->info, string, input_bfd, r->input_section, r->reloc->r_offset);
e23eba97 1849 return TRUE;
07d6d2b8 1850 }
e23eba97
NC
1851
1852 perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
1853 input_bfd, r->contents);
1854 }
1855
1856 return TRUE;
1857}
1858
1859/* Relocate a RISC-V ELF section.
1860
1861 The RELOCATE_SECTION function is called by the new ELF backend linker
1862 to handle the relocations for a section.
1863
1864 The relocs are always passed as Rela structures.
1865
1866 This function is responsible for adjusting the section contents as
1867 necessary, and (if generating a relocatable output file) adjusting
1868 the reloc addend as necessary.
1869
1870 This function does not have to worry about setting the reloc
1871 address or the reloc symbol index.
1872
1873 LOCAL_SYMS is a pointer to the swapped in local symbols.
1874
1875 LOCAL_SECTIONS is an array giving the section in the input file
1876 corresponding to the st_shndx field of each local symbol.
1877
1878 The global hash table entry for the global symbols can be found
1879 via elf_sym_hashes (input_bfd).
1880
1881 When generating relocatable output, this function must handle
1882 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1883 going to be the section symbol corresponding to the output
1884 section, which means that the addend must be adjusted
1885 accordingly. */
1886
1887static bfd_boolean
1888riscv_elf_relocate_section (bfd *output_bfd,
1889 struct bfd_link_info *info,
1890 bfd *input_bfd,
1891 asection *input_section,
1892 bfd_byte *contents,
1893 Elf_Internal_Rela *relocs,
1894 Elf_Internal_Sym *local_syms,
1895 asection **local_sections)
1896{
1897 Elf_Internal_Rela *rel;
1898 Elf_Internal_Rela *relend;
1899 riscv_pcrel_relocs pcrel_relocs;
1900 bfd_boolean ret = FALSE;
e23eba97
NC
1901 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
1902 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1903 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1904 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
b1308d2c 1905 bfd_boolean absolute;
e23eba97
NC
1906
1907 if (!riscv_init_pcrel_relocs (&pcrel_relocs))
1908 return FALSE;
1909
1910 relend = relocs + input_section->reloc_count;
1911 for (rel = relocs; rel < relend; rel++)
1912 {
1913 unsigned long r_symndx;
1914 struct elf_link_hash_entry *h;
1915 Elf_Internal_Sym *sym;
1916 asection *sec;
1917 bfd_vma relocation;
1918 bfd_reloc_status_type r = bfd_reloc_ok;
02dd9d25 1919 const char *name = NULL;
e23eba97
NC
1920 bfd_vma off, ie_off;
1921 bfd_boolean unresolved_reloc, is_ie = FALSE;
1922 bfd_vma pc = sec_addr (input_section) + rel->r_offset;
1923 int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
0aa13fee 1924 reloc_howto_type *howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
e23eba97 1925 const char *msg = NULL;
330a6637 1926 char *msg_buf = NULL;
6487709f 1927 bfd_boolean resolved_to_zero;
e23eba97 1928
f3185997
NC
1929 if (howto == NULL
1930 || r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
e23eba97
NC
1931 continue;
1932
1933 /* This is a final link. */
1934 r_symndx = ELFNN_R_SYM (rel->r_info);
1935 h = NULL;
1936 sym = NULL;
1937 sec = NULL;
1938 unresolved_reloc = FALSE;
1939 if (r_symndx < symtab_hdr->sh_info)
1940 {
1941 sym = local_syms + r_symndx;
1942 sec = local_sections[r_symndx];
1943 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
02dd9d25
NC
1944
1945 /* Relocate against local STT_GNU_IFUNC symbol. */
1946 if (!bfd_link_relocatable (info)
1947 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
1948 {
1949 h = riscv_elf_get_local_sym_hash (htab, input_bfd, rel, FALSE);
1950 if (h == NULL)
1951 abort ();
1952
1953 /* Set STT_GNU_IFUNC symbol value. */
1954 h->root.u.def.value = sym->st_value;
1955 h->root.u.def.section = sec;
1956 }
e23eba97
NC
1957 }
1958 else
1959 {
1960 bfd_boolean warned, ignored;
1961
1962 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1963 r_symndx, symtab_hdr, sym_hashes,
1964 h, sec, relocation,
1965 unresolved_reloc, warned, ignored);
1966 if (warned)
1967 {
1968 /* To avoid generating warning messages about truncated
1969 relocations, set the relocation's address to be the same as
1970 the start of this section. */
1971 if (input_section->output_section != NULL)
1972 relocation = input_section->output_section->vma;
1973 else
1974 relocation = 0;
1975 }
1976 }
1977
1978 if (sec != NULL && discarded_section (sec))
1979 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1980 rel, 1, relend, howto, 0, contents);
1981
1982 if (bfd_link_relocatable (info))
1983 continue;
1984
02dd9d25
NC
1985 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1986 it here if it is defined in a non-shared object. */
1987 if (h != NULL
1988 && h->type == STT_GNU_IFUNC
1989 && h->def_regular)
1990 {
1991 asection *plt, *base_got;
1992
1993 if ((input_section->flags & SEC_ALLOC) == 0)
1994 {
1995 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
1996 STT_GNU_IFUNC symbol as STT_FUNC. */
1997 if (elf_section_type (input_section) == SHT_NOTE)
1998 goto skip_ifunc;
1999
2000 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2001 sections because such sections are not SEC_ALLOC and
2002 thus ld.so will not process them. */
2003 if ((input_section->flags & SEC_DEBUGGING) != 0)
2004 continue;
2005
2006 abort ();
2007 }
2008 else if (h->plt.offset == (bfd_vma) -1
2009 /* The following relocation may not need the .plt entries
2010 when all references to a STT_GNU_IFUNC symbols are done
2011 via GOT or static function pointers. */
2012 && r_type != R_RISCV_32
2013 && r_type != R_RISCV_64
2014 && r_type != R_RISCV_HI20
2015 && r_type != R_RISCV_GOT_HI20
2016 && r_type != R_RISCV_LO12_I
2017 && r_type != R_RISCV_LO12_S)
2018 goto bad_ifunc_reloc;
2019
2020 /* STT_GNU_IFUNC symbol must go through PLT. */
2021 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2022 relocation = plt->output_section->vma
2023 + plt->output_offset
2024 + h->plt.offset;
2025
2026 switch (r_type)
2027 {
2028 case R_RISCV_32:
2029 case R_RISCV_64:
2030 if (rel->r_addend != 0)
2031 {
2032 if (h->root.root.string)
2033 name = h->root.root.string;
2034 else
2035 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
2036
2037 _bfd_error_handler
2038 /* xgettext:c-format */
2039 (_("%pB: relocation %s against STT_GNU_IFUNC "
2040 "symbol `%s' has non-zero addend: %" PRId64),
2041 input_bfd, howto->name, name, (int64_t) rel->r_addend);
2042 bfd_set_error (bfd_error_bad_value);
2043 return FALSE;
2044 }
2045
2046 /* Generate dynamic relocation only when there is a non-GOT
2047 reference in a shared object or there is no PLT. */
2048 if ((bfd_link_pic (info) && h->non_got_ref)
2049 || h->plt.offset == (bfd_vma) -1)
2050 {
2051 Elf_Internal_Rela outrel;
2052 asection *sreloc;
2053
2054 /* Need a dynamic relocation to get the real function
2055 address. */
2056 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2057 info,
2058 input_section,
2059 rel->r_offset);
2060 if (outrel.r_offset == (bfd_vma) -1
2061 || outrel.r_offset == (bfd_vma) -2)
2062 abort ();
2063
2064 outrel.r_offset += input_section->output_section->vma
2065 + input_section->output_offset;
2066
2067 if (h->dynindx == -1
2068 || h->forced_local
2069 || bfd_link_executable (info))
2070 {
2071 info->callbacks->minfo
2072 (_("Local IFUNC function `%s' in %pB\n"),
2073 h->root.root.string,
2074 h->root.u.def.section->owner);
2075
2076 /* This symbol is resolved locally. */
2077 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
2078 outrel.r_addend = h->root.u.def.value
2079 + h->root.u.def.section->output_section->vma
2080 + h->root.u.def.section->output_offset;
2081 }
2082 else
2083 {
2084 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2085 outrel.r_addend = 0;
2086 }
2087
2088 /* Dynamic relocations are stored in
2089 1. .rela.ifunc section in PIC object.
2090 2. .rela.got section in dynamic executable.
2091 3. .rela.iplt section in static executable. */
2092 if (bfd_link_pic (info))
2093 sreloc = htab->elf.irelifunc;
2094 else if (htab->elf.splt != NULL)
2095 sreloc = htab->elf.srelgot;
2096 else
2097 sreloc = htab->elf.irelplt;
2098
2099 riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2100
2101 /* If this reloc is against an external symbol, we
2102 do not want to fiddle with the addend. Otherwise,
2103 we need to include the symbol value so that it
2104 becomes an addend for the dynamic reloc. For an
2105 internal symbol, we have updated addend. */
2106 continue;
2107 }
2108 goto do_relocation;
2109
2110 case R_RISCV_GOT_HI20:
2111 base_got = htab->elf.sgot;
2112 off = h->got.offset;
2113
2114 if (base_got == NULL)
2115 abort ();
2116
2117 if (off == (bfd_vma) -1)
2118 {
2119 bfd_vma plt_idx;
2120
2121 /* We can't use h->got.offset here to save state, or
2122 even just remember the offset, as finish_dynamic_symbol
2123 would use that as offset into .got. */
2124
2125 if (htab->elf.splt != NULL)
2126 {
2127 plt_idx = (h->plt.offset - PLT_HEADER_SIZE)
2128 / PLT_ENTRY_SIZE;
2129 off = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
2130 base_got = htab->elf.sgotplt;
2131 }
2132 else
2133 {
2134 plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
2135 off = plt_idx * GOT_ENTRY_SIZE;
2136 base_got = htab->elf.igotplt;
2137 }
2138
2139 if (h->dynindx == -1
2140 || h->forced_local
2141 || info->symbolic)
2142 {
2143 /* This references the local definition. We must
2144 initialize this entry in the global offset table.
2145 Since the offset must always be a multiple of 8,
2146 we use the least significant bit to record
2147 whether we have initialized it already.
2148
2149 When doing a dynamic link, we create a .rela.got
2150 relocation entry to initialize the value. This
2151 is done in the finish_dynamic_symbol routine. */
2152 if ((off & 1) != 0)
2153 off &= ~1;
2154 else
2155 {
2156 bfd_put_NN (output_bfd, relocation,
2157 base_got->contents + off);
2158 /* Note that this is harmless for the case,
2159 as -1 | 1 still is -1. */
2160 h->got.offset |= 1;
2161 }
2162 }
2163 }
2164
2165 relocation = base_got->output_section->vma
2166 + base_got->output_offset + off;
2167
2168 r_type = ELFNN_R_TYPE (rel->r_info);
2169 howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
2170 if (howto == NULL)
2171 r = bfd_reloc_notsupported;
2172 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2173 relocation, FALSE))
2174 r = bfd_reloc_overflow;
2175 goto do_relocation;
2176
2177 case R_RISCV_CALL:
2178 case R_RISCV_CALL_PLT:
2179 case R_RISCV_HI20:
2180 case R_RISCV_LO12_I:
2181 case R_RISCV_LO12_S:
2182 goto do_relocation;
2183
2184 case R_RISCV_PCREL_HI20:
2185 r_type = ELFNN_R_TYPE (rel->r_info);
2186 howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
2187 if (howto == NULL)
2188 r = bfd_reloc_notsupported;
2189 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2190 relocation, FALSE))
2191 r = bfd_reloc_overflow;
2192 goto do_relocation;
2193
2194 default:
2195 bad_ifunc_reloc:
2196 if (h->root.root.string)
2197 name = h->root.root.string;
2198 else
2199 /* The entry of local ifunc is fake in global hash table,
2200 we should find the name by the original local symbol. */
2201 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, NULL);
2202
2203 _bfd_error_handler
2204 /* xgettext:c-format */
2205 (_("%pB: relocation %s against STT_GNU_IFUNC "
2206 "symbol `%s' isn't supported"), input_bfd,
2207 howto->name, name);
2208 bfd_set_error (bfd_error_bad_value);
2209 return FALSE;
2210 }
2211 }
2212
2213 skip_ifunc:
e23eba97
NC
2214 if (h != NULL)
2215 name = h->root.root.string;
2216 else
2217 {
2218 name = (bfd_elf_string_from_elf_section
2219 (input_bfd, symtab_hdr->sh_link, sym->st_name));
2220 if (name == NULL || *name == '\0')
fd361982 2221 name = bfd_section_name (sec);
e23eba97
NC
2222 }
2223
6487709f
JW
2224 resolved_to_zero = (h != NULL
2225 && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
2226
e23eba97
NC
2227 switch (r_type)
2228 {
2229 case R_RISCV_NONE:
45f76423 2230 case R_RISCV_RELAX:
e23eba97
NC
2231 case R_RISCV_TPREL_ADD:
2232 case R_RISCV_COPY:
2233 case R_RISCV_JUMP_SLOT:
2234 case R_RISCV_RELATIVE:
2235 /* These require nothing of us at all. */
2236 continue;
2237
2238 case R_RISCV_HI20:
2239 case R_RISCV_BRANCH:
2240 case R_RISCV_RVC_BRANCH:
2241 case R_RISCV_RVC_LUI:
2242 case R_RISCV_LO12_I:
2243 case R_RISCV_LO12_S:
45f76423
AW
2244 case R_RISCV_SET6:
2245 case R_RISCV_SET8:
2246 case R_RISCV_SET16:
2247 case R_RISCV_SET32:
a6cbf936 2248 case R_RISCV_32_PCREL:
ff6f4d9b 2249 case R_RISCV_DELETE:
e23eba97
NC
2250 /* These require no special handling beyond perform_relocation. */
2251 break;
2252
2253 case R_RISCV_GOT_HI20:
2254 if (h != NULL)
2255 {
2256 bfd_boolean dyn, pic;
2257
2258 off = h->got.offset;
2259 BFD_ASSERT (off != (bfd_vma) -1);
2260 dyn = elf_hash_table (info)->dynamic_sections_created;
2261 pic = bfd_link_pic (info);
2262
2263 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
2264 || (pic && SYMBOL_REFERENCES_LOCAL (info, h)))
2265 {
2266 /* This is actually a static link, or it is a
2267 -Bsymbolic link and the symbol is defined
2268 locally, or the symbol was forced to be local
2269 because of a version file. We must initialize
2270 this entry in the global offset table. Since the
2271 offset must always be a multiple of the word size,
2272 we use the least significant bit to record whether
2273 we have initialized it already.
2274
2275 When doing a dynamic link, we create a .rela.got
2276 relocation entry to initialize the value. This
2277 is done in the finish_dynamic_symbol routine. */
2278 if ((off & 1) != 0)
2279 off &= ~1;
2280 else
2281 {
2282 bfd_put_NN (output_bfd, relocation,
2283 htab->elf.sgot->contents + off);
2284 h->got.offset |= 1;
2285 }
2286 }
2287 else
2288 unresolved_reloc = FALSE;
2289 }
2290 else
2291 {
2292 BFD_ASSERT (local_got_offsets != NULL
2293 && local_got_offsets[r_symndx] != (bfd_vma) -1);
2294
2295 off = local_got_offsets[r_symndx];
2296
2297 /* The offset must always be a multiple of the word size.
2298 So, we can use the least significant bit to record
2299 whether we have already processed this entry. */
2300 if ((off & 1) != 0)
2301 off &= ~1;
2302 else
2303 {
2304 if (bfd_link_pic (info))
2305 {
2306 asection *s;
2307 Elf_Internal_Rela outrel;
2308
2309 /* We need to generate a R_RISCV_RELATIVE reloc
2310 for the dynamic linker. */
2311 s = htab->elf.srelgot;
2312 BFD_ASSERT (s != NULL);
2313
2314 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2315 outrel.r_info =
2316 ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2317 outrel.r_addend = relocation;
2318 relocation = 0;
2319 riscv_elf_append_rela (output_bfd, s, &outrel);
2320 }
2321
2322 bfd_put_NN (output_bfd, relocation,
2323 htab->elf.sgot->contents + off);
2324 local_got_offsets[r_symndx] |= 1;
2325 }
2326 }
2327 relocation = sec_addr (htab->elf.sgot) + off;
b1308d2c
PD
2328 absolute = riscv_zero_pcrel_hi_reloc (rel,
2329 info,
2330 pc,
2331 relocation,
2332 contents,
2333 howto,
2334 input_bfd);
2335 r_type = ELFNN_R_TYPE (rel->r_info);
0aa13fee 2336 howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
f3185997
NC
2337 if (howto == NULL)
2338 r = bfd_reloc_notsupported;
2339 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2340 relocation, absolute))
e23eba97
NC
2341 r = bfd_reloc_overflow;
2342 break;
2343
2344 case R_RISCV_ADD8:
2345 case R_RISCV_ADD16:
2346 case R_RISCV_ADD32:
2347 case R_RISCV_ADD64:
2348 {
2349 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2350 contents + rel->r_offset);
2351 relocation = old_value + relocation;
2352 }
2353 break;
2354
45f76423 2355 case R_RISCV_SUB6:
e23eba97
NC
2356 case R_RISCV_SUB8:
2357 case R_RISCV_SUB16:
2358 case R_RISCV_SUB32:
2359 case R_RISCV_SUB64:
2360 {
2361 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2362 contents + rel->r_offset);
2363 relocation = old_value - relocation;
2364 }
2365 break;
2366
e23eba97 2367 case R_RISCV_CALL:
85f78364 2368 case R_RISCV_CALL_PLT:
cf7a5066
JW
2369 /* Handle a call to an undefined weak function. This won't be
2370 relaxed, so we have to handle it here. */
2371 if (h != NULL && h->root.type == bfd_link_hash_undefweak
85f78364 2372 && (!bfd_link_pic (info) || h->plt.offset == MINUS_ONE))
cf7a5066
JW
2373 {
2374 /* We can use x0 as the base register. */
2375 bfd_vma insn = bfd_get_32 (input_bfd,
2376 contents + rel->r_offset + 4);
2377 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2378 bfd_put_32 (input_bfd, insn, contents + rel->r_offset + 4);
2379 /* Set the relocation value so that we get 0 after the pc
2380 relative adjustment. */
2381 relocation = sec_addr (input_section) + rel->r_offset;
2382 }
2383 /* Fall through. */
2384
e23eba97
NC
2385 case R_RISCV_JAL:
2386 case R_RISCV_RVC_JUMP:
85f78364 2387 /* This line has to match the check in _bfd_riscv_relax_section. */
e23eba97
NC
2388 if (bfd_link_pic (info) && h != NULL && h->plt.offset != MINUS_ONE)
2389 {
2390 /* Refer to the PLT entry. */
2391 relocation = sec_addr (htab->elf.splt) + h->plt.offset;
2392 unresolved_reloc = FALSE;
2393 }
2394 break;
2395
2396 case R_RISCV_TPREL_HI20:
2397 relocation = tpoff (info, relocation);
2398 break;
2399
2400 case R_RISCV_TPREL_LO12_I:
2401 case R_RISCV_TPREL_LO12_S:
45f76423
AW
2402 relocation = tpoff (info, relocation);
2403 break;
2404
2405 case R_RISCV_TPREL_I:
2406 case R_RISCV_TPREL_S:
e23eba97
NC
2407 relocation = tpoff (info, relocation);
2408 if (VALID_ITYPE_IMM (relocation + rel->r_addend))
2409 {
2410 /* We can use tp as the base register. */
2411 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2412 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2413 insn |= X_TP << OP_SH_RS1;
2414 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2415 }
45f76423
AW
2416 else
2417 r = bfd_reloc_overflow;
e23eba97
NC
2418 break;
2419
2420 case R_RISCV_GPREL_I:
2421 case R_RISCV_GPREL_S:
2422 {
2423 bfd_vma gp = riscv_global_pointer_value (info);
2424 bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
2425 if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
2426 {
2427 /* We can use x0 or gp as the base register. */
2428 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2429 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2430 if (!x0_base)
2431 {
2432 rel->r_addend -= gp;
2433 insn |= X_GP << OP_SH_RS1;
2434 }
2435 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2436 }
2437 else
2438 r = bfd_reloc_overflow;
2439 break;
2440 }
2441
2442 case R_RISCV_PCREL_HI20:
b1308d2c
PD
2443 absolute = riscv_zero_pcrel_hi_reloc (rel,
2444 info,
2445 pc,
2446 relocation,
2447 contents,
2448 howto,
2449 input_bfd);
2450 r_type = ELFNN_R_TYPE (rel->r_info);
0aa13fee 2451 howto = riscv_elf_rtype_to_howto (input_bfd, r_type);
f3185997
NC
2452 if (howto == NULL)
2453 r = bfd_reloc_notsupported;
2454 else if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2455 relocation + rel->r_addend,
2456 absolute))
e23eba97
NC
2457 r = bfd_reloc_overflow;
2458 break;
2459
2460 case R_RISCV_PCREL_LO12_I:
2461 case R_RISCV_PCREL_LO12_S:
551703cf
JW
2462 /* We don't allow section symbols plus addends as the auipc address,
2463 because then riscv_relax_delete_bytes would have to search through
2464 all relocs to update these addends. This is also ambiguous, as
2465 we do allow offsets to be added to the target address, which are
2466 not to be used to find the auipc address. */
a9f5a551
JW
2467 if (((sym != NULL && (ELF_ST_TYPE (sym->st_info) == STT_SECTION))
2468 || (h != NULL && h->type == STT_SECTION))
2469 && rel->r_addend)
2a0d9853 2470 {
330a6637 2471 msg = _("%pcrel_lo section symbol with an addend");
2a0d9853
JW
2472 r = bfd_reloc_dangerous;
2473 break;
2474 }
2475
e23eba97
NC
2476 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info,
2477 howto, rel, relocation, name,
2478 contents))
2479 continue;
2480 r = bfd_reloc_overflow;
2481 break;
2482
2483 case R_RISCV_TLS_DTPREL32:
2484 case R_RISCV_TLS_DTPREL64:
2485 relocation = dtpoff (info, relocation);
2486 break;
2487
2488 case R_RISCV_32:
2489 case R_RISCV_64:
2490 if ((input_section->flags & SEC_ALLOC) == 0)
2491 break;
2492
2493 if ((bfd_link_pic (info)
2494 && (h == NULL
6487709f
JW
2495 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2496 && !resolved_to_zero)
e23eba97
NC
2497 || h->root.type != bfd_link_hash_undefweak)
2498 && (! howto->pc_relative
2499 || !SYMBOL_CALLS_LOCAL (info, h)))
2500 || (!bfd_link_pic (info)
2501 && h != NULL
2502 && h->dynindx != -1
2503 && !h->non_got_ref
2504 && ((h->def_dynamic
2505 && !h->def_regular)
2506 || h->root.type == bfd_link_hash_undefweak
2507 || h->root.type == bfd_link_hash_undefined)))
2508 {
2509 Elf_Internal_Rela outrel;
02dd9d25 2510 asection *sreloc;
e23eba97
NC
2511 bfd_boolean skip_static_relocation, skip_dynamic_relocation;
2512
2513 /* When generating a shared object, these relocations
2514 are copied into the output file to be resolved at run
2515 time. */
2516
2517 outrel.r_offset =
2518 _bfd_elf_section_offset (output_bfd, info, input_section,
2519 rel->r_offset);
2520 skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
2521 skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
2522 outrel.r_offset += sec_addr (input_section);
2523
2524 if (skip_dynamic_relocation)
2525 memset (&outrel, 0, sizeof outrel);
2526 else if (h != NULL && h->dynindx != -1
2527 && !(bfd_link_pic (info)
2528 && SYMBOLIC_BIND (info, h)
2529 && h->def_regular))
2530 {
2531 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2532 outrel.r_addend = rel->r_addend;
2533 }
2534 else
2535 {
2536 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2537 outrel.r_addend = relocation + rel->r_addend;
2538 }
2539
02dd9d25 2540 sreloc = elf_section_data (input_section)->sreloc;
e23eba97
NC
2541 riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2542 if (skip_static_relocation)
2543 continue;
2544 }
2545 break;
2546
2547 case R_RISCV_TLS_GOT_HI20:
2548 is_ie = TRUE;
2549 /* Fall through. */
2550
2551 case R_RISCV_TLS_GD_HI20:
2552 if (h != NULL)
2553 {
2554 off = h->got.offset;
2555 h->got.offset |= 1;
2556 }
2557 else
2558 {
2559 off = local_got_offsets[r_symndx];
2560 local_got_offsets[r_symndx] |= 1;
2561 }
2562
2563 tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
2564 BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
2565 /* If this symbol is referenced by both GD and IE TLS, the IE
2566 reference's GOT slot follows the GD reference's slots. */
2567 ie_off = 0;
2568 if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
2569 ie_off = 2 * GOT_ENTRY_SIZE;
2570
2571 if ((off & 1) != 0)
2572 off &= ~1;
2573 else
2574 {
2575 Elf_Internal_Rela outrel;
2576 int indx = 0;
2577 bfd_boolean need_relocs = FALSE;
2578
2579 if (htab->elf.srelgot == NULL)
2580 abort ();
2581
2582 if (h != NULL)
2583 {
2584 bfd_boolean dyn, pic;
2585 dyn = htab->elf.dynamic_sections_created;
2586 pic = bfd_link_pic (info);
2587
2588 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
2589 && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
2590 indx = h->dynindx;
2591 }
2592
2593 /* The GOT entries have not been initialized yet. Do it
07d6d2b8 2594 now, and emit any relocations. */
e23eba97
NC
2595 if ((bfd_link_pic (info) || indx != 0)
2596 && (h == NULL
2597 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2598 || h->root.type != bfd_link_hash_undefweak))
2599 need_relocs = TRUE;
2600
2601 if (tls_type & GOT_TLS_GD)
2602 {
2603 if (need_relocs)
2604 {
2605 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2606 outrel.r_addend = 0;
2607 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
2608 bfd_put_NN (output_bfd, 0,
2609 htab->elf.sgot->contents + off);
2610 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2611 if (indx == 0)
2612 {
2613 BFD_ASSERT (! unresolved_reloc);
2614 bfd_put_NN (output_bfd,
2615 dtpoff (info, relocation),
2616 (htab->elf.sgot->contents + off +
2617 RISCV_ELF_WORD_BYTES));
2618 }
2619 else
2620 {
2621 bfd_put_NN (output_bfd, 0,
2622 (htab->elf.sgot->contents + off +
2623 RISCV_ELF_WORD_BYTES));
2624 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
2625 outrel.r_offset += RISCV_ELF_WORD_BYTES;
2626 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2627 }
2628 }
2629 else
2630 {
2631 /* If we are not emitting relocations for a
2632 general dynamic reference, then we must be in a
2633 static link or an executable link with the
2634 symbol binding locally. Mark it as belonging
2635 to module 1, the executable. */
2636 bfd_put_NN (output_bfd, 1,
2637 htab->elf.sgot->contents + off);
2638 bfd_put_NN (output_bfd,
2639 dtpoff (info, relocation),
2640 (htab->elf.sgot->contents + off +
2641 RISCV_ELF_WORD_BYTES));
2642 }
2643 }
2644
2645 if (tls_type & GOT_TLS_IE)
2646 {
2647 if (need_relocs)
2648 {
2649 bfd_put_NN (output_bfd, 0,
2650 htab->elf.sgot->contents + off + ie_off);
2651 outrel.r_offset = sec_addr (htab->elf.sgot)
2652 + off + ie_off;
2653 outrel.r_addend = 0;
2654 if (indx == 0)
2655 outrel.r_addend = tpoff (info, relocation);
2656 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
2657 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2658 }
2659 else
2660 {
2661 bfd_put_NN (output_bfd, tpoff (info, relocation),
2662 htab->elf.sgot->contents + off + ie_off);
2663 }
2664 }
2665 }
2666
2667 BFD_ASSERT (off < (bfd_vma) -2);
2668 relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
b1308d2c
PD
2669 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2670 relocation, FALSE))
e23eba97
NC
2671 r = bfd_reloc_overflow;
2672 unresolved_reloc = FALSE;
2673 break;
2674
2675 default:
2676 r = bfd_reloc_notsupported;
2677 }
2678
2679 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2680 because such sections are not SEC_ALLOC and thus ld.so will
2681 not process them. */
2682 if (unresolved_reloc
2683 && !((input_section->flags & SEC_DEBUGGING) != 0
2684 && h->def_dynamic)
2685 && _bfd_elf_section_offset (output_bfd, info, input_section,
2686 rel->r_offset) != (bfd_vma) -1)
2687 {
330a6637
JW
2688 switch (r_type)
2689 {
330a6637
JW
2690 case R_RISCV_JAL:
2691 case R_RISCV_RVC_JUMP:
2692 if (asprintf (&msg_buf,
2693 _("%%X%%P: relocation %s against `%s' can "
2694 "not be used when making a shared object; "
2695 "recompile with -fPIC\n"),
2696 howto->name,
2697 h->root.root.string) == -1)
2698 msg_buf = NULL;
2699 break;
2700
2701 default:
2702 if (asprintf (&msg_buf,
2703 _("%%X%%P: unresolvable %s relocation against "
2704 "symbol `%s'\n"),
2705 howto->name,
2706 h->root.root.string) == -1)
2707 msg_buf = NULL;
2708 break;
2709 }
2710
2711 msg = msg_buf;
2712 r = bfd_reloc_notsupported;
e23eba97
NC
2713 }
2714
02dd9d25 2715 do_relocation:
e23eba97
NC
2716 if (r == bfd_reloc_ok)
2717 r = perform_relocation (howto, rel, relocation, input_section,
2718 input_bfd, contents);
2719
330a6637
JW
2720 /* We should have already detected the error and set message before.
2721 If the error message isn't set since the linker runs out of memory
2722 or we don't set it before, then we should set the default message
2723 with the "internal error" string here. */
e23eba97
NC
2724 switch (r)
2725 {
2726 case bfd_reloc_ok:
2727 continue;
2728
2729 case bfd_reloc_overflow:
2730 info->callbacks->reloc_overflow
2731 (info, (h ? &h->root : NULL), name, howto->name,
2732 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2733 break;
2734
2735 case bfd_reloc_undefined:
2736 info->callbacks->undefined_symbol
2737 (info, name, input_bfd, input_section, rel->r_offset,
2738 TRUE);
2739 break;
2740
2741 case bfd_reloc_outofrange:
330a6637
JW
2742 if (msg == NULL)
2743 msg = _("%X%P: internal error: out of range error\n");
e23eba97
NC
2744 break;
2745
2746 case bfd_reloc_notsupported:
330a6637
JW
2747 if (msg == NULL)
2748 msg = _("%X%P: internal error: unsupported relocation error\n");
e23eba97
NC
2749 break;
2750
2751 case bfd_reloc_dangerous:
330a6637
JW
2752 /* The error message should already be set. */
2753 if (msg == NULL)
2754 msg = _("dangerous relocation error");
2a0d9853 2755 info->callbacks->reloc_dangerous
330a6637 2756 (info, msg, input_bfd, input_section, rel->r_offset);
e23eba97
NC
2757 break;
2758
2759 default:
2a0d9853 2760 msg = _("%X%P: internal error: unknown error\n");
e23eba97
NC
2761 break;
2762 }
2763
330a6637
JW
2764 /* Do not report error message for the dangerous relocation again. */
2765 if (msg && r != bfd_reloc_dangerous)
2a0d9853
JW
2766 info->callbacks->einfo (msg);
2767
c9594989
AM
2768 /* Free the unused `msg_buf`. */
2769 free (msg_buf);
330a6637 2770
3f48fe4a
JW
2771 /* We already reported the error via a callback, so don't try to report
2772 it again by returning false. That leads to spurious errors. */
ed01220c 2773 ret = TRUE;
e23eba97
NC
2774 goto out;
2775 }
2776
2777 ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
dc1e8a47 2778 out:
e23eba97
NC
2779 riscv_free_pcrel_relocs (&pcrel_relocs);
2780 return ret;
2781}
2782
2783/* Finish up dynamic symbol handling. We set the contents of various
2784 dynamic sections here. */
2785
2786static bfd_boolean
2787riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
2788 struct bfd_link_info *info,
2789 struct elf_link_hash_entry *h,
2790 Elf_Internal_Sym *sym)
2791{
2792 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2793 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2794
2795 if (h->plt.offset != (bfd_vma) -1)
2796 {
2797 /* We've decided to create a PLT entry for this symbol. */
2798 bfd_byte *loc;
02dd9d25 2799 bfd_vma i, header_address, plt_idx, got_offset, got_address;
e23eba97
NC
2800 uint32_t plt_entry[PLT_ENTRY_INSNS];
2801 Elf_Internal_Rela rela;
02dd9d25
NC
2802 asection *plt, *gotplt, *relplt;
2803
2804 /* When building a static executable, use .iplt, .igot.plt and
2805 .rela.iplt sections for STT_GNU_IFUNC symbols. */
2806 if (htab->elf.splt != NULL)
2807 {
2808 plt = htab->elf.splt;
2809 gotplt = htab->elf.sgotplt;
2810 relplt = htab->elf.srelplt;
2811 }
2812 else
2813 {
2814 plt = htab->elf.iplt;
2815 gotplt = htab->elf.igotplt;
2816 relplt = htab->elf.irelplt;
2817 }
2818
2819 /* This symbol has an entry in the procedure linkage table. Set
2820 it up. */
2821 if ((h->dynindx == -1
2822 && !((h->forced_local || bfd_link_executable (info))
2823 && h->def_regular
2824 && h->type == STT_GNU_IFUNC))
2825 || plt == NULL
2826 || gotplt == NULL
2827 || relplt == NULL)
2828 return FALSE;
e23eba97
NC
2829
2830 /* Calculate the address of the PLT header. */
02dd9d25 2831 header_address = sec_addr (plt);
e23eba97 2832
02dd9d25
NC
2833 /* Calculate the index of the entry and the offset of .got.plt entry.
2834 For static executables, we don't reserve anything. */
2835 if (plt == htab->elf.splt)
2836 {
2837 plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2838 got_offset = GOTPLT_HEADER_SIZE + (plt_idx * GOT_ENTRY_SIZE);
2839 }
2840 else
2841 {
2842 plt_idx = h->plt.offset / PLT_ENTRY_SIZE;
2843 got_offset = plt_idx * GOT_ENTRY_SIZE;
2844 }
e23eba97
NC
2845
2846 /* Calculate the address of the .got.plt entry. */
02dd9d25 2847 got_address = sec_addr (gotplt) + got_offset;
e23eba97
NC
2848
2849 /* Find out where the .plt entry should go. */
02dd9d25 2850 loc = plt->contents + h->plt.offset;
e23eba97
NC
2851
2852 /* Fill in the PLT entry itself. */
5ef23793
JW
2853 if (! riscv_make_plt_entry (output_bfd, got_address,
2854 header_address + h->plt.offset,
2855 plt_entry))
2856 return FALSE;
2857
e23eba97
NC
2858 for (i = 0; i < PLT_ENTRY_INSNS; i++)
2859 bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i);
2860
2861 /* Fill in the initial value of the .got.plt entry. */
02dd9d25
NC
2862 loc = gotplt->contents + (got_address - sec_addr (gotplt));
2863 bfd_put_NN (output_bfd, sec_addr (plt), loc);
e23eba97 2864
e23eba97 2865 rela.r_offset = got_address;
e23eba97 2866
02dd9d25
NC
2867 if (h->dynindx == -1
2868 || ((bfd_link_executable (info)
2869 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2870 && h->def_regular
2871 && h->type == STT_GNU_IFUNC))
2872 {
2873 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2874 h->root.root.string,
2875 h->root.u.def.section->owner);
2876
2877 /* If an STT_GNU_IFUNC symbol is locally defined, generate
2878 R_RISCV_IRELATIVE instead of R_RISCV_JUMP_SLOT. */
2879 asection *sec = h->root.u.def.section;
2880 rela.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
2881 rela.r_addend = h->root.u.def.value
2882 + sec->output_section->vma
2883 + sec->output_offset;
2884 }
2885 else
2886 {
2887 /* Fill in the entry in the .rela.plt section. */
2888 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
2889 rela.r_addend = 0;
2890 }
2891
2892 loc = relplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
e23eba97
NC
2893 bed->s->swap_reloca_out (output_bfd, &rela, loc);
2894
2895 if (!h->def_regular)
2896 {
2897 /* Mark the symbol as undefined, rather than as defined in
2898 the .plt section. Leave the value alone. */
2899 sym->st_shndx = SHN_UNDEF;
2900 /* If the symbol is weak, we do need to clear the value.
2901 Otherwise, the PLT entry would provide a definition for
2902 the symbol even if the symbol wasn't defined anywhere,
2903 and so the symbol would never be NULL. */
2904 if (!h->ref_regular_nonweak)
2905 sym->st_value = 0;
2906 }
2907 }
2908
2909 if (h->got.offset != (bfd_vma) -1
6487709f
JW
2910 && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE))
2911 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
e23eba97
NC
2912 {
2913 asection *sgot;
2914 asection *srela;
2915 Elf_Internal_Rela rela;
51a8a7c2 2916 bfd_boolean use_elf_append_rela = TRUE;
e23eba97
NC
2917
2918 /* This symbol has an entry in the GOT. Set it up. */
2919
2920 sgot = htab->elf.sgot;
2921 srela = htab->elf.srelgot;
2922 BFD_ASSERT (sgot != NULL && srela != NULL);
2923
2924 rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
2925
02dd9d25
NC
2926 /* Handle the ifunc symbol in GOT entry. */
2927 if (h->def_regular
2928 && h->type == STT_GNU_IFUNC)
2929 {
2930 if (h->plt.offset == (bfd_vma) -1)
2931 {
2932 /* STT_GNU_IFUNC is referenced without PLT. */
51a8a7c2 2933
02dd9d25
NC
2934 if (htab->elf.splt == NULL)
2935 {
51a8a7c2 2936 /* Use .rela.iplt section to store .got relocations
02dd9d25
NC
2937 in static executable. */
2938 srela = htab->elf.irelplt;
51a8a7c2
NC
2939
2940 /* Do not use riscv_elf_append_rela to add dynamic
2941 relocs. */
2942 use_elf_append_rela = FALSE;
02dd9d25 2943 }
51a8a7c2 2944
02dd9d25
NC
2945 if (SYMBOL_REFERENCES_LOCAL (info, h))
2946 {
2947 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2948 h->root.root.string,
2949 h->root.u.def.section->owner);
2950
2951 rela.r_info = ELFNN_R_INFO (0, R_RISCV_IRELATIVE);
2952 rela.r_addend = (h->root.u.def.value
2953 + h->root.u.def.section->output_section->vma
2954 + h->root.u.def.section->output_offset);
2955 }
2956 else
2957 {
2958 /* Generate R_RISCV_NN. */
2959 BFD_ASSERT((h->got.offset & 1) == 0);
2960 BFD_ASSERT (h->dynindx != -1);
2961 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
2962 rela.r_addend = 0;
2963 }
2964 }
2965 else if (bfd_link_pic (info))
2966 {
2967 /* Generate R_RISCV_NN. */
2968 BFD_ASSERT((h->got.offset & 1) == 0);
2969 BFD_ASSERT (h->dynindx != -1);
2970 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
2971 rela.r_addend = 0;
2972 }
2973 else
2974 {
2975 asection *plt;
2976
2977 if (!h->pointer_equality_needed)
2978 abort ();
2979
2980 /* For non-shared object, we can't use .got.plt, which
2981 contains the real function address if we need pointer
2982 equality. We load the GOT entry with the PLT entry. */
2983 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2984 bfd_put_NN (output_bfd, (plt->output_section->vma
2985 + plt->output_offset
2986 + h->plt.offset),
2987 htab->elf.sgot->contents
2988 + (h->got.offset & ~(bfd_vma) 1));
2989 return TRUE;
2990 }
2991 }
02dd9d25
NC
2992 else if (bfd_link_pic (info)
2993 && SYMBOL_REFERENCES_LOCAL (info, h))
e23eba97 2994 {
51a8a7c2
NC
2995 /* If this is a local symbol reference, we just want to emit
2996 a RELATIVE reloc. This can happen if it is a -Bsymbolic link,
2997 or a pie link, or the symbol was forced to be local because
2998 of a version file. The entry in the global offset table will
2999 already have been initialized in the relocate_section function. */
25eb8346 3000 BFD_ASSERT((h->got.offset & 1) != 0);
e23eba97
NC
3001 asection *sec = h->root.u.def.section;
3002 rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
3003 rela.r_addend = (h->root.u.def.value
3004 + sec->output_section->vma
3005 + sec->output_offset);
3006 }
3007 else
3008 {
25eb8346 3009 BFD_ASSERT((h->got.offset & 1) == 0);
e23eba97
NC
3010 BFD_ASSERT (h->dynindx != -1);
3011 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
3012 rela.r_addend = 0;
3013 }
3014
3015 bfd_put_NN (output_bfd, 0,
3016 sgot->contents + (h->got.offset & ~(bfd_vma) 1));
51a8a7c2
NC
3017
3018 if (use_elf_append_rela)
3019 riscv_elf_append_rela (output_bfd, srela, &rela);
3020 else
3021 {
3022 /* Use riscv_elf_append_rela to add the dynamic relocs into
3023 .rela.iplt may cause the overwrite problems. Since we insert
3024 the relocs for PLT didn't handle the reloc_index of .rela.iplt,
3025 but the riscv_elf_append_rela adds the relocs to the place
3026 that are calculated from the reloc_index (in seqential).
3027
3028 One solution is that add these dynamic relocs (GOT IFUNC)
3029 from the last of .rela.iplt section. */
3030 bfd_vma iplt_idx = htab->last_iplt_index--;
3031 bfd_byte *loc = srela->contents
3032 + iplt_idx * sizeof (ElfNN_External_Rela);
3033 bed->s->swap_reloca_out (output_bfd, &rela, loc);
3034 }
e23eba97
NC
3035 }
3036
3037 if (h->needs_copy)
3038 {
3039 Elf_Internal_Rela rela;
5474d94f 3040 asection *s;
e23eba97
NC
3041
3042 /* This symbols needs a copy reloc. Set it up. */
3043 BFD_ASSERT (h->dynindx != -1);
3044
3045 rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
3046 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
3047 rela.r_addend = 0;
afbf7e8e 3048 if (h->root.u.def.section == htab->elf.sdynrelro)
5474d94f
AM
3049 s = htab->elf.sreldynrelro;
3050 else
3051 s = htab->elf.srelbss;
3052 riscv_elf_append_rela (output_bfd, s, &rela);
e23eba97
NC
3053 }
3054
3055 /* Mark some specially defined symbols as absolute. */
3056 if (h == htab->elf.hdynamic
3057 || (h == htab->elf.hgot || h == htab->elf.hplt))
3058 sym->st_shndx = SHN_ABS;
3059
3060 return TRUE;
3061}
3062
02dd9d25
NC
3063/* Finish up local dynamic symbol handling. We set the contents of
3064 various dynamic sections here. */
3065
3066static bfd_boolean
3067riscv_elf_finish_local_dynamic_symbol (void **slot, void *inf)
3068{
3069 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) *slot;
3070 struct bfd_link_info *info = (struct bfd_link_info *) inf;
3071
3072 return riscv_elf_finish_dynamic_symbol (info->output_bfd, info, h, NULL);
3073}
3074
e23eba97
NC
3075/* Finish up the dynamic sections. */
3076
3077static bfd_boolean
3078riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
3079 bfd *dynobj, asection *sdyn)
3080{
3081 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
3082 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
3083 size_t dynsize = bed->s->sizeof_dyn;
3084 bfd_byte *dyncon, *dynconend;
3085
3086 dynconend = sdyn->contents + sdyn->size;
3087 for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
3088 {
3089 Elf_Internal_Dyn dyn;
3090 asection *s;
3091
3092 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
3093
3094 switch (dyn.d_tag)
3095 {
3096 case DT_PLTGOT:
3097 s = htab->elf.sgotplt;
3098 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3099 break;
3100 case DT_JMPREL:
3101 s = htab->elf.srelplt;
3102 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3103 break;
3104 case DT_PLTRELSZ:
3105 s = htab->elf.srelplt;
3106 dyn.d_un.d_val = s->size;
3107 break;
3108 default:
3109 continue;
3110 }
3111
3112 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
3113 }
3114 return TRUE;
3115}
3116
3117static bfd_boolean
3118riscv_elf_finish_dynamic_sections (bfd *output_bfd,
3119 struct bfd_link_info *info)
3120{
3121 bfd *dynobj;
3122 asection *sdyn;
3123 struct riscv_elf_link_hash_table *htab;
3124
3125 htab = riscv_elf_hash_table (info);
3126 BFD_ASSERT (htab != NULL);
3127 dynobj = htab->elf.dynobj;
3128
3129 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
3130
3131 if (elf_hash_table (info)->dynamic_sections_created)
3132 {
3133 asection *splt;
3134 bfd_boolean ret;
3135
3136 splt = htab->elf.splt;
3137 BFD_ASSERT (splt != NULL && sdyn != NULL);
3138
3139 ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
3140
535b785f 3141 if (!ret)
e23eba97
NC
3142 return ret;
3143
3144 /* Fill in the head and tail entries in the procedure linkage table. */
3145 if (splt->size > 0)
3146 {
3147 int i;
3148 uint32_t plt_header[PLT_HEADER_INSNS];
5ef23793
JW
3149 ret = riscv_make_plt_header (output_bfd,
3150 sec_addr (htab->elf.sgotplt),
3151 sec_addr (splt), plt_header);
3152 if (!ret)
3153 return ret;
e23eba97
NC
3154
3155 for (i = 0; i < PLT_HEADER_INSNS; i++)
3156 bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i);
e23eba97 3157
cc162427
AW
3158 elf_section_data (splt->output_section)->this_hdr.sh_entsize
3159 = PLT_ENTRY_SIZE;
3160 }
e23eba97
NC
3161 }
3162
3163 if (htab->elf.sgotplt)
3164 {
3165 asection *output_section = htab->elf.sgotplt->output_section;
3166
3167 if (bfd_is_abs_section (output_section))
3168 {
3169 (*_bfd_error_handler)
871b3ab2 3170 (_("discarded output section: `%pA'"), htab->elf.sgotplt);
e23eba97
NC
3171 return FALSE;
3172 }
3173
3174 if (htab->elf.sgotplt->size > 0)
3175 {
3176 /* Write the first two entries in .got.plt, needed for the dynamic
3177 linker. */
3178 bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
3179 bfd_put_NN (output_bfd, (bfd_vma) 0,
3180 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
3181 }
3182
3183 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3184 }
3185
3186 if (htab->elf.sgot)
3187 {
3188 asection *output_section = htab->elf.sgot->output_section;
3189
3190 if (htab->elf.sgot->size > 0)
3191 {
3192 /* Set the first entry in the global offset table to the address of
3193 the dynamic section. */
3194 bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
3195 bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
3196 }
3197
3198 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
3199 }
3200
02dd9d25
NC
3201 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
3202 htab_traverse (htab->loc_hash_table,
3203 riscv_elf_finish_local_dynamic_symbol,
3204 info);
3205
e23eba97
NC
3206 return TRUE;
3207}
3208
3209/* Return address for Ith PLT stub in section PLT, for relocation REL
3210 or (bfd_vma) -1 if it should not be included. */
3211
3212static bfd_vma
3213riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
3214 const arelent *rel ATTRIBUTE_UNUSED)
3215{
3216 return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
3217}
3218
3219static enum elf_reloc_type_class
3220riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3221 const asection *rel_sec ATTRIBUTE_UNUSED,
3222 const Elf_Internal_Rela *rela)
3223{
3224 switch (ELFNN_R_TYPE (rela->r_info))
3225 {
3226 case R_RISCV_RELATIVE:
3227 return reloc_class_relative;
3228 case R_RISCV_JUMP_SLOT:
3229 return reloc_class_plt;
3230 case R_RISCV_COPY:
3231 return reloc_class_copy;
3232 default:
3233 return reloc_class_normal;
3234 }
3235}
3236
0242af40
JW
3237/* Given the ELF header flags in FLAGS, it returns a string that describes the
3238 float ABI. */
3239
3240static const char *
3241riscv_float_abi_string (flagword flags)
3242{
3243 switch (flags & EF_RISCV_FLOAT_ABI)
3244 {
3245 case EF_RISCV_FLOAT_ABI_SOFT:
3246 return "soft-float";
3247 break;
3248 case EF_RISCV_FLOAT_ABI_SINGLE:
3249 return "single-float";
3250 break;
3251 case EF_RISCV_FLOAT_ABI_DOUBLE:
3252 return "double-float";
3253 break;
3254 case EF_RISCV_FLOAT_ABI_QUAD:
3255 return "quad-float";
3256 break;
3257 default:
3258 abort ();
3259 }
3260}
3261
7d7a7d7c
JW
3262/* The information of architecture attribute. */
3263static riscv_subset_list_t in_subsets;
3264static riscv_subset_list_t out_subsets;
3265static riscv_subset_list_t merged_subsets;
3266
3267/* Predicator for standard extension. */
3268
3269static bfd_boolean
3270riscv_std_ext_p (const char *name)
3271{
3272 return (strlen (name) == 1) && (name[0] != 'x') && (name[0] != 's');
3273}
3274
32f0ce4d 3275/* Check if the versions are compatible. */
7d7a7d7c 3276
32f0ce4d 3277static bfd_boolean
7d7a7d7c
JW
3278riscv_version_mismatch (bfd *ibfd,
3279 struct riscv_subset_t *in,
3280 struct riscv_subset_t *out)
3281{
32f0ce4d
NC
3282 if (in == NULL || out == NULL)
3283 return TRUE;
3284
3285 /* Since there are no version conflicts for now, we just report
3286 warning when the versions are mis-matched. */
3287 if (in->major_version != out->major_version
3288 || in->minor_version != out->minor_version)
3289 {
3290 _bfd_error_handler
3291 (_("warning: %pB: mis-matched ISA version %d.%d for '%s' "
3292 "extension, the output version is %d.%d"),
3293 ibfd,
3294 in->major_version,
3295 in->minor_version,
3296 in->name,
3297 out->major_version,
3298 out->minor_version);
3299
3300 /* Update the output ISA versions to the newest ones. */
3301 if ((in->major_version > out->major_version)
3302 || (in->major_version == out->major_version
3303 && in->minor_version > out->minor_version))
3304 {
3305 out->major_version = in->major_version;
3306 out->minor_version = in->minor_version;
3307 }
3308 }
3309
3310 return TRUE;
7d7a7d7c
JW
3311}
3312
3313/* Return true if subset is 'i' or 'e'. */
3314
3315static bfd_boolean
3316riscv_i_or_e_p (bfd *ibfd,
3317 const char *arch,
3318 struct riscv_subset_t *subset)
3319{
3320 if ((strcasecmp (subset->name, "e") != 0)
3321 && (strcasecmp (subset->name, "i") != 0))
3322 {
3323 _bfd_error_handler
9184ef8a
NC
3324 (_("error: %pB: corrupted ISA string '%s'. "
3325 "First letter should be 'i' or 'e' but got '%s'"),
7d7a7d7c
JW
3326 ibfd, arch, subset->name);
3327 return FALSE;
3328 }
3329 return TRUE;
3330}
3331
3332/* Merge standard extensions.
3333
3334 Return Value:
3335 Return FALSE if failed to merge.
3336
3337 Arguments:
3338 `bfd`: bfd handler.
3339 `in_arch`: Raw arch string for input object.
3340 `out_arch`: Raw arch string for output object.
3341 `pin`: subset list for input object, and it'll skip all merged subset after
3342 merge.
3343 `pout`: Like `pin`, but for output object. */
3344
3345static bfd_boolean
3346riscv_merge_std_ext (bfd *ibfd,
3347 const char *in_arch,
3348 const char *out_arch,
3349 struct riscv_subset_t **pin,
3350 struct riscv_subset_t **pout)
3351{
3352 const char *standard_exts = riscv_supported_std_ext ();
3353 const char *p;
3354 struct riscv_subset_t *in = *pin;
3355 struct riscv_subset_t *out = *pout;
3356
3357 /* First letter should be 'i' or 'e'. */
3358 if (!riscv_i_or_e_p (ibfd, in_arch, in))
3359 return FALSE;
3360
3361 if (!riscv_i_or_e_p (ibfd, out_arch, out))
3362 return FALSE;
3363
8f595e9b 3364 if (strcasecmp (in->name, out->name) != 0)
7d7a7d7c
JW
3365 {
3366 /* TODO: We might allow merge 'i' with 'e'. */
3367 _bfd_error_handler
9184ef8a 3368 (_("error: %pB: mis-matched ISA string to merge '%s' and '%s'"),
7d7a7d7c
JW
3369 ibfd, in->name, out->name);
3370 return FALSE;
3371 }
32f0ce4d
NC
3372 else if (!riscv_version_mismatch (ibfd, in, out))
3373 return FALSE;
7d7a7d7c
JW
3374 else
3375 riscv_add_subset (&merged_subsets,
32f0ce4d 3376 out->name, out->major_version, out->minor_version);
7d7a7d7c
JW
3377
3378 in = in->next;
3379 out = out->next;
3380
3381 /* Handle standard extension first. */
3382 for (p = standard_exts; *p; ++p)
3383 {
3384 char find_ext[2] = {*p, '\0'};
3385 struct riscv_subset_t *find_in =
3386 riscv_lookup_subset (&in_subsets, find_ext);
3387 struct riscv_subset_t *find_out =
3388 riscv_lookup_subset (&out_subsets, find_ext);
3389
3390 if (find_in == NULL && find_out == NULL)
3391 continue;
3392
32f0ce4d
NC
3393 if (!riscv_version_mismatch (ibfd, find_in, find_out))
3394 return FALSE;
7d7a7d7c 3395
32f0ce4d 3396 struct riscv_subset_t *merged = find_out ? find_out : find_in;
7d7a7d7c
JW
3397 riscv_add_subset (&merged_subsets, merged->name,
3398 merged->major_version, merged->minor_version);
3399 }
3400
3401 /* Skip all standard extensions. */
3402 while ((in != NULL) && riscv_std_ext_p (in->name)) in = in->next;
3403 while ((out != NULL) && riscv_std_ext_p (out->name)) out = out->next;
3404
3405 *pin = in;
3406 *pout = out;
3407
3408 return TRUE;
3409}
3410
403d1bd9
JW
3411/* If C is a prefix class, then return the EXT string without the prefix.
3412 Otherwise return the entire EXT string. */
7d7a7d7c 3413
403d1bd9
JW
3414static const char *
3415riscv_skip_prefix (const char *ext, riscv_isa_ext_class_t c)
3416{
3417 switch (c)
3418 {
3419 case RV_ISA_CLASS_X: return &ext[1];
3420 case RV_ISA_CLASS_S: return &ext[1];
3421 case RV_ISA_CLASS_Z: return &ext[1];
3422 default: return ext;
3423 }
3424}
3425
3426/* Compare prefixed extension names canonically. */
3427
3428static int
3429riscv_prefix_cmp (const char *a, const char *b)
3430{
3431 riscv_isa_ext_class_t ca = riscv_get_prefix_class (a);
3432 riscv_isa_ext_class_t cb = riscv_get_prefix_class (b);
3433
3434 /* Extension name without prefix */
3435 const char *anp = riscv_skip_prefix (a, ca);
3436 const char *bnp = riscv_skip_prefix (b, cb);
3437
3438 if (ca == cb)
3439 return strcasecmp (anp, bnp);
3440
3441 return (int)ca - (int)cb;
3442}
3443
3444/* Merge multi letter extensions. PIN is a pointer to the head of the input
3445 object subset list. Likewise for POUT and the output object. Return TRUE
3446 on success and FALSE when a conflict is found. */
7d7a7d7c
JW
3447
3448static bfd_boolean
403d1bd9
JW
3449riscv_merge_multi_letter_ext (bfd *ibfd,
3450 riscv_subset_t **pin,
3451 riscv_subset_t **pout)
7d7a7d7c
JW
3452{
3453 riscv_subset_t *in = *pin;
3454 riscv_subset_t *out = *pout;
403d1bd9 3455 riscv_subset_t *tail;
7d7a7d7c 3456
403d1bd9 3457 int cmp;
7d7a7d7c 3458
403d1bd9 3459 while (in && out)
7d7a7d7c 3460 {
403d1bd9
JW
3461 cmp = riscv_prefix_cmp (in->name, out->name);
3462
3463 if (cmp < 0)
3464 {
3465 /* `in' comes before `out', append `in' and increment. */
3466 riscv_add_subset (&merged_subsets, in->name, in->major_version,
3467 in->minor_version);
3468 in = in->next;
3469 }
3470 else if (cmp > 0)
3471 {
3472 /* `out' comes before `in', append `out' and increment. */
3473 riscv_add_subset (&merged_subsets, out->name, out->major_version,
3474 out->minor_version);
3475 out = out->next;
3476 }
3477 else
7d7a7d7c 3478 {
403d1bd9 3479 /* Both present, check version and increment both. */
32f0ce4d
NC
3480 if (!riscv_version_mismatch (ibfd, in, out))
3481 return FALSE;
403d1bd9
JW
3482
3483 riscv_add_subset (&merged_subsets, out->name, out->major_version,
3484 out->minor_version);
3485 out = out->next;
3486 in = in->next;
7d7a7d7c 3487 }
7d7a7d7c
JW
3488 }
3489
403d1bd9
JW
3490 if (in || out) {
3491 /* If we're here, either `in' or `out' is running longer than
3492 the other. So, we need to append the corresponding tail. */
3493 tail = in ? in : out;
3494
3495 while (tail)
3496 {
3497 riscv_add_subset (&merged_subsets, tail->name, tail->major_version,
3498 tail->minor_version);
3499 tail = tail->next;
3500 }
3501 }
3502
7d7a7d7c
JW
3503 return TRUE;
3504}
3505
3506/* Merge Tag_RISCV_arch attribute. */
3507
3508static char *
3509riscv_merge_arch_attr_info (bfd *ibfd, char *in_arch, char *out_arch)
3510{
3511 riscv_subset_t *in, *out;
3512 char *merged_arch_str;
3513
3514 unsigned xlen_in, xlen_out;
3515 merged_subsets.head = NULL;
3516 merged_subsets.tail = NULL;
3517
3518 riscv_parse_subset_t rpe_in;
3519 riscv_parse_subset_t rpe_out;
3520
8f595e9b
NC
3521 /* Only assembler needs to check the default version of ISA, so just set
3522 the rpe_in.get_default_version and rpe_out.get_default_version to NULL. */
7d7a7d7c
JW
3523 rpe_in.subset_list = &in_subsets;
3524 rpe_in.error_handler = _bfd_error_handler;
3525 rpe_in.xlen = &xlen_in;
8f595e9b 3526 rpe_in.get_default_version = NULL;
7d7a7d7c
JW
3527
3528 rpe_out.subset_list = &out_subsets;
3529 rpe_out.error_handler = _bfd_error_handler;
3530 rpe_out.xlen = &xlen_out;
8f595e9b 3531 rpe_out.get_default_version = NULL;
7d7a7d7c
JW
3532
3533 if (in_arch == NULL && out_arch == NULL)
3534 return NULL;
3535
3536 if (in_arch == NULL && out_arch != NULL)
3537 return out_arch;
3538
3539 if (in_arch != NULL && out_arch == NULL)
3540 return in_arch;
3541
3542 /* Parse subset from arch string. */
3543 if (!riscv_parse_subset (&rpe_in, in_arch))
3544 return NULL;
3545
3546 if (!riscv_parse_subset (&rpe_out, out_arch))
3547 return NULL;
3548
3549 /* Checking XLEN. */
3550 if (xlen_out != xlen_in)
3551 {
3552 _bfd_error_handler
3553 (_("error: %pB: ISA string of input (%s) doesn't match "
9184ef8a 3554 "output (%s)"), ibfd, in_arch, out_arch);
7d7a7d7c
JW
3555 return NULL;
3556 }
3557
3558 /* Merge subset list. */
3559 in = in_subsets.head;
3560 out = out_subsets.head;
3561
3562 /* Merge standard extension. */
3563 if (!riscv_merge_std_ext (ibfd, in_arch, out_arch, &in, &out))
3564 return NULL;
403d1bd9
JW
3565
3566 /* Merge all non-single letter extensions with single call. */
3567 if (!riscv_merge_multi_letter_ext (ibfd, &in, &out))
7d7a7d7c
JW
3568 return NULL;
3569
3570 if (xlen_in != xlen_out)
3571 {
3572 _bfd_error_handler
3573 (_("error: %pB: XLEN of input (%u) doesn't match "
9184ef8a 3574 "output (%u)"), ibfd, xlen_in, xlen_out);
7d7a7d7c
JW
3575 return NULL;
3576 }
3577
3578 if (xlen_in != ARCH_SIZE)
3579 {
3580 _bfd_error_handler
9184ef8a
NC
3581 (_("error: %pB: unsupported XLEN (%u), you might be "
3582 "using wrong emulation"), ibfd, xlen_in);
7d7a7d7c
JW
3583 return NULL;
3584 }
3585
3586 merged_arch_str = riscv_arch_str (ARCH_SIZE, &merged_subsets);
3587
3588 /* Release the subset lists. */
3589 riscv_release_subset_list (&in_subsets);
3590 riscv_release_subset_list (&out_subsets);
3591 riscv_release_subset_list (&merged_subsets);
3592
3593 return merged_arch_str;
3594}
3595
3596/* Merge object attributes from IBFD into output_bfd of INFO.
3597 Raise an error if there are conflicting attributes. */
3598
3599static bfd_boolean
3600riscv_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
3601{
3602 bfd *obfd = info->output_bfd;
3603 obj_attribute *in_attr;
3604 obj_attribute *out_attr;
3605 bfd_boolean result = TRUE;
cbd7581f 3606 bfd_boolean priv_attrs_merged = FALSE;
7d7a7d7c
JW
3607 const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
3608 unsigned int i;
3609
3610 /* Skip linker created files. */
3611 if (ibfd->flags & BFD_LINKER_CREATED)
3612 return TRUE;
3613
3614 /* Skip any input that doesn't have an attribute section.
3615 This enables to link object files without attribute section with
3616 any others. */
3617 if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
3618 return TRUE;
3619
3620 if (!elf_known_obj_attributes_proc (obfd)[0].i)
3621 {
3622 /* This is the first object. Copy the attributes. */
3623 _bfd_elf_copy_obj_attributes (ibfd, obfd);
3624
3625 out_attr = elf_known_obj_attributes_proc (obfd);
3626
3627 /* Use the Tag_null value to indicate the attributes have been
3628 initialized. */
3629 out_attr[0].i = 1;
3630
3631 return TRUE;
3632 }
3633
3634 in_attr = elf_known_obj_attributes_proc (ibfd);
3635 out_attr = elf_known_obj_attributes_proc (obfd);
3636
3637 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
3638 {
3639 switch (i)
3640 {
3641 case Tag_RISCV_arch:
3642 if (!out_attr[Tag_RISCV_arch].s)
3643 out_attr[Tag_RISCV_arch].s = in_attr[Tag_RISCV_arch].s;
3644 else if (in_attr[Tag_RISCV_arch].s
3645 && out_attr[Tag_RISCV_arch].s)
3646 {
3647 /* Check arch compatible. */
3648 char *merged_arch =
3649 riscv_merge_arch_attr_info (ibfd,
3650 in_attr[Tag_RISCV_arch].s,
3651 out_attr[Tag_RISCV_arch].s);
3652 if (merged_arch == NULL)
3653 {
3654 result = FALSE;
3655 out_attr[Tag_RISCV_arch].s = "";
3656 }
3657 else
3658 out_attr[Tag_RISCV_arch].s = merged_arch;
3659 }
3660 break;
41285764 3661
7d7a7d7c
JW
3662 case Tag_RISCV_priv_spec:
3663 case Tag_RISCV_priv_spec_minor:
3664 case Tag_RISCV_priv_spec_revision:
cbd7581f
NC
3665 /* If we have handled the priv attributes, then skip it. */
3666 if (!priv_attrs_merged)
41285764 3667 {
cbd7581f
NC
3668 unsigned int Tag_a = Tag_RISCV_priv_spec;
3669 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
3670 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
39ff0b81
NC
3671 enum riscv_priv_spec_class in_priv_spec;
3672 enum riscv_priv_spec_class out_priv_spec;
3673
3674 /* Get the priv spec class from elf attribute numbers. */
3675 riscv_get_priv_spec_class_from_numbers (in_attr[Tag_a].i,
3676 in_attr[Tag_b].i,
3677 in_attr[Tag_c].i,
3678 &in_priv_spec);
3679 riscv_get_priv_spec_class_from_numbers (out_attr[Tag_a].i,
3680 out_attr[Tag_b].i,
3681 out_attr[Tag_c].i,
3682 &out_priv_spec);
cbd7581f
NC
3683
3684 /* Allow to link the object without the priv specs. */
39ff0b81 3685 if (out_priv_spec == PRIV_SPEC_CLASS_NONE)
cbd7581f
NC
3686 {
3687 out_attr[Tag_a].i = in_attr[Tag_a].i;
3688 out_attr[Tag_b].i = in_attr[Tag_b].i;
3689 out_attr[Tag_c].i = in_attr[Tag_c].i;
3690 }
39ff0b81
NC
3691 else if (in_priv_spec != PRIV_SPEC_CLASS_NONE
3692 && in_priv_spec != out_priv_spec)
cbd7581f
NC
3693 {
3694 _bfd_error_handler
39ff0b81 3695 (_("warning: %pB use privilege spec version %u.%u.%u but "
9184ef8a 3696 "the output use version %u.%u.%u"),
cbd7581f
NC
3697 ibfd,
3698 in_attr[Tag_a].i,
3699 in_attr[Tag_b].i,
3700 in_attr[Tag_c].i,
3701 out_attr[Tag_a].i,
3702 out_attr[Tag_b].i,
3703 out_attr[Tag_c].i);
39ff0b81 3704
9184ef8a 3705 /* The priv spec v1.9.1 can not be linked with other spec
39ff0b81
NC
3706 versions since the conflicts. We plan to drop the
3707 v1.9.1 in a year or two, so this confict should be
3708 removed in the future. */
3709 if (in_priv_spec == PRIV_SPEC_CLASS_1P9P1
3710 || out_priv_spec == PRIV_SPEC_CLASS_1P9P1)
3711 {
3712 _bfd_error_handler
3713 (_("warning: privilege spec version 1.9.1 can not be "
9184ef8a 3714 "linked with other spec versions"));
39ff0b81
NC
3715 }
3716
9184ef8a 3717 /* Update the output priv spec to the newest one. */
39ff0b81
NC
3718 if (in_priv_spec > out_priv_spec)
3719 {
3720 out_attr[Tag_a].i = in_attr[Tag_a].i;
3721 out_attr[Tag_b].i = in_attr[Tag_b].i;
3722 out_attr[Tag_c].i = in_attr[Tag_c].i;
3723 }
cbd7581f
NC
3724 }
3725 priv_attrs_merged = TRUE;
7d7a7d7c
JW
3726 }
3727 break;
41285764 3728
7d7a7d7c
JW
3729 case Tag_RISCV_unaligned_access:
3730 out_attr[i].i |= in_attr[i].i;
3731 break;
41285764 3732
7d7a7d7c
JW
3733 case Tag_RISCV_stack_align:
3734 if (out_attr[i].i == 0)
3735 out_attr[i].i = in_attr[i].i;
3736 else if (in_attr[i].i != 0
3737 && out_attr[i].i != 0
3738 && out_attr[i].i != in_attr[i].i)
3739 {
3740 _bfd_error_handler
3741 (_("error: %pB use %u-byte stack aligned but the output "
9184ef8a 3742 "use %u-byte stack aligned"),
7d7a7d7c
JW
3743 ibfd, in_attr[i].i, out_attr[i].i);
3744 result = FALSE;
3745 }
3746 break;
41285764 3747
7d7a7d7c
JW
3748 default:
3749 result &= _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
3750 }
3751
3752 /* If out_attr was copied from in_attr then it won't have a type yet. */
3753 if (in_attr[i].type && !out_attr[i].type)
3754 out_attr[i].type = in_attr[i].type;
3755 }
3756
3757 /* Merge Tag_compatibility attributes and any common GNU ones. */
3758 if (!_bfd_elf_merge_object_attributes (ibfd, info))
3759 return FALSE;
3760
3761 /* Check for any attributes not known on RISC-V. */
3762 result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
3763
3764 return result;
3765}
3766
e23eba97
NC
3767/* Merge backend specific data from an object file to the output
3768 object file when linking. */
3769
3770static bfd_boolean
3771_bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3772{
3773 bfd *obfd = info->output_bfd;
87f98bac 3774 flagword new_flags, old_flags;
e23eba97
NC
3775
3776 if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
3777 return TRUE;
3778
3779 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
3780 {
3781 (*_bfd_error_handler)
871b3ab2 3782 (_("%pB: ABI is incompatible with that of the selected emulation:\n"
96b0927d
PD
3783 " target emulation `%s' does not match `%s'"),
3784 ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
e23eba97
NC
3785 return FALSE;
3786 }
3787
3788 if (!_bfd_elf_merge_object_attributes (ibfd, info))
3789 return FALSE;
3790
7d7a7d7c
JW
3791 if (!riscv_merge_attributes (ibfd, info))
3792 return FALSE;
3793
87f98bac
JW
3794 new_flags = elf_elfheader (ibfd)->e_flags;
3795 old_flags = elf_elfheader (obfd)->e_flags;
3796
e23eba97
NC
3797 if (! elf_flags_init (obfd))
3798 {
3799 elf_flags_init (obfd) = TRUE;
3800 elf_elfheader (obfd)->e_flags = new_flags;
3801 return TRUE;
3802 }
3803
87f98bac
JW
3804 /* Check to see if the input BFD actually contains any sections. If not,
3805 its flags may not have been initialized either, but it cannot actually
3806 cause any incompatibility. Do not short-circuit dynamic objects; their
3807 section list may be emptied by elf_link_add_object_symbols.
3808
3809 Also check to see if there are no code sections in the input. In this
3810 case, there is no need to check for code specific flags. */
3811 if (!(ibfd->flags & DYNAMIC))
3812 {
3813 bfd_boolean null_input_bfd = TRUE;
3814 bfd_boolean only_data_sections = TRUE;
3815 asection *sec;
3816
3817 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
3818 {
fd361982 3819 if ((bfd_section_flags (sec)
87f98bac
JW
3820 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3821 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
3822 only_data_sections = FALSE;
3823
3824 null_input_bfd = FALSE;
3825 break;
3826 }
3827
3828 if (null_input_bfd || only_data_sections)
3829 return TRUE;
3830 }
3831
2922d21d
AW
3832 /* Disallow linking different float ABIs. */
3833 if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
e23eba97
NC
3834 {
3835 (*_bfd_error_handler)
0242af40
JW
3836 (_("%pB: can't link %s modules with %s modules"), ibfd,
3837 riscv_float_abi_string (new_flags),
3838 riscv_float_abi_string (old_flags));
e23eba97
NC
3839 goto fail;
3840 }
3841
7f999549
JW
3842 /* Disallow linking RVE and non-RVE. */
3843 if ((old_flags ^ new_flags) & EF_RISCV_RVE)
3844 {
3845 (*_bfd_error_handler)
3846 (_("%pB: can't link RVE with other target"), ibfd);
3847 goto fail;
3848 }
3849
e23eba97
NC
3850 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
3851 elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
3852
3853 return TRUE;
3854
dc1e8a47 3855 fail:
e23eba97
NC
3856 bfd_set_error (bfd_error_bad_value);
3857 return FALSE;
3858}
3859
3860/* Delete some bytes from a section while relaxing. */
3861
3862static bfd_boolean
7f02625e
JW
3863riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count,
3864 struct bfd_link_info *link_info)
e23eba97
NC
3865{
3866 unsigned int i, symcount;
3867 bfd_vma toaddr = sec->size;
3868 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
3869 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3870 unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3871 struct bfd_elf_section_data *data = elf_section_data (sec);
3872 bfd_byte *contents = data->this_hdr.contents;
3873
3874 /* Actually delete the bytes. */
3875 sec->size -= count;
3876 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
3877
3878 /* Adjust the location of all of the relocs. Note that we need not
3879 adjust the addends, since all PC-relative references must be against
3880 symbols, which we will adjust below. */
3881 for (i = 0; i < sec->reloc_count; i++)
3882 if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
3883 data->relocs[i].r_offset -= count;
3884
3885 /* Adjust the local symbols defined in this section. */
3886 for (i = 0; i < symtab_hdr->sh_info; i++)
3887 {
3888 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
3889 if (sym->st_shndx == sec_shndx)
3890 {
3891 /* If the symbol is in the range of memory we just moved, we
3892 have to adjust its value. */
3893 if (sym->st_value > addr && sym->st_value <= toaddr)
3894 sym->st_value -= count;
3895
3896 /* If the symbol *spans* the bytes we just deleted (i.e. its
3897 *end* is in the moved bytes but its *start* isn't), then we
788af978
JW
3898 must adjust its size.
3899
3900 This test needs to use the original value of st_value, otherwise
3901 we might accidentally decrease size when deleting bytes right
3902 before the symbol. But since deleted relocs can't span across
3903 symbols, we can't have both a st_value and a st_size decrease,
3904 so it is simpler to just use an else. */
3905 else if (sym->st_value <= addr
3906 && sym->st_value + sym->st_size > addr
3907 && sym->st_value + sym->st_size <= toaddr)
e23eba97
NC
3908 sym->st_size -= count;
3909 }
3910 }
3911
3912 /* Now adjust the global symbols defined in this section. */
3913 symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
3914 - symtab_hdr->sh_info);
3915
3916 for (i = 0; i < symcount; i++)
3917 {
3918 struct elf_link_hash_entry *sym_hash = sym_hashes[i];
3919
7f02625e
JW
3920 /* The '--wrap SYMBOL' option is causing a pain when the object file,
3921 containing the definition of __wrap_SYMBOL, includes a direct
3922 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
3923 the same symbol (which is __wrap_SYMBOL), but still exist as two
3924 different symbols in 'sym_hashes', we don't want to adjust
137b5cbd
JW
3925 the global symbol __wrap_SYMBOL twice. */
3926 /* The same problem occurs with symbols that are versioned_hidden, as
3927 foo becomes an alias for foo@BAR, and hence they need the same
3928 treatment. */
3929 if (link_info->wrap_hash != NULL
3930 || sym_hash->versioned == versioned_hidden)
7f02625e
JW
3931 {
3932 struct elf_link_hash_entry **cur_sym_hashes;
3933
3934 /* Loop only over the symbols which have already been checked. */
3935 for (cur_sym_hashes = sym_hashes; cur_sym_hashes < &sym_hashes[i];
3936 cur_sym_hashes++)
3937 {
3938 /* If the current symbol is identical to 'sym_hash', that means
3939 the symbol was already adjusted (or at least checked). */
3940 if (*cur_sym_hashes == sym_hash)
3941 break;
3942 }
3943 /* Don't adjust the symbol again. */
3944 if (cur_sym_hashes < &sym_hashes[i])
3945 continue;
3946 }
3947
e23eba97
NC
3948 if ((sym_hash->root.type == bfd_link_hash_defined
3949 || sym_hash->root.type == bfd_link_hash_defweak)
3950 && sym_hash->root.u.def.section == sec)
3951 {
3952 /* As above, adjust the value if needed. */
3953 if (sym_hash->root.u.def.value > addr
3954 && sym_hash->root.u.def.value <= toaddr)
3955 sym_hash->root.u.def.value -= count;
3956
3957 /* As above, adjust the size if needed. */
788af978
JW
3958 else if (sym_hash->root.u.def.value <= addr
3959 && sym_hash->root.u.def.value + sym_hash->size > addr
3960 && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
e23eba97
NC
3961 sym_hash->size -= count;
3962 }
3963 }
3964
3965 return TRUE;
3966}
3967
9d06997a
PD
3968/* A second format for recording PC-relative hi relocations. This stores the
3969 information required to relax them to GP-relative addresses. */
3970
3971typedef struct riscv_pcgp_hi_reloc riscv_pcgp_hi_reloc;
3972struct riscv_pcgp_hi_reloc
3973{
3974 bfd_vma hi_sec_off;
3975 bfd_vma hi_addend;
3976 bfd_vma hi_addr;
3977 unsigned hi_sym;
3978 asection *sym_sec;
9d1da81b 3979 bfd_boolean undefined_weak;
9d06997a
PD
3980 riscv_pcgp_hi_reloc *next;
3981};
3982
3983typedef struct riscv_pcgp_lo_reloc riscv_pcgp_lo_reloc;
3984struct riscv_pcgp_lo_reloc
3985{
3986 bfd_vma hi_sec_off;
3987 riscv_pcgp_lo_reloc *next;
3988};
3989
3990typedef struct
3991{
3992 riscv_pcgp_hi_reloc *hi;
3993 riscv_pcgp_lo_reloc *lo;
3994} riscv_pcgp_relocs;
3995
5f9aecea
JW
3996/* Initialize the pcgp reloc info in P. */
3997
9d06997a
PD
3998static bfd_boolean
3999riscv_init_pcgp_relocs (riscv_pcgp_relocs *p)
4000{
4001 p->hi = NULL;
4002 p->lo = NULL;
4003 return TRUE;
4004}
4005
5f9aecea
JW
4006/* Free the pcgp reloc info in P. */
4007
9d06997a
PD
4008static void
4009riscv_free_pcgp_relocs (riscv_pcgp_relocs *p,
4010 bfd *abfd ATTRIBUTE_UNUSED,
4011 asection *sec ATTRIBUTE_UNUSED)
4012{
4013 riscv_pcgp_hi_reloc *c;
4014 riscv_pcgp_lo_reloc *l;
4015
4016 for (c = p->hi; c != NULL;)
4017 {
4018 riscv_pcgp_hi_reloc *next = c->next;
4019 free (c);
4020 c = next;
4021 }
4022
4023 for (l = p->lo; l != NULL;)
4024 {
4025 riscv_pcgp_lo_reloc *next = l->next;
4026 free (l);
4027 l = next;
4028 }
4029}
4030
5f9aecea
JW
4031/* Record pcgp hi part reloc info in P, using HI_SEC_OFF as the lookup index.
4032 The HI_ADDEND, HI_ADDR, HI_SYM, and SYM_SEC args contain info required to
4033 relax the corresponding lo part reloc. */
4034
9d06997a
PD
4035static bfd_boolean
4036riscv_record_pcgp_hi_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off,
4037 bfd_vma hi_addend, bfd_vma hi_addr,
9d1da81b
JW
4038 unsigned hi_sym, asection *sym_sec,
4039 bfd_boolean undefined_weak)
9d06997a
PD
4040{
4041 riscv_pcgp_hi_reloc *new = bfd_malloc (sizeof(*new));
4042 if (!new)
4043 return FALSE;
4044 new->hi_sec_off = hi_sec_off;
4045 new->hi_addend = hi_addend;
4046 new->hi_addr = hi_addr;
4047 new->hi_sym = hi_sym;
4048 new->sym_sec = sym_sec;
9d1da81b 4049 new->undefined_weak = undefined_weak;
9d06997a
PD
4050 new->next = p->hi;
4051 p->hi = new;
4052 return TRUE;
4053}
4054
5f9aecea
JW
4055/* Look up hi part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4056 This is used by a lo part reloc to find the corresponding hi part reloc. */
4057
9d06997a
PD
4058static riscv_pcgp_hi_reloc *
4059riscv_find_pcgp_hi_reloc(riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4060{
4061 riscv_pcgp_hi_reloc *c;
4062
4063 for (c = p->hi; c != NULL; c = c->next)
4064 if (c->hi_sec_off == hi_sec_off)
4065 return c;
4066 return NULL;
4067}
4068
5f9aecea
JW
4069/* Record pcgp lo part reloc info in P, using HI_SEC_OFF as the lookup info.
4070 This is used to record relocs that can't be relaxed. */
9d06997a
PD
4071
4072static bfd_boolean
4073riscv_record_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4074{
4075 riscv_pcgp_lo_reloc *new = bfd_malloc (sizeof(*new));
4076 if (!new)
4077 return FALSE;
4078 new->hi_sec_off = hi_sec_off;
4079 new->next = p->lo;
4080 p->lo = new;
4081 return TRUE;
4082}
4083
5f9aecea
JW
4084/* Look up lo part pcgp reloc info in P, using HI_SEC_OFF as the lookup index.
4085 This is used by a hi part reloc to find the corresponding lo part reloc. */
4086
9d06997a
PD
4087static bfd_boolean
4088riscv_find_pcgp_lo_reloc (riscv_pcgp_relocs *p, bfd_vma hi_sec_off)
4089{
4090 riscv_pcgp_lo_reloc *c;
4091
4092 for (c = p->lo; c != NULL; c = c->next)
4093 if (c->hi_sec_off == hi_sec_off)
4094 return TRUE;
4095 return FALSE;
4096}
4097
45f76423
AW
4098typedef bfd_boolean (*relax_func_t) (bfd *, asection *, asection *,
4099 struct bfd_link_info *,
4100 Elf_Internal_Rela *,
9d06997a 4101 bfd_vma, bfd_vma, bfd_vma, bfd_boolean *,
9d1da81b
JW
4102 riscv_pcgp_relocs *,
4103 bfd_boolean undefined_weak);
45f76423 4104
e23eba97
NC
4105/* Relax AUIPC + JALR into JAL. */
4106
4107static bfd_boolean
4108_bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
4109 struct bfd_link_info *link_info,
4110 Elf_Internal_Rela *rel,
4111 bfd_vma symval,
45f76423
AW
4112 bfd_vma max_alignment,
4113 bfd_vma reserve_size ATTRIBUTE_UNUSED,
9d06997a 4114 bfd_boolean *again,
9d1da81b
JW
4115 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
4116 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
e23eba97
NC
4117{
4118 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
1174d920 4119 bfd_vma foff = symval - (sec_addr (sec) + rel->r_offset);
e23eba97
NC
4120 bfd_boolean near_zero = (symval + RISCV_IMM_REACH/2) < RISCV_IMM_REACH;
4121 bfd_vma auipc, jalr;
4122 int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4123
4124 /* If the call crosses section boundaries, an alignment directive could
c6261a00
JW
4125 cause the PC-relative offset to later increase, so we need to add in the
4126 max alignment of any section inclusive from the call to the target.
4127 Otherwise, we only need to use the alignment of the current section. */
4128 if (VALID_UJTYPE_IMM (foff))
4129 {
4130 if (sym_sec->output_section == sec->output_section
4131 && sym_sec->output_section != bfd_abs_section_ptr)
4132 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
1174d920 4133 foff += ((bfd_signed_vma) foff < 0 ? -max_alignment : max_alignment);
c6261a00 4134 }
e23eba97
NC
4135
4136 /* See if this function call can be shortened. */
4137 if (!VALID_UJTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
4138 return TRUE;
4139
4140 /* Shorten the function call. */
4141 BFD_ASSERT (rel->r_offset + 8 <= sec->size);
4142
4143 auipc = bfd_get_32 (abfd, contents + rel->r_offset);
4144 jalr = bfd_get_32 (abfd, contents + rel->r_offset + 4);
4145 rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
ae2b14c7 4146 rvc = rvc && VALID_RVC_J_IMM (foff);
e23eba97 4147
ae2b14c7
JW
4148 /* C.J exists on RV32 and RV64, but C.JAL is RV32-only. */
4149 rvc = rvc && (rd == 0 || (rd == X_RA && ARCH_SIZE == 32));
4150
4151 if (rvc)
e23eba97
NC
4152 {
4153 /* Relax to C.J[AL] rd, addr. */
4154 r_type = R_RISCV_RVC_JUMP;
4155 auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
4156 len = 2;
4157 }
4158 else if (VALID_UJTYPE_IMM (foff))
4159 {
4160 /* Relax to JAL rd, addr. */
4161 r_type = R_RISCV_JAL;
4162 auipc = MATCH_JAL | (rd << OP_SH_RD);
4163 }
4164 else /* near_zero */
4165 {
4166 /* Relax to JALR rd, x0, addr. */
4167 r_type = R_RISCV_LO12_I;
4168 auipc = MATCH_JALR | (rd << OP_SH_RD);
4169 }
4170
4171 /* Replace the R_RISCV_CALL reloc. */
4172 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
4173 /* Replace the AUIPC. */
4174 bfd_put (8 * len, abfd, auipc, contents + rel->r_offset);
4175
4176 /* Delete unnecessary JALR. */
4177 *again = TRUE;
7f02625e
JW
4178 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len,
4179 link_info);
e23eba97
NC
4180}
4181
4182/* Traverse all output sections and return the max alignment. */
4183
1d61f794 4184static bfd_vma
e23eba97
NC
4185_bfd_riscv_get_max_alignment (asection *sec)
4186{
4187 unsigned int max_alignment_power = 0;
4188 asection *o;
4189
4190 for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
4191 {
4192 if (o->alignment_power > max_alignment_power)
4193 max_alignment_power = o->alignment_power;
4194 }
4195
1d61f794 4196 return (bfd_vma) 1 << max_alignment_power;
e23eba97
NC
4197}
4198
4199/* Relax non-PIC global variable references. */
4200
4201static bfd_boolean
4202_bfd_riscv_relax_lui (bfd *abfd,
4203 asection *sec,
4204 asection *sym_sec,
4205 struct bfd_link_info *link_info,
4206 Elf_Internal_Rela *rel,
4207 bfd_vma symval,
45f76423
AW
4208 bfd_vma max_alignment,
4209 bfd_vma reserve_size,
9d06997a 4210 bfd_boolean *again,
9d1da81b
JW
4211 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
4212 bfd_boolean undefined_weak)
e23eba97
NC
4213{
4214 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4215 bfd_vma gp = riscv_global_pointer_value (link_info);
4216 int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
4217
e23eba97
NC
4218 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
4219
d0f744f9
AW
4220 if (gp)
4221 {
507685a3
JW
4222 /* If gp and the symbol are in the same output section, which is not the
4223 abs section, then consider only that output section's alignment. */
d0f744f9 4224 struct bfd_link_hash_entry *h =
b5292032
PD
4225 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE,
4226 TRUE);
507685a3
JW
4227 if (h->u.def.section->output_section == sym_sec->output_section
4228 && sym_sec->output_section != bfd_abs_section_ptr)
d0f744f9
AW
4229 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
4230 }
4231
e23eba97
NC
4232 /* Is the reference in range of x0 or gp?
4233 Valid gp range conservatively because of alignment issue. */
9d1da81b
JW
4234 if (undefined_weak
4235 || (VALID_ITYPE_IMM (symval)
4236 || (symval >= gp
4237 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
4238 || (symval < gp
4239 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
e23eba97
NC
4240 {
4241 unsigned sym = ELFNN_R_SYM (rel->r_info);
4242 switch (ELFNN_R_TYPE (rel->r_info))
4243 {
4244 case R_RISCV_LO12_I:
9d1da81b
JW
4245 if (undefined_weak)
4246 {
4247 /* Change the RS1 to zero. */
4248 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
4249 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
4250 bfd_put_32 (abfd, insn, contents + rel->r_offset);
4251 }
4252 else
4253 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
e23eba97
NC
4254 return TRUE;
4255
4256 case R_RISCV_LO12_S:
9d1da81b
JW
4257 if (undefined_weak)
4258 {
4259 /* Change the RS1 to zero. */
4260 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
4261 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
4262 bfd_put_32 (abfd, insn, contents + rel->r_offset);
4263 }
4264 else
4265 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
e23eba97
NC
4266 return TRUE;
4267
4268 case R_RISCV_HI20:
4269 /* We can delete the unnecessary LUI and reloc. */
4270 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
4271 *again = TRUE;
7f02625e
JW
4272 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4,
4273 link_info);
e23eba97
NC
4274
4275 default:
4276 abort ();
4277 }
4278 }
4279
4280 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
0f52d45a
JW
4281 account for this assuming page alignment at worst. In the presence of
4282 RELRO segment the linker aligns it by one page size, therefore sections
4283 after the segment can be moved more than one page. */
4284
e23eba97
NC
4285 if (use_rvc
4286 && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
4287 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
0f52d45a
JW
4288 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval)
4289 + (link_info->relro ? 2 * ELF_MAXPAGESIZE
4290 : ELF_MAXPAGESIZE)))
e23eba97 4291 {
3342be5d 4292 /* Replace LUI with C.LUI if legal (i.e., rd != x0 and rd != x2/sp). */
e23eba97 4293 bfd_vma lui = bfd_get_32 (abfd, contents + rel->r_offset);
3342be5d
AW
4294 unsigned rd = ((unsigned)lui >> OP_SH_RD) & OP_MASK_RD;
4295 if (rd == 0 || rd == X_SP)
e23eba97
NC
4296 return TRUE;
4297
4298 lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
4299 bfd_put_32 (abfd, lui, contents + rel->r_offset);
4300
4301 /* Replace the R_RISCV_HI20 reloc. */
4302 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
4303
4304 *again = TRUE;
7f02625e
JW
4305 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2,
4306 link_info);
e23eba97
NC
4307 }
4308
4309 return TRUE;
4310}
4311
4312/* Relax non-PIC TLS references. */
4313
4314static bfd_boolean
4315_bfd_riscv_relax_tls_le (bfd *abfd,
4316 asection *sec,
4317 asection *sym_sec ATTRIBUTE_UNUSED,
4318 struct bfd_link_info *link_info,
4319 Elf_Internal_Rela *rel,
4320 bfd_vma symval,
45f76423
AW
4321 bfd_vma max_alignment ATTRIBUTE_UNUSED,
4322 bfd_vma reserve_size ATTRIBUTE_UNUSED,
9d06997a 4323 bfd_boolean *again,
9d1da81b
JW
4324 riscv_pcgp_relocs *prcel_relocs ATTRIBUTE_UNUSED,
4325 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
e23eba97
NC
4326{
4327 /* See if this symbol is in range of tp. */
4328 if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
4329 return TRUE;
4330
e23eba97 4331 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
45f76423
AW
4332 switch (ELFNN_R_TYPE (rel->r_info))
4333 {
4334 case R_RISCV_TPREL_LO12_I:
4335 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_I);
4336 return TRUE;
e23eba97 4337
45f76423
AW
4338 case R_RISCV_TPREL_LO12_S:
4339 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_S);
4340 return TRUE;
4341
4342 case R_RISCV_TPREL_HI20:
4343 case R_RISCV_TPREL_ADD:
4344 /* We can delete the unnecessary instruction and reloc. */
4345 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
4346 *again = TRUE;
7f02625e 4347 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4, link_info);
45f76423
AW
4348
4349 default:
4350 abort ();
4351 }
e23eba97
NC
4352}
4353
4354/* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
4355
4356static bfd_boolean
4357_bfd_riscv_relax_align (bfd *abfd, asection *sec,
9eb7b0ac 4358 asection *sym_sec,
7f02625e 4359 struct bfd_link_info *link_info,
e23eba97
NC
4360 Elf_Internal_Rela *rel,
4361 bfd_vma symval,
45f76423
AW
4362 bfd_vma max_alignment ATTRIBUTE_UNUSED,
4363 bfd_vma reserve_size ATTRIBUTE_UNUSED,
9d06997a 4364 bfd_boolean *again ATTRIBUTE_UNUSED,
9d1da81b
JW
4365 riscv_pcgp_relocs *pcrel_relocs ATTRIBUTE_UNUSED,
4366 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
e23eba97
NC
4367{
4368 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
4369 bfd_vma alignment = 1, pos;
4370 while (alignment <= rel->r_addend)
4371 alignment *= 2;
4372
4373 symval -= rel->r_addend;
4374 bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
4375 bfd_vma nop_bytes = aligned_addr - symval;
4376
4377 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
4378 sec->sec_flg0 = TRUE;
4379
4380 /* Make sure there are enough NOPs to actually achieve the alignment. */
4381 if (rel->r_addend < nop_bytes)
9eb7b0ac 4382 {
f2b740ac
AM
4383 _bfd_error_handler
4384 (_("%pB(%pA+%#" PRIx64 "): %" PRId64 " bytes required for alignment "
4385 "to %" PRId64 "-byte boundary, but only %" PRId64 " present"),
4386 abfd, sym_sec, (uint64_t) rel->r_offset,
4387 (int64_t) nop_bytes, (int64_t) alignment, (int64_t) rel->r_addend);
9eb7b0ac
PD
4388 bfd_set_error (bfd_error_bad_value);
4389 return FALSE;
4390 }
e23eba97
NC
4391
4392 /* Delete the reloc. */
4393 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
4394
4395 /* If the number of NOPs is already correct, there's nothing to do. */
4396 if (nop_bytes == rel->r_addend)
4397 return TRUE;
4398
4399 /* Write as many RISC-V NOPs as we need. */
4400 for (pos = 0; pos < (nop_bytes & -4); pos += 4)
4401 bfd_put_32 (abfd, RISCV_NOP, contents + rel->r_offset + pos);
4402
4403 /* Write a final RVC NOP if need be. */
4404 if (nop_bytes % 4 != 0)
4405 bfd_put_16 (abfd, RVC_NOP, contents + rel->r_offset + pos);
4406
4407 /* Delete the excess bytes. */
4408 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
7f02625e 4409 rel->r_addend - nop_bytes, link_info);
e23eba97
NC
4410}
4411
ff6f4d9b
PD
4412/* Relax PC-relative references to GP-relative references. */
4413
9d06997a 4414static bfd_boolean
5f9aecea 4415_bfd_riscv_relax_pc (bfd *abfd ATTRIBUTE_UNUSED,
9d06997a
PD
4416 asection *sec,
4417 asection *sym_sec,
4418 struct bfd_link_info *link_info,
4419 Elf_Internal_Rela *rel,
4420 bfd_vma symval,
4421 bfd_vma max_alignment,
4422 bfd_vma reserve_size,
4423 bfd_boolean *again ATTRIBUTE_UNUSED,
9d1da81b
JW
4424 riscv_pcgp_relocs *pcgp_relocs,
4425 bfd_boolean undefined_weak)
9d06997a 4426{
9d1da81b 4427 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
9d06997a
PD
4428 bfd_vma gp = riscv_global_pointer_value (link_info);
4429
4430 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
4431
4432 /* Chain the _LO relocs to their cooresponding _HI reloc to compute the
4433 * actual target address. */
e65b1a78
MR
4434 riscv_pcgp_hi_reloc hi_reloc;
4435 memset (&hi_reloc, 0, sizeof (hi_reloc));
9d06997a
PD
4436 switch (ELFNN_R_TYPE (rel->r_info))
4437 {
4438 case R_RISCV_PCREL_LO12_I:
4439 case R_RISCV_PCREL_LO12_S:
4440 {
a05f27b6
JW
4441 /* If the %lo has an addend, it isn't for the label pointing at the
4442 hi part instruction, but rather for the symbol pointed at by the
4443 hi part instruction. So we must subtract it here for the lookup.
4444 It is still used below in the final symbol address. */
4445 bfd_vma hi_sec_off = symval - sec_addr (sym_sec) - rel->r_addend;
9d06997a 4446 riscv_pcgp_hi_reloc *hi = riscv_find_pcgp_hi_reloc (pcgp_relocs,
a05f27b6 4447 hi_sec_off);
9d06997a
PD
4448 if (hi == NULL)
4449 {
a05f27b6 4450 riscv_record_pcgp_lo_reloc (pcgp_relocs, hi_sec_off);
9d06997a
PD
4451 return TRUE;
4452 }
4453
4454 hi_reloc = *hi;
4455 symval = hi_reloc.hi_addr;
4456 sym_sec = hi_reloc.sym_sec;
9d1da81b
JW
4457
4458 /* We can not know whether the undefined weak symbol is referenced
4459 according to the information of R_RISCV_PCREL_LO12_I/S. Therefore,
4460 we have to record the 'undefined_weak' flag when handling the
4461 corresponding R_RISCV_HI20 reloc in riscv_record_pcgp_hi_reloc. */
4462 undefined_weak = hi_reloc.undefined_weak;
9d06997a
PD
4463 }
4464 break;
4465
4466 case R_RISCV_PCREL_HI20:
4467 /* Mergeable symbols and code might later move out of range. */
9d1da81b
JW
4468 if (! undefined_weak
4469 && sym_sec->flags & (SEC_MERGE | SEC_CODE))
9d06997a
PD
4470 return TRUE;
4471
4472 /* If the cooresponding lo relocation has already been seen then it's not
4473 * safe to relax this relocation. */
4474 if (riscv_find_pcgp_lo_reloc (pcgp_relocs, rel->r_offset))
07d6d2b8 4475 return TRUE;
9d06997a
PD
4476
4477 break;
4478
4479 default:
4480 abort ();
4481 }
4482
4483 if (gp)
4484 {
507685a3
JW
4485 /* If gp and the symbol are in the same output section, which is not the
4486 abs section, then consider only that output section's alignment. */
9d06997a 4487 struct bfd_link_hash_entry *h =
507685a3
JW
4488 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE,
4489 TRUE);
4490 if (h->u.def.section->output_section == sym_sec->output_section
4491 && sym_sec->output_section != bfd_abs_section_ptr)
9d06997a
PD
4492 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
4493 }
4494
4495 /* Is the reference in range of x0 or gp?
4496 Valid gp range conservatively because of alignment issue. */
9d1da81b
JW
4497 if (undefined_weak
4498 || (VALID_ITYPE_IMM (symval)
4499 || (symval >= gp
4500 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
4501 || (symval < gp
4502 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size))))
9d06997a
PD
4503 {
4504 unsigned sym = hi_reloc.hi_sym;
4505 switch (ELFNN_R_TYPE (rel->r_info))
4506 {
4507 case R_RISCV_PCREL_LO12_I:
9d1da81b
JW
4508 if (undefined_weak)
4509 {
4510 /* Change the RS1 to zero, and then modify the relocation
4511 type to R_RISCV_LO12_I. */
4512 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
4513 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
4514 bfd_put_32 (abfd, insn, contents + rel->r_offset);
4515 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_I);
4516 rel->r_addend = hi_reloc.hi_addend;
4517 }
4518 else
4519 {
4520 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
4521 rel->r_addend += hi_reloc.hi_addend;
4522 }
5f9aecea 4523 return TRUE;
9d06997a
PD
4524
4525 case R_RISCV_PCREL_LO12_S:
9d1da81b
JW
4526 if (undefined_weak)
4527 {
4528 /* Change the RS1 to zero, and then modify the relocation
4529 type to R_RISCV_LO12_S. */
4530 bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
4531 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
4532 bfd_put_32 (abfd, insn, contents + rel->r_offset);
4533 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_LO12_S);
4534 rel->r_addend = hi_reloc.hi_addend;
4535 }
4536 else
4537 {
4538 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
4539 rel->r_addend += hi_reloc.hi_addend;
4540 }
5f9aecea 4541 return TRUE;
9d06997a
PD
4542
4543 case R_RISCV_PCREL_HI20:
07d6d2b8 4544 riscv_record_pcgp_hi_reloc (pcgp_relocs,
9d06997a
PD
4545 rel->r_offset,
4546 rel->r_addend,
4547 symval,
4548 ELFNN_R_SYM(rel->r_info),
9d1da81b
JW
4549 sym_sec,
4550 undefined_weak);
9d06997a
PD
4551 /* We can delete the unnecessary AUIPC and reloc. */
4552 rel->r_info = ELFNN_R_INFO (0, R_RISCV_DELETE);
4553 rel->r_addend = 4;
5f9aecea 4554 return TRUE;
9d06997a
PD
4555
4556 default:
4557 abort ();
4558 }
4559 }
4560
4561 return TRUE;
4562}
4563
4564/* Relax PC-relative references to GP-relative references. */
4565
ff6f4d9b
PD
4566static bfd_boolean
4567_bfd_riscv_relax_delete (bfd *abfd,
4568 asection *sec,
4569 asection *sym_sec ATTRIBUTE_UNUSED,
7f02625e 4570 struct bfd_link_info *link_info,
ff6f4d9b
PD
4571 Elf_Internal_Rela *rel,
4572 bfd_vma symval ATTRIBUTE_UNUSED,
4573 bfd_vma max_alignment ATTRIBUTE_UNUSED,
4574 bfd_vma reserve_size ATTRIBUTE_UNUSED,
9d06997a 4575 bfd_boolean *again ATTRIBUTE_UNUSED,
9d1da81b
JW
4576 riscv_pcgp_relocs *pcgp_relocs ATTRIBUTE_UNUSED,
4577 bfd_boolean undefined_weak ATTRIBUTE_UNUSED)
ff6f4d9b 4578{
7f02625e
JW
4579 if (!riscv_relax_delete_bytes(abfd, sec, rel->r_offset, rel->r_addend,
4580 link_info))
ff6f4d9b
PD
4581 return FALSE;
4582 rel->r_info = ELFNN_R_INFO(0, R_RISCV_NONE);
4583 return TRUE;
4584}
4585
4586/* Relax a section. Pass 0 shortens code sequences unless disabled. Pass 1
4587 deletes the bytes that pass 0 made obselete. Pass 2, which cannot be
4588 disabled, handles code alignment directives. */
e23eba97
NC
4589
4590static bfd_boolean
4591_bfd_riscv_relax_section (bfd *abfd, asection *sec,
4592 struct bfd_link_info *info,
4593 bfd_boolean *again)
4594{
4595 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
4596 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
4597 struct bfd_elf_section_data *data = elf_section_data (sec);
4598 Elf_Internal_Rela *relocs;
4599 bfd_boolean ret = FALSE;
4600 unsigned int i;
45f76423 4601 bfd_vma max_alignment, reserve_size = 0;
9d06997a 4602 riscv_pcgp_relocs pcgp_relocs;
e23eba97
NC
4603
4604 *again = FALSE;
4605
4606 if (bfd_link_relocatable (info)
4607 || sec->sec_flg0
4608 || (sec->flags & SEC_RELOC) == 0
4609 || sec->reloc_count == 0
4610 || (info->disable_target_specific_optimizations
4611 && info->relax_pass == 0))
4612 return TRUE;
4613
9d06997a
PD
4614 riscv_init_pcgp_relocs (&pcgp_relocs);
4615
e23eba97
NC
4616 /* Read this BFD's relocs if we haven't done so already. */
4617 if (data->relocs)
4618 relocs = data->relocs;
4619 else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
4620 info->keep_memory)))
4621 goto fail;
4622
fc3c5343
L
4623 if (htab)
4624 {
4625 max_alignment = htab->max_alignment;
4626 if (max_alignment == (bfd_vma) -1)
4627 {
4628 max_alignment = _bfd_riscv_get_max_alignment (sec);
4629 htab->max_alignment = max_alignment;
4630 }
4631 }
4632 else
4633 max_alignment = _bfd_riscv_get_max_alignment (sec);
e23eba97
NC
4634
4635 /* Examine and consider relaxing each reloc. */
4636 for (i = 0; i < sec->reloc_count; i++)
4637 {
4638 asection *sym_sec;
4639 Elf_Internal_Rela *rel = relocs + i;
45f76423 4640 relax_func_t relax_func;
e23eba97
NC
4641 int type = ELFNN_R_TYPE (rel->r_info);
4642 bfd_vma symval;
04b865dc 4643 char symtype;
9d1da81b 4644 bfd_boolean undefined_weak = FALSE;
e23eba97 4645
ff6f4d9b 4646 relax_func = NULL;
e23eba97
NC
4647 if (info->relax_pass == 0)
4648 {
4649 if (type == R_RISCV_CALL || type == R_RISCV_CALL_PLT)
4650 relax_func = _bfd_riscv_relax_call;
4651 else if (type == R_RISCV_HI20
4652 || type == R_RISCV_LO12_I
4653 || type == R_RISCV_LO12_S)
4654 relax_func = _bfd_riscv_relax_lui;
9d06997a
PD
4655 else if (!bfd_link_pic(info)
4656 && (type == R_RISCV_PCREL_HI20
4657 || type == R_RISCV_PCREL_LO12_I
4658 || type == R_RISCV_PCREL_LO12_S))
4659 relax_func = _bfd_riscv_relax_pc;
45f76423
AW
4660 else if (type == R_RISCV_TPREL_HI20
4661 || type == R_RISCV_TPREL_ADD
4662 || type == R_RISCV_TPREL_LO12_I
4663 || type == R_RISCV_TPREL_LO12_S)
e23eba97 4664 relax_func = _bfd_riscv_relax_tls_le;
45f76423
AW
4665 else
4666 continue;
4667
4668 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
4669 if (i == sec->reloc_count - 1
4670 || ELFNN_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
4671 || rel->r_offset != (rel + 1)->r_offset)
4672 continue;
4673
4674 /* Skip over the R_RISCV_RELAX. */
4675 i++;
e23eba97 4676 }
ff6f4d9b 4677 else if (info->relax_pass == 1 && type == R_RISCV_DELETE)
07d6d2b8 4678 relax_func = _bfd_riscv_relax_delete;
ff6f4d9b 4679 else if (info->relax_pass == 2 && type == R_RISCV_ALIGN)
e23eba97 4680 relax_func = _bfd_riscv_relax_align;
45f76423 4681 else
e23eba97
NC
4682 continue;
4683
4684 data->relocs = relocs;
4685
4686 /* Read this BFD's contents if we haven't done so already. */
4687 if (!data->this_hdr.contents
4688 && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
4689 goto fail;
4690
4691 /* Read this BFD's symbols if we haven't done so already. */
4692 if (symtab_hdr->sh_info != 0
4693 && !symtab_hdr->contents
4694 && !(symtab_hdr->contents =
4695 (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
4696 symtab_hdr->sh_info,
4697 0, NULL, NULL, NULL)))
4698 goto fail;
4699
4700 /* Get the value of the symbol referred to by the reloc. */
4701 if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
4702 {
4703 /* A local symbol. */
4704 Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
4705 + ELFNN_R_SYM (rel->r_info));
45f76423
AW
4706 reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
4707 ? 0 : isym->st_size - rel->r_addend;
e23eba97 4708
02dd9d25
NC
4709 /* Relocate against local STT_GNU_IFUNC symbol. we have created
4710 a fake global symbol entry for this, so deal with the local ifunc
4711 as a global. */
4712 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4713 continue;
4714
e23eba97 4715 if (isym->st_shndx == SHN_UNDEF)
04b865dc 4716 sym_sec = sec, symval = rel->r_offset;
e23eba97
NC
4717 else
4718 {
4719 BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
4720 sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
09ca4b9d
JW
4721#if 0
4722 /* The purpose of this code is unknown. It breaks linker scripts
4723 for embedded development that place sections at address zero.
4724 This code is believed to be unnecessary. Disabling it but not
4725 yet removing it, in case something breaks. */
e23eba97
NC
4726 if (sec_addr (sym_sec) == 0)
4727 continue;
09ca4b9d 4728#endif
04b865dc 4729 symval = isym->st_value;
e23eba97 4730 }
04b865dc 4731 symtype = ELF_ST_TYPE (isym->st_info);
e23eba97
NC
4732 }
4733 else
4734 {
4735 unsigned long indx;
4736 struct elf_link_hash_entry *h;
4737
4738 indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
4739 h = elf_sym_hashes (abfd)[indx];
4740
4741 while (h->root.type == bfd_link_hash_indirect
4742 || h->root.type == bfd_link_hash_warning)
4743 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4744
02dd9d25
NC
4745 /* Disable the relaxation for ifunc. */
4746 if (h != NULL && h->type == STT_GNU_IFUNC)
4747 continue;
4748
9d1da81b
JW
4749 if (h->root.type == bfd_link_hash_undefweak
4750 && (relax_func == _bfd_riscv_relax_lui
4751 || relax_func == _bfd_riscv_relax_pc))
4752 {
4753 /* For the lui and auipc relaxations, since the symbol
4754 value of an undefined weak symbol is always be zero,
4755 we can optimize the patterns into a single LI/MV/ADDI
4756 instruction.
4757
4758 Note that, creating shared libraries and pie output may
4759 break the rule above. Fortunately, since we do not relax
4760 pc relocs when creating shared libraries and pie output,
4761 and the absolute address access for R_RISCV_HI20 isn't
4762 allowed when "-fPIC" is set, the problem of creating shared
4763 libraries can not happen currently. Once we support the
4764 auipc relaxations when creating shared libraries, then we will
4765 need the more rigorous checking for this optimization. */
4766 undefined_weak = TRUE;
4767 }
4768
85f78364
JW
4769 /* This line has to match the check in riscv_elf_relocate_section
4770 in the R_RISCV_CALL[_PLT] case. */
4771 if (bfd_link_pic (info) && h->plt.offset != MINUS_ONE)
04b865dc
JW
4772 {
4773 sym_sec = htab->elf.splt;
4774 symval = h->plt.offset;
4775 }
9d1da81b
JW
4776 else if (undefined_weak)
4777 {
4778 symval = 0;
4779 sym_sec = bfd_und_section_ptr;
4780 }
a2714d6c
AM
4781 else if ((h->root.type == bfd_link_hash_defined
4782 || h->root.type == bfd_link_hash_defweak)
4783 && h->root.u.def.section != NULL
4784 && h->root.u.def.section->output_section != NULL)
04b865dc
JW
4785 {
4786 symval = h->root.u.def.value;
4787 sym_sec = h->root.u.def.section;
4788 }
a2714d6c
AM
4789 else
4790 continue;
e23eba97 4791
45f76423
AW
4792 if (h->type != STT_FUNC)
4793 reserve_size =
4794 (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
04b865dc 4795 symtype = h->type;
e23eba97
NC
4796 }
4797
04b865dc
JW
4798 if (sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE
4799 && (sym_sec->flags & SEC_MERGE))
4800 {
4801 /* At this stage in linking, no SEC_MERGE symbol has been
4802 adjusted, so all references to such symbols need to be
4803 passed through _bfd_merged_section_offset. (Later, in
4804 relocate_section, all SEC_MERGE symbols *except* for
4805 section symbols have been adjusted.)
4806
4807 gas may reduce relocations against symbols in SEC_MERGE
4808 sections to a relocation against the section symbol when
4809 the original addend was zero. When the reloc is against
4810 a section symbol we should include the addend in the
4811 offset passed to _bfd_merged_section_offset, since the
4812 location of interest is the original symbol. On the
4813 other hand, an access to "sym+addend" where "sym" is not
4814 a section symbol should not include the addend; Such an
4815 access is presumed to be an offset from "sym"; The
4816 location of interest is just "sym". */
4817 if (symtype == STT_SECTION)
4818 symval += rel->r_addend;
4819
4820 symval = _bfd_merged_section_offset (abfd, &sym_sec,
4821 elf_section_data (sym_sec)->sec_info,
4822 symval);
4823
4824 if (symtype != STT_SECTION)
4825 symval += rel->r_addend;
4826 }
4827 else
4828 symval += rel->r_addend;
4829
4830 symval += sec_addr (sym_sec);
e23eba97
NC
4831
4832 if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
9d06997a 4833 max_alignment, reserve_size, again,
9d1da81b 4834 &pcgp_relocs, undefined_weak))
e23eba97
NC
4835 goto fail;
4836 }
4837
4838 ret = TRUE;
4839
dc1e8a47 4840 fail:
e23eba97
NC
4841 if (relocs != data->relocs)
4842 free (relocs);
9d06997a 4843 riscv_free_pcgp_relocs(&pcgp_relocs, abfd, sec);
e23eba97
NC
4844
4845 return ret;
4846}
4847
4848#if ARCH_SIZE == 32
79b8e8ab 4849# define PRSTATUS_SIZE 204
e23eba97
NC
4850# define PRSTATUS_OFFSET_PR_CURSIG 12
4851# define PRSTATUS_OFFSET_PR_PID 24
4852# define PRSTATUS_OFFSET_PR_REG 72
4853# define ELF_GREGSET_T_SIZE 128
4854# define PRPSINFO_SIZE 128
4855# define PRPSINFO_OFFSET_PR_PID 16
4856# define PRPSINFO_OFFSET_PR_FNAME 32
4857# define PRPSINFO_OFFSET_PR_PSARGS 48
4858#else
4859# define PRSTATUS_SIZE 376
4860# define PRSTATUS_OFFSET_PR_CURSIG 12
4861# define PRSTATUS_OFFSET_PR_PID 32
4862# define PRSTATUS_OFFSET_PR_REG 112
4863# define ELF_GREGSET_T_SIZE 256
4864# define PRPSINFO_SIZE 136
4865# define PRPSINFO_OFFSET_PR_PID 24
4866# define PRPSINFO_OFFSET_PR_FNAME 40
4867# define PRPSINFO_OFFSET_PR_PSARGS 56
4868#endif
4869
4870/* Support for core dump NOTE sections. */
4871
4872static bfd_boolean
4873riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
4874{
4875 switch (note->descsz)
4876 {
4877 default:
4878 return FALSE;
4879
4880 case PRSTATUS_SIZE: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
4881 /* pr_cursig */
4882 elf_tdata (abfd)->core->signal
4883 = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
4884
4885 /* pr_pid */
4886 elf_tdata (abfd)->core->lwpid
4887 = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
4888 break;
4889 }
4890
4891 /* Make a ".reg/999" section. */
4892 return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
4893 note->descpos + PRSTATUS_OFFSET_PR_REG);
4894}
4895
4896static bfd_boolean
4897riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
4898{
4899 switch (note->descsz)
4900 {
4901 default:
4902 return FALSE;
4903
4904 case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
4905 /* pr_pid */
4906 elf_tdata (abfd)->core->pid
4907 = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
4908
4909 /* pr_fname */
4910 elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
4911 (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME, 16);
4912
4913 /* pr_psargs */
4914 elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
4915 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS, 80);
4916 break;
4917 }
4918
4919 /* Note that for some reason, a spurious space is tacked
4920 onto the end of the args in some (at least one anyway)
4921 implementations, so strip it off if it exists. */
4922
4923 {
4924 char *command = elf_tdata (abfd)->core->command;
4925 int n = strlen (command);
4926
4927 if (0 < n && command[n - 1] == ' ')
4928 command[n - 1] = '\0';
4929 }
4930
4931 return TRUE;
4932}
4933
640d6bfd
KLC
4934/* Set the right mach type. */
4935static bfd_boolean
4936riscv_elf_object_p (bfd *abfd)
4937{
4938 /* There are only two mach types in RISCV currently. */
4939 if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0)
4940 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
4941 else
4942 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
4943
4944 return TRUE;
4945}
4946
2dc8dd17
JW
4947/* Determine whether an object attribute tag takes an integer, a
4948 string or both. */
4949
4950static int
4951riscv_elf_obj_attrs_arg_type (int tag)
4952{
4953 return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
4954}
e23eba97 4955
f938669f
SP
4956/* Determine the size of the header of for the GOT section. */
4957
4958static bfd_vma
4959riscv_elf_got_header_size (struct bfd_link_info* info ATTRIBUTE_UNUSED)
4960{
4961 return ARCH_SIZE / 8;
4962}
4963
e23eba97
NC
4964#define TARGET_LITTLE_SYM riscv_elfNN_vec
4965#define TARGET_LITTLE_NAME "elfNN-littleriscv"
4966
4967#define elf_backend_reloc_type_class riscv_reloc_type_class
4968
4969#define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
4970#define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
4971#define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
4972#define bfd_elfNN_bfd_merge_private_bfd_data \
4973 _bfd_riscv_elf_merge_private_bfd_data
4974
4975#define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
4976#define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
4977#define elf_backend_check_relocs riscv_elf_check_relocs
4978#define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
4979#define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
4980#define elf_backend_relocate_section riscv_elf_relocate_section
4981#define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
4982#define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
4983#define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
e23eba97 4984#define elf_backend_plt_sym_val riscv_elf_plt_sym_val
07d6d2b8
AM
4985#define elf_backend_grok_prstatus riscv_elf_grok_prstatus
4986#define elf_backend_grok_psinfo riscv_elf_grok_psinfo
f938669f 4987#define elf_backend_got_header_size riscv_elf_got_header_size
07d6d2b8 4988#define elf_backend_object_p riscv_elf_object_p
e23eba97
NC
4989#define elf_info_to_howto_rel NULL
4990#define elf_info_to_howto riscv_info_to_howto_rela
4991#define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
fc46e8bd 4992#define bfd_elfNN_mkobject elfNN_riscv_mkobject
e23eba97
NC
4993
4994#define elf_backend_init_index_section _bfd_elf_init_1_index_section
4995
4996#define elf_backend_can_gc_sections 1
4997#define elf_backend_can_refcount 1
4998#define elf_backend_want_got_plt 1
4999#define elf_backend_plt_readonly 1
5000#define elf_backend_plt_alignment 4
5001#define elf_backend_want_plt_sym 1
5474d94f 5002#define elf_backend_want_dynrelro 1
e23eba97
NC
5003#define elf_backend_rela_normal 1
5004#define elf_backend_default_execstack 0
5005
2dc8dd17
JW
5006#undef elf_backend_obj_attrs_vendor
5007#define elf_backend_obj_attrs_vendor "riscv"
5008#undef elf_backend_obj_attrs_arg_type
5009#define elf_backend_obj_attrs_arg_type riscv_elf_obj_attrs_arg_type
5010#undef elf_backend_obj_attrs_section_type
5011#define elf_backend_obj_attrs_section_type SHT_RISCV_ATTRIBUTES
5012#undef elf_backend_obj_attrs_section
5013#define elf_backend_obj_attrs_section ".riscv.attributes"
5014
e23eba97 5015#include "elfNN-target.h"