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