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