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