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