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