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