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