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