ELF: Call check_relocs after opening all inputs
[deliverable/binutils-gdb.git] / bfd / elfnn-riscv.c
1 /* RISC-V-specific support for NN-bit ELF.
2 Copyright (C) 2011-2017 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 #define ARCH_SIZE NN
36
37 #define MINUS_ONE ((bfd_vma)0 - 1)
38
39 #define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
40
41 #define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
42
43 /* The name of the dynamic interpreter. This is put in the .interp
44 section. */
45
46 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
47 #define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
48
49 #define ELF_ARCH bfd_arch_riscv
50 #define ELF_TARGET_ID RISCV_ELF_DATA
51 #define ELF_MACHINE_CODE EM_RISCV
52 #define ELF_MAXPAGESIZE 0x1000
53 #define ELF_COMMONPAGESIZE 0x1000
54
55 /* The RISC-V linker needs to keep track of the number of relocs that it
56 decides to copy as dynamic relocs in check_relocs for each symbol.
57 This is so that it can later discard them if they are found to be
58 unnecessary. We store the information in a field extending the
59 regular ELF linker hash table. */
60
61 struct riscv_elf_dyn_relocs
62 {
63 struct riscv_elf_dyn_relocs *next;
64
65 /* The input section of the reloc. */
66 asection *sec;
67
68 /* Total number of relocs copied for the input section. */
69 bfd_size_type count;
70
71 /* Number of pc-relative relocs copied for the input section. */
72 bfd_size_type pc_count;
73 };
74
75 /* RISC-V ELF linker hash entry. */
76
77 struct riscv_elf_link_hash_entry
78 {
79 struct elf_link_hash_entry elf;
80
81 /* Track dynamic relocs copied for this symbol. */
82 struct riscv_elf_dyn_relocs *dyn_relocs;
83
84 #define GOT_UNKNOWN 0
85 #define GOT_NORMAL 1
86 #define GOT_TLS_GD 2
87 #define GOT_TLS_IE 4
88 #define GOT_TLS_LE 8
89 char tls_type;
90 };
91
92 #define riscv_elf_hash_entry(ent) \
93 ((struct riscv_elf_link_hash_entry *)(ent))
94
95 struct _bfd_riscv_elf_obj_tdata
96 {
97 struct elf_obj_tdata root;
98
99 /* tls_type for each local got entry. */
100 char *local_got_tls_type;
101 };
102
103 #define _bfd_riscv_elf_tdata(abfd) \
104 ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
105
106 #define _bfd_riscv_elf_local_got_tls_type(abfd) \
107 (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
108
109 #define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
110 (*((h) != NULL ? &riscv_elf_hash_entry (h)->tls_type \
111 : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
112
113 #define is_riscv_elf(bfd) \
114 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
115 && elf_tdata (bfd) != NULL \
116 && elf_object_id (bfd) == RISCV_ELF_DATA)
117
118 #include "elf/common.h"
119 #include "elf/internal.h"
120
121 struct riscv_elf_link_hash_table
122 {
123 struct elf_link_hash_table elf;
124
125 /* Short-cuts to get to dynamic linker sections. */
126 asection *sdyntdata;
127
128 /* Small local sym to section mapping cache. */
129 struct sym_cache sym_cache;
130
131 /* The max alignment of output sections. */
132 bfd_vma max_alignment;
133 };
134
135
136 /* Get the RISC-V ELF linker hash table from a link_info structure. */
137 #define riscv_elf_hash_table(p) \
138 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
139 == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
140
141 static void
142 riscv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
143 arelent *cache_ptr,
144 Elf_Internal_Rela *dst)
145 {
146 cache_ptr->howto = riscv_elf_rtype_to_howto (ELFNN_R_TYPE (dst->r_info));
147 }
148
149 static void
150 riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
151 {
152 const struct elf_backend_data *bed;
153 bfd_byte *loc;
154
155 bed = get_elf_backend_data (abfd);
156 loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
157 bed->s->swap_reloca_out (abfd, rel, loc);
158 }
159
160 /* PLT/GOT stuff. */
161
162 #define PLT_HEADER_INSNS 8
163 #define PLT_ENTRY_INSNS 4
164 #define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
165 #define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
166
167 #define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
168
169 #define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
170
171 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
172
173 static bfd_vma
174 riscv_elf_got_plt_val (bfd_vma plt_index, struct bfd_link_info *info)
175 {
176 return sec_addr (riscv_elf_hash_table (info)->elf.sgotplt)
177 + GOTPLT_HEADER_SIZE + (plt_index * GOT_ENTRY_SIZE);
178 }
179
180 #if ARCH_SIZE == 32
181 # define MATCH_LREG MATCH_LW
182 #else
183 # define MATCH_LREG MATCH_LD
184 #endif
185
186 /* Generate a PLT header. */
187
188 static void
189 riscv_make_plt_header (bfd_vma gotplt_addr, bfd_vma addr, uint32_t *entry)
190 {
191 bfd_vma gotplt_offset_high = RISCV_PCREL_HIGH_PART (gotplt_addr, addr);
192 bfd_vma gotplt_offset_low = RISCV_PCREL_LOW_PART (gotplt_addr, addr);
193
194 /* auipc t2, %hi(.got.plt)
195 sub t1, t1, t3 # shifted .got.plt offset + hdr size + 12
196 l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
197 addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
198 addi t0, t2, %lo(.got.plt) # &.got.plt
199 srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
200 l[w|d] t0, PTRSIZE(t0) # link map
201 jr t3 */
202
203 entry[0] = RISCV_UTYPE (AUIPC, X_T2, gotplt_offset_high);
204 entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T3);
205 entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, gotplt_offset_low);
206 entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, -(PLT_HEADER_SIZE + 12));
207 entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, gotplt_offset_low);
208 entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
209 entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
210 entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
211 }
212
213 /* Generate a PLT entry. */
214
215 static void
216 riscv_make_plt_entry (bfd_vma got, bfd_vma addr, uint32_t *entry)
217 {
218 /* auipc t3, %hi(.got.plt entry)
219 l[w|d] t3, %lo(.got.plt entry)(t3)
220 jalr t1, t3
221 nop */
222
223 entry[0] = RISCV_UTYPE (AUIPC, X_T3, RISCV_PCREL_HIGH_PART (got, addr));
224 entry[1] = RISCV_ITYPE (LREG, X_T3, X_T3, RISCV_PCREL_LOW_PART (got, addr));
225 entry[2] = RISCV_ITYPE (JALR, X_T1, X_T3, 0);
226 entry[3] = RISCV_NOP;
227 }
228
229 /* Create an entry in an RISC-V ELF linker hash table. */
230
231 static struct bfd_hash_entry *
232 link_hash_newfunc (struct bfd_hash_entry *entry,
233 struct bfd_hash_table *table, const char *string)
234 {
235 /* Allocate the structure if it has not already been allocated by a
236 subclass. */
237 if (entry == NULL)
238 {
239 entry =
240 bfd_hash_allocate (table,
241 sizeof (struct riscv_elf_link_hash_entry));
242 if (entry == NULL)
243 return entry;
244 }
245
246 /* Call the allocation method of the superclass. */
247 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
248 if (entry != NULL)
249 {
250 struct riscv_elf_link_hash_entry *eh;
251
252 eh = (struct riscv_elf_link_hash_entry *) entry;
253 eh->dyn_relocs = NULL;
254 eh->tls_type = GOT_UNKNOWN;
255 }
256
257 return entry;
258 }
259
260 /* Create a RISC-V ELF linker hash table. */
261
262 static struct bfd_link_hash_table *
263 riscv_elf_link_hash_table_create (bfd *abfd)
264 {
265 struct riscv_elf_link_hash_table *ret;
266 bfd_size_type amt = sizeof (struct riscv_elf_link_hash_table);
267
268 ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
269 if (ret == NULL)
270 return NULL;
271
272 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
273 sizeof (struct riscv_elf_link_hash_entry),
274 RISCV_ELF_DATA))
275 {
276 free (ret);
277 return NULL;
278 }
279
280 ret->max_alignment = (bfd_vma) -1;
281 return &ret->elf.root;
282 }
283
284 /* Create the .got section. */
285
286 static bfd_boolean
287 riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
288 {
289 flagword flags;
290 asection *s, *s_got;
291 struct elf_link_hash_entry *h;
292 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
293 struct elf_link_hash_table *htab = elf_hash_table (info);
294
295 /* This function may be called more than once. */
296 if (htab->sgot != NULL)
297 return TRUE;
298
299 flags = bed->dynamic_sec_flags;
300
301 s = bfd_make_section_anyway_with_flags (abfd,
302 (bed->rela_plts_and_copies_p
303 ? ".rela.got" : ".rel.got"),
304 (bed->dynamic_sec_flags
305 | SEC_READONLY));
306 if (s == NULL
307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
308 return FALSE;
309 htab->srelgot = s;
310
311 s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
312 if (s == NULL
313 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
314 return FALSE;
315 htab->sgot = s;
316
317 /* The first bit of the global offset table is the header. */
318 s->size += bed->got_header_size;
319
320 if (bed->want_got_plt)
321 {
322 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
323 if (s == NULL
324 || !bfd_set_section_alignment (abfd, s,
325 bed->s->log_file_align))
326 return FALSE;
327 htab->sgotplt = s;
328
329 /* Reserve room for the header. */
330 s->size += GOTPLT_HEADER_SIZE;
331 }
332
333 if (bed->want_got_sym)
334 {
335 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
336 section. We don't do this in the linker script because we don't want
337 to define the symbol if we are not creating a global offset
338 table. */
339 h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
340 "_GLOBAL_OFFSET_TABLE_");
341 elf_hash_table (info)->hgot = h;
342 if (h == NULL)
343 return FALSE;
344 }
345
346 return TRUE;
347 }
348
349 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
350 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
351 hash table. */
352
353 static bfd_boolean
354 riscv_elf_create_dynamic_sections (bfd *dynobj,
355 struct bfd_link_info *info)
356 {
357 struct riscv_elf_link_hash_table *htab;
358
359 htab = riscv_elf_hash_table (info);
360 BFD_ASSERT (htab != NULL);
361
362 if (!riscv_elf_create_got_section (dynobj, info))
363 return FALSE;
364
365 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
366 return FALSE;
367
368 if (!bfd_link_pic (info))
369 {
370 htab->sdyntdata =
371 bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
372 SEC_ALLOC | SEC_THREAD_LOCAL);
373 }
374
375 if (!htab->elf.splt || !htab->elf.srelplt || !htab->elf.sdynbss
376 || (!bfd_link_pic (info) && (!htab->elf.srelbss || !htab->sdyntdata)))
377 abort ();
378
379 return TRUE;
380 }
381
382 /* Copy the extra info we tack onto an elf_link_hash_entry. */
383
384 static void
385 riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
386 struct elf_link_hash_entry *dir,
387 struct elf_link_hash_entry *ind)
388 {
389 struct riscv_elf_link_hash_entry *edir, *eind;
390
391 edir = (struct riscv_elf_link_hash_entry *) dir;
392 eind = (struct riscv_elf_link_hash_entry *) ind;
393
394 if (eind->dyn_relocs != NULL)
395 {
396 if (edir->dyn_relocs != NULL)
397 {
398 struct riscv_elf_dyn_relocs **pp;
399 struct riscv_elf_dyn_relocs *p;
400
401 /* Add reloc counts against the indirect sym to the direct sym
402 list. Merge any entries against the same section. */
403 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
404 {
405 struct riscv_elf_dyn_relocs *q;
406
407 for (q = edir->dyn_relocs; q != NULL; q = q->next)
408 if (q->sec == p->sec)
409 {
410 q->pc_count += p->pc_count;
411 q->count += p->count;
412 *pp = p->next;
413 break;
414 }
415 if (q == NULL)
416 pp = &p->next;
417 }
418 *pp = edir->dyn_relocs;
419 }
420
421 edir->dyn_relocs = eind->dyn_relocs;
422 eind->dyn_relocs = NULL;
423 }
424
425 if (ind->root.type == bfd_link_hash_indirect
426 && dir->got.refcount <= 0)
427 {
428 edir->tls_type = eind->tls_type;
429 eind->tls_type = GOT_UNKNOWN;
430 }
431 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
432 }
433
434 static bfd_boolean
435 riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
436 unsigned long symndx, char tls_type)
437 {
438 char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
439
440 *new_tls_type |= tls_type;
441 if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
442 {
443 (*_bfd_error_handler)
444 (_("%B: `%s' accessed both as normal and thread local symbol"),
445 abfd, h ? h->root.root.string : "<local>");
446 return FALSE;
447 }
448 return TRUE;
449 }
450
451 static bfd_boolean
452 riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
453 struct elf_link_hash_entry *h, long symndx)
454 {
455 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
456 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
457
458 if (htab->elf.sgot == NULL)
459 {
460 if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
461 return FALSE;
462 }
463
464 if (h != NULL)
465 {
466 h->got.refcount += 1;
467 return TRUE;
468 }
469
470 /* This is a global offset table entry for a local symbol. */
471 if (elf_local_got_refcounts (abfd) == NULL)
472 {
473 bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
474 if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
475 return FALSE;
476 _bfd_riscv_elf_local_got_tls_type (abfd)
477 = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
478 }
479 elf_local_got_refcounts (abfd) [symndx] += 1;
480
481 return TRUE;
482 }
483
484 static bfd_boolean
485 bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
486 {
487 (*_bfd_error_handler)
488 (_("%B: relocation %s against `%s' can not be used when making a shared "
489 "object; recompile with -fPIC"),
490 abfd, riscv_elf_rtype_to_howto (r_type)->name,
491 h != NULL ? h->root.root.string : "a local symbol");
492 bfd_set_error (bfd_error_bad_value);
493 return FALSE;
494 }
495 /* Look through the relocs for a section during the first phase, and
496 allocate space in the global offset table or procedure linkage
497 table. */
498
499 static bfd_boolean
500 riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
501 asection *sec, const Elf_Internal_Rela *relocs)
502 {
503 struct riscv_elf_link_hash_table *htab;
504 Elf_Internal_Shdr *symtab_hdr;
505 struct elf_link_hash_entry **sym_hashes;
506 const Elf_Internal_Rela *rel;
507 asection *sreloc = NULL;
508
509 if (bfd_link_relocatable (info))
510 return TRUE;
511
512 htab = riscv_elf_hash_table (info);
513 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
514 sym_hashes = elf_sym_hashes (abfd);
515
516 if (htab->elf.dynobj == NULL)
517 htab->elf.dynobj = abfd;
518
519 for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
520 {
521 unsigned int r_type;
522 unsigned int r_symndx;
523 struct elf_link_hash_entry *h;
524
525 r_symndx = ELFNN_R_SYM (rel->r_info);
526 r_type = ELFNN_R_TYPE (rel->r_info);
527
528 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
529 {
530 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
531 abfd, r_symndx);
532 return FALSE;
533 }
534
535 if (r_symndx < symtab_hdr->sh_info)
536 h = NULL;
537 else
538 {
539 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
540 while (h->root.type == bfd_link_hash_indirect
541 || h->root.type == bfd_link_hash_warning)
542 h = (struct elf_link_hash_entry *) h->root.u.i.link;
543
544 /* PR15323, ref flags aren't set for references in the same
545 object. */
546 h->root.non_ir_ref_regular = 1;
547 }
548
549 switch (r_type)
550 {
551 case R_RISCV_TLS_GD_HI20:
552 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
553 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
554 return FALSE;
555 break;
556
557 case R_RISCV_TLS_GOT_HI20:
558 if (bfd_link_pic (info))
559 info->flags |= DF_STATIC_TLS;
560 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
561 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
562 return FALSE;
563 break;
564
565 case R_RISCV_GOT_HI20:
566 if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
567 || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
568 return FALSE;
569 break;
570
571 case R_RISCV_CALL_PLT:
572 /* This symbol requires a procedure linkage table entry. We
573 actually build the entry in adjust_dynamic_symbol,
574 because this might be a case of linking PIC code without
575 linking in any dynamic objects, in which case we don't
576 need to generate a procedure linkage table after all. */
577
578 if (h != NULL)
579 {
580 h->needs_plt = 1;
581 h->plt.refcount += 1;
582 }
583 break;
584
585 case R_RISCV_CALL:
586 case R_RISCV_JAL:
587 case R_RISCV_BRANCH:
588 case R_RISCV_RVC_BRANCH:
589 case R_RISCV_RVC_JUMP:
590 case R_RISCV_PCREL_HI20:
591 /* In shared libraries, these relocs are known to bind locally. */
592 if (bfd_link_pic (info))
593 break;
594 goto static_reloc;
595
596 case R_RISCV_TPREL_HI20:
597 if (!bfd_link_executable (info))
598 return bad_static_reloc (abfd, r_type, h);
599 if (h != NULL)
600 riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
601 goto static_reloc;
602
603 case R_RISCV_HI20:
604 if (bfd_link_pic (info))
605 return bad_static_reloc (abfd, r_type, h);
606 /* Fall through. */
607
608 case R_RISCV_COPY:
609 case R_RISCV_JUMP_SLOT:
610 case R_RISCV_RELATIVE:
611 case R_RISCV_64:
612 case R_RISCV_32:
613 /* Fall through. */
614
615 static_reloc:
616 /* This reloc might not bind locally. */
617 if (h != NULL)
618 h->non_got_ref = 1;
619
620 if (h != NULL && !bfd_link_pic (info))
621 {
622 /* We may need a .plt entry if the function this reloc
623 refers to is in a shared lib. */
624 h->plt.refcount += 1;
625 }
626
627 /* If we are creating a shared library, and this is a reloc
628 against a global symbol, or a non PC relative reloc
629 against a local symbol, then we need to copy the reloc
630 into the shared library. However, if we are linking with
631 -Bsymbolic, we do not need to copy a reloc against a
632 global symbol which is defined in an object we are
633 including in the link (i.e., DEF_REGULAR is set). At
634 this point we have not seen all the input files, so it is
635 possible that DEF_REGULAR is not set now but will be set
636 later (it is never cleared). In case of a weak definition,
637 DEF_REGULAR may be cleared later by a strong definition in
638 a shared library. We account for that possibility below by
639 storing information in the relocs_copied field of the hash
640 table entry. A similar situation occurs when creating
641 shared libraries and symbol visibility changes render the
642 symbol local.
643
644 If on the other hand, we are creating an executable, we
645 may need to keep relocations for symbols satisfied by a
646 dynamic library if we manage to avoid copy relocs for the
647 symbol. */
648 if ((bfd_link_pic (info)
649 && (sec->flags & SEC_ALLOC) != 0
650 && (! riscv_elf_rtype_to_howto (r_type)->pc_relative
651 || (h != NULL
652 && (! info->symbolic
653 || h->root.type == bfd_link_hash_defweak
654 || !h->def_regular))))
655 || (!bfd_link_pic (info)
656 && (sec->flags & SEC_ALLOC) != 0
657 && h != NULL
658 && (h->root.type == bfd_link_hash_defweak
659 || !h->def_regular)))
660 {
661 struct riscv_elf_dyn_relocs *p;
662 struct riscv_elf_dyn_relocs **head;
663
664 /* When creating a shared object, we must copy these
665 relocs into the output file. We create a reloc
666 section in dynobj and make room for the reloc. */
667 if (sreloc == NULL)
668 {
669 sreloc = _bfd_elf_make_dynamic_reloc_section
670 (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
671 abfd, /*rela?*/ TRUE);
672
673 if (sreloc == NULL)
674 return FALSE;
675 }
676
677 /* If this is a global symbol, we count the number of
678 relocations we need for this symbol. */
679 if (h != NULL)
680 head = &((struct riscv_elf_link_hash_entry *) h)->dyn_relocs;
681 else
682 {
683 /* Track dynamic relocs needed for local syms too.
684 We really need local syms available to do this
685 easily. Oh well. */
686
687 asection *s;
688 void *vpp;
689 Elf_Internal_Sym *isym;
690
691 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
692 abfd, r_symndx);
693 if (isym == NULL)
694 return FALSE;
695
696 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
697 if (s == NULL)
698 s = sec;
699
700 vpp = &elf_section_data (s)->local_dynrel;
701 head = (struct riscv_elf_dyn_relocs **) vpp;
702 }
703
704 p = *head;
705 if (p == NULL || p->sec != sec)
706 {
707 bfd_size_type amt = sizeof *p;
708 p = ((struct riscv_elf_dyn_relocs *)
709 bfd_alloc (htab->elf.dynobj, amt));
710 if (p == NULL)
711 return FALSE;
712 p->next = *head;
713 *head = p;
714 p->sec = sec;
715 p->count = 0;
716 p->pc_count = 0;
717 }
718
719 p->count += 1;
720 p->pc_count += riscv_elf_rtype_to_howto (r_type)->pc_relative;
721 }
722
723 break;
724
725 case R_RISCV_GNU_VTINHERIT:
726 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
727 return FALSE;
728 break;
729
730 case R_RISCV_GNU_VTENTRY:
731 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
732 return FALSE;
733 break;
734
735 default:
736 break;
737 }
738 }
739
740 return TRUE;
741 }
742
743 static asection *
744 riscv_elf_gc_mark_hook (asection *sec,
745 struct bfd_link_info *info,
746 Elf_Internal_Rela *rel,
747 struct elf_link_hash_entry *h,
748 Elf_Internal_Sym *sym)
749 {
750 if (h != NULL)
751 switch (ELFNN_R_TYPE (rel->r_info))
752 {
753 case R_RISCV_GNU_VTINHERIT:
754 case R_RISCV_GNU_VTENTRY:
755 return NULL;
756 }
757
758 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
759 }
760
761 /* Adjust a symbol defined by a dynamic object and referenced by a
762 regular object. The current definition is in some section of the
763 dynamic object, but we're not including those sections. We have to
764 change the definition to something the rest of the link can
765 understand. */
766
767 static bfd_boolean
768 riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
769 struct elf_link_hash_entry *h)
770 {
771 struct riscv_elf_link_hash_table *htab;
772 struct riscv_elf_link_hash_entry * eh;
773 struct riscv_elf_dyn_relocs *p;
774 bfd *dynobj;
775 asection *s, *srel;
776
777 htab = riscv_elf_hash_table (info);
778 BFD_ASSERT (htab != NULL);
779
780 dynobj = htab->elf.dynobj;
781
782 /* Make sure we know what is going on here. */
783 BFD_ASSERT (dynobj != NULL
784 && (h->needs_plt
785 || h->type == STT_GNU_IFUNC
786 || h->u.weakdef != NULL
787 || (h->def_dynamic
788 && h->ref_regular
789 && !h->def_regular)));
790
791 /* If this is a function, put it in the procedure linkage table. We
792 will fill in the contents of the procedure linkage table later
793 (although we could actually do it here). */
794 if (h->type == STT_FUNC || h->type == STT_GNU_IFUNC || h->needs_plt)
795 {
796 if (h->plt.refcount <= 0
797 || SYMBOL_CALLS_LOCAL (info, h)
798 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
799 && h->root.type == bfd_link_hash_undefweak))
800 {
801 /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
802 input file, but the symbol was never referred to by a dynamic
803 object, or if all references were garbage collected. In such
804 a case, we don't actually need to build a PLT entry. */
805 h->plt.offset = (bfd_vma) -1;
806 h->needs_plt = 0;
807 }
808
809 return TRUE;
810 }
811 else
812 h->plt.offset = (bfd_vma) -1;
813
814 /* If this is a weak symbol, and there is a real definition, the
815 processor independent code will have arranged for us to see the
816 real definition first, and we can just use the same value. */
817 if (h->u.weakdef != NULL)
818 {
819 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
820 || h->u.weakdef->root.type == bfd_link_hash_defweak);
821 h->root.u.def.section = h->u.weakdef->root.u.def.section;
822 h->root.u.def.value = h->u.weakdef->root.u.def.value;
823 return TRUE;
824 }
825
826 /* This is a reference to a symbol defined by a dynamic object which
827 is not a function. */
828
829 /* If we are creating a shared library, we must presume that the
830 only references to the symbol are via the global offset table.
831 For such cases we need not do anything here; the relocations will
832 be handled correctly by relocate_section. */
833 if (bfd_link_pic (info))
834 return TRUE;
835
836 /* If there are no references to this symbol that do not use the
837 GOT, we don't need to generate a copy reloc. */
838 if (!h->non_got_ref)
839 return TRUE;
840
841 /* If -z nocopyreloc was given, we won't generate them either. */
842 if (info->nocopyreloc)
843 {
844 h->non_got_ref = 0;
845 return TRUE;
846 }
847
848 eh = (struct riscv_elf_link_hash_entry *) h;
849 for (p = eh->dyn_relocs; p != NULL; p = p->next)
850 {
851 s = p->sec->output_section;
852 if (s != NULL && (s->flags & SEC_READONLY) != 0)
853 break;
854 }
855
856 /* If we didn't find any dynamic relocs in read-only sections, then
857 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
858 if (p == NULL)
859 {
860 h->non_got_ref = 0;
861 return TRUE;
862 }
863
864 /* We must allocate the symbol in our .dynbss section, which will
865 become part of the .bss section of the executable. There will be
866 an entry for this symbol in the .dynsym section. The dynamic
867 object will contain position independent code, so all references
868 from the dynamic object to this symbol will go through the global
869 offset table. The dynamic linker will use the .dynsym entry to
870 determine the address it must put in the global offset table, so
871 both the dynamic object and the regular object will refer to the
872 same memory location for the variable. */
873
874 /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
875 to copy the initial value out of the dynamic object and into the
876 runtime process image. We need to remember the offset into the
877 .rel.bss section we are going to use. */
878 if (eh->tls_type & ~GOT_NORMAL)
879 {
880 s = htab->sdyntdata;
881 srel = htab->elf.srelbss;
882 }
883 else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
884 {
885 s = htab->elf.sdynrelro;
886 srel = htab->elf.sreldynrelro;
887 }
888 else
889 {
890 s = htab->elf.sdynbss;
891 srel = htab->elf.srelbss;
892 }
893 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
894 {
895 srel->size += sizeof (ElfNN_External_Rela);
896 h->needs_copy = 1;
897 }
898
899 return _bfd_elf_adjust_dynamic_copy (info, h, s);
900 }
901
902 /* Allocate space in .plt, .got and associated reloc sections for
903 dynamic relocs. */
904
905 static bfd_boolean
906 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
907 {
908 struct bfd_link_info *info;
909 struct riscv_elf_link_hash_table *htab;
910 struct riscv_elf_link_hash_entry *eh;
911 struct riscv_elf_dyn_relocs *p;
912
913 if (h->root.type == bfd_link_hash_indirect)
914 return TRUE;
915
916 info = (struct bfd_link_info *) inf;
917 htab = riscv_elf_hash_table (info);
918 BFD_ASSERT (htab != NULL);
919
920 if (htab->elf.dynamic_sections_created
921 && h->plt.refcount > 0)
922 {
923 /* Make sure this symbol is output as a dynamic symbol.
924 Undefined weak syms won't yet be marked as dynamic. */
925 if (h->dynindx == -1
926 && !h->forced_local)
927 {
928 if (! bfd_elf_link_record_dynamic_symbol (info, h))
929 return FALSE;
930 }
931
932 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
933 {
934 asection *s = htab->elf.splt;
935
936 if (s->size == 0)
937 s->size = PLT_HEADER_SIZE;
938
939 h->plt.offset = s->size;
940
941 /* Make room for this entry. */
942 s->size += PLT_ENTRY_SIZE;
943
944 /* We also need to make an entry in the .got.plt section. */
945 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
946
947 /* We also need to make an entry in the .rela.plt section. */
948 htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
949
950 /* If this symbol is not defined in a regular file, and we are
951 not generating a shared library, then set the symbol to this
952 location in the .plt. This is required to make function
953 pointers compare as equal between the normal executable and
954 the shared library. */
955 if (! bfd_link_pic (info)
956 && !h->def_regular)
957 {
958 h->root.u.def.section = s;
959 h->root.u.def.value = h->plt.offset;
960 }
961 }
962 else
963 {
964 h->plt.offset = (bfd_vma) -1;
965 h->needs_plt = 0;
966 }
967 }
968 else
969 {
970 h->plt.offset = (bfd_vma) -1;
971 h->needs_plt = 0;
972 }
973
974 if (h->got.refcount > 0)
975 {
976 asection *s;
977 bfd_boolean dyn;
978 int tls_type = riscv_elf_hash_entry (h)->tls_type;
979
980 /* Make sure this symbol is output as a dynamic symbol.
981 Undefined weak syms won't yet be marked as dynamic. */
982 if (h->dynindx == -1
983 && !h->forced_local)
984 {
985 if (! bfd_elf_link_record_dynamic_symbol (info, h))
986 return FALSE;
987 }
988
989 s = htab->elf.sgot;
990 h->got.offset = s->size;
991 dyn = htab->elf.dynamic_sections_created;
992 if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
993 {
994 /* TLS_GD needs two dynamic relocs and two GOT slots. */
995 if (tls_type & GOT_TLS_GD)
996 {
997 s->size += 2 * RISCV_ELF_WORD_BYTES;
998 htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
999 }
1000
1001 /* TLS_IE needs one dynamic reloc and one GOT slot. */
1002 if (tls_type & GOT_TLS_IE)
1003 {
1004 s->size += RISCV_ELF_WORD_BYTES;
1005 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1006 }
1007 }
1008 else
1009 {
1010 s->size += RISCV_ELF_WORD_BYTES;
1011 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
1012 htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1013 }
1014 }
1015 else
1016 h->got.offset = (bfd_vma) -1;
1017
1018 eh = (struct riscv_elf_link_hash_entry *) h;
1019 if (eh->dyn_relocs == NULL)
1020 return TRUE;
1021
1022 /* In the shared -Bsymbolic case, discard space allocated for
1023 dynamic pc-relative relocs against symbols which turn out to be
1024 defined in regular objects. For the normal shared case, discard
1025 space for pc-relative relocs that have become local due to symbol
1026 visibility changes. */
1027
1028 if (bfd_link_pic (info))
1029 {
1030 if (SYMBOL_CALLS_LOCAL (info, h))
1031 {
1032 struct riscv_elf_dyn_relocs **pp;
1033
1034 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1035 {
1036 p->count -= p->pc_count;
1037 p->pc_count = 0;
1038 if (p->count == 0)
1039 *pp = p->next;
1040 else
1041 pp = &p->next;
1042 }
1043 }
1044
1045 /* Also discard relocs on undefined weak syms with non-default
1046 visibility. */
1047 if (eh->dyn_relocs != NULL
1048 && h->root.type == bfd_link_hash_undefweak)
1049 {
1050 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1051 eh->dyn_relocs = NULL;
1052
1053 /* Make sure undefined weak symbols are output as a dynamic
1054 symbol in PIEs. */
1055 else if (h->dynindx == -1
1056 && !h->forced_local)
1057 {
1058 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1059 return FALSE;
1060 }
1061 }
1062 }
1063 else
1064 {
1065 /* For the non-shared case, discard space for relocs against
1066 symbols which turn out to need copy relocs or are not
1067 dynamic. */
1068
1069 if (!h->non_got_ref
1070 && ((h->def_dynamic
1071 && !h->def_regular)
1072 || (htab->elf.dynamic_sections_created
1073 && (h->root.type == bfd_link_hash_undefweak
1074 || h->root.type == bfd_link_hash_undefined))))
1075 {
1076 /* Make sure this symbol is output as a dynamic symbol.
1077 Undefined weak syms won't yet be marked as dynamic. */
1078 if (h->dynindx == -1
1079 && !h->forced_local)
1080 {
1081 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1082 return FALSE;
1083 }
1084
1085 /* If that succeeded, we know we'll be keeping all the
1086 relocs. */
1087 if (h->dynindx != -1)
1088 goto keep;
1089 }
1090
1091 eh->dyn_relocs = NULL;
1092
1093 keep: ;
1094 }
1095
1096 /* Finally, allocate space. */
1097 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1098 {
1099 asection *sreloc = elf_section_data (p->sec)->sreloc;
1100 sreloc->size += p->count * sizeof (ElfNN_External_Rela);
1101 }
1102
1103 return TRUE;
1104 }
1105
1106 /* Find any dynamic relocs that apply to read-only sections. */
1107
1108 static bfd_boolean
1109 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1110 {
1111 struct riscv_elf_link_hash_entry *eh;
1112 struct riscv_elf_dyn_relocs *p;
1113
1114 eh = (struct riscv_elf_link_hash_entry *) h;
1115 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1116 {
1117 asection *s = p->sec->output_section;
1118
1119 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1120 {
1121 ((struct bfd_link_info *) inf)->flags |= DF_TEXTREL;
1122 return FALSE;
1123 }
1124 }
1125 return TRUE;
1126 }
1127
1128 static bfd_boolean
1129 riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1130 {
1131 struct riscv_elf_link_hash_table *htab;
1132 bfd *dynobj;
1133 asection *s;
1134 bfd *ibfd;
1135
1136 htab = riscv_elf_hash_table (info);
1137 BFD_ASSERT (htab != NULL);
1138 dynobj = htab->elf.dynobj;
1139 BFD_ASSERT (dynobj != NULL);
1140
1141 if (elf_hash_table (info)->dynamic_sections_created)
1142 {
1143 /* Set the contents of the .interp section to the interpreter. */
1144 if (bfd_link_executable (info) && !info->nointerp)
1145 {
1146 s = bfd_get_linker_section (dynobj, ".interp");
1147 BFD_ASSERT (s != NULL);
1148 s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
1149 s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
1150 }
1151 }
1152
1153 /* Set up .got offsets for local syms, and space for local dynamic
1154 relocs. */
1155 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1156 {
1157 bfd_signed_vma *local_got;
1158 bfd_signed_vma *end_local_got;
1159 char *local_tls_type;
1160 bfd_size_type locsymcount;
1161 Elf_Internal_Shdr *symtab_hdr;
1162 asection *srel;
1163
1164 if (! is_riscv_elf (ibfd))
1165 continue;
1166
1167 for (s = ibfd->sections; s != NULL; s = s->next)
1168 {
1169 struct riscv_elf_dyn_relocs *p;
1170
1171 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1172 {
1173 if (!bfd_is_abs_section (p->sec)
1174 && bfd_is_abs_section (p->sec->output_section))
1175 {
1176 /* Input section has been discarded, either because
1177 it is a copy of a linkonce section or due to
1178 linker script /DISCARD/, so we'll be discarding
1179 the relocs too. */
1180 }
1181 else if (p->count != 0)
1182 {
1183 srel = elf_section_data (p->sec)->sreloc;
1184 srel->size += p->count * sizeof (ElfNN_External_Rela);
1185 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1186 info->flags |= DF_TEXTREL;
1187 }
1188 }
1189 }
1190
1191 local_got = elf_local_got_refcounts (ibfd);
1192 if (!local_got)
1193 continue;
1194
1195 symtab_hdr = &elf_symtab_hdr (ibfd);
1196 locsymcount = symtab_hdr->sh_info;
1197 end_local_got = local_got + locsymcount;
1198 local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
1199 s = htab->elf.sgot;
1200 srel = htab->elf.srelgot;
1201 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1202 {
1203 if (*local_got > 0)
1204 {
1205 *local_got = s->size;
1206 s->size += RISCV_ELF_WORD_BYTES;
1207 if (*local_tls_type & GOT_TLS_GD)
1208 s->size += RISCV_ELF_WORD_BYTES;
1209 if (bfd_link_pic (info)
1210 || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
1211 srel->size += sizeof (ElfNN_External_Rela);
1212 }
1213 else
1214 *local_got = (bfd_vma) -1;
1215 }
1216 }
1217
1218 /* Allocate global sym .plt and .got entries, and space for global
1219 sym dynamic relocs. */
1220 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1221
1222 if (htab->elf.sgotplt)
1223 {
1224 struct elf_link_hash_entry *got;
1225 got = elf_link_hash_lookup (elf_hash_table (info),
1226 "_GLOBAL_OFFSET_TABLE_",
1227 FALSE, FALSE, FALSE);
1228
1229 /* Don't allocate .got.plt section if there are no GOT nor PLT
1230 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1231 if ((got == NULL
1232 || !got->ref_regular_nonweak)
1233 && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
1234 && (htab->elf.splt == NULL
1235 || htab->elf.splt->size == 0)
1236 && (htab->elf.sgot == NULL
1237 || (htab->elf.sgot->size
1238 == get_elf_backend_data (output_bfd)->got_header_size)))
1239 htab->elf.sgotplt->size = 0;
1240 }
1241
1242 /* The check_relocs and adjust_dynamic_symbol entry points have
1243 determined the sizes of the various dynamic sections. Allocate
1244 memory for them. */
1245 for (s = dynobj->sections; s != NULL; s = s->next)
1246 {
1247 if ((s->flags & SEC_LINKER_CREATED) == 0)
1248 continue;
1249
1250 if (s == htab->elf.splt
1251 || s == htab->elf.sgot
1252 || s == htab->elf.sgotplt
1253 || s == htab->elf.sdynbss
1254 || s == htab->elf.sdynrelro)
1255 {
1256 /* Strip this section if we don't need it; see the
1257 comment below. */
1258 }
1259 else if (strncmp (s->name, ".rela", 5) == 0)
1260 {
1261 if (s->size != 0)
1262 {
1263 /* We use the reloc_count field as a counter if we need
1264 to copy relocs into the output file. */
1265 s->reloc_count = 0;
1266 }
1267 }
1268 else
1269 {
1270 /* It's not one of our sections. */
1271 continue;
1272 }
1273
1274 if (s->size == 0)
1275 {
1276 /* If we don't need this section, strip it from the
1277 output file. This is mostly to handle .rela.bss and
1278 .rela.plt. We must create both sections in
1279 create_dynamic_sections, because they must be created
1280 before the linker maps input sections to output
1281 sections. The linker does that before
1282 adjust_dynamic_symbol is called, and it is that
1283 function which decides whether anything needs to go
1284 into these sections. */
1285 s->flags |= SEC_EXCLUDE;
1286 continue;
1287 }
1288
1289 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1290 continue;
1291
1292 /* Allocate memory for the section contents. Zero the memory
1293 for the benefit of .rela.plt, which has 4 unused entries
1294 at the beginning, and we don't want garbage. */
1295 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1296 if (s->contents == NULL)
1297 return FALSE;
1298 }
1299
1300 if (elf_hash_table (info)->dynamic_sections_created)
1301 {
1302 /* Add some entries to the .dynamic section. We fill in the
1303 values later, in riscv_elf_finish_dynamic_sections, but we
1304 must add the entries now so that we get the correct size for
1305 the .dynamic section. The DT_DEBUG entry is filled in by the
1306 dynamic linker and used by the debugger. */
1307 #define add_dynamic_entry(TAG, VAL) \
1308 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1309
1310 if (bfd_link_executable (info))
1311 {
1312 if (!add_dynamic_entry (DT_DEBUG, 0))
1313 return FALSE;
1314 }
1315
1316 if (htab->elf.srelplt->size != 0)
1317 {
1318 if (!add_dynamic_entry (DT_PLTGOT, 0)
1319 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1320 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1321 || !add_dynamic_entry (DT_JMPREL, 0))
1322 return FALSE;
1323 }
1324
1325 if (!add_dynamic_entry (DT_RELA, 0)
1326 || !add_dynamic_entry (DT_RELASZ, 0)
1327 || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
1328 return FALSE;
1329
1330 /* If any dynamic relocs apply to a read-only section,
1331 then we need a DT_TEXTREL entry. */
1332 if ((info->flags & DF_TEXTREL) == 0)
1333 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
1334
1335 if (info->flags & DF_TEXTREL)
1336 {
1337 if (!add_dynamic_entry (DT_TEXTREL, 0))
1338 return FALSE;
1339 }
1340 }
1341 #undef add_dynamic_entry
1342
1343 return TRUE;
1344 }
1345
1346 #define TP_OFFSET 0
1347 #define DTP_OFFSET 0x800
1348
1349 /* Return the relocation value for a TLS dtp-relative reloc. */
1350
1351 static bfd_vma
1352 dtpoff (struct bfd_link_info *info, bfd_vma address)
1353 {
1354 /* If tls_sec is NULL, we should have signalled an error already. */
1355 if (elf_hash_table (info)->tls_sec == NULL)
1356 return 0;
1357 return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1358 }
1359
1360 /* Return the relocation value for a static TLS tp-relative relocation. */
1361
1362 static bfd_vma
1363 tpoff (struct bfd_link_info *info, bfd_vma address)
1364 {
1365 /* If tls_sec is NULL, we should have signalled an error already. */
1366 if (elf_hash_table (info)->tls_sec == NULL)
1367 return 0;
1368 return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1369 }
1370
1371 /* Return the global pointer's value, or 0 if it is not in use. */
1372
1373 static bfd_vma
1374 riscv_global_pointer_value (struct bfd_link_info *info)
1375 {
1376 struct bfd_link_hash_entry *h;
1377
1378 h = bfd_link_hash_lookup (info->hash, RISCV_GP_SYMBOL, FALSE, FALSE, TRUE);
1379 if (h == NULL || h->type != bfd_link_hash_defined)
1380 return 0;
1381
1382 return h->u.def.value + sec_addr (h->u.def.section);
1383 }
1384
1385 /* Emplace a static relocation. */
1386
1387 static bfd_reloc_status_type
1388 perform_relocation (const reloc_howto_type *howto,
1389 const Elf_Internal_Rela *rel,
1390 bfd_vma value,
1391 asection *input_section,
1392 bfd *input_bfd,
1393 bfd_byte *contents)
1394 {
1395 if (howto->pc_relative)
1396 value -= sec_addr (input_section) + rel->r_offset;
1397 value += rel->r_addend;
1398
1399 switch (ELFNN_R_TYPE (rel->r_info))
1400 {
1401 case R_RISCV_HI20:
1402 case R_RISCV_TPREL_HI20:
1403 case R_RISCV_PCREL_HI20:
1404 case R_RISCV_GOT_HI20:
1405 case R_RISCV_TLS_GOT_HI20:
1406 case R_RISCV_TLS_GD_HI20:
1407 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1408 return bfd_reloc_overflow;
1409 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1410 break;
1411
1412 case R_RISCV_LO12_I:
1413 case R_RISCV_GPREL_I:
1414 case R_RISCV_TPREL_LO12_I:
1415 case R_RISCV_TPREL_I:
1416 case R_RISCV_PCREL_LO12_I:
1417 value = ENCODE_ITYPE_IMM (value);
1418 break;
1419
1420 case R_RISCV_LO12_S:
1421 case R_RISCV_GPREL_S:
1422 case R_RISCV_TPREL_LO12_S:
1423 case R_RISCV_TPREL_S:
1424 case R_RISCV_PCREL_LO12_S:
1425 value = ENCODE_STYPE_IMM (value);
1426 break;
1427
1428 case R_RISCV_CALL:
1429 case R_RISCV_CALL_PLT:
1430 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1431 return bfd_reloc_overflow;
1432 value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1433 | (ENCODE_ITYPE_IMM (value) << 32);
1434 break;
1435
1436 case R_RISCV_JAL:
1437 if (!VALID_UJTYPE_IMM (value))
1438 return bfd_reloc_overflow;
1439 value = ENCODE_UJTYPE_IMM (value);
1440 break;
1441
1442 case R_RISCV_BRANCH:
1443 if (!VALID_SBTYPE_IMM (value))
1444 return bfd_reloc_overflow;
1445 value = ENCODE_SBTYPE_IMM (value);
1446 break;
1447
1448 case R_RISCV_RVC_BRANCH:
1449 if (!VALID_RVC_B_IMM (value))
1450 return bfd_reloc_overflow;
1451 value = ENCODE_RVC_B_IMM (value);
1452 break;
1453
1454 case R_RISCV_RVC_JUMP:
1455 if (!VALID_RVC_J_IMM (value))
1456 return bfd_reloc_overflow;
1457 value = ENCODE_RVC_J_IMM (value);
1458 break;
1459
1460 case R_RISCV_RVC_LUI:
1461 if (!VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value)))
1462 return bfd_reloc_overflow;
1463 value = ENCODE_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (value));
1464 break;
1465
1466 case R_RISCV_32:
1467 case R_RISCV_64:
1468 case R_RISCV_ADD8:
1469 case R_RISCV_ADD16:
1470 case R_RISCV_ADD32:
1471 case R_RISCV_ADD64:
1472 case R_RISCV_SUB6:
1473 case R_RISCV_SUB8:
1474 case R_RISCV_SUB16:
1475 case R_RISCV_SUB32:
1476 case R_RISCV_SUB64:
1477 case R_RISCV_SET6:
1478 case R_RISCV_SET8:
1479 case R_RISCV_SET16:
1480 case R_RISCV_SET32:
1481 case R_RISCV_32_PCREL:
1482 case R_RISCV_TLS_DTPREL32:
1483 case R_RISCV_TLS_DTPREL64:
1484 break;
1485
1486 default:
1487 return bfd_reloc_notsupported;
1488 }
1489
1490 bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
1491 word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
1492 bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
1493
1494 return bfd_reloc_ok;
1495 }
1496
1497 /* Remember all PC-relative high-part relocs we've encountered to help us
1498 later resolve the corresponding low-part relocs. */
1499
1500 typedef struct
1501 {
1502 bfd_vma address;
1503 bfd_vma value;
1504 } riscv_pcrel_hi_reloc;
1505
1506 typedef struct riscv_pcrel_lo_reloc
1507 {
1508 asection * input_section;
1509 struct bfd_link_info * info;
1510 reloc_howto_type * howto;
1511 const Elf_Internal_Rela * reloc;
1512 bfd_vma addr;
1513 const char * name;
1514 bfd_byte * contents;
1515 struct riscv_pcrel_lo_reloc * next;
1516 } riscv_pcrel_lo_reloc;
1517
1518 typedef struct
1519 {
1520 htab_t hi_relocs;
1521 riscv_pcrel_lo_reloc *lo_relocs;
1522 } riscv_pcrel_relocs;
1523
1524 static hashval_t
1525 riscv_pcrel_reloc_hash (const void *entry)
1526 {
1527 const riscv_pcrel_hi_reloc *e = entry;
1528 return (hashval_t)(e->address >> 2);
1529 }
1530
1531 static bfd_boolean
1532 riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
1533 {
1534 const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
1535 return e1->address == e2->address;
1536 }
1537
1538 static bfd_boolean
1539 riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
1540 {
1541
1542 p->lo_relocs = NULL;
1543 p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
1544 riscv_pcrel_reloc_eq, free);
1545 return p->hi_relocs != NULL;
1546 }
1547
1548 static void
1549 riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
1550 {
1551 riscv_pcrel_lo_reloc *cur = p->lo_relocs;
1552
1553 while (cur != NULL)
1554 {
1555 riscv_pcrel_lo_reloc *next = cur->next;
1556 free (cur);
1557 cur = next;
1558 }
1559
1560 htab_delete (p->hi_relocs);
1561 }
1562
1563 static bfd_boolean
1564 riscv_zero_pcrel_hi_reloc (Elf_Internal_Rela *rel,
1565 struct bfd_link_info *info,
1566 bfd_vma pc,
1567 bfd_vma addr,
1568 bfd_byte *contents,
1569 const reloc_howto_type *howto,
1570 bfd *input_bfd)
1571 {
1572 /* We may need to reference low addreses in PC-relative modes even when the
1573 * PC is far away from these addresses. For example, undefweak references
1574 * need to produce the address 0 when linked. As 0 is far from the arbitrary
1575 * addresses that we can link PC-relative programs at, the linker can't
1576 * actually relocate references to those symbols. In order to allow these
1577 * programs to work we simply convert the PC-relative auipc sequences to
1578 * 0-relative lui sequences. */
1579 if (bfd_link_pic (info))
1580 return FALSE;
1581
1582 /* If it's possible to reference the symbol using auipc we do so, as that's
1583 * more in the spirit of the PC-relative relocations we're processing. */
1584 bfd_vma offset = addr - pc;
1585 if (ARCH_SIZE == 32 || VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (offset)))
1586 return FALSE;
1587
1588 /* If it's impossible to reference this with a LUI-based offset then don't
1589 * bother to convert it at all so users still see the PC-relative relocation
1590 * in the truncation message. */
1591 if (ARCH_SIZE > 32 && !VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (addr)))
1592 return FALSE;
1593
1594 rel->r_info = ELFNN_R_INFO(addr, R_RISCV_HI20);
1595
1596 bfd_vma insn = bfd_get(howto->bitsize, input_bfd, contents + rel->r_offset);
1597 insn = (insn & ~MASK_AUIPC) | MATCH_LUI;
1598 bfd_put(howto->bitsize, input_bfd, insn, contents + rel->r_offset);
1599 return TRUE;
1600 }
1601
1602 static bfd_boolean
1603 riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr,
1604 bfd_vma value, bfd_boolean absolute)
1605 {
1606 bfd_vma offset = absolute ? value : value - addr;
1607 riscv_pcrel_hi_reloc entry = {addr, offset};
1608 riscv_pcrel_hi_reloc **slot =
1609 (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
1610
1611 BFD_ASSERT (*slot == NULL);
1612 *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
1613 if (*slot == NULL)
1614 return FALSE;
1615 **slot = entry;
1616 return TRUE;
1617 }
1618
1619 static bfd_boolean
1620 riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
1621 asection *input_section,
1622 struct bfd_link_info *info,
1623 reloc_howto_type *howto,
1624 const Elf_Internal_Rela *reloc,
1625 bfd_vma addr,
1626 const char *name,
1627 bfd_byte *contents)
1628 {
1629 riscv_pcrel_lo_reloc *entry;
1630 entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
1631 if (entry == NULL)
1632 return FALSE;
1633 *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr,
1634 name, contents, p->lo_relocs};
1635 p->lo_relocs = entry;
1636 return TRUE;
1637 }
1638
1639 static bfd_boolean
1640 riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
1641 {
1642 riscv_pcrel_lo_reloc *r;
1643
1644 for (r = p->lo_relocs; r != NULL; r = r->next)
1645 {
1646 bfd *input_bfd = r->input_section->owner;
1647
1648 riscv_pcrel_hi_reloc search = {r->addr, 0};
1649 riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
1650 if (entry == NULL)
1651 {
1652 ((*r->info->callbacks->reloc_overflow)
1653 (r->info, NULL, r->name, r->howto->name, (bfd_vma) 0,
1654 input_bfd, r->input_section, r->reloc->r_offset));
1655 return TRUE;
1656 }
1657
1658 perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
1659 input_bfd, r->contents);
1660 }
1661
1662 return TRUE;
1663 }
1664
1665 /* Relocate a RISC-V ELF section.
1666
1667 The RELOCATE_SECTION function is called by the new ELF backend linker
1668 to handle the relocations for a section.
1669
1670 The relocs are always passed as Rela structures.
1671
1672 This function is responsible for adjusting the section contents as
1673 necessary, and (if generating a relocatable output file) adjusting
1674 the reloc addend as necessary.
1675
1676 This function does not have to worry about setting the reloc
1677 address or the reloc symbol index.
1678
1679 LOCAL_SYMS is a pointer to the swapped in local symbols.
1680
1681 LOCAL_SECTIONS is an array giving the section in the input file
1682 corresponding to the st_shndx field of each local symbol.
1683
1684 The global hash table entry for the global symbols can be found
1685 via elf_sym_hashes (input_bfd).
1686
1687 When generating relocatable output, this function must handle
1688 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1689 going to be the section symbol corresponding to the output
1690 section, which means that the addend must be adjusted
1691 accordingly. */
1692
1693 static bfd_boolean
1694 riscv_elf_relocate_section (bfd *output_bfd,
1695 struct bfd_link_info *info,
1696 bfd *input_bfd,
1697 asection *input_section,
1698 bfd_byte *contents,
1699 Elf_Internal_Rela *relocs,
1700 Elf_Internal_Sym *local_syms,
1701 asection **local_sections)
1702 {
1703 Elf_Internal_Rela *rel;
1704 Elf_Internal_Rela *relend;
1705 riscv_pcrel_relocs pcrel_relocs;
1706 bfd_boolean ret = FALSE;
1707 asection *sreloc = elf_section_data (input_section)->sreloc;
1708 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
1709 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1710 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1711 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
1712 bfd_boolean absolute;
1713
1714 if (!riscv_init_pcrel_relocs (&pcrel_relocs))
1715 return FALSE;
1716
1717 relend = relocs + input_section->reloc_count;
1718 for (rel = relocs; rel < relend; rel++)
1719 {
1720 unsigned long r_symndx;
1721 struct elf_link_hash_entry *h;
1722 Elf_Internal_Sym *sym;
1723 asection *sec;
1724 bfd_vma relocation;
1725 bfd_reloc_status_type r = bfd_reloc_ok;
1726 const char *name;
1727 bfd_vma off, ie_off;
1728 bfd_boolean unresolved_reloc, is_ie = FALSE;
1729 bfd_vma pc = sec_addr (input_section) + rel->r_offset;
1730 int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
1731 reloc_howto_type *howto = riscv_elf_rtype_to_howto (r_type);
1732 const char *msg = NULL;
1733
1734 if (r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
1735 continue;
1736
1737 /* This is a final link. */
1738 r_symndx = ELFNN_R_SYM (rel->r_info);
1739 h = NULL;
1740 sym = NULL;
1741 sec = NULL;
1742 unresolved_reloc = FALSE;
1743 if (r_symndx < symtab_hdr->sh_info)
1744 {
1745 sym = local_syms + r_symndx;
1746 sec = local_sections[r_symndx];
1747 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1748 }
1749 else
1750 {
1751 bfd_boolean warned, ignored;
1752
1753 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1754 r_symndx, symtab_hdr, sym_hashes,
1755 h, sec, relocation,
1756 unresolved_reloc, warned, ignored);
1757 if (warned)
1758 {
1759 /* To avoid generating warning messages about truncated
1760 relocations, set the relocation's address to be the same as
1761 the start of this section. */
1762 if (input_section->output_section != NULL)
1763 relocation = input_section->output_section->vma;
1764 else
1765 relocation = 0;
1766 }
1767 }
1768
1769 if (sec != NULL && discarded_section (sec))
1770 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1771 rel, 1, relend, howto, 0, contents);
1772
1773 if (bfd_link_relocatable (info))
1774 continue;
1775
1776 if (h != NULL)
1777 name = h->root.root.string;
1778 else
1779 {
1780 name = (bfd_elf_string_from_elf_section
1781 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1782 if (name == NULL || *name == '\0')
1783 name = bfd_section_name (input_bfd, sec);
1784 }
1785
1786 switch (r_type)
1787 {
1788 case R_RISCV_NONE:
1789 case R_RISCV_RELAX:
1790 case R_RISCV_TPREL_ADD:
1791 case R_RISCV_COPY:
1792 case R_RISCV_JUMP_SLOT:
1793 case R_RISCV_RELATIVE:
1794 /* These require nothing of us at all. */
1795 continue;
1796
1797 case R_RISCV_HI20:
1798 case R_RISCV_BRANCH:
1799 case R_RISCV_RVC_BRANCH:
1800 case R_RISCV_RVC_LUI:
1801 case R_RISCV_LO12_I:
1802 case R_RISCV_LO12_S:
1803 case R_RISCV_SET6:
1804 case R_RISCV_SET8:
1805 case R_RISCV_SET16:
1806 case R_RISCV_SET32:
1807 case R_RISCV_32_PCREL:
1808 /* These require no special handling beyond perform_relocation. */
1809 break;
1810
1811 case R_RISCV_GOT_HI20:
1812 if (h != NULL)
1813 {
1814 bfd_boolean dyn, pic;
1815
1816 off = h->got.offset;
1817 BFD_ASSERT (off != (bfd_vma) -1);
1818 dyn = elf_hash_table (info)->dynamic_sections_created;
1819 pic = bfd_link_pic (info);
1820
1821 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
1822 || (pic && SYMBOL_REFERENCES_LOCAL (info, h)))
1823 {
1824 /* This is actually a static link, or it is a
1825 -Bsymbolic link and the symbol is defined
1826 locally, or the symbol was forced to be local
1827 because of a version file. We must initialize
1828 this entry in the global offset table. Since the
1829 offset must always be a multiple of the word size,
1830 we use the least significant bit to record whether
1831 we have initialized it already.
1832
1833 When doing a dynamic link, we create a .rela.got
1834 relocation entry to initialize the value. This
1835 is done in the finish_dynamic_symbol routine. */
1836 if ((off & 1) != 0)
1837 off &= ~1;
1838 else
1839 {
1840 bfd_put_NN (output_bfd, relocation,
1841 htab->elf.sgot->contents + off);
1842 h->got.offset |= 1;
1843 }
1844 }
1845 else
1846 unresolved_reloc = FALSE;
1847 }
1848 else
1849 {
1850 BFD_ASSERT (local_got_offsets != NULL
1851 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1852
1853 off = local_got_offsets[r_symndx];
1854
1855 /* The offset must always be a multiple of the word size.
1856 So, we can use the least significant bit to record
1857 whether we have already processed this entry. */
1858 if ((off & 1) != 0)
1859 off &= ~1;
1860 else
1861 {
1862 if (bfd_link_pic (info))
1863 {
1864 asection *s;
1865 Elf_Internal_Rela outrel;
1866
1867 /* We need to generate a R_RISCV_RELATIVE reloc
1868 for the dynamic linker. */
1869 s = htab->elf.srelgot;
1870 BFD_ASSERT (s != NULL);
1871
1872 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
1873 outrel.r_info =
1874 ELFNN_R_INFO (0, R_RISCV_RELATIVE);
1875 outrel.r_addend = relocation;
1876 relocation = 0;
1877 riscv_elf_append_rela (output_bfd, s, &outrel);
1878 }
1879
1880 bfd_put_NN (output_bfd, relocation,
1881 htab->elf.sgot->contents + off);
1882 local_got_offsets[r_symndx] |= 1;
1883 }
1884 }
1885 relocation = sec_addr (htab->elf.sgot) + off;
1886 absolute = riscv_zero_pcrel_hi_reloc (rel,
1887 info,
1888 pc,
1889 relocation,
1890 contents,
1891 howto,
1892 input_bfd);
1893 r_type = ELFNN_R_TYPE (rel->r_info);
1894 howto = riscv_elf_rtype_to_howto (r_type);
1895 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
1896 relocation, absolute))
1897 r = bfd_reloc_overflow;
1898 break;
1899
1900 case R_RISCV_ADD8:
1901 case R_RISCV_ADD16:
1902 case R_RISCV_ADD32:
1903 case R_RISCV_ADD64:
1904 {
1905 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
1906 contents + rel->r_offset);
1907 relocation = old_value + relocation;
1908 }
1909 break;
1910
1911 case R_RISCV_SUB6:
1912 case R_RISCV_SUB8:
1913 case R_RISCV_SUB16:
1914 case R_RISCV_SUB32:
1915 case R_RISCV_SUB64:
1916 {
1917 bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
1918 contents + rel->r_offset);
1919 relocation = old_value - relocation;
1920 }
1921 break;
1922
1923 case R_RISCV_CALL_PLT:
1924 case R_RISCV_CALL:
1925 case R_RISCV_JAL:
1926 case R_RISCV_RVC_JUMP:
1927 if (bfd_link_pic (info) && h != NULL && h->plt.offset != MINUS_ONE)
1928 {
1929 /* Refer to the PLT entry. */
1930 relocation = sec_addr (htab->elf.splt) + h->plt.offset;
1931 unresolved_reloc = FALSE;
1932 }
1933 break;
1934
1935 case R_RISCV_TPREL_HI20:
1936 relocation = tpoff (info, relocation);
1937 break;
1938
1939 case R_RISCV_TPREL_LO12_I:
1940 case R_RISCV_TPREL_LO12_S:
1941 relocation = tpoff (info, relocation);
1942 break;
1943
1944 case R_RISCV_TPREL_I:
1945 case R_RISCV_TPREL_S:
1946 relocation = tpoff (info, relocation);
1947 if (VALID_ITYPE_IMM (relocation + rel->r_addend))
1948 {
1949 /* We can use tp as the base register. */
1950 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
1951 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
1952 insn |= X_TP << OP_SH_RS1;
1953 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
1954 }
1955 else
1956 r = bfd_reloc_overflow;
1957 break;
1958
1959 case R_RISCV_GPREL_I:
1960 case R_RISCV_GPREL_S:
1961 {
1962 bfd_vma gp = riscv_global_pointer_value (info);
1963 bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
1964 if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
1965 {
1966 /* We can use x0 or gp as the base register. */
1967 bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
1968 insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
1969 if (!x0_base)
1970 {
1971 rel->r_addend -= gp;
1972 insn |= X_GP << OP_SH_RS1;
1973 }
1974 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
1975 }
1976 else
1977 r = bfd_reloc_overflow;
1978 break;
1979 }
1980
1981 case R_RISCV_PCREL_HI20:
1982 absolute = riscv_zero_pcrel_hi_reloc (rel,
1983 info,
1984 pc,
1985 relocation,
1986 contents,
1987 howto,
1988 input_bfd);
1989 r_type = ELFNN_R_TYPE (rel->r_info);
1990 howto = riscv_elf_rtype_to_howto (r_type);
1991 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
1992 relocation + rel->r_addend,
1993 absolute))
1994 r = bfd_reloc_overflow;
1995 break;
1996
1997 case R_RISCV_PCREL_LO12_I:
1998 case R_RISCV_PCREL_LO12_S:
1999 if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info,
2000 howto, rel, relocation, name,
2001 contents))
2002 continue;
2003 r = bfd_reloc_overflow;
2004 break;
2005
2006 case R_RISCV_TLS_DTPREL32:
2007 case R_RISCV_TLS_DTPREL64:
2008 relocation = dtpoff (info, relocation);
2009 break;
2010
2011 case R_RISCV_32:
2012 case R_RISCV_64:
2013 if ((input_section->flags & SEC_ALLOC) == 0)
2014 break;
2015
2016 if ((bfd_link_pic (info)
2017 && (h == NULL
2018 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2019 || h->root.type != bfd_link_hash_undefweak)
2020 && (! howto->pc_relative
2021 || !SYMBOL_CALLS_LOCAL (info, h)))
2022 || (!bfd_link_pic (info)
2023 && h != NULL
2024 && h->dynindx != -1
2025 && !h->non_got_ref
2026 && ((h->def_dynamic
2027 && !h->def_regular)
2028 || h->root.type == bfd_link_hash_undefweak
2029 || h->root.type == bfd_link_hash_undefined)))
2030 {
2031 Elf_Internal_Rela outrel;
2032 bfd_boolean skip_static_relocation, skip_dynamic_relocation;
2033
2034 /* When generating a shared object, these relocations
2035 are copied into the output file to be resolved at run
2036 time. */
2037
2038 outrel.r_offset =
2039 _bfd_elf_section_offset (output_bfd, info, input_section,
2040 rel->r_offset);
2041 skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
2042 skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
2043 outrel.r_offset += sec_addr (input_section);
2044
2045 if (skip_dynamic_relocation)
2046 memset (&outrel, 0, sizeof outrel);
2047 else if (h != NULL && h->dynindx != -1
2048 && !(bfd_link_pic (info)
2049 && SYMBOLIC_BIND (info, h)
2050 && h->def_regular))
2051 {
2052 outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2053 outrel.r_addend = rel->r_addend;
2054 }
2055 else
2056 {
2057 outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2058 outrel.r_addend = relocation + rel->r_addend;
2059 }
2060
2061 riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2062 if (skip_static_relocation)
2063 continue;
2064 }
2065 break;
2066
2067 case R_RISCV_TLS_GOT_HI20:
2068 is_ie = TRUE;
2069 /* Fall through. */
2070
2071 case R_RISCV_TLS_GD_HI20:
2072 if (h != NULL)
2073 {
2074 off = h->got.offset;
2075 h->got.offset |= 1;
2076 }
2077 else
2078 {
2079 off = local_got_offsets[r_symndx];
2080 local_got_offsets[r_symndx] |= 1;
2081 }
2082
2083 tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
2084 BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
2085 /* If this symbol is referenced by both GD and IE TLS, the IE
2086 reference's GOT slot follows the GD reference's slots. */
2087 ie_off = 0;
2088 if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
2089 ie_off = 2 * GOT_ENTRY_SIZE;
2090
2091 if ((off & 1) != 0)
2092 off &= ~1;
2093 else
2094 {
2095 Elf_Internal_Rela outrel;
2096 int indx = 0;
2097 bfd_boolean need_relocs = FALSE;
2098
2099 if (htab->elf.srelgot == NULL)
2100 abort ();
2101
2102 if (h != NULL)
2103 {
2104 bfd_boolean dyn, pic;
2105 dyn = htab->elf.dynamic_sections_created;
2106 pic = bfd_link_pic (info);
2107
2108 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
2109 && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
2110 indx = h->dynindx;
2111 }
2112
2113 /* The GOT entries have not been initialized yet. Do it
2114 now, and emit any relocations. */
2115 if ((bfd_link_pic (info) || indx != 0)
2116 && (h == NULL
2117 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2118 || h->root.type != bfd_link_hash_undefweak))
2119 need_relocs = TRUE;
2120
2121 if (tls_type & GOT_TLS_GD)
2122 {
2123 if (need_relocs)
2124 {
2125 outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2126 outrel.r_addend = 0;
2127 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
2128 bfd_put_NN (output_bfd, 0,
2129 htab->elf.sgot->contents + off);
2130 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2131 if (indx == 0)
2132 {
2133 BFD_ASSERT (! unresolved_reloc);
2134 bfd_put_NN (output_bfd,
2135 dtpoff (info, relocation),
2136 (htab->elf.sgot->contents + off +
2137 RISCV_ELF_WORD_BYTES));
2138 }
2139 else
2140 {
2141 bfd_put_NN (output_bfd, 0,
2142 (htab->elf.sgot->contents + off +
2143 RISCV_ELF_WORD_BYTES));
2144 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
2145 outrel.r_offset += RISCV_ELF_WORD_BYTES;
2146 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2147 }
2148 }
2149 else
2150 {
2151 /* If we are not emitting relocations for a
2152 general dynamic reference, then we must be in a
2153 static link or an executable link with the
2154 symbol binding locally. Mark it as belonging
2155 to module 1, the executable. */
2156 bfd_put_NN (output_bfd, 1,
2157 htab->elf.sgot->contents + off);
2158 bfd_put_NN (output_bfd,
2159 dtpoff (info, relocation),
2160 (htab->elf.sgot->contents + off +
2161 RISCV_ELF_WORD_BYTES));
2162 }
2163 }
2164
2165 if (tls_type & GOT_TLS_IE)
2166 {
2167 if (need_relocs)
2168 {
2169 bfd_put_NN (output_bfd, 0,
2170 htab->elf.sgot->contents + off + ie_off);
2171 outrel.r_offset = sec_addr (htab->elf.sgot)
2172 + off + ie_off;
2173 outrel.r_addend = 0;
2174 if (indx == 0)
2175 outrel.r_addend = tpoff (info, relocation);
2176 outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
2177 riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2178 }
2179 else
2180 {
2181 bfd_put_NN (output_bfd, tpoff (info, relocation),
2182 htab->elf.sgot->contents + off + ie_off);
2183 }
2184 }
2185 }
2186
2187 BFD_ASSERT (off < (bfd_vma) -2);
2188 relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
2189 if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2190 relocation, FALSE))
2191 r = bfd_reloc_overflow;
2192 unresolved_reloc = FALSE;
2193 break;
2194
2195 default:
2196 r = bfd_reloc_notsupported;
2197 }
2198
2199 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2200 because such sections are not SEC_ALLOC and thus ld.so will
2201 not process them. */
2202 if (unresolved_reloc
2203 && !((input_section->flags & SEC_DEBUGGING) != 0
2204 && h->def_dynamic)
2205 && _bfd_elf_section_offset (output_bfd, info, input_section,
2206 rel->r_offset) != (bfd_vma) -1)
2207 {
2208 (*_bfd_error_handler)
2209 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
2210 input_bfd,
2211 input_section,
2212 rel->r_offset,
2213 howto->name,
2214 h->root.root.string);
2215 continue;
2216 }
2217
2218 if (r == bfd_reloc_ok)
2219 r = perform_relocation (howto, rel, relocation, input_section,
2220 input_bfd, contents);
2221
2222 switch (r)
2223 {
2224 case bfd_reloc_ok:
2225 continue;
2226
2227 case bfd_reloc_overflow:
2228 info->callbacks->reloc_overflow
2229 (info, (h ? &h->root : NULL), name, howto->name,
2230 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2231 break;
2232
2233 case bfd_reloc_undefined:
2234 info->callbacks->undefined_symbol
2235 (info, name, input_bfd, input_section, rel->r_offset,
2236 TRUE);
2237 break;
2238
2239 case bfd_reloc_outofrange:
2240 msg = _("internal error: out of range error");
2241 break;
2242
2243 case bfd_reloc_notsupported:
2244 msg = _("internal error: unsupported relocation error");
2245 break;
2246
2247 case bfd_reloc_dangerous:
2248 msg = _("internal error: dangerous relocation");
2249 break;
2250
2251 default:
2252 msg = _("internal error: unknown error");
2253 break;
2254 }
2255
2256 if (msg)
2257 info->callbacks->warning
2258 (info, msg, name, input_bfd, input_section, rel->r_offset);
2259 goto out;
2260 }
2261
2262 ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
2263 out:
2264 riscv_free_pcrel_relocs (&pcrel_relocs);
2265 return ret;
2266 }
2267
2268 /* Finish up dynamic symbol handling. We set the contents of various
2269 dynamic sections here. */
2270
2271 static bfd_boolean
2272 riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
2273 struct bfd_link_info *info,
2274 struct elf_link_hash_entry *h,
2275 Elf_Internal_Sym *sym)
2276 {
2277 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2278 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2279
2280 if (h->plt.offset != (bfd_vma) -1)
2281 {
2282 /* We've decided to create a PLT entry for this symbol. */
2283 bfd_byte *loc;
2284 bfd_vma i, header_address, plt_idx, got_address;
2285 uint32_t plt_entry[PLT_ENTRY_INSNS];
2286 Elf_Internal_Rela rela;
2287
2288 BFD_ASSERT (h->dynindx != -1);
2289
2290 /* Calculate the address of the PLT header. */
2291 header_address = sec_addr (htab->elf.splt);
2292
2293 /* Calculate the index of the entry. */
2294 plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2295
2296 /* Calculate the address of the .got.plt entry. */
2297 got_address = riscv_elf_got_plt_val (plt_idx, info);
2298
2299 /* Find out where the .plt entry should go. */
2300 loc = htab->elf.splt->contents + h->plt.offset;
2301
2302 /* Fill in the PLT entry itself. */
2303 riscv_make_plt_entry (got_address, header_address + h->plt.offset,
2304 plt_entry);
2305 for (i = 0; i < PLT_ENTRY_INSNS; i++)
2306 bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i);
2307
2308 /* Fill in the initial value of the .got.plt entry. */
2309 loc = htab->elf.sgotplt->contents
2310 + (got_address - sec_addr (htab->elf.sgotplt));
2311 bfd_put_NN (output_bfd, sec_addr (htab->elf.splt), loc);
2312
2313 /* Fill in the entry in the .rela.plt section. */
2314 rela.r_offset = got_address;
2315 rela.r_addend = 0;
2316 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
2317
2318 loc = htab->elf.srelplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
2319 bed->s->swap_reloca_out (output_bfd, &rela, loc);
2320
2321 if (!h->def_regular)
2322 {
2323 /* Mark the symbol as undefined, rather than as defined in
2324 the .plt section. Leave the value alone. */
2325 sym->st_shndx = SHN_UNDEF;
2326 /* If the symbol is weak, we do need to clear the value.
2327 Otherwise, the PLT entry would provide a definition for
2328 the symbol even if the symbol wasn't defined anywhere,
2329 and so the symbol would never be NULL. */
2330 if (!h->ref_regular_nonweak)
2331 sym->st_value = 0;
2332 }
2333 }
2334
2335 if (h->got.offset != (bfd_vma) -1
2336 && !(riscv_elf_hash_entry (h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
2337 {
2338 asection *sgot;
2339 asection *srela;
2340 Elf_Internal_Rela rela;
2341
2342 /* This symbol has an entry in the GOT. Set it up. */
2343
2344 sgot = htab->elf.sgot;
2345 srela = htab->elf.srelgot;
2346 BFD_ASSERT (sgot != NULL && srela != NULL);
2347
2348 rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
2349
2350 /* If this is a -Bsymbolic link, and the symbol is defined
2351 locally, we just want to emit a RELATIVE reloc. Likewise if
2352 the symbol was forced to be local because of a version file.
2353 The entry in the global offset table will already have been
2354 initialized in the relocate_section function. */
2355 if (bfd_link_pic (info)
2356 && (info->symbolic || h->dynindx == -1)
2357 && h->def_regular)
2358 {
2359 asection *sec = h->root.u.def.section;
2360 rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2361 rela.r_addend = (h->root.u.def.value
2362 + sec->output_section->vma
2363 + sec->output_offset);
2364 }
2365 else
2366 {
2367 BFD_ASSERT (h->dynindx != -1);
2368 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
2369 rela.r_addend = 0;
2370 }
2371
2372 bfd_put_NN (output_bfd, 0,
2373 sgot->contents + (h->got.offset & ~(bfd_vma) 1));
2374 riscv_elf_append_rela (output_bfd, srela, &rela);
2375 }
2376
2377 if (h->needs_copy)
2378 {
2379 Elf_Internal_Rela rela;
2380 asection *s;
2381
2382 /* This symbols needs a copy reloc. Set it up. */
2383 BFD_ASSERT (h->dynindx != -1);
2384
2385 rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
2386 rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
2387 rela.r_addend = 0;
2388 if (h->root.u.def.section == htab->elf.sdynrelro)
2389 s = htab->elf.sreldynrelro;
2390 else
2391 s = htab->elf.srelbss;
2392 riscv_elf_append_rela (output_bfd, s, &rela);
2393 }
2394
2395 /* Mark some specially defined symbols as absolute. */
2396 if (h == htab->elf.hdynamic
2397 || (h == htab->elf.hgot || h == htab->elf.hplt))
2398 sym->st_shndx = SHN_ABS;
2399
2400 return TRUE;
2401 }
2402
2403 /* Finish up the dynamic sections. */
2404
2405 static bfd_boolean
2406 riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
2407 bfd *dynobj, asection *sdyn)
2408 {
2409 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2410 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2411 size_t dynsize = bed->s->sizeof_dyn;
2412 bfd_byte *dyncon, *dynconend;
2413
2414 dynconend = sdyn->contents + sdyn->size;
2415 for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
2416 {
2417 Elf_Internal_Dyn dyn;
2418 asection *s;
2419
2420 bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
2421
2422 switch (dyn.d_tag)
2423 {
2424 case DT_PLTGOT:
2425 s = htab->elf.sgotplt;
2426 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2427 break;
2428 case DT_JMPREL:
2429 s = htab->elf.srelplt;
2430 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2431 break;
2432 case DT_PLTRELSZ:
2433 s = htab->elf.srelplt;
2434 dyn.d_un.d_val = s->size;
2435 break;
2436 default:
2437 continue;
2438 }
2439
2440 bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
2441 }
2442 return TRUE;
2443 }
2444
2445 static bfd_boolean
2446 riscv_elf_finish_dynamic_sections (bfd *output_bfd,
2447 struct bfd_link_info *info)
2448 {
2449 bfd *dynobj;
2450 asection *sdyn;
2451 struct riscv_elf_link_hash_table *htab;
2452
2453 htab = riscv_elf_hash_table (info);
2454 BFD_ASSERT (htab != NULL);
2455 dynobj = htab->elf.dynobj;
2456
2457 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2458
2459 if (elf_hash_table (info)->dynamic_sections_created)
2460 {
2461 asection *splt;
2462 bfd_boolean ret;
2463
2464 splt = htab->elf.splt;
2465 BFD_ASSERT (splt != NULL && sdyn != NULL);
2466
2467 ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
2468
2469 if (!ret)
2470 return ret;
2471
2472 /* Fill in the head and tail entries in the procedure linkage table. */
2473 if (splt->size > 0)
2474 {
2475 int i;
2476 uint32_t plt_header[PLT_HEADER_INSNS];
2477 riscv_make_plt_header (sec_addr (htab->elf.sgotplt),
2478 sec_addr (splt), plt_header);
2479
2480 for (i = 0; i < PLT_HEADER_INSNS; i++)
2481 bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i);
2482
2483 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2484 = PLT_ENTRY_SIZE;
2485 }
2486 }
2487
2488 if (htab->elf.sgotplt)
2489 {
2490 asection *output_section = htab->elf.sgotplt->output_section;
2491
2492 if (bfd_is_abs_section (output_section))
2493 {
2494 (*_bfd_error_handler)
2495 (_("discarded output section: `%A'"), htab->elf.sgotplt);
2496 return FALSE;
2497 }
2498
2499 if (htab->elf.sgotplt->size > 0)
2500 {
2501 /* Write the first two entries in .got.plt, needed for the dynamic
2502 linker. */
2503 bfd_put_NN (output_bfd, (bfd_vma) -1, htab->elf.sgotplt->contents);
2504 bfd_put_NN (output_bfd, (bfd_vma) 0,
2505 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
2506 }
2507
2508 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
2509 }
2510
2511 if (htab->elf.sgot)
2512 {
2513 asection *output_section = htab->elf.sgot->output_section;
2514
2515 if (htab->elf.sgot->size > 0)
2516 {
2517 /* Set the first entry in the global offset table to the address of
2518 the dynamic section. */
2519 bfd_vma val = sdyn ? sec_addr (sdyn) : 0;
2520 bfd_put_NN (output_bfd, val, htab->elf.sgot->contents);
2521 }
2522
2523 elf_section_data (output_section)->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
2524 }
2525
2526 return TRUE;
2527 }
2528
2529 /* Return address for Ith PLT stub in section PLT, for relocation REL
2530 or (bfd_vma) -1 if it should not be included. */
2531
2532 static bfd_vma
2533 riscv_elf_plt_sym_val (bfd_vma i, const asection *plt,
2534 const arelent *rel ATTRIBUTE_UNUSED)
2535 {
2536 return plt->vma + PLT_HEADER_SIZE + i * PLT_ENTRY_SIZE;
2537 }
2538
2539 static enum elf_reloc_type_class
2540 riscv_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2541 const asection *rel_sec ATTRIBUTE_UNUSED,
2542 const Elf_Internal_Rela *rela)
2543 {
2544 switch (ELFNN_R_TYPE (rela->r_info))
2545 {
2546 case R_RISCV_RELATIVE:
2547 return reloc_class_relative;
2548 case R_RISCV_JUMP_SLOT:
2549 return reloc_class_plt;
2550 case R_RISCV_COPY:
2551 return reloc_class_copy;
2552 default:
2553 return reloc_class_normal;
2554 }
2555 }
2556
2557 /* Merge backend specific data from an object file to the output
2558 object file when linking. */
2559
2560 static bfd_boolean
2561 _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2562 {
2563 bfd *obfd = info->output_bfd;
2564 flagword new_flags = elf_elfheader (ibfd)->e_flags;
2565 flagword old_flags = elf_elfheader (obfd)->e_flags;
2566
2567 if (!is_riscv_elf (ibfd) || !is_riscv_elf (obfd))
2568 return TRUE;
2569
2570 if (strcmp (bfd_get_target (ibfd), bfd_get_target (obfd)) != 0)
2571 {
2572 (*_bfd_error_handler)
2573 (_("%B: ABI is incompatible with that of the selected emulation:\n"
2574 " target emulation `%s' does not match `%s'"),
2575 ibfd, bfd_get_target (ibfd), bfd_get_target (obfd));
2576 return FALSE;
2577 }
2578
2579 if (!_bfd_elf_merge_object_attributes (ibfd, info))
2580 return FALSE;
2581
2582 if (! elf_flags_init (obfd))
2583 {
2584 elf_flags_init (obfd) = TRUE;
2585 elf_elfheader (obfd)->e_flags = new_flags;
2586 return TRUE;
2587 }
2588
2589 /* Disallow linking different float ABIs. */
2590 if ((old_flags ^ new_flags) & EF_RISCV_FLOAT_ABI)
2591 {
2592 (*_bfd_error_handler)
2593 (_("%B: can't link hard-float modules with soft-float modules"), ibfd);
2594 goto fail;
2595 }
2596
2597 /* Allow linking RVC and non-RVC, and keep the RVC flag. */
2598 elf_elfheader (obfd)->e_flags |= new_flags & EF_RISCV_RVC;
2599
2600 return TRUE;
2601
2602 fail:
2603 bfd_set_error (bfd_error_bad_value);
2604 return FALSE;
2605 }
2606
2607 /* Delete some bytes from a section while relaxing. */
2608
2609 static bfd_boolean
2610 riscv_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, size_t count)
2611 {
2612 unsigned int i, symcount;
2613 bfd_vma toaddr = sec->size;
2614 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
2615 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2616 unsigned int sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2617 struct bfd_elf_section_data *data = elf_section_data (sec);
2618 bfd_byte *contents = data->this_hdr.contents;
2619
2620 /* Actually delete the bytes. */
2621 sec->size -= count;
2622 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2623
2624 /* Adjust the location of all of the relocs. Note that we need not
2625 adjust the addends, since all PC-relative references must be against
2626 symbols, which we will adjust below. */
2627 for (i = 0; i < sec->reloc_count; i++)
2628 if (data->relocs[i].r_offset > addr && data->relocs[i].r_offset < toaddr)
2629 data->relocs[i].r_offset -= count;
2630
2631 /* Adjust the local symbols defined in this section. */
2632 for (i = 0; i < symtab_hdr->sh_info; i++)
2633 {
2634 Elf_Internal_Sym *sym = (Elf_Internal_Sym *) symtab_hdr->contents + i;
2635 if (sym->st_shndx == sec_shndx)
2636 {
2637 /* If the symbol is in the range of memory we just moved, we
2638 have to adjust its value. */
2639 if (sym->st_value > addr && sym->st_value <= toaddr)
2640 sym->st_value -= count;
2641
2642 /* If the symbol *spans* the bytes we just deleted (i.e. its
2643 *end* is in the moved bytes but its *start* isn't), then we
2644 must adjust its size. */
2645 if (sym->st_value <= addr
2646 && sym->st_value + sym->st_size > addr
2647 && sym->st_value + sym->st_size <= toaddr)
2648 sym->st_size -= count;
2649 }
2650 }
2651
2652 /* Now adjust the global symbols defined in this section. */
2653 symcount = ((symtab_hdr->sh_size / sizeof (ElfNN_External_Sym))
2654 - symtab_hdr->sh_info);
2655
2656 for (i = 0; i < symcount; i++)
2657 {
2658 struct elf_link_hash_entry *sym_hash = sym_hashes[i];
2659
2660 if ((sym_hash->root.type == bfd_link_hash_defined
2661 || sym_hash->root.type == bfd_link_hash_defweak)
2662 && sym_hash->root.u.def.section == sec)
2663 {
2664 /* As above, adjust the value if needed. */
2665 if (sym_hash->root.u.def.value > addr
2666 && sym_hash->root.u.def.value <= toaddr)
2667 sym_hash->root.u.def.value -= count;
2668
2669 /* As above, adjust the size if needed. */
2670 if (sym_hash->root.u.def.value <= addr
2671 && sym_hash->root.u.def.value + sym_hash->size > addr
2672 && sym_hash->root.u.def.value + sym_hash->size <= toaddr)
2673 sym_hash->size -= count;
2674 }
2675 }
2676
2677 return TRUE;
2678 }
2679
2680 typedef bfd_boolean (*relax_func_t) (bfd *, asection *, asection *,
2681 struct bfd_link_info *,
2682 Elf_Internal_Rela *,
2683 bfd_vma, bfd_vma, bfd_vma, bfd_boolean *);
2684
2685 /* Relax AUIPC + JALR into JAL. */
2686
2687 static bfd_boolean
2688 _bfd_riscv_relax_call (bfd *abfd, asection *sec, asection *sym_sec,
2689 struct bfd_link_info *link_info,
2690 Elf_Internal_Rela *rel,
2691 bfd_vma symval,
2692 bfd_vma max_alignment,
2693 bfd_vma reserve_size ATTRIBUTE_UNUSED,
2694 bfd_boolean *again)
2695 {
2696 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
2697 bfd_signed_vma foff = symval - (sec_addr (sec) + rel->r_offset);
2698 bfd_boolean near_zero = (symval + RISCV_IMM_REACH/2) < RISCV_IMM_REACH;
2699 bfd_vma auipc, jalr;
2700 int rd, r_type, len = 4, rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
2701
2702 /* If the call crosses section boundaries, an alignment directive could
2703 cause the PC-relative offset to later increase. */
2704 if (VALID_UJTYPE_IMM (foff) && sym_sec->output_section != sec->output_section)
2705 foff += (foff < 0 ? -max_alignment : max_alignment);
2706
2707 /* See if this function call can be shortened. */
2708 if (!VALID_UJTYPE_IMM (foff) && !(!bfd_link_pic (link_info) && near_zero))
2709 return TRUE;
2710
2711 /* Shorten the function call. */
2712 BFD_ASSERT (rel->r_offset + 8 <= sec->size);
2713
2714 auipc = bfd_get_32 (abfd, contents + rel->r_offset);
2715 jalr = bfd_get_32 (abfd, contents + rel->r_offset + 4);
2716 rd = (jalr >> OP_SH_RD) & OP_MASK_RD;
2717 rvc = rvc && VALID_RVC_J_IMM (foff) && ARCH_SIZE == 32;
2718
2719 if (rvc && (rd == 0 || rd == X_RA))
2720 {
2721 /* Relax to C.J[AL] rd, addr. */
2722 r_type = R_RISCV_RVC_JUMP;
2723 auipc = rd == 0 ? MATCH_C_J : MATCH_C_JAL;
2724 len = 2;
2725 }
2726 else if (VALID_UJTYPE_IMM (foff))
2727 {
2728 /* Relax to JAL rd, addr. */
2729 r_type = R_RISCV_JAL;
2730 auipc = MATCH_JAL | (rd << OP_SH_RD);
2731 }
2732 else /* near_zero */
2733 {
2734 /* Relax to JALR rd, x0, addr. */
2735 r_type = R_RISCV_LO12_I;
2736 auipc = MATCH_JALR | (rd << OP_SH_RD);
2737 }
2738
2739 /* Replace the R_RISCV_CALL reloc. */
2740 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), r_type);
2741 /* Replace the AUIPC. */
2742 bfd_put (8 * len, abfd, auipc, contents + rel->r_offset);
2743
2744 /* Delete unnecessary JALR. */
2745 *again = TRUE;
2746 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + len, 8 - len);
2747 }
2748
2749 /* Traverse all output sections and return the max alignment. */
2750
2751 static bfd_vma
2752 _bfd_riscv_get_max_alignment (asection *sec)
2753 {
2754 unsigned int max_alignment_power = 0;
2755 asection *o;
2756
2757 for (o = sec->output_section->owner->sections; o != NULL; o = o->next)
2758 {
2759 if (o->alignment_power > max_alignment_power)
2760 max_alignment_power = o->alignment_power;
2761 }
2762
2763 return (bfd_vma) 1 << max_alignment_power;
2764 }
2765
2766 /* Relax non-PIC global variable references. */
2767
2768 static bfd_boolean
2769 _bfd_riscv_relax_lui (bfd *abfd,
2770 asection *sec,
2771 asection *sym_sec,
2772 struct bfd_link_info *link_info,
2773 Elf_Internal_Rela *rel,
2774 bfd_vma symval,
2775 bfd_vma max_alignment,
2776 bfd_vma reserve_size,
2777 bfd_boolean *again)
2778 {
2779 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
2780 bfd_vma gp = riscv_global_pointer_value (link_info);
2781 int use_rvc = elf_elfheader (abfd)->e_flags & EF_RISCV_RVC;
2782
2783 /* Mergeable symbols and code might later move out of range. */
2784 if (sym_sec->flags & (SEC_MERGE | SEC_CODE))
2785 return TRUE;
2786
2787 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
2788
2789 if (gp)
2790 {
2791 /* If gp and the symbol are in the same output section, then
2792 consider only that section's alignment. */
2793 struct bfd_link_hash_entry *h =
2794 bfd_link_hash_lookup (link_info->hash, RISCV_GP_SYMBOL, FALSE, FALSE,
2795 TRUE);
2796 if (h->u.def.section->output_section == sym_sec->output_section)
2797 max_alignment = (bfd_vma) 1 << sym_sec->output_section->alignment_power;
2798 }
2799
2800 /* Is the reference in range of x0 or gp?
2801 Valid gp range conservatively because of alignment issue. */
2802 if (VALID_ITYPE_IMM (symval)
2803 || (symval >= gp
2804 && VALID_ITYPE_IMM (symval - gp + max_alignment + reserve_size))
2805 || (symval < gp
2806 && VALID_ITYPE_IMM (symval - gp - max_alignment - reserve_size)))
2807 {
2808 unsigned sym = ELFNN_R_SYM (rel->r_info);
2809 switch (ELFNN_R_TYPE (rel->r_info))
2810 {
2811 case R_RISCV_LO12_I:
2812 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_I);
2813 return TRUE;
2814
2815 case R_RISCV_LO12_S:
2816 rel->r_info = ELFNN_R_INFO (sym, R_RISCV_GPREL_S);
2817 return TRUE;
2818
2819 case R_RISCV_HI20:
2820 /* We can delete the unnecessary LUI and reloc. */
2821 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
2822 *again = TRUE;
2823 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4);
2824
2825 default:
2826 abort ();
2827 }
2828 }
2829
2830 /* Can we relax LUI to C.LUI? Alignment might move the section forward;
2831 account for this assuming page alignment at worst. */
2832 if (use_rvc
2833 && ELFNN_R_TYPE (rel->r_info) == R_RISCV_HI20
2834 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval))
2835 && VALID_RVC_LUI_IMM (RISCV_CONST_HIGH_PART (symval + ELF_MAXPAGESIZE)))
2836 {
2837 /* Replace LUI with C.LUI if legal (i.e., rd != x2/sp). */
2838 bfd_vma lui = bfd_get_32 (abfd, contents + rel->r_offset);
2839 if (((lui >> OP_SH_RD) & OP_MASK_RD) == X_SP)
2840 return TRUE;
2841
2842 lui = (lui & (OP_MASK_RD << OP_SH_RD)) | MATCH_C_LUI;
2843 bfd_put_32 (abfd, lui, contents + rel->r_offset);
2844
2845 /* Replace the R_RISCV_HI20 reloc. */
2846 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_RVC_LUI);
2847
2848 *again = TRUE;
2849 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + 2, 2);
2850 }
2851
2852 return TRUE;
2853 }
2854
2855 /* Relax non-PIC TLS references. */
2856
2857 static bfd_boolean
2858 _bfd_riscv_relax_tls_le (bfd *abfd,
2859 asection *sec,
2860 asection *sym_sec ATTRIBUTE_UNUSED,
2861 struct bfd_link_info *link_info,
2862 Elf_Internal_Rela *rel,
2863 bfd_vma symval,
2864 bfd_vma max_alignment ATTRIBUTE_UNUSED,
2865 bfd_vma reserve_size ATTRIBUTE_UNUSED,
2866 bfd_boolean *again)
2867 {
2868 /* See if this symbol is in range of tp. */
2869 if (RISCV_CONST_HIGH_PART (tpoff (link_info, symval)) != 0)
2870 return TRUE;
2871
2872 BFD_ASSERT (rel->r_offset + 4 <= sec->size);
2873 switch (ELFNN_R_TYPE (rel->r_info))
2874 {
2875 case R_RISCV_TPREL_LO12_I:
2876 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_I);
2877 return TRUE;
2878
2879 case R_RISCV_TPREL_LO12_S:
2880 rel->r_info = ELFNN_R_INFO (ELFNN_R_SYM (rel->r_info), R_RISCV_TPREL_S);
2881 return TRUE;
2882
2883 case R_RISCV_TPREL_HI20:
2884 case R_RISCV_TPREL_ADD:
2885 /* We can delete the unnecessary instruction and reloc. */
2886 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
2887 *again = TRUE;
2888 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset, 4);
2889
2890 default:
2891 abort ();
2892 }
2893 }
2894
2895 /* Implement R_RISCV_ALIGN by deleting excess alignment NOPs. */
2896
2897 static bfd_boolean
2898 _bfd_riscv_relax_align (bfd *abfd, asection *sec,
2899 asection *sym_sec,
2900 struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
2901 Elf_Internal_Rela *rel,
2902 bfd_vma symval,
2903 bfd_vma max_alignment ATTRIBUTE_UNUSED,
2904 bfd_vma reserve_size ATTRIBUTE_UNUSED,
2905 bfd_boolean *again ATTRIBUTE_UNUSED)
2906 {
2907 bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
2908 bfd_vma alignment = 1, pos;
2909 while (alignment <= rel->r_addend)
2910 alignment *= 2;
2911
2912 symval -= rel->r_addend;
2913 bfd_vma aligned_addr = ((symval - 1) & ~(alignment - 1)) + alignment;
2914 bfd_vma nop_bytes = aligned_addr - symval;
2915
2916 /* Once we've handled an R_RISCV_ALIGN, we can't relax anything else. */
2917 sec->sec_flg0 = TRUE;
2918
2919 /* Make sure there are enough NOPs to actually achieve the alignment. */
2920 if (rel->r_addend < nop_bytes)
2921 {
2922 (*_bfd_error_handler)
2923 (_("%B(%A+0x%lx): %d bytes required for alignment"
2924 "to %d-byte boundary, but only %d present"),
2925 abfd, sym_sec, rel->r_offset, nop_bytes, alignment, rel->r_addend);
2926 bfd_set_error (bfd_error_bad_value);
2927 return FALSE;
2928 }
2929
2930 /* Delete the reloc. */
2931 rel->r_info = ELFNN_R_INFO (0, R_RISCV_NONE);
2932
2933 /* If the number of NOPs is already correct, there's nothing to do. */
2934 if (nop_bytes == rel->r_addend)
2935 return TRUE;
2936
2937 /* Write as many RISC-V NOPs as we need. */
2938 for (pos = 0; pos < (nop_bytes & -4); pos += 4)
2939 bfd_put_32 (abfd, RISCV_NOP, contents + rel->r_offset + pos);
2940
2941 /* Write a final RVC NOP if need be. */
2942 if (nop_bytes % 4 != 0)
2943 bfd_put_16 (abfd, RVC_NOP, contents + rel->r_offset + pos);
2944
2945 /* Delete the excess bytes. */
2946 return riscv_relax_delete_bytes (abfd, sec, rel->r_offset + nop_bytes,
2947 rel->r_addend - nop_bytes);
2948 }
2949
2950 /* Relax a section. Pass 0 shortens code sequences unless disabled.
2951 Pass 1, which cannot be disabled, handles code alignment directives. */
2952
2953 static bfd_boolean
2954 _bfd_riscv_relax_section (bfd *abfd, asection *sec,
2955 struct bfd_link_info *info,
2956 bfd_boolean *again)
2957 {
2958 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
2959 struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2960 struct bfd_elf_section_data *data = elf_section_data (sec);
2961 Elf_Internal_Rela *relocs;
2962 bfd_boolean ret = FALSE;
2963 unsigned int i;
2964 bfd_vma max_alignment, reserve_size = 0;
2965
2966 *again = FALSE;
2967
2968 if (bfd_link_relocatable (info)
2969 || sec->sec_flg0
2970 || (sec->flags & SEC_RELOC) == 0
2971 || sec->reloc_count == 0
2972 || (info->disable_target_specific_optimizations
2973 && info->relax_pass == 0))
2974 return TRUE;
2975
2976 /* Read this BFD's relocs if we haven't done so already. */
2977 if (data->relocs)
2978 relocs = data->relocs;
2979 else if (!(relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2980 info->keep_memory)))
2981 goto fail;
2982
2983 if (htab)
2984 {
2985 max_alignment = htab->max_alignment;
2986 if (max_alignment == (bfd_vma) -1)
2987 {
2988 max_alignment = _bfd_riscv_get_max_alignment (sec);
2989 htab->max_alignment = max_alignment;
2990 }
2991 }
2992 else
2993 max_alignment = _bfd_riscv_get_max_alignment (sec);
2994
2995 /* Examine and consider relaxing each reloc. */
2996 for (i = 0; i < sec->reloc_count; i++)
2997 {
2998 asection *sym_sec;
2999 Elf_Internal_Rela *rel = relocs + i;
3000 relax_func_t relax_func;
3001 int type = ELFNN_R_TYPE (rel->r_info);
3002 bfd_vma symval;
3003
3004 if (info->relax_pass == 0)
3005 {
3006 if (type == R_RISCV_CALL || type == R_RISCV_CALL_PLT)
3007 relax_func = _bfd_riscv_relax_call;
3008 else if (type == R_RISCV_HI20
3009 || type == R_RISCV_LO12_I
3010 || type == R_RISCV_LO12_S)
3011 relax_func = _bfd_riscv_relax_lui;
3012 else if (type == R_RISCV_TPREL_HI20
3013 || type == R_RISCV_TPREL_ADD
3014 || type == R_RISCV_TPREL_LO12_I
3015 || type == R_RISCV_TPREL_LO12_S)
3016 relax_func = _bfd_riscv_relax_tls_le;
3017 else
3018 continue;
3019
3020 /* Only relax this reloc if it is paired with R_RISCV_RELAX. */
3021 if (i == sec->reloc_count - 1
3022 || ELFNN_R_TYPE ((rel + 1)->r_info) != R_RISCV_RELAX
3023 || rel->r_offset != (rel + 1)->r_offset)
3024 continue;
3025
3026 /* Skip over the R_RISCV_RELAX. */
3027 i++;
3028 }
3029 else if (type == R_RISCV_ALIGN)
3030 relax_func = _bfd_riscv_relax_align;
3031 else
3032 continue;
3033
3034 data->relocs = relocs;
3035
3036 /* Read this BFD's contents if we haven't done so already. */
3037 if (!data->this_hdr.contents
3038 && !bfd_malloc_and_get_section (abfd, sec, &data->this_hdr.contents))
3039 goto fail;
3040
3041 /* Read this BFD's symbols if we haven't done so already. */
3042 if (symtab_hdr->sh_info != 0
3043 && !symtab_hdr->contents
3044 && !(symtab_hdr->contents =
3045 (unsigned char *) bfd_elf_get_elf_syms (abfd, symtab_hdr,
3046 symtab_hdr->sh_info,
3047 0, NULL, NULL, NULL)))
3048 goto fail;
3049
3050 /* Get the value of the symbol referred to by the reloc. */
3051 if (ELFNN_R_SYM (rel->r_info) < symtab_hdr->sh_info)
3052 {
3053 /* A local symbol. */
3054 Elf_Internal_Sym *isym = ((Elf_Internal_Sym *) symtab_hdr->contents
3055 + ELFNN_R_SYM (rel->r_info));
3056 reserve_size = (isym->st_size - rel->r_addend) > isym->st_size
3057 ? 0 : isym->st_size - rel->r_addend;
3058
3059 if (isym->st_shndx == SHN_UNDEF)
3060 sym_sec = sec, symval = sec_addr (sec) + rel->r_offset;
3061 else
3062 {
3063 BFD_ASSERT (isym->st_shndx < elf_numsections (abfd));
3064 sym_sec = elf_elfsections (abfd)[isym->st_shndx]->bfd_section;
3065 if (sec_addr (sym_sec) == 0)
3066 continue;
3067 symval = sec_addr (sym_sec) + isym->st_value;
3068 }
3069 }
3070 else
3071 {
3072 unsigned long indx;
3073 struct elf_link_hash_entry *h;
3074
3075 indx = ELFNN_R_SYM (rel->r_info) - symtab_hdr->sh_info;
3076 h = elf_sym_hashes (abfd)[indx];
3077
3078 while (h->root.type == bfd_link_hash_indirect
3079 || h->root.type == bfd_link_hash_warning)
3080 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3081
3082 if (h->plt.offset != MINUS_ONE)
3083 symval = sec_addr (htab->elf.splt) + h->plt.offset;
3084 else if (h->root.u.def.section->output_section == NULL
3085 || (h->root.type != bfd_link_hash_defined
3086 && h->root.type != bfd_link_hash_defweak))
3087 continue;
3088 else
3089 symval = sec_addr (h->root.u.def.section) + h->root.u.def.value;
3090
3091 if (h->type != STT_FUNC)
3092 reserve_size =
3093 (h->size - rel->r_addend) > h->size ? 0 : h->size - rel->r_addend;
3094 sym_sec = h->root.u.def.section;
3095 }
3096
3097 symval += rel->r_addend;
3098
3099 if (!relax_func (abfd, sec, sym_sec, info, rel, symval,
3100 max_alignment, reserve_size, again))
3101 goto fail;
3102 }
3103
3104 ret = TRUE;
3105
3106 fail:
3107 if (relocs != data->relocs)
3108 free (relocs);
3109
3110 return ret;
3111 }
3112
3113 #if ARCH_SIZE == 32
3114 # define PRSTATUS_SIZE 0 /* FIXME */
3115 # define PRSTATUS_OFFSET_PR_CURSIG 12
3116 # define PRSTATUS_OFFSET_PR_PID 24
3117 # define PRSTATUS_OFFSET_PR_REG 72
3118 # define ELF_GREGSET_T_SIZE 128
3119 # define PRPSINFO_SIZE 128
3120 # define PRPSINFO_OFFSET_PR_PID 16
3121 # define PRPSINFO_OFFSET_PR_FNAME 32
3122 # define PRPSINFO_OFFSET_PR_PSARGS 48
3123 #else
3124 # define PRSTATUS_SIZE 376
3125 # define PRSTATUS_OFFSET_PR_CURSIG 12
3126 # define PRSTATUS_OFFSET_PR_PID 32
3127 # define PRSTATUS_OFFSET_PR_REG 112
3128 # define ELF_GREGSET_T_SIZE 256
3129 # define PRPSINFO_SIZE 136
3130 # define PRPSINFO_OFFSET_PR_PID 24
3131 # define PRPSINFO_OFFSET_PR_FNAME 40
3132 # define PRPSINFO_OFFSET_PR_PSARGS 56
3133 #endif
3134
3135 /* Support for core dump NOTE sections. */
3136
3137 static bfd_boolean
3138 riscv_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3139 {
3140 switch (note->descsz)
3141 {
3142 default:
3143 return FALSE;
3144
3145 case PRSTATUS_SIZE: /* sizeof(struct elf_prstatus) on Linux/RISC-V. */
3146 /* pr_cursig */
3147 elf_tdata (abfd)->core->signal
3148 = bfd_get_16 (abfd, note->descdata + PRSTATUS_OFFSET_PR_CURSIG);
3149
3150 /* pr_pid */
3151 elf_tdata (abfd)->core->lwpid
3152 = bfd_get_32 (abfd, note->descdata + PRSTATUS_OFFSET_PR_PID);
3153 break;
3154 }
3155
3156 /* Make a ".reg/999" section. */
3157 return _bfd_elfcore_make_pseudosection (abfd, ".reg", ELF_GREGSET_T_SIZE,
3158 note->descpos + PRSTATUS_OFFSET_PR_REG);
3159 }
3160
3161 static bfd_boolean
3162 riscv_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3163 {
3164 switch (note->descsz)
3165 {
3166 default:
3167 return FALSE;
3168
3169 case PRPSINFO_SIZE: /* sizeof(struct elf_prpsinfo) on Linux/RISC-V. */
3170 /* pr_pid */
3171 elf_tdata (abfd)->core->pid
3172 = bfd_get_32 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PID);
3173
3174 /* pr_fname */
3175 elf_tdata (abfd)->core->program = _bfd_elfcore_strndup
3176 (abfd, note->descdata + PRPSINFO_OFFSET_PR_FNAME, 16);
3177
3178 /* pr_psargs */
3179 elf_tdata (abfd)->core->command = _bfd_elfcore_strndup
3180 (abfd, note->descdata + PRPSINFO_OFFSET_PR_PSARGS, 80);
3181 break;
3182 }
3183
3184 /* Note that for some reason, a spurious space is tacked
3185 onto the end of the args in some (at least one anyway)
3186 implementations, so strip it off if it exists. */
3187
3188 {
3189 char *command = elf_tdata (abfd)->core->command;
3190 int n = strlen (command);
3191
3192 if (0 < n && command[n - 1] == ' ')
3193 command[n - 1] = '\0';
3194 }
3195
3196 return TRUE;
3197 }
3198
3199 /* Set the right mach type. */
3200 static bfd_boolean
3201 riscv_elf_object_p (bfd *abfd)
3202 {
3203 /* There are only two mach types in RISCV currently. */
3204 if (strcmp (abfd->xvec->name, "elf32-littleriscv") == 0)
3205 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv32);
3206 else
3207 bfd_default_set_arch_mach (abfd, bfd_arch_riscv, bfd_mach_riscv64);
3208
3209 return TRUE;
3210 }
3211
3212
3213 #define TARGET_LITTLE_SYM riscv_elfNN_vec
3214 #define TARGET_LITTLE_NAME "elfNN-littleriscv"
3215
3216 #define elf_backend_reloc_type_class riscv_reloc_type_class
3217
3218 #define bfd_elfNN_bfd_reloc_name_lookup riscv_reloc_name_lookup
3219 #define bfd_elfNN_bfd_link_hash_table_create riscv_elf_link_hash_table_create
3220 #define bfd_elfNN_bfd_reloc_type_lookup riscv_reloc_type_lookup
3221 #define bfd_elfNN_bfd_merge_private_bfd_data \
3222 _bfd_riscv_elf_merge_private_bfd_data
3223
3224 #define elf_backend_copy_indirect_symbol riscv_elf_copy_indirect_symbol
3225 #define elf_backend_create_dynamic_sections riscv_elf_create_dynamic_sections
3226 #define elf_backend_check_relocs riscv_elf_check_relocs
3227 #define elf_backend_adjust_dynamic_symbol riscv_elf_adjust_dynamic_symbol
3228 #define elf_backend_size_dynamic_sections riscv_elf_size_dynamic_sections
3229 #define elf_backend_relocate_section riscv_elf_relocate_section
3230 #define elf_backend_finish_dynamic_symbol riscv_elf_finish_dynamic_symbol
3231 #define elf_backend_finish_dynamic_sections riscv_elf_finish_dynamic_sections
3232 #define elf_backend_gc_mark_hook riscv_elf_gc_mark_hook
3233 #define elf_backend_plt_sym_val riscv_elf_plt_sym_val
3234 #define elf_backend_grok_prstatus riscv_elf_grok_prstatus
3235 #define elf_backend_grok_psinfo riscv_elf_grok_psinfo
3236 #define elf_backend_object_p riscv_elf_object_p
3237 #define elf_info_to_howto_rel NULL
3238 #define elf_info_to_howto riscv_info_to_howto_rela
3239 #define bfd_elfNN_bfd_relax_section _bfd_riscv_relax_section
3240
3241 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3242
3243 #define elf_backend_can_gc_sections 1
3244 #define elf_backend_can_refcount 1
3245 #define elf_backend_want_got_plt 1
3246 #define elf_backend_plt_readonly 1
3247 #define elf_backend_plt_alignment 4
3248 #define elf_backend_want_plt_sym 1
3249 #define elf_backend_got_header_size (ARCH_SIZE / 8)
3250 #define elf_backend_want_dynrelro 1
3251 #define elf_backend_rela_normal 1
3252 #define elf_backend_default_execstack 0
3253
3254 #include "elfNN-target.h"
This page took 0.099626 seconds and 5 git commands to generate.