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