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