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