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