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