blob: 66d42e4ab470ab75b448197d0a9aa334709cd4a6 [file] [log] [blame]
Patrick Georgif0bbc952015-03-07 10:57:25 +01001Created from https://github.com/riscv/riscv-gnu-toolchain,
2commit ddce5d17f14831f4957e57c415aca77817c2a82c
3
4diff -urN original-binutils/bfd/archures.c binutils/bfd/archures.c
5--- original-binutils/bfd/archures.c 2014-10-14 09:32:02.000000000 +0200
6+++ binutils-2.25/bfd/archures.c 2015-03-07 09:55:02.355135671 +0100
7@@ -597,6 +597,7 @@
8 extern const bfd_arch_info_type bfd_plugin_arch;
9 extern const bfd_arch_info_type bfd_powerpc_archs[];
10 #define bfd_powerpc_arch bfd_powerpc_archs[0]
11+extern const bfd_arch_info_type bfd_riscv_arch;
12 extern const bfd_arch_info_type bfd_rs6000_arch;
13 extern const bfd_arch_info_type bfd_rl78_arch;
14 extern const bfd_arch_info_type bfd_rx_arch;
15@@ -683,6 +684,7 @@
16 &bfd_or1k_arch,
17 &bfd_pdp11_arch,
18 &bfd_powerpc_arch,
19+ &bfd_riscv_arch,
20 &bfd_rs6000_arch,
21 &bfd_rl78_arch,
22 &bfd_rx_arch,
23diff -urN original-binutils/bfd/bfd-in2.h binutils/bfd/bfd-in2.h
24--- original-binutils/bfd/bfd-in2.h 2014-11-04 10:54:41.000000000 +0100
25+++ binutils-2.25/bfd/bfd-in2.h 2015-03-07 09:55:02.359135671 +0100
26@@ -2043,6 +2043,9 @@
27 #define bfd_mach_ppc_e6500 5007
28 #define bfd_mach_ppc_titan 83
29 #define bfd_mach_ppc_vle 84
30+ bfd_arch_riscv, /* RISC-V */
31+#define bfd_mach_riscv32 132
32+#define bfd_mach_riscv64 164
33 bfd_arch_rs6000, /* IBM RS/6000 */
34 #define bfd_mach_rs6k 6000
35 #define bfd_mach_rs6k_rs1 6001
36@@ -5531,6 +5534,41 @@
37 value in a word. The relocation is relative offset from */
38 BFD_RELOC_MICROBLAZE_32_GOTOFF,
39
40+/* RISC-V relocations */
41+ BFD_RELOC_RISCV_HI20,
42+ BFD_RELOC_RISCV_PCREL_HI20,
43+ BFD_RELOC_RISCV_PCREL_LO12_I,
44+ BFD_RELOC_RISCV_PCREL_LO12_S,
45+ BFD_RELOC_RISCV_LO12_I,
46+ BFD_RELOC_RISCV_LO12_S,
47+ BFD_RELOC_RISCV_GPREL12_I,
48+ BFD_RELOC_RISCV_GPREL12_S,
49+ BFD_RELOC_RISCV_TPREL_HI20,
50+ BFD_RELOC_RISCV_TPREL_LO12_I,
51+ BFD_RELOC_RISCV_TPREL_LO12_S,
52+ BFD_RELOC_RISCV_TPREL_ADD,
53+ BFD_RELOC_RISCV_CALL,
54+ BFD_RELOC_RISCV_CALL_PLT,
55+ BFD_RELOC_RISCV_ADD8,
56+ BFD_RELOC_RISCV_ADD16,
57+ BFD_RELOC_RISCV_ADD32,
58+ BFD_RELOC_RISCV_ADD64,
59+ BFD_RELOC_RISCV_SUB8,
60+ BFD_RELOC_RISCV_SUB16,
61+ BFD_RELOC_RISCV_SUB32,
62+ BFD_RELOC_RISCV_SUB64,
63+ BFD_RELOC_RISCV_GOT_HI20,
64+ BFD_RELOC_RISCV_TLS_GOT_HI20,
65+ BFD_RELOC_RISCV_TLS_GD_HI20,
66+ BFD_RELOC_RISCV_JMP,
67+ BFD_RELOC_RISCV_TLS_DTPMOD32,
68+ BFD_RELOC_RISCV_TLS_DTPREL32,
69+ BFD_RELOC_RISCV_TLS_DTPMOD64,
70+ BFD_RELOC_RISCV_TLS_DTPREL64,
71+ BFD_RELOC_RISCV_TLS_TPREL32,
72+ BFD_RELOC_RISCV_TLS_TPREL64,
73+ BFD_RELOC_RISCV_ALIGN,
74+
75 /* This is used to tell the dynamic linker to copy the value out of
76 the dynamic object into the runtime process image. */
77 BFD_RELOC_MICROBLAZE_COPY,
78diff -urN original-binutils/bfd/config.bfd binutils/bfd/config.bfd
79--- original-binutils/bfd/config.bfd 2014-10-14 09:32:02.000000000 +0200
80+++ binutils-2.25/bfd/config.bfd 2015-03-07 09:55:02.359135671 +0100
81@@ -119,6 +119,7 @@
82 pdp11*) targ_archs=bfd_pdp11_arch ;;
83 pj*) targ_archs="bfd_pj_arch bfd_i386_arch";;
84 powerpc*) targ_archs="bfd_rs6000_arch bfd_powerpc_arch" ;;
85+riscv*) targ_archs=bfd_riscv_arch ;;
86 rs6000) targ_archs="bfd_rs6000_arch bfd_powerpc_arch" ;;
87 s390*) targ_archs=bfd_s390_arch ;;
88 sh*) targ_archs=bfd_sh_arch ;;
89@@ -1319,6 +1320,14 @@
90 targ_defvec=rl78_elf32_vec
91 ;;
92
93+#ifdef BFD64
94+ riscv*-*-*)
95+ targ_defvec=riscv_elf64_vec
96+ targ_selvecs="riscv_elf32_vec riscv_elf64_vec"
97+ want64=true
98+ ;;
99+#endif
100+
101 rx-*-elf)
102 targ_defvec=rx_elf32_le_vec
103 targ_selvecs="rx_elf32_be_vec rx_elf32_le_vec rx_elf32_be_ns_vec"
104diff -urN original-binutils/bfd/configure binutils/bfd/configure
105--- original-binutils/bfd/configure 2014-12-23 15:22:04.000000000 +0100
106+++ binutils-2.25/bfd/configure 2015-03-07 09:55:02.367135671 +0100
107@@ -15506,6 +15506,8 @@
108 powerpc_pei_vec) tb="$tb pei-ppc.lo peigen.lo cofflink.lo" ;;
109 powerpc_pei_le_vec) tb="$tb pei-ppc.lo peigen.lo cofflink.lo" ;;
110 powerpc_xcoff_vec) tb="$tb coff-rs6000.lo xcofflink.lo" ;;
111+ riscv_elf32_vec) tb="$tb elf32-riscv.lo elfxx-riscv.lo elf32.lo $elf" ;;
112+ riscv_elf64_vec) tb="$tb elf64-riscv.lo elf64.lo elfxx-riscv.lo elf32.lo $elf"; target_size=64 ;;
113 rl78_elf32_vec) tb="$tb elf32-rl78.lo elf32.lo $elf" ;;
114 rs6000_xcoff64_vec) tb="$tb coff64-rs6000.lo xcofflink.lo aix5ppc-core.lo"; target_size=64 ;;
115 rs6000_xcoff64_aix_vec) tb="$tb coff64-rs6000.lo xcofflink.lo aix5ppc-core.lo"; target_size=64 ;;
116diff -urN original-binutils/bfd/configure.ac binutils/bfd/configure.ac
117--- original-binutils/bfd/configure.ac 2014-10-14 09:32:02.000000000 +0200
118+++ binutils-2.25/bfd/configure.ac 2015-03-07 09:55:02.367135671 +0100
119@@ -907,6 +907,8 @@
120 powerpc_pei_vec) tb="$tb pei-ppc.lo peigen.lo cofflink.lo" ;;
121 powerpc_pei_le_vec) tb="$tb pei-ppc.lo peigen.lo cofflink.lo" ;;
122 powerpc_xcoff_vec) tb="$tb coff-rs6000.lo xcofflink.lo" ;;
123+ riscv_elf32_vec) tb="$tb elf32-riscv.lo elfxx-riscv.lo elf32.lo $elf" ;;
124+ riscv_elf64_vec) tb="$tb elf64-riscv.lo elf64.lo elfxx-riscv.lo elf32.lo $elf"; target_size=64 ;;
125 rl78_elf32_vec) tb="$tb elf32-rl78.lo elf32.lo $elf" ;;
126 rs6000_xcoff64_vec) tb="$tb coff64-rs6000.lo xcofflink.lo aix5ppc-core.lo"; target_size=64 ;;
127 rs6000_xcoff64_aix_vec) tb="$tb coff64-rs6000.lo xcofflink.lo aix5ppc-core.lo"; target_size=64 ;;
128diff -urN original-binutils/bfd/cpu-riscv.c binutils/bfd/cpu-riscv.c
129--- original-binutils/bfd/cpu-riscv.c 1970-01-01 01:00:00.000000000 +0100
130+++ binutils-2.25/bfd/cpu-riscv.c 2015-03-07 09:51:45.655139025 +0100
131@@ -0,0 +1,80 @@
132+/* BFD backend for RISC-V
133+ Copyright 2011-2014 Free Software Foundation, Inc.
134+
135+ Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley.
136+ Based on MIPS target.
137+
138+ This file is part of BFD, the Binary File Descriptor library.
139+
140+ This program is free software; you can redistribute it and/or modify
141+ it under the terms of the GNU General Public License as published by
142+ the Free Software Foundation; either version 3 of the License, or
143+ (at your option) any later version.
144+
145+ This program is distributed in the hope that it will be useful,
146+ but WITHOUT ANY WARRANTY; without even the implied warranty of
147+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
148+ GNU General Public License for more details.
149+
150+ You should have received a copy of the GNU General Public License
151+ along with this program; if not, write to the Free Software
152+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
153+ MA 02110-1301, USA. */
154+
155+#include "sysdep.h"
156+#include "bfd.h"
157+#include "libbfd.h"
158+
159+static const bfd_arch_info_type *riscv_compatible
160+ (const bfd_arch_info_type *, const bfd_arch_info_type *);
161+
162+/* The default routine tests bits_per_word, which is wrong on RISC-V, as
163+ RISC-V word size doesn't correlate with reloc size. */
164+
165+static const bfd_arch_info_type *
166+riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
167+{
168+ if (a->arch != b->arch)
169+ return NULL;
170+
171+ /* Machine compatibility is checked in
172+ _bfd_riscv_elf_merge_private_bfd_data. */
173+
174+ return a;
175+}
176+
177+#define N(BITS_WORD, BITS_ADDR, NUMBER, PRINT, DEFAULT, NEXT) \
178+ { \
179+ BITS_WORD, /* bits in a word */ \
180+ BITS_ADDR, /* bits in an address */ \
181+ 8, /* 8 bits in a byte */ \
182+ bfd_arch_riscv, \
183+ NUMBER, \
184+ "riscv", \
185+ PRINT, \
186+ 3, \
187+ DEFAULT, \
188+ riscv_compatible, \
189+ bfd_default_scan, \
190+ bfd_arch_default_fill, \
191+ NEXT, \
192+ }
193+
194+enum
195+{
196+ I_riscv64,
197+ I_riscv32
198+};
199+
200+#define NN(index) (&arch_info_struct[(index) + 1])
201+
202+static const bfd_arch_info_type arch_info_struct[] =
203+{
204+ N (64, 64, bfd_mach_riscv64, "riscv:rv64", FALSE, NN(I_riscv64)),
205+ N (32, 32, bfd_mach_riscv32, "riscv:rv32", FALSE, 0)
206+};
207+
208+/* The default architecture is riscv:rv64. */
209+
210+const bfd_arch_info_type bfd_riscv_arch =
211+N (64, 64, 0, "riscv", TRUE, &arch_info_struct[0]);
212diff -urN original-binutils/bfd/elf-bfd.h binutils/bfd/elf-bfd.h
213--- original-binutils/bfd/elf-bfd.h 2014-12-23 09:47:10.000000000 +0100
214+++ binutils-2.25/bfd/elf-bfd.h 2015-03-07 09:55:02.367135671 +0100
215@@ -433,6 +433,7 @@
216 XGATE_ELF_DATA,
217 TILEGX_ELF_DATA,
218 TILEPRO_ELF_DATA,
219+ RISCV_ELF_DATA,
220 GENERIC_ELF_DATA
221 };
222
223diff -urN original-binutils/bfd/elfnn-riscv.c binutils/bfd/elfnn-riscv.c
224--- original-binutils/bfd/elfnn-riscv.c 1970-01-01 01:00:00.000000000 +0100
225+++ binutils-2.25/bfd/elfnn-riscv.c 2015-03-07 09:51:45.655139025 +0100
226@@ -0,0 +1,2954 @@
227+/* RISC-V-specific support for NN-bit ELF.
228+ Copyright 2011-2014 Free Software Foundation, Inc.
229+
230+ Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley.
231+ Based on TILE-Gx and MIPS targets.
232+
233+ This file is part of BFD, the Binary File Descriptor library.
234+
235+ This program is free software; you can redistribute it and/or modify
236+ it under the terms of the GNU General Public License as published by
237+ the Free Software Foundation; either version 3 of the License, or
238+ (at your option) any later version.
239+
240+ This program is distributed in the hope that it will be useful,
241+ but WITHOUT ANY WARRANTY; without even the implied warranty of
242+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
243+ GNU General Public License for more details.
244+
245+ You should have received a copy of the GNU General Public License
246+ along with this program; if not, write to the Free Software
247+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
248+ MA 02110-1301, USA. */
249+
250+
251+/* This file handles RISC-V ELF targets. */
252+
253+#include "sysdep.h"
254+#include "bfd.h"
255+#include "libbfd.h"
256+#include "bfdlink.h"
257+#include "genlink.h"
258+#include "elf-bfd.h"
259+#include "elfxx-riscv.h"
260+#include "elf/riscv.h"
261+#include "opcode/riscv.h"
262+
263+#define ARCH_SIZE NN
264+
265+#define MINUS_ONE ((bfd_vma)0 - 1)
266+
267+#define RISCV_ELF_LOG_WORD_BYTES (ARCH_SIZE == 32 ? 2 : 3)
268+
269+#define RISCV_ELF_WORD_BYTES (1 << RISCV_ELF_LOG_WORD_BYTES)
270+
271+/* The name of the dynamic interpreter. This is put in the .interp
272+ section. */
273+
274+#define ELF64_DYNAMIC_INTERPRETER "/lib/ld.so.1"
275+#define ELF32_DYNAMIC_INTERPRETER "/lib32/ld.so.1"
276+
277+/* The RISC-V linker needs to keep track of the number of relocs that it
278+ decides to copy as dynamic relocs in check_relocs for each symbol.
279+ This is so that it can later discard them if they are found to be
280+ unnecessary. We store the information in a field extending the
281+ regular ELF linker hash table. */
282+
283+struct riscv_elf_dyn_relocs
284+{
285+ struct riscv_elf_dyn_relocs *next;
286+
287+ /* The input section of the reloc. */
288+ asection *sec;
289+
290+ /* Total number of relocs copied for the input section. */
291+ bfd_size_type count;
292+
293+ /* Number of pc-relative relocs copied for the input section. */
294+ bfd_size_type pc_count;
295+};
296+
297+/* RISC-V ELF linker hash entry. */
298+
299+struct riscv_elf_link_hash_entry
300+{
301+ struct elf_link_hash_entry elf;
302+
303+ /* Track dynamic relocs copied for this symbol. */
304+ struct riscv_elf_dyn_relocs *dyn_relocs;
305+
306+#define GOT_UNKNOWN 0
307+#define GOT_NORMAL 1
308+#define GOT_TLS_GD 2
309+#define GOT_TLS_IE 4
310+#define GOT_TLS_LE 8
311+ char tls_type;
312+};
313+
314+#define riscv_elf_hash_entry(ent) \
315+ ((struct riscv_elf_link_hash_entry *)(ent))
316+
317+struct _bfd_riscv_elf_obj_tdata
318+{
319+ struct elf_obj_tdata root;
320+
321+ /* tls_type for each local got entry. */
322+ char *local_got_tls_type;
323+};
324+
325+#define _bfd_riscv_elf_tdata(abfd) \
326+ ((struct _bfd_riscv_elf_obj_tdata *) (abfd)->tdata.any)
327+
328+#define _bfd_riscv_elf_local_got_tls_type(abfd) \
329+ (_bfd_riscv_elf_tdata (abfd)->local_got_tls_type)
330+
331+#define _bfd_riscv_elf_tls_type(abfd, h, symndx) \
332+ (*((h) != NULL ? &riscv_elf_hash_entry(h)->tls_type \
333+ : &_bfd_riscv_elf_local_got_tls_type (abfd) [symndx]))
334+
335+#define is_riscv_elf(bfd) \
336+ (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
337+ && elf_tdata (bfd) != NULL \
338+ && elf_object_id (bfd) == RISCV_ELF_DATA)
339+
340+#include "elf/common.h"
341+#include "elf/internal.h"
342+
343+struct riscv_elf_link_hash_table
344+{
345+ struct elf_link_hash_table elf;
346+
347+ /* Short-cuts to get to dynamic linker sections. */
348+ asection *sdynbss;
349+ asection *srelbss;
350+ asection *sdyntdata;
351+
352+ /* Small local sym to section mapping cache. */
353+ struct sym_cache sym_cache;
354+};
355+
356+
357+/* Get the RISC-V ELF linker hash table from a link_info structure. */
358+#define riscv_elf_hash_table(p) \
359+ (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
360+ == RISCV_ELF_DATA ? ((struct riscv_elf_link_hash_table *) ((p)->hash)) : NULL)
361+
362+static void
363+riscv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
364+ arelent *cache_ptr,
365+ Elf_Internal_Rela *dst)
366+{
367+ cache_ptr->howto = riscv_elf_rtype_to_howto (ELFNN_R_TYPE (dst->r_info));
368+}
369+
370+static void
371+riscv_elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
372+{
373+ const struct elf_backend_data *bed;
374+ bfd_byte *loc;
375+
376+ bed = get_elf_backend_data (abfd);
377+ loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela);
378+ bed->s->swap_reloca_out (abfd, rel, loc);
379+}
380+
381+/* PLT/GOT stuff */
382+
383+#define PLT_HEADER_INSNS 8
384+#define PLT_ENTRY_INSNS 4
385+#define PLT_HEADER_SIZE (PLT_HEADER_INSNS * 4)
386+#define PLT_ENTRY_SIZE (PLT_ENTRY_INSNS * 4)
387+
388+#define GOT_ENTRY_SIZE RISCV_ELF_WORD_BYTES
389+
390+#define GOTPLT_HEADER_SIZE (2 * GOT_ENTRY_SIZE)
391+
392+#define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
393+
394+static bfd_vma
395+riscv_elf_got_plt_val (bfd_vma plt_index, struct bfd_link_info *info)
396+{
397+ return sec_addr (riscv_elf_hash_table (info)->elf.sgotplt)
398+ + GOTPLT_HEADER_SIZE + (plt_index * GOT_ENTRY_SIZE);
399+}
400+
401+#if ARCH_SIZE == 32
402+# define MATCH_LREG MATCH_LW
403+#else
404+# define MATCH_LREG MATCH_LD
405+#endif
406+
407+/* The format of the first PLT entry. */
408+
409+static void
410+riscv_make_plt0_entry(bfd_vma gotplt_addr, bfd_vma addr, uint32_t *entry)
411+{
412+ /* auipc t2, %hi(.got.plt)
413+ sub t1, t1, t0 # shifted .got.plt offset + hdr size + 12
414+ l[w|d] t3, %lo(.got.plt)(t2) # _dl_runtime_resolve
415+ addi t1, t1, -(hdr size + 12) # shifted .got.plt offset
416+ addi t0, t2, %lo(.got.plt) # &.got.plt
417+ srli t1, t1, log2(16/PTRSIZE) # .got.plt offset
418+ l[w|d] t0, PTRSIZE(t0) # link map
419+ jr t3 */
420+
421+ entry[0] = RISCV_UTYPE (AUIPC, X_T2, RISCV_PCREL_HIGH_PART (gotplt_addr, addr));
422+ entry[1] = RISCV_RTYPE (SUB, X_T1, X_T1, X_T0);
423+ entry[2] = RISCV_ITYPE (LREG, X_T3, X_T2, RISCV_PCREL_LOW_PART (gotplt_addr, addr));
424+ entry[3] = RISCV_ITYPE (ADDI, X_T1, X_T1, -(PLT_HEADER_SIZE + 12));
425+ entry[4] = RISCV_ITYPE (ADDI, X_T0, X_T2, RISCV_PCREL_LOW_PART (gotplt_addr, addr));
426+ entry[5] = RISCV_ITYPE (SRLI, X_T1, X_T1, 4 - RISCV_ELF_LOG_WORD_BYTES);
427+ entry[6] = RISCV_ITYPE (LREG, X_T0, X_T0, RISCV_ELF_WORD_BYTES);
428+ entry[7] = RISCV_ITYPE (JALR, 0, X_T3, 0);
429+}
430+
431+/* The format of subsequent PLT entries. */
432+
433+static void
434+riscv_make_plt_entry(bfd_vma got_address, bfd_vma addr, uint32_t *entry)
435+{
436+ /* auipc t1, %hi(.got.plt entry)
437+ l[w|d] t0, %lo(.got.plt entry)(t1)
438+ jalr t1, t0
439+ nop */
440+
441+ entry[0] = RISCV_UTYPE (AUIPC, X_T1, RISCV_PCREL_HIGH_PART (got_address, addr));
442+ entry[1] = RISCV_ITYPE (LREG, X_T0, X_T1, RISCV_PCREL_LOW_PART(got_address, addr));
443+ entry[2] = RISCV_ITYPE (JALR, X_T1, X_T0, 0);
444+ entry[3] = RISCV_NOP;
445+}
446+
447+/* Create an entry in an RISC-V ELF linker hash table. */
448+
449+static struct bfd_hash_entry *
450+link_hash_newfunc (struct bfd_hash_entry *entry,
451+ struct bfd_hash_table *table, const char *string)
452+{
453+ /* Allocate the structure if it has not already been allocated by a
454+ subclass. */
455+ if (entry == NULL)
456+ {
457+ entry =
458+ bfd_hash_allocate (table,
459+ sizeof (struct riscv_elf_link_hash_entry));
460+ if (entry == NULL)
461+ return entry;
462+ }
463+
464+ /* Call the allocation method of the superclass. */
465+ entry = _bfd_elf_link_hash_newfunc (entry, table, string);
466+ if (entry != NULL)
467+ {
468+ struct riscv_elf_link_hash_entry *eh;
469+
470+ eh = (struct riscv_elf_link_hash_entry *) entry;
471+ eh->dyn_relocs = NULL;
472+ eh->tls_type = GOT_UNKNOWN;
473+ }
474+
475+ return entry;
476+}
477+
478+/* Create a RISC-V ELF linker hash table. */
479+
480+static struct bfd_link_hash_table *
481+riscv_elf_link_hash_table_create (bfd *abfd)
482+{
483+ struct riscv_elf_link_hash_table *ret;
484+ bfd_size_type amt = sizeof (struct riscv_elf_link_hash_table);
485+
486+ ret = (struct riscv_elf_link_hash_table *) bfd_zmalloc (amt);
487+ if (ret == NULL)
488+ return NULL;
489+
490+ if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
491+ sizeof (struct riscv_elf_link_hash_entry),
492+ RISCV_ELF_DATA))
493+ {
494+ free (ret);
495+ return NULL;
496+ }
497+
498+ return &ret->elf.root;
499+}
500+
501+/* Create the .got section. */
502+
503+static bfd_boolean
504+riscv_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
505+{
506+ flagword flags;
507+ asection *s, *s_got;
508+ struct elf_link_hash_entry *h;
509+ const struct elf_backend_data *bed = get_elf_backend_data (abfd);
510+ struct elf_link_hash_table *htab = elf_hash_table (info);
511+
512+ /* This function may be called more than once. */
513+ s = bfd_get_linker_section (abfd, ".got");
514+ if (s != NULL)
515+ return TRUE;
516+
517+ flags = bed->dynamic_sec_flags;
518+
519+ s = bfd_make_section_anyway_with_flags (abfd,
520+ (bed->rela_plts_and_copies_p
521+ ? ".rela.got" : ".rel.got"),
522+ (bed->dynamic_sec_flags
523+ | SEC_READONLY));
524+ if (s == NULL
525+ || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
526+ return FALSE;
527+ htab->srelgot = s;
528+
529+ s = s_got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
530+ if (s == NULL
531+ || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align))
532+ return FALSE;
533+ htab->sgot = s;
534+
535+ /* The first bit of the global offset table is the header. */
536+ s->size += bed->got_header_size;
537+
538+ if (bed->want_got_plt)
539+ {
540+ s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
541+ if (s == NULL
542+ || !bfd_set_section_alignment (abfd, s,
543+ bed->s->log_file_align))
544+ return FALSE;
545+ htab->sgotplt = s;
546+
547+ /* Reserve room for the header. */
548+ s->size += GOTPLT_HEADER_SIZE;
549+ }
550+
551+ if (bed->want_got_sym)
552+ {
553+ /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
554+ section. We don't do this in the linker script because we don't want
555+ to define the symbol if we are not creating a global offset
556+ table. */
557+ h = _bfd_elf_define_linkage_sym (abfd, info, s_got,
558+ "_GLOBAL_OFFSET_TABLE_");
559+ elf_hash_table (info)->hgot = h;
560+ if (h == NULL)
561+ return FALSE;
562+ }
563+
564+ return TRUE;
565+}
566+
567+/* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
568+ .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
569+ hash table. */
570+
571+static bfd_boolean
572+riscv_elf_create_dynamic_sections (bfd *dynobj,
573+ struct bfd_link_info *info)
574+{
575+ struct riscv_elf_link_hash_table *htab;
576+
577+ htab = riscv_elf_hash_table (info);
578+ BFD_ASSERT (htab != NULL);
579+
580+ if (!riscv_elf_create_got_section (dynobj, info))
581+ return FALSE;
582+
583+ if (!_bfd_elf_create_dynamic_sections (dynobj, info))
584+ return FALSE;
585+
586+ htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
587+ if (!info->shared)
588+ {
589+ htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
590+ htab->sdyntdata =
591+ bfd_make_section_anyway_with_flags (dynobj, ".tdata.dyn",
592+ SEC_ALLOC | SEC_THREAD_LOCAL);
593+ }
594+
595+ if (!htab->elf.splt || !htab->elf.srelplt || !htab->sdynbss
596+ || (!info->shared && (!htab->srelbss || !htab->sdyntdata)))
597+ abort ();
598+
599+ return TRUE;
600+}
601+
602+/* Copy the extra info we tack onto an elf_link_hash_entry. */
603+
604+static void
605+riscv_elf_copy_indirect_symbol (struct bfd_link_info *info,
606+ struct elf_link_hash_entry *dir,
607+ struct elf_link_hash_entry *ind)
608+{
609+ struct riscv_elf_link_hash_entry *edir, *eind;
610+
611+ edir = (struct riscv_elf_link_hash_entry *) dir;
612+ eind = (struct riscv_elf_link_hash_entry *) ind;
613+
614+ if (eind->dyn_relocs != NULL)
615+ {
616+ if (edir->dyn_relocs != NULL)
617+ {
618+ struct riscv_elf_dyn_relocs **pp;
619+ struct riscv_elf_dyn_relocs *p;
620+
621+ /* Add reloc counts against the indirect sym to the direct sym
622+ list. Merge any entries against the same section. */
623+ for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
624+ {
625+ struct riscv_elf_dyn_relocs *q;
626+
627+ for (q = edir->dyn_relocs; q != NULL; q = q->next)
628+ if (q->sec == p->sec)
629+ {
630+ q->pc_count += p->pc_count;
631+ q->count += p->count;
632+ *pp = p->next;
633+ break;
634+ }
635+ if (q == NULL)
636+ pp = &p->next;
637+ }
638+ *pp = edir->dyn_relocs;
639+ }
640+
641+ edir->dyn_relocs = eind->dyn_relocs;
642+ eind->dyn_relocs = NULL;
643+ }
644+
645+ if (ind->root.type == bfd_link_hash_indirect
646+ && dir->got.refcount <= 0)
647+ {
648+ edir->tls_type = eind->tls_type;
649+ eind->tls_type = GOT_UNKNOWN;
650+ }
651+ _bfd_elf_link_hash_copy_indirect (info, dir, ind);
652+}
653+
654+static bfd_boolean
655+riscv_elf_record_tls_type (bfd *abfd, struct elf_link_hash_entry *h,
656+ unsigned long symndx, char tls_type)
657+{
658+ char *new_tls_type = &_bfd_riscv_elf_tls_type (abfd, h, symndx);
659+ *new_tls_type |= tls_type;
660+ if ((*new_tls_type & GOT_NORMAL) && (*new_tls_type & ~GOT_NORMAL))
661+ {
662+ (*_bfd_error_handler)
663+ (_("%B: `%s' accessed both as normal and thread local symbol"),
664+ abfd, h ? h->root.root.string : "<local>");
665+ return FALSE;
666+ }
667+ return TRUE;
668+}
669+
670+static bfd_boolean
671+riscv_elf_record_got_reference (bfd *abfd, struct bfd_link_info *info,
672+ struct elf_link_hash_entry *h, long symndx)
673+{
674+ struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
675+ Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
676+
677+ if (htab->elf.sgot == NULL)
678+ {
679+ if (!riscv_elf_create_got_section (htab->elf.dynobj, info))
680+ return FALSE;
681+ }
682+
683+ if (h != NULL)
684+ {
685+ h->got.refcount += 1;
686+ return TRUE;
687+ }
688+
689+ /* This is a global offset table entry for a local symbol. */
690+ if (elf_local_got_refcounts (abfd) == NULL)
691+ {
692+ bfd_size_type size = symtab_hdr->sh_info * (sizeof (bfd_vma) + 1);
693+ if (!(elf_local_got_refcounts (abfd) = bfd_zalloc (abfd, size)))
694+ return FALSE;
695+ _bfd_riscv_elf_local_got_tls_type (abfd)
696+ = (char *) (elf_local_got_refcounts (abfd) + symtab_hdr->sh_info);
697+ }
698+ elf_local_got_refcounts (abfd) [symndx] += 1;
699+
700+ return TRUE;
701+}
702+
703+static bfd_boolean
704+bad_static_reloc (bfd *abfd, unsigned r_type, struct elf_link_hash_entry *h)
705+{
706+ (*_bfd_error_handler)
707+ (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
708+ abfd, riscv_elf_rtype_to_howto (r_type)->name,
709+ h != NULL ? h->root.root.string : "a local symbol");
710+ bfd_set_error (bfd_error_bad_value);
711+ return FALSE;
712+}
713+/* Look through the relocs for a section during the first phase, and
714+ allocate space in the global offset table or procedure linkage
715+ table. */
716+
717+static bfd_boolean
718+riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
719+ asection *sec, const Elf_Internal_Rela *relocs)
720+{
721+ struct riscv_elf_link_hash_table *htab;
722+ Elf_Internal_Shdr *symtab_hdr;
723+ struct elf_link_hash_entry **sym_hashes;
724+ const Elf_Internal_Rela *rel;
725+ asection *sreloc = NULL;
726+
727+ if (info->relocatable)
728+ return TRUE;
729+
730+ htab = riscv_elf_hash_table (info);
731+ symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
732+ sym_hashes = elf_sym_hashes (abfd);
733+
734+ if (htab->elf.dynobj == NULL)
735+ htab->elf.dynobj = abfd;
736+
737+ for (rel = relocs; rel < relocs + sec->reloc_count; rel++)
738+ {
739+ unsigned int r_type;
740+ unsigned long r_symndx;
741+ struct elf_link_hash_entry *h;
742+
743+ r_symndx = ELFNN_R_SYM (rel->r_info);
744+ r_type = ELFNN_R_TYPE (rel->r_info);
745+
746+ if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
747+ {
748+ (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
749+ abfd, r_symndx);
750+ return FALSE;
751+ }
752+
753+ if (r_symndx < symtab_hdr->sh_info)
754+ h = NULL;
755+ else
756+ {
757+ h = sym_hashes[r_symndx - symtab_hdr->sh_info];
758+ while (h->root.type == bfd_link_hash_indirect
759+ || h->root.type == bfd_link_hash_warning)
760+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
761+
762+ /* PR15323, ref flags aren't set for references in the same
763+ object. */
764+ h->root.non_ir_ref = 1;
765+ }
766+
767+ switch (r_type)
768+ {
769+ case R_RISCV_TLS_GD_HI20:
770+ if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
771+ || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD))
772+ return FALSE;
773+ break;
774+
775+ case R_RISCV_TLS_GOT_HI20:
776+ if (info->shared)
777+ info->flags |= DF_STATIC_TLS;
778+ if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
779+ || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_IE))
780+ return FALSE;
781+ break;
782+
783+ case R_RISCV_GOT_HI20:
784+ if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx)
785+ || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_NORMAL))
786+ return FALSE;
787+ break;
788+
789+ case R_RISCV_CALL_PLT:
790+ /* This symbol requires a procedure linkage table entry. We
791+ actually build the entry in adjust_dynamic_symbol,
792+ because this might be a case of linking PIC code without
793+ linking in any dynamic objects, in which case we don't
794+ need to generate a procedure linkage table after all. */
795+
796+ if (h != NULL)
797+ {
798+ h->needs_plt = 1;
799+ h->plt.refcount += 1;
800+ }
801+ break;
802+
803+ case R_RISCV_CALL:
804+ case R_RISCV_JAL:
805+ case R_RISCV_BRANCH:
806+ case R_RISCV_PCREL_HI20:
807+ /* In shared libs, these relocs are known to bind locally. */
808+ if (info->shared)
809+ break;
810+ goto static_reloc;
811+
812+ case R_RISCV_TPREL_HI20:
813+ if (!info->executable)
814+ return bad_static_reloc (abfd, r_type, h);
815+ if (h != NULL)
816+ riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_LE);
817+ goto static_reloc;
818+
819+ case R_RISCV_HI20:
820+ if (info->shared)
821+ return bad_static_reloc (abfd, r_type, h);
822+ /* Fall through. */
823+
824+ case R_RISCV_COPY:
825+ case R_RISCV_JUMP_SLOT:
826+ case R_RISCV_RELATIVE:
827+ case R_RISCV_64:
828+ case R_RISCV_32:
829+ /* Fall through. */
830+
831+ static_reloc:
832+ if (h != NULL)
833+ h->non_got_ref = 1;
834+
835+ if (h != NULL && !info->shared)
836+ {
837+ /* We may need a .plt entry if the function this reloc
838+ refers to is in a shared lib. */
839+ h->plt.refcount += 1;
840+ }
841+
842+ /* If we are creating a shared library, and this is a reloc
843+ against a global symbol, or a non PC relative reloc
844+ against a local symbol, then we need to copy the reloc
845+ into the shared library. However, if we are linking with
846+ -Bsymbolic, we do not need to copy a reloc against a
847+ global symbol which is defined in an object we are
848+ including in the link (i.e., DEF_REGULAR is set). At
849+ this point we have not seen all the input files, so it is
850+ possible that DEF_REGULAR is not set now but will be set
851+ later (it is never cleared). In case of a weak definition,
852+ DEF_REGULAR may be cleared later by a strong definition in
853+ a shared library. We account for that possibility below by
854+ storing information in the relocs_copied field of the hash
855+ table entry. A similar situation occurs when creating
856+ shared libraries and symbol visibility changes render the
857+ symbol local.
858+
859+ If on the other hand, we are creating an executable, we
860+ may need to keep relocations for symbols satisfied by a
861+ dynamic library if we manage to avoid copy relocs for the
862+ symbol. */
863+ if ((info->shared
864+ && (sec->flags & SEC_ALLOC) != 0
865+ && (! riscv_elf_rtype_to_howto (r_type)->pc_relative
866+ || (h != NULL
867+ && (! info->symbolic
868+ || h->root.type == bfd_link_hash_defweak
869+ || !h->def_regular))))
870+ || (!info->shared
871+ && (sec->flags & SEC_ALLOC) != 0
872+ && h != NULL
873+ && (h->root.type == bfd_link_hash_defweak
874+ || !h->def_regular)))
875+ {
876+ struct riscv_elf_dyn_relocs *p;
877+ struct riscv_elf_dyn_relocs **head;
878+
879+ /* When creating a shared object, we must copy these
880+ relocs into the output file. We create a reloc
881+ section in dynobj and make room for the reloc. */
882+ if (sreloc == NULL)
883+ {
884+ sreloc = _bfd_elf_make_dynamic_reloc_section
885+ (sec, htab->elf.dynobj, RISCV_ELF_LOG_WORD_BYTES,
886+ abfd, /*rela?*/ TRUE);
887+
888+ if (sreloc == NULL)
889+ return FALSE;
890+ }
891+
892+ /* If this is a global symbol, we count the number of
893+ relocations we need for this symbol. */
894+ if (h != NULL)
895+ head = &((struct riscv_elf_link_hash_entry *) h)->dyn_relocs;
896+ else
897+ {
898+ /* Track dynamic relocs needed for local syms too.
899+ We really need local syms available to do this
900+ easily. Oh well. */
901+
902+ asection *s;
903+ void *vpp;
904+ Elf_Internal_Sym *isym;
905+
906+ isym = bfd_sym_from_r_symndx (&htab->sym_cache,
907+ abfd, r_symndx);
908+ if (isym == NULL)
909+ return FALSE;
910+
911+ s = bfd_section_from_elf_index (abfd, isym->st_shndx);
912+ if (s == NULL)
913+ s = sec;
914+
915+ vpp = &elf_section_data (s)->local_dynrel;
916+ head = (struct riscv_elf_dyn_relocs **) vpp;
917+ }
918+
919+ p = *head;
920+ if (p == NULL || p->sec != sec)
921+ {
922+ bfd_size_type amt = sizeof *p;
923+ p = ((struct riscv_elf_dyn_relocs *)
924+ bfd_alloc (htab->elf.dynobj, amt));
925+ if (p == NULL)
926+ return FALSE;
927+ p->next = *head;
928+ *head = p;
929+ p->sec = sec;
930+ p->count = 0;
931+ p->pc_count = 0;
932+ }
933+
934+ p->count += 1;
935+ p->pc_count += riscv_elf_rtype_to_howto (r_type)->pc_relative;
936+ }
937+
938+ break;
939+
940+ case R_RISCV_GNU_VTINHERIT:
941+ if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
942+ return FALSE;
943+ break;
944+
945+ case R_RISCV_GNU_VTENTRY:
946+ if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
947+ return FALSE;
948+ break;
949+
950+ default:
951+ break;
952+ }
953+ }
954+
955+ return TRUE;
956+}
957+
958+static asection *
959+riscv_elf_gc_mark_hook (asection *sec,
960+ struct bfd_link_info *info,
961+ Elf_Internal_Rela *rel,
962+ struct elf_link_hash_entry *h,
963+ Elf_Internal_Sym *sym)
964+{
965+ if (h != NULL)
966+ switch (ELFNN_R_TYPE (rel->r_info))
967+ {
968+ case R_RISCV_GNU_VTINHERIT:
969+ case R_RISCV_GNU_VTENTRY:
970+ return NULL;
971+ }
972+
973+ return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
974+}
975+
976+/* Update the got entry reference counts for the section being removed. */
977+static bfd_boolean
978+riscv_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
979+ asection *sec, const Elf_Internal_Rela *relocs)
980+{
981+ const Elf_Internal_Rela *rel, *relend;
982+ Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (abfd);
983+ struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
984+ bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
985+
986+ if (info->relocatable)
987+ return TRUE;
988+
989+ elf_section_data (sec)->local_dynrel = NULL;
990+
991+ for (rel = relocs, relend = relocs + sec->reloc_count; rel < relend; rel++)
992+ {
993+ unsigned long r_symndx;
994+ struct elf_link_hash_entry *h = NULL;
995+
996+ r_symndx = ELFNN_R_SYM (rel->r_info);
997+ if (r_symndx >= symtab_hdr->sh_info)
998+ {
999+ struct riscv_elf_link_hash_entry *eh;
1000+ struct riscv_elf_dyn_relocs **pp;
1001+ struct riscv_elf_dyn_relocs *p;
1002+
1003+ h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1004+ while (h->root.type == bfd_link_hash_indirect
1005+ || h->root.type == bfd_link_hash_warning)
1006+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
1007+ eh = (struct riscv_elf_link_hash_entry *) h;
1008+ for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1009+ if (p->sec == sec)
1010+ {
1011+ /* Everything must go for SEC. */
1012+ *pp = p->next;
1013+ break;
1014+ }
1015+ }
1016+
1017+ switch (ELFNN_R_TYPE (rel->r_info))
1018+ {
1019+ case R_RISCV_GOT_HI20:
1020+ case R_RISCV_TLS_GOT_HI20:
1021+ case R_RISCV_TLS_GD_HI20:
1022+ if (h != NULL)
1023+ {
1024+ if (h->got.refcount > 0)
1025+ h->got.refcount--;
1026+ }
1027+ else
1028+ {
1029+ if (local_got_refcounts &&
1030+ local_got_refcounts[r_symndx] > 0)
1031+ local_got_refcounts[r_symndx]--;
1032+ }
1033+ break;
1034+
1035+ case R_RISCV_HI20:
1036+ case R_RISCV_PCREL_HI20:
1037+ case R_RISCV_COPY:
1038+ case R_RISCV_JUMP_SLOT:
1039+ case R_RISCV_RELATIVE:
1040+ case R_RISCV_64:
1041+ case R_RISCV_32:
1042+ case R_RISCV_BRANCH:
1043+ case R_RISCV_CALL:
1044+ case R_RISCV_JAL:
1045+ if (info->shared)
1046+ break;
1047+ /* Fall through. */
1048+
1049+ case R_RISCV_CALL_PLT:
1050+ if (h != NULL)
1051+ {
1052+ if (h->plt.refcount > 0)
1053+ h->plt.refcount--;
1054+ }
1055+ break;
1056+
1057+ default:
1058+ break;
1059+ }
1060+ }
1061+
1062+ return TRUE;
1063+}
1064+
1065+/* Adjust a symbol defined by a dynamic object and referenced by a
1066+ regular object. The current definition is in some section of the
1067+ dynamic object, but we're not including those sections. We have to
1068+ change the definition to something the rest of the link can
1069+ understand. */
1070+
1071+static bfd_boolean
1072+riscv_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1073+ struct elf_link_hash_entry *h)
1074+{
1075+ struct riscv_elf_link_hash_table *htab;
1076+ struct riscv_elf_link_hash_entry * eh;
1077+ struct riscv_elf_dyn_relocs *p;
1078+ bfd *dynobj;
1079+ asection *s;
1080+
1081+ htab = riscv_elf_hash_table (info);
1082+ BFD_ASSERT (htab != NULL);
1083+
1084+ dynobj = htab->elf.dynobj;
1085+
1086+ /* Make sure we know what is going on here. */
1087+ BFD_ASSERT (dynobj != NULL
1088+ && (h->needs_plt
1089+ || h->u.weakdef != NULL
1090+ || (h->def_dynamic
1091+ && h->ref_regular
1092+ && !h->def_regular)));
1093+
1094+ /* If this is a function, put it in the procedure linkage table. We
1095+ will fill in the contents of the procedure linkage table later
1096+ (although we could actually do it here). */
1097+ if (h->type == STT_FUNC || h->needs_plt)
1098+ {
1099+ if (h->plt.refcount <= 0
1100+ || SYMBOL_CALLS_LOCAL (info, h)
1101+ || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1102+ && h->root.type == bfd_link_hash_undefweak))
1103+ {
1104+ /* This case can occur if we saw a R_RISCV_CALL_PLT reloc in an
1105+ input file, but the symbol was never referred to by a dynamic
1106+ object, or if all references were garbage collected. In such
1107+ a case, we don't actually need to build a PLT entry. */
1108+ h->plt.offset = (bfd_vma) -1;
1109+ h->needs_plt = 0;
1110+ }
1111+
1112+ return TRUE;
1113+ }
1114+ else
1115+ h->plt.offset = (bfd_vma) -1;
1116+
1117+ /* If this is a weak symbol, and there is a real definition, the
1118+ processor independent code will have arranged for us to see the
1119+ real definition first, and we can just use the same value. */
1120+ if (h->u.weakdef != NULL)
1121+ {
1122+ BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1123+ || h->u.weakdef->root.type == bfd_link_hash_defweak);
1124+ h->root.u.def.section = h->u.weakdef->root.u.def.section;
1125+ h->root.u.def.value = h->u.weakdef->root.u.def.value;
1126+ return TRUE;
1127+ }
1128+
1129+ /* This is a reference to a symbol defined by a dynamic object which
1130+ is not a function. */
1131+
1132+ /* If we are creating a shared library, we must presume that the
1133+ only references to the symbol are via the global offset table.
1134+ For such cases we need not do anything here; the relocations will
1135+ be handled correctly by relocate_section. */
1136+ if (info->shared)
1137+ return TRUE;
1138+
1139+ /* If there are no references to this symbol that do not use the
1140+ GOT, we don't need to generate a copy reloc. */
1141+ if (!h->non_got_ref)
1142+ return TRUE;
1143+
1144+ /* If -z nocopyreloc was given, we won't generate them either. */
1145+ if (info->nocopyreloc)
1146+ {
1147+ h->non_got_ref = 0;
1148+ return TRUE;
1149+ }
1150+
1151+ eh = (struct riscv_elf_link_hash_entry *) h;
1152+ for (p = eh->dyn_relocs; p != NULL; p = p->next)
1153+ {
1154+ s = p->sec->output_section;
1155+ if (s != NULL && (s->flags & SEC_READONLY) != 0)
1156+ break;
1157+ }
1158+
1159+ /* If we didn't find any dynamic relocs in read-only sections, then
1160+ we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1161+ if (p == NULL)
1162+ {
1163+ h->non_got_ref = 0;
1164+ return TRUE;
1165+ }
1166+
1167+ /* We must allocate the symbol in our .dynbss section, which will
1168+ become part of the .bss section of the executable. There will be
1169+ an entry for this symbol in the .dynsym section. The dynamic
1170+ object will contain position independent code, so all references
1171+ from the dynamic object to this symbol will go through the global
1172+ offset table. The dynamic linker will use the .dynsym entry to
1173+ determine the address it must put in the global offset table, so
1174+ both the dynamic object and the regular object will refer to the
1175+ same memory location for the variable. */
1176+
1177+ /* We must generate a R_RISCV_COPY reloc to tell the dynamic linker
1178+ to copy the initial value out of the dynamic object and into the
1179+ runtime process image. We need to remember the offset into the
1180+ .rel.bss section we are going to use. */
1181+ if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
1182+ {
1183+ htab->srelbss->size += sizeof (ElfNN_External_Rela);
1184+ h->needs_copy = 1;
1185+ }
1186+
1187+ if (eh->tls_type & ~GOT_NORMAL)
1188+ return _bfd_elf_adjust_dynamic_copy (h, htab->sdyntdata);
1189+
1190+ return _bfd_elf_adjust_dynamic_copy (h, htab->sdynbss);
1191+}
1192+
1193+/* Allocate space in .plt, .got and associated reloc sections for
1194+ dynamic relocs. */
1195+
1196+static bfd_boolean
1197+allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1198+{
1199+ struct bfd_link_info *info;
1200+ struct riscv_elf_link_hash_table *htab;
1201+ struct riscv_elf_link_hash_entry *eh;
1202+ struct riscv_elf_dyn_relocs *p;
1203+
1204+ if (h->root.type == bfd_link_hash_indirect)
1205+ return TRUE;
1206+
1207+ info = (struct bfd_link_info *) inf;
1208+ htab = riscv_elf_hash_table (info);
1209+ BFD_ASSERT (htab != NULL);
1210+
1211+ if (htab->elf.dynamic_sections_created
1212+ && h->plt.refcount > 0)
1213+ {
1214+ /* Make sure this symbol is output as a dynamic symbol.
1215+ Undefined weak syms won't yet be marked as dynamic. */
1216+ if (h->dynindx == -1
1217+ && !h->forced_local)
1218+ {
1219+ if (! bfd_elf_link_record_dynamic_symbol (info, h))
1220+ return FALSE;
1221+ }
1222+
1223+ if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1224+ {
1225+ asection *s = htab->elf.splt;
1226+
1227+ if (s->size == 0)
1228+ s->size = PLT_HEADER_SIZE;
1229+
1230+ h->plt.offset = s->size;
1231+
1232+ /* Make room for this entry. */
1233+ s->size += PLT_ENTRY_SIZE;
1234+
1235+ /* We also need to make an entry in the .got.plt section. */
1236+ htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1237+
1238+ /* We also need to make an entry in the .rela.plt section. */
1239+ htab->elf.srelplt->size += sizeof (ElfNN_External_Rela);
1240+
1241+ /* If this symbol is not defined in a regular file, and we are
1242+ not generating a shared library, then set the symbol to this
1243+ location in the .plt. This is required to make function
1244+ pointers compare as equal between the normal executable and
1245+ the shared library. */
1246+ if (! info->shared
1247+ && !h->def_regular)
1248+ {
1249+ h->root.u.def.section = s;
1250+ h->root.u.def.value = h->plt.offset;
1251+ }
1252+ }
1253+ else
1254+ {
1255+ h->plt.offset = (bfd_vma) -1;
1256+ h->needs_plt = 0;
1257+ }
1258+ }
1259+ else
1260+ {
1261+ h->plt.offset = (bfd_vma) -1;
1262+ h->needs_plt = 0;
1263+ }
1264+
1265+ if (h->got.refcount > 0)
1266+ {
1267+ asection *s;
1268+ bfd_boolean dyn;
1269+ int tls_type = riscv_elf_hash_entry(h)->tls_type;
1270+
1271+ /* Make sure this symbol is output as a dynamic symbol.
1272+ Undefined weak syms won't yet be marked as dynamic. */
1273+ if (h->dynindx == -1
1274+ && !h->forced_local)
1275+ {
1276+ if (! bfd_elf_link_record_dynamic_symbol (info, h))
1277+ return FALSE;
1278+ }
1279+
1280+ s = htab->elf.sgot;
1281+ h->got.offset = s->size;
1282+ dyn = htab->elf.dynamic_sections_created;
1283+ if (tls_type & (GOT_TLS_GD | GOT_TLS_IE))
1284+ {
1285+ /* TLS_GD needs two dynamic relocs and two GOT slots. */
1286+ if (tls_type & GOT_TLS_GD)
1287+ {
1288+ s->size += 2 * RISCV_ELF_WORD_BYTES;
1289+ htab->elf.srelgot->size += 2 * sizeof (ElfNN_External_Rela);
1290+ }
1291+
1292+ /* TLS_IE needs one dynamic reloc and one GOT slot. */
1293+ if (tls_type & GOT_TLS_IE)
1294+ {
1295+ s->size += RISCV_ELF_WORD_BYTES;
1296+ htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1297+ }
1298+ }
1299+ else
1300+ {
1301+ s->size += RISCV_ELF_WORD_BYTES;
1302+ if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
1303+ htab->elf.srelgot->size += sizeof (ElfNN_External_Rela);
1304+ }
1305+ }
1306+ else
1307+ h->got.offset = (bfd_vma) -1;
1308+
1309+ eh = (struct riscv_elf_link_hash_entry *) h;
1310+ if (eh->dyn_relocs == NULL)
1311+ return TRUE;
1312+
1313+ /* In the shared -Bsymbolic case, discard space allocated for
1314+ dynamic pc-relative relocs against symbols which turn out to be
1315+ defined in regular objects. For the normal shared case, discard
1316+ space for pc-relative relocs that have become local due to symbol
1317+ visibility changes. */
1318+
1319+ if (info->shared)
1320+ {
1321+ if (SYMBOL_CALLS_LOCAL (info, h))
1322+ {
1323+ struct riscv_elf_dyn_relocs **pp;
1324+
1325+ for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1326+ {
1327+ p->count -= p->pc_count;
1328+ p->pc_count = 0;
1329+ if (p->count == 0)
1330+ *pp = p->next;
1331+ else
1332+ pp = &p->next;
1333+ }
1334+ }
1335+
1336+ /* Also discard relocs on undefined weak syms with non-default
1337+ visibility. */
1338+ if (eh->dyn_relocs != NULL
1339+ && h->root.type == bfd_link_hash_undefweak)
1340+ {
1341+ if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1342+ eh->dyn_relocs = NULL;
1343+
1344+ /* Make sure undefined weak symbols are output as a dynamic
1345+ symbol in PIEs. */
1346+ else if (h->dynindx == -1
1347+ && !h->forced_local)
1348+ {
1349+ if (! bfd_elf_link_record_dynamic_symbol (info, h))
1350+ return FALSE;
1351+ }
1352+ }
1353+ }
1354+ else
1355+ {
1356+ /* For the non-shared case, discard space for relocs against
1357+ symbols which turn out to need copy relocs or are not
1358+ dynamic. */
1359+
1360+ if (!h->non_got_ref
1361+ && ((h->def_dynamic
1362+ && !h->def_regular)
1363+ || (htab->elf.dynamic_sections_created
1364+ && (h->root.type == bfd_link_hash_undefweak
1365+ || h->root.type == bfd_link_hash_undefined))))
1366+ {
1367+ /* Make sure this symbol is output as a dynamic symbol.
1368+ Undefined weak syms won't yet be marked as dynamic. */
1369+ if (h->dynindx == -1
1370+ && !h->forced_local)
1371+ {
1372+ if (! bfd_elf_link_record_dynamic_symbol (info, h))
1373+ return FALSE;
1374+ }
1375+
1376+ /* If that succeeded, we know we'll be keeping all the
1377+ relocs. */
1378+ if (h->dynindx != -1)
1379+ goto keep;
1380+ }
1381+
1382+ eh->dyn_relocs = NULL;
1383+
1384+ keep: ;
1385+ }
1386+
1387+ /* Finally, allocate space. */
1388+ for (p = eh->dyn_relocs; p != NULL; p = p->next)
1389+ {
1390+ asection *sreloc = elf_section_data (p->sec)->sreloc;
1391+ sreloc->size += p->count * sizeof (ElfNN_External_Rela);
1392+ }
1393+
1394+ return TRUE;
1395+}
1396+
1397+/* Find any dynamic relocs that apply to read-only sections. */
1398+
1399+static bfd_boolean
1400+readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1401+{
1402+ struct riscv_elf_link_hash_entry *eh;
1403+ struct riscv_elf_dyn_relocs *p;
1404+
1405+ eh = (struct riscv_elf_link_hash_entry *) h;
1406+ for (p = eh->dyn_relocs; p != NULL; p = p->next)
1407+ {
1408+ asection *s = p->sec->output_section;
1409+
1410+ if (s != NULL && (s->flags & SEC_READONLY) != 0)
1411+ {
1412+ ((struct bfd_link_info *) inf)->flags |= DF_TEXTREL;
1413+
1414+ /* Short-circuit the traversal. */
1415+ return FALSE;
1416+ }
1417+ }
1418+ return TRUE;
1419+}
1420+
1421+static bfd_boolean
1422+riscv_elf_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1423+{
1424+ struct riscv_elf_link_hash_table *htab;
1425+ bfd *dynobj;
1426+ asection *s;
1427+ bfd *ibfd;
1428+
1429+ htab = riscv_elf_hash_table (info);
1430+ BFD_ASSERT (htab != NULL);
1431+ dynobj = htab->elf.dynobj;
1432+ BFD_ASSERT (dynobj != NULL);
1433+
1434+ if (elf_hash_table (info)->dynamic_sections_created)
1435+ {
1436+ /* Set the contents of the .interp section to the interpreter. */
1437+ if (info->executable)
1438+ {
1439+ s = bfd_get_linker_section (dynobj, ".interp");
1440+ BFD_ASSERT (s != NULL);
1441+ s->size = strlen (ELFNN_DYNAMIC_INTERPRETER) + 1;
1442+ s->contents = (unsigned char *) ELFNN_DYNAMIC_INTERPRETER;
1443+ }
1444+ }
1445+
1446+ /* Set up .got offsets for local syms, and space for local dynamic
1447+ relocs. */
1448+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1449+ {
1450+ bfd_signed_vma *local_got;
1451+ bfd_signed_vma *end_local_got;
1452+ char *local_tls_type;
1453+ bfd_size_type locsymcount;
1454+ Elf_Internal_Shdr *symtab_hdr;
1455+ asection *srel;
1456+
1457+ if (! is_riscv_elf (ibfd))
1458+ continue;
1459+
1460+ for (s = ibfd->sections; s != NULL; s = s->next)
1461+ {
1462+ struct riscv_elf_dyn_relocs *p;
1463+
1464+ for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
1465+ {
1466+ if (!bfd_is_abs_section (p->sec)
1467+ && bfd_is_abs_section (p->sec->output_section))
1468+ {
1469+ /* Input section has been discarded, either because
1470+ it is a copy of a linkonce section or due to
1471+ linker script /DISCARD/, so we'll be discarding
1472+ the relocs too. */
1473+ }
1474+ else if (p->count != 0)
1475+ {
1476+ srel = elf_section_data (p->sec)->sreloc;
1477+ srel->size += p->count * sizeof (ElfNN_External_Rela);
1478+ if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1479+ info->flags |= DF_TEXTREL;
1480+ }
1481+ }
1482+ }
1483+
1484+ local_got = elf_local_got_refcounts (ibfd);
1485+ if (!local_got)
1486+ continue;
1487+
1488+ symtab_hdr = &elf_symtab_hdr (ibfd);
1489+ locsymcount = symtab_hdr->sh_info;
1490+ end_local_got = local_got + locsymcount;
1491+ local_tls_type = _bfd_riscv_elf_local_got_tls_type (ibfd);
1492+ s = htab->elf.sgot;
1493+ srel = htab->elf.srelgot;
1494+ for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1495+ {
1496+ if (*local_got > 0)
1497+ {
1498+ *local_got = s->size;
1499+ s->size += RISCV_ELF_WORD_BYTES;
1500+ if (*local_tls_type & GOT_TLS_GD)
1501+ s->size += RISCV_ELF_WORD_BYTES;
1502+ if (info->shared
1503+ || (*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
1504+ srel->size += sizeof (ElfNN_External_Rela);
1505+ }
1506+ else
1507+ *local_got = (bfd_vma) -1;
1508+ }
1509+ }
1510+
1511+ /* Allocate global sym .plt and .got entries, and space for global
1512+ sym dynamic relocs. */
1513+ elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
1514+
1515+ if (htab->elf.sgotplt)
1516+ {
1517+ struct elf_link_hash_entry *got;
1518+ got = elf_link_hash_lookup (elf_hash_table (info),
1519+ "_GLOBAL_OFFSET_TABLE_",
1520+ FALSE, FALSE, FALSE);
1521+
1522+ /* Don't allocate .got.plt section if there are no GOT nor PLT
1523+ entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
1524+ if ((got == NULL
1525+ || !got->ref_regular_nonweak)
1526+ && (htab->elf.sgotplt->size == GOTPLT_HEADER_SIZE)
1527+ && (htab->elf.splt == NULL
1528+ || htab->elf.splt->size == 0)
1529+ && (htab->elf.sgot == NULL
1530+ || (htab->elf.sgot->size
1531+ == get_elf_backend_data (output_bfd)->got_header_size)))
1532+ htab->elf.sgotplt->size = 0;
1533+ }
1534+
1535+ /* The check_relocs and adjust_dynamic_symbol entry points have
1536+ determined the sizes of the various dynamic sections. Allocate
1537+ memory for them. */
1538+ for (s = dynobj->sections; s != NULL; s = s->next)
1539+ {
1540+ if ((s->flags & SEC_LINKER_CREATED) == 0)
1541+ continue;
1542+
1543+ if (s == htab->elf.splt
1544+ || s == htab->elf.sgot
1545+ || s == htab->elf.sgotplt
1546+ || s == htab->sdynbss)
1547+ {
1548+ /* Strip this section if we don't need it; see the
1549+ comment below. */
1550+ }
1551+ else if (strncmp (s->name, ".rela", 5) == 0)
1552+ {
1553+ if (s->size != 0)
1554+ {
1555+ /* We use the reloc_count field as a counter if we need
1556+ to copy relocs into the output file. */
1557+ s->reloc_count = 0;
1558+ }
1559+ }
1560+ else
1561+ {
1562+ /* It's not one of our sections. */
1563+ continue;
1564+ }
1565+
1566+ if (s->size == 0)
1567+ {
1568+ /* If we don't need this section, strip it from the
1569+ output file. This is mostly to handle .rela.bss and
1570+ .rela.plt. We must create both sections in
1571+ create_dynamic_sections, because they must be created
1572+ before the linker maps input sections to output
1573+ sections. The linker does that before
1574+ adjust_dynamic_symbol is called, and it is that
1575+ function which decides whether anything needs to go
1576+ into these sections. */
1577+ s->flags |= SEC_EXCLUDE;
1578+ continue;
1579+ }
1580+
1581+ if ((s->flags & SEC_HAS_CONTENTS) == 0)
1582+ continue;
1583+
1584+ /* Allocate memory for the section contents. Zero the memory
1585+ for the benefit of .rela.plt, which has 4 unused entries
1586+ at the beginning, and we don't want garbage. */
1587+ s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1588+ if (s->contents == NULL)
1589+ return FALSE;
1590+ }
1591+
1592+ if (elf_hash_table (info)->dynamic_sections_created)
1593+ {
1594+ /* Add some entries to the .dynamic section. We fill in the
1595+ values later, in riscv_elf_finish_dynamic_sections, but we
1596+ must add the entries now so that we get the correct size for
1597+ the .dynamic section. The DT_DEBUG entry is filled in by the
1598+ dynamic linker and used by the debugger. */
1599+#define add_dynamic_entry(TAG, VAL) \
1600+ _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1601+
1602+ if (info->executable)
1603+ {
1604+ if (!add_dynamic_entry (DT_DEBUG, 0))
1605+ return FALSE;
1606+ }
1607+
1608+ if (htab->elf.srelplt->size != 0)
1609+ {
1610+ if (!add_dynamic_entry (DT_PLTGOT, 0)
1611+ || !add_dynamic_entry (DT_PLTRELSZ, 0)
1612+ || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1613+ || !add_dynamic_entry (DT_JMPREL, 0))
1614+ return FALSE;
1615+ }
1616+
1617+ if (!add_dynamic_entry (DT_RELA, 0)
1618+ || !add_dynamic_entry (DT_RELASZ, 0)
1619+ || !add_dynamic_entry (DT_RELAENT, sizeof (ElfNN_External_Rela)))
1620+ return FALSE;
1621+
1622+ /* If any dynamic relocs apply to a read-only section,
1623+ then we need a DT_TEXTREL entry. */
1624+ if ((info->flags & DF_TEXTREL) == 0)
1625+ elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
1626+
1627+ if (info->flags & DF_TEXTREL)
1628+ {
1629+ if (!add_dynamic_entry (DT_TEXTREL, 0))
1630+ return FALSE;
1631+ }
1632+ }
1633+#undef add_dynamic_entry
1634+
1635+ return TRUE;
1636+}
1637+
1638+#define TP_OFFSET 0
1639+#define DTP_OFFSET 0x800
1640+
1641+/* Return the relocation value for a TLS dtp-relative reloc. */
1642+
1643+static bfd_vma
1644+dtpoff (struct bfd_link_info *info, bfd_vma address)
1645+{
1646+ /* If tls_sec is NULL, we should have signalled an error already. */
1647+ if (elf_hash_table (info)->tls_sec == NULL)
1648+ return 0;
1649+ return address - elf_hash_table (info)->tls_sec->vma - DTP_OFFSET;
1650+}
1651+
1652+/* Return the relocation value for a static TLS tp-relative relocation. */
1653+
1654+static bfd_vma
1655+tpoff (struct bfd_link_info *info, bfd_vma address)
1656+{
1657+ /* If tls_sec is NULL, we should have signalled an error already. */
1658+ if (elf_hash_table (info)->tls_sec == NULL)
1659+ return 0;
1660+ return address - elf_hash_table (info)->tls_sec->vma - TP_OFFSET;
1661+}
1662+
1663+/* Return the global pointer's value, or 0 if it is not in use. */
1664+
1665+static bfd_vma
1666+riscv_global_pointer_value (struct bfd_link_info *info)
1667+{
1668+ struct bfd_link_hash_entry *h;
1669+
1670+ h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
1671+ if (h == NULL || h->type != bfd_link_hash_defined)
1672+ return 0;
1673+
1674+ return h->u.def.value + sec_addr (h->u.def.section);
1675+}
1676+
1677+/* Emplace a static relocation. */
1678+
1679+static bfd_reloc_status_type
1680+perform_relocation (const reloc_howto_type *howto,
1681+ const Elf_Internal_Rela *rel,
1682+ bfd_vma value,
1683+ asection *input_section,
1684+ bfd *input_bfd,
1685+ bfd_byte *contents)
1686+{
1687+ if (howto->pc_relative)
1688+ value -= sec_addr (input_section) + rel->r_offset;
1689+ value += rel->r_addend;
1690+
1691+ switch (ELFNN_R_TYPE (rel->r_info))
1692+ {
1693+ case R_RISCV_HI20:
1694+ case R_RISCV_TPREL_HI20:
1695+ case R_RISCV_PCREL_HI20:
1696+ case R_RISCV_GOT_HI20:
1697+ case R_RISCV_TLS_GOT_HI20:
1698+ case R_RISCV_TLS_GD_HI20:
1699+ value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value));
1700+ break;
1701+
1702+ case R_RISCV_LO12_I:
1703+ case R_RISCV_TPREL_LO12_I:
1704+ case R_RISCV_PCREL_LO12_I:
1705+ value = ENCODE_ITYPE_IMM (value);
1706+ break;
1707+
1708+ case R_RISCV_LO12_S:
1709+ case R_RISCV_TPREL_LO12_S:
1710+ case R_RISCV_PCREL_LO12_S:
1711+ value = ENCODE_STYPE_IMM (value);
1712+ break;
1713+
1714+ case R_RISCV_CALL:
1715+ case R_RISCV_CALL_PLT:
1716+ if (!VALID_UTYPE_IMM (RISCV_CONST_HIGH_PART (value)))
1717+ return bfd_reloc_overflow;
1718+ value = ENCODE_UTYPE_IMM (RISCV_CONST_HIGH_PART (value))
1719+ | (ENCODE_ITYPE_IMM (value) << 32);
1720+ break;
1721+
1722+ case R_RISCV_JAL:
1723+ if (!VALID_UJTYPE_IMM (value))
1724+ return bfd_reloc_overflow;
1725+ value = ENCODE_UJTYPE_IMM (value);
1726+ break;
1727+
1728+ case R_RISCV_BRANCH:
1729+ if (!VALID_SBTYPE_IMM (value))
1730+ return bfd_reloc_overflow;
1731+ value = ENCODE_SBTYPE_IMM (value);
1732+ break;
1733+
1734+ case R_RISCV_32:
1735+ case R_RISCV_64:
1736+ case R_RISCV_ADD8:
1737+ case R_RISCV_ADD16:
1738+ case R_RISCV_ADD32:
1739+ case R_RISCV_ADD64:
1740+ case R_RISCV_SUB8:
1741+ case R_RISCV_SUB16:
1742+ case R_RISCV_SUB32:
1743+ case R_RISCV_SUB64:
1744+ case R_RISCV_TLS_DTPREL32:
1745+ case R_RISCV_TLS_DTPREL64:
1746+ break;
1747+
1748+ default:
1749+ return bfd_reloc_notsupported;
1750+ }
1751+
1752+ bfd_vma word = bfd_get (howto->bitsize, input_bfd, contents + rel->r_offset);
1753+ word = (word & ~howto->dst_mask) | (value & howto->dst_mask);
1754+ bfd_put (howto->bitsize, input_bfd, word, contents + rel->r_offset);
1755+
1756+ return bfd_reloc_ok;
1757+}
1758+
1759+/* Remember all PC-relative high-part relocs we've encountered to help us
1760+ later resolve the corresponding low-part relocs. */
1761+
1762+typedef struct {
1763+ bfd_vma address;
1764+ bfd_vma value;
1765+} riscv_pcrel_hi_reloc;
1766+
1767+typedef struct riscv_pcrel_lo_reloc {
1768+ asection *input_section;
1769+ struct bfd_link_info *info;
1770+ reloc_howto_type *howto;
1771+ const Elf_Internal_Rela *reloc;
1772+ bfd_vma addr;
1773+ const char *name;
1774+ bfd_byte *contents;
1775+ struct riscv_pcrel_lo_reloc *next;
1776+} riscv_pcrel_lo_reloc;
1777+
1778+typedef struct {
1779+ htab_t hi_relocs;
1780+ riscv_pcrel_lo_reloc *lo_relocs;
1781+} riscv_pcrel_relocs;
1782+
1783+static hashval_t
1784+riscv_pcrel_reloc_hash (const void *entry)
1785+{
1786+ const riscv_pcrel_hi_reloc *e = entry;
1787+ return (hashval_t)(e->address >> 2);
1788+}
1789+
1790+static bfd_boolean
1791+riscv_pcrel_reloc_eq (const void *entry1, const void *entry2)
1792+{
1793+ const riscv_pcrel_hi_reloc *e1 = entry1, *e2 = entry2;
1794+ return e1->address == e2->address;
1795+}
1796+
1797+static bfd_boolean
1798+riscv_init_pcrel_relocs (riscv_pcrel_relocs *p)
1799+{
1800+
1801+ p->lo_relocs = NULL;
1802+ p->hi_relocs = htab_create (1024, riscv_pcrel_reloc_hash,
1803+ riscv_pcrel_reloc_eq, free);
1804+ return p->hi_relocs != NULL;
1805+}
1806+
1807+static void
1808+riscv_free_pcrel_relocs (riscv_pcrel_relocs *p)
1809+{
1810+ riscv_pcrel_lo_reloc *cur = p->lo_relocs;
1811+ while (cur != NULL)
1812+ {
1813+ riscv_pcrel_lo_reloc *next = cur->next;
1814+ free (cur);
1815+ cur = next;
1816+ }
1817+
1818+ htab_delete (p->hi_relocs);
1819+}
1820+
1821+static bfd_boolean
1822+riscv_record_pcrel_hi_reloc (riscv_pcrel_relocs *p, bfd_vma addr, bfd_vma value)
1823+{
1824+ riscv_pcrel_hi_reloc entry = {addr, value - addr};
1825+ riscv_pcrel_hi_reloc **slot =
1826+ (riscv_pcrel_hi_reloc **) htab_find_slot (p->hi_relocs, &entry, INSERT);
1827+ BFD_ASSERT (*slot == NULL);
1828+ *slot = (riscv_pcrel_hi_reloc *) bfd_malloc (sizeof (riscv_pcrel_hi_reloc));
1829+ if (*slot == NULL)
1830+ return FALSE;
1831+ **slot = entry;
1832+ return TRUE;
1833+}
1834+
1835+static bfd_boolean
1836+riscv_record_pcrel_lo_reloc (riscv_pcrel_relocs *p,
1837+ asection *input_section,
1838+ struct bfd_link_info *info,
1839+ reloc_howto_type *howto,
1840+ const Elf_Internal_Rela *reloc,
1841+ bfd_vma addr,
1842+ const char *name,
1843+ bfd_byte *contents)
1844+{
1845+ riscv_pcrel_lo_reloc *entry;
1846+ entry = (riscv_pcrel_lo_reloc *) bfd_malloc (sizeof (riscv_pcrel_lo_reloc));
1847+ if (entry == NULL)
1848+ return FALSE;
1849+ *entry = (riscv_pcrel_lo_reloc) {input_section, info, howto, reloc, addr,
1850+ name, contents, p->lo_relocs};
1851+ p->lo_relocs = entry;
1852+ return TRUE;
1853+}
1854+
1855+static bfd_boolean
1856+riscv_resolve_pcrel_lo_relocs (riscv_pcrel_relocs *p)
1857+{
1858+ riscv_pcrel_lo_reloc *r;
1859+ for (r = p->lo_relocs; r != NULL; r = r->next)
1860+ {
1861+ bfd *input_bfd = r->input_section->owner;
1862+ riscv_pcrel_hi_reloc search = {r->addr, 0};
1863+ riscv_pcrel_hi_reloc *entry = htab_find (p->hi_relocs, &search);
1864+ if (entry == NULL)
1865+ return ((*r->info->callbacks->reloc_overflow)
1866+ (r->info, NULL, r->name, r->howto->name, (bfd_vma) 0,
1867+ input_bfd, r->input_section, r->reloc->r_offset));
1868+
1869+ perform_relocation (r->howto, r->reloc, entry->value, r->input_section,
1870+ input_bfd, r->contents);
1871+ }
1872+
1873+ return TRUE;
1874+}
1875+
1876+/* Relocate a RISC-V ELF section.
1877+
1878+ The RELOCATE_SECTION function is called by the new ELF backend linker
1879+ to handle the relocations for a section.
1880+
1881+ The relocs are always passed as Rela structures.
1882+
1883+ This function is responsible for adjusting the section contents as
1884+ necessary, and (if generating a relocatable output file) adjusting
1885+ the reloc addend as necessary.
1886+
1887+ This function does not have to worry about setting the reloc
1888+ address or the reloc symbol index.
1889+
1890+ LOCAL_SYMS is a pointer to the swapped in local symbols.
1891+
1892+ LOCAL_SECTIONS is an array giving the section in the input file
1893+ corresponding to the st_shndx field of each local symbol.
1894+
1895+ The global hash table entry for the global symbols can be found
1896+ via elf_sym_hashes (input_bfd).
1897+
1898+ When generating relocatable output, this function must handle
1899+ STB_LOCAL/STT_SECTION symbols specially. The output symbol is
1900+ going to be the section symbol corresponding to the output
1901+ section, which means that the addend must be adjusted
1902+ accordingly. */
1903+
1904+static bfd_boolean
1905+riscv_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1906+ bfd *input_bfd, asection *input_section,
1907+ bfd_byte *contents, Elf_Internal_Rela *relocs,
1908+ Elf_Internal_Sym *local_syms,
1909+ asection **local_sections)
1910+{
1911+ Elf_Internal_Rela *rel;
1912+ Elf_Internal_Rela *relend;
1913+ riscv_pcrel_relocs pcrel_relocs;
1914+ bfd_boolean ret = FALSE;
1915+ asection *sreloc = elf_section_data (input_section)->sreloc;
1916+ struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
1917+ Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_bfd);
1918+ struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
1919+ bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
1920+
1921+ if (!riscv_init_pcrel_relocs (&pcrel_relocs))
1922+ return FALSE;
1923+
1924+ relend = relocs + input_section->reloc_count;
1925+ for (rel = relocs; rel < relend; rel++)
1926+ {
1927+ unsigned long r_symndx;
1928+ struct elf_link_hash_entry *h;
1929+ Elf_Internal_Sym *sym;
1930+ asection *sec;
1931+ bfd_vma relocation;
1932+ bfd_reloc_status_type r = bfd_reloc_ok;
1933+ const char *name;
1934+ bfd_vma off, ie_off;
1935+ bfd_boolean unresolved_reloc, is_ie = FALSE;
1936+ bfd_vma pc = sec_addr (input_section) + rel->r_offset;
1937+ int r_type = ELFNN_R_TYPE (rel->r_info), tls_type;
1938+ reloc_howto_type *howto = riscv_elf_rtype_to_howto (r_type);
1939+ const char *msg = NULL;
1940+
1941+ if (r_type == R_RISCV_GNU_VTINHERIT || r_type == R_RISCV_GNU_VTENTRY)
1942+ continue;
1943+
1944+ /* This is a final link. */
1945+ r_symndx = ELFNN_R_SYM (rel->r_info);
1946+ h = NULL;
1947+ sym = NULL;
1948+ sec = NULL;
1949+ unresolved_reloc = FALSE;
1950+ if (r_symndx < symtab_hdr->sh_info)
1951+ {
1952+ sym = local_syms + r_symndx;
1953+ sec = local_sections[r_symndx];
1954+ relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1955+ }
1956+ else
1957+ {
1958+ bfd_boolean warned, ignored;
1959+
1960+ RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1961+ r_symndx, symtab_hdr, sym_hashes,
1962+ h, sec, relocation,
1963+ unresolved_reloc, warned, ignored);
1964+ if (warned)
1965+ {
1966+ /* To avoid generating warning messages about truncated
1967+ relocations, set the relocation's address to be the same as
1968+ the start of this section. */
1969+ if (input_section->output_section != NULL)
1970+ relocation = input_section->output_section->vma;
1971+ else
1972+ relocation = 0;
1973+ }
1974+ }
1975+
1976+ if (sec != NULL && discarded_section (sec))
1977+ RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1978+ rel, 1, relend, howto, 0, contents);
1979+
1980+ if (info->relocatable)
1981+ continue;
1982+
1983+ if (h != NULL)
1984+ name = h->root.root.string;
1985+ else
1986+ {
1987+ name = (bfd_elf_string_from_elf_section
1988+ (input_bfd, symtab_hdr->sh_link, sym->st_name));
1989+ if (name == NULL || *name == '\0')
1990+ name = bfd_section_name (input_bfd, sec);
1991+ }
1992+
1993+ switch (r_type)
1994+ {
1995+ case R_RISCV_NONE:
1996+ case R_RISCV_TPREL_ADD:
1997+ case R_RISCV_COPY:
1998+ case R_RISCV_JUMP_SLOT:
1999+ case R_RISCV_RELATIVE:
2000+ /* These require nothing of us at all. */
2001+ continue;
2002+
2003+ case R_RISCV_BRANCH:
2004+ case R_RISCV_HI20:
2005+ /* These require no special handling beyond perform_relocation. */
2006+ break;
2007+
2008+ case R_RISCV_GOT_HI20:
2009+ if (h != NULL)
2010+ {
2011+ bfd_boolean dyn;
2012+
2013+ off = h->got.offset;
2014+ BFD_ASSERT (off != (bfd_vma) -1);
2015+ dyn = elf_hash_table (info)->dynamic_sections_created;
2016+
2017+ if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2018+ || (info->shared
2019+ && SYMBOL_REFERENCES_LOCAL (info, h)))
2020+ {
2021+ /* This is actually a static link, or it is a
2022+ -Bsymbolic link and the symbol is defined
2023+ locally, or the symbol was forced to be local
2024+ because of a version file. We must initialize
2025+ this entry in the global offset table. Since the
2026+ offset must always be a multiple of the word size,
2027+ we use the least significant bit to record whether
2028+ we have initialized it already.
2029+
2030+ When doing a dynamic link, we create a .rela.got
2031+ relocation entry to initialize the value. This
2032+ is done in the finish_dynamic_symbol routine. */
2033+ if ((off & 1) != 0)
2034+ off &= ~1;
2035+ else
2036+ {
2037+ bfd_put_NN (output_bfd, relocation,
2038+ htab->elf.sgot->contents + off);
2039+ h->got.offset |= 1;
2040+ }
2041+ }
2042+ else
2043+ unresolved_reloc = FALSE;
2044+ }
2045+ else
2046+ {
2047+ BFD_ASSERT (local_got_offsets != NULL
2048+ && local_got_offsets[r_symndx] != (bfd_vma) -1);
2049+
2050+ off = local_got_offsets[r_symndx];
2051+
2052+ /* The offset must always be a multiple of 8 on 64-bit.
2053+ We use the least significant bit to record
2054+ whether we have already processed this entry. */
2055+ if ((off & 1) != 0)
2056+ off &= ~1;
2057+ else
2058+ {
2059+ if (info->shared)
2060+ {
2061+ asection *s;
2062+ Elf_Internal_Rela outrel;
2063+
2064+ /* We need to generate a R_RISCV_RELATIVE reloc
2065+ for the dynamic linker. */
2066+ s = htab->elf.srelgot;
2067+ BFD_ASSERT (s != NULL);
2068+
2069+ outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2070+ outrel.r_info =
2071+ ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2072+ outrel.r_addend = relocation;
2073+ relocation = 0;
2074+ riscv_elf_append_rela (output_bfd, s, &outrel);
2075+ }
2076+
2077+ bfd_put_NN (output_bfd, relocation,
2078+ htab->elf.sgot->contents + off);
2079+ local_got_offsets[r_symndx] |= 1;
2080+ }
2081+ }
2082+ relocation = sec_addr (htab->elf.sgot) + off;
2083+ if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation))
2084+ r = bfd_reloc_overflow;
2085+ break;
2086+
2087+ case R_RISCV_ADD8:
2088+ case R_RISCV_ADD16:
2089+ case R_RISCV_ADD32:
2090+ case R_RISCV_ADD64:
2091+ {
2092+ bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2093+ contents + rel->r_offset);
2094+ relocation = old_value + relocation;
2095+ }
2096+ break;
2097+
2098+ case R_RISCV_SUB8:
2099+ case R_RISCV_SUB16:
2100+ case R_RISCV_SUB32:
2101+ case R_RISCV_SUB64:
2102+ {
2103+ bfd_vma old_value = bfd_get (howto->bitsize, input_bfd,
2104+ contents + rel->r_offset);
2105+ relocation = old_value - relocation;
2106+ }
2107+ break;
2108+
2109+ case R_RISCV_CALL_PLT:
2110+ case R_RISCV_CALL:
2111+ case R_RISCV_JAL:
2112+ if (info->shared && h != NULL && h->plt.offset != MINUS_ONE)
2113+ {
2114+ /* Refer to the PLT entry. */
2115+ relocation = sec_addr (htab->elf.splt) + h->plt.offset;
2116+ unresolved_reloc = FALSE;
2117+ }
2118+ break;
2119+
2120+ case R_RISCV_TPREL_HI20:
2121+ relocation = tpoff (info, relocation);
2122+ break;
2123+
2124+ case R_RISCV_TPREL_LO12_I:
2125+ case R_RISCV_TPREL_LO12_S:
2126+ relocation = tpoff (info, relocation);
2127+ if (VALID_ITYPE_IMM (relocation + rel->r_addend))
2128+ {
2129+ /* We can use tp as the base register. */
2130+ bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2131+ insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2132+ insn |= X_TP << OP_SH_RS1;
2133+ bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2134+ }
2135+ break;
2136+
2137+ case R_RISCV_LO12_I:
2138+ case R_RISCV_LO12_S:
2139+ {
2140+ bfd_vma gp = riscv_global_pointer_value (info);
2141+ bfd_boolean x0_base = VALID_ITYPE_IMM (relocation + rel->r_addend);
2142+ if (x0_base || VALID_ITYPE_IMM (relocation + rel->r_addend - gp))
2143+ {
2144+ /* We can use x0 or gp as the base register. */
2145+ bfd_vma insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2146+ insn &= ~(OP_MASK_RS1 << OP_SH_RS1);
2147+ if (!x0_base)
2148+ {
2149+ rel->r_addend -= gp;
2150+ insn |= X_GP << OP_SH_RS1;
2151+ }
2152+ bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2153+ }
2154+ break;
2155+ }
2156+
2157+ case R_RISCV_PCREL_HI20:
2158+ if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc,
2159+ relocation + rel->r_addend))
2160+ r = bfd_reloc_overflow;
2161+ break;
2162+
2163+ case R_RISCV_PCREL_LO12_I:
2164+ case R_RISCV_PCREL_LO12_S:
2165+ if (riscv_record_pcrel_lo_reloc (&pcrel_relocs, input_section, info,
2166+ howto, rel, relocation, name,
2167+ contents))
2168+ continue;
2169+ r = bfd_reloc_overflow;
2170+ break;
2171+
2172+ case R_RISCV_TLS_DTPREL32:
2173+ case R_RISCV_TLS_DTPREL64:
2174+ relocation = dtpoff (info, relocation);
2175+ break;
2176+
2177+ case R_RISCV_32:
2178+ case R_RISCV_64:
2179+ if ((input_section->flags & SEC_ALLOC) == 0)
2180+ break;
2181+
2182+ if ((info->shared
2183+ && (h == NULL
2184+ || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2185+ || h->root.type != bfd_link_hash_undefweak)
2186+ && (! howto->pc_relative
2187+ || !SYMBOL_CALLS_LOCAL (info, h)))
2188+ || (!info->shared
2189+ && h != NULL
2190+ && h->dynindx != -1
2191+ && !h->non_got_ref
2192+ && ((h->def_dynamic
2193+ && !h->def_regular)
2194+ || h->root.type == bfd_link_hash_undefweak
2195+ || h->root.type == bfd_link_hash_undefined)))
2196+ {
2197+ Elf_Internal_Rela outrel;
2198+ bfd_boolean skip_static_relocation, skip_dynamic_relocation;
2199+
2200+ /* When generating a shared object, these relocations
2201+ are copied into the output file to be resolved at run
2202+ time. */
2203+
2204+ outrel.r_offset =
2205+ _bfd_elf_section_offset (output_bfd, info, input_section,
2206+ rel->r_offset);
2207+ skip_static_relocation = outrel.r_offset != (bfd_vma) -2;
2208+ skip_dynamic_relocation = outrel.r_offset >= (bfd_vma) -2;
2209+ outrel.r_offset += sec_addr (input_section);
2210+
2211+ if (skip_dynamic_relocation)
2212+ memset (&outrel, 0, sizeof outrel);
2213+ else if (h != NULL && h->dynindx != -1
2214+ && !(info->shared
2215+ && SYMBOLIC_BIND (info, h)
2216+ && h->def_regular))
2217+ {
2218+ outrel.r_info = ELFNN_R_INFO (h->dynindx, r_type);
2219+ outrel.r_addend = rel->r_addend;
2220+ }
2221+ else
2222+ {
2223+ outrel.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2224+ outrel.r_addend = relocation + rel->r_addend;
2225+ }
2226+
2227+ riscv_elf_append_rela (output_bfd, sreloc, &outrel);
2228+ if (skip_static_relocation)
2229+ continue;
2230+ }
2231+ break;
2232+
2233+ case R_RISCV_TLS_GOT_HI20:
2234+ is_ie = TRUE;
2235+ /* Fall through. */
2236+
2237+ case R_RISCV_TLS_GD_HI20:
2238+ if (h != NULL)
2239+ {
2240+ off = h->got.offset;
2241+ h->got.offset |= 1;
2242+ }
2243+ else
2244+ {
2245+ off = local_got_offsets[r_symndx];
2246+ local_got_offsets[r_symndx] |= 1;
2247+ }
2248+
2249+ tls_type = _bfd_riscv_elf_tls_type (input_bfd, h, r_symndx);
2250+ BFD_ASSERT (tls_type & (GOT_TLS_IE | GOT_TLS_GD));
2251+ /* If this symbol is referenced by both GD and IE TLS, the IE
2252+ reference's GOT slot follows the GD reference's slots. */
2253+ ie_off = 0;
2254+ if ((tls_type & GOT_TLS_GD) && (tls_type & GOT_TLS_IE))
2255+ ie_off = 2 * GOT_ENTRY_SIZE;
2256+
2257+ if ((off & 1) != 0)
2258+ off &= ~1;
2259+ else
2260+ {
2261+ Elf_Internal_Rela outrel;
2262+ int indx = 0;
2263+ bfd_boolean need_relocs = FALSE;
2264+
2265+ if (htab->elf.srelgot == NULL)
2266+ abort ();
2267+
2268+ if (h != NULL)
2269+ {
2270+ bfd_boolean dyn;
2271+ dyn = htab->elf.dynamic_sections_created;
2272+
2273+ if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2274+ && (!info->shared
2275+ || !SYMBOL_REFERENCES_LOCAL (info, h)))
2276+ {
2277+ indx = h->dynindx;
2278+ }
2279+ }
2280+
2281+ /* The GOT entries have not been initialized yet. Do it
2282+ now, and emit any relocations. */
2283+ if ((info->shared || indx != 0)
2284+ && (h == NULL
2285+ || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2286+ || h->root.type != bfd_link_hash_undefweak))
2287+ need_relocs = TRUE;
2288+
2289+ if (tls_type & GOT_TLS_GD)
2290+ {
2291+ if (need_relocs)
2292+ {
2293+ outrel.r_offset = sec_addr (htab->elf.sgot) + off;
2294+ outrel.r_addend = 0;
2295+ outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPMODNN);
2296+ bfd_put_NN (output_bfd, 0,
2297+ htab->elf.sgot->contents + off);
2298+ riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2299+ if (indx == 0)
2300+ {
2301+ BFD_ASSERT (! unresolved_reloc);
2302+ bfd_put_NN (output_bfd,
2303+ dtpoff (info, relocation),
2304+ (htab->elf.sgot->contents + off +
2305+ RISCV_ELF_WORD_BYTES));
2306+ }
2307+ else
2308+ {
2309+ bfd_put_NN (output_bfd, 0,
2310+ (htab->elf.sgot->contents + off +
2311+ RISCV_ELF_WORD_BYTES));
2312+ outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_DTPRELNN);
2313+ outrel.r_offset += RISCV_ELF_WORD_BYTES;
2314+ riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2315+ }
2316+ }
2317+ else
2318+ {
2319+ /* If we are not emitting relocations for a
2320+ general dynamic reference, then we must be in a
2321+ static link or an executable link with the
2322+ symbol binding locally. Mark it as belonging
2323+ to module 1, the executable. */
2324+ bfd_put_NN (output_bfd, 1,
2325+ htab->elf.sgot->contents + off);
2326+ bfd_put_NN (output_bfd,
2327+ dtpoff (info, relocation),
2328+ (htab->elf.sgot->contents + off +
2329+ RISCV_ELF_WORD_BYTES));
2330+ }
2331+ }
2332+
2333+ if (tls_type & GOT_TLS_IE)
2334+ {
2335+ if (need_relocs)
2336+ {
2337+ bfd_put_NN (output_bfd, 0,
2338+ htab->elf.sgot->contents + off + ie_off);
2339+ outrel.r_offset = sec_addr (htab->elf.sgot)
2340+ + off + ie_off;
2341+ outrel.r_addend = 0;
2342+ if (indx == 0)
2343+ outrel.r_addend = tpoff (info, relocation);
2344+ outrel.r_info = ELFNN_R_INFO (indx, R_RISCV_TLS_TPRELNN);
2345+ riscv_elf_append_rela (output_bfd, htab->elf.srelgot, &outrel);
2346+ }
2347+ else
2348+ {
2349+ bfd_put_NN (output_bfd, tpoff (info, relocation),
2350+ htab->elf.sgot->contents + off + ie_off);
2351+ }
2352+ }
2353+ }
2354+
2355+ BFD_ASSERT (off < (bfd_vma) -2);
2356+ relocation = sec_addr (htab->elf.sgot) + off + (is_ie ? ie_off : 0);
2357+ if (!riscv_record_pcrel_hi_reloc (&pcrel_relocs, pc, relocation))
2358+ r = bfd_reloc_overflow;
2359+ unresolved_reloc = FALSE;
2360+ break;
2361+
2362+ default:
2363+ r = bfd_reloc_notsupported;
2364+ }
2365+
2366+ /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2367+ because such sections are not SEC_ALLOC and thus ld.so will
2368+ not process them. */
2369+ if (unresolved_reloc
2370+ && !((input_section->flags & SEC_DEBUGGING) != 0
2371+ && h->def_dynamic)
2372+ && _bfd_elf_section_offset (output_bfd, info, input_section,
2373+ rel->r_offset) != (bfd_vma) -1)
2374+ {
2375+ (*_bfd_error_handler)
2376+ (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
2377+ input_bfd,
2378+ input_section,
2379+ (long) rel->r_offset,
2380+ howto->name,
2381+ h->root.root.string);
2382+ continue;
2383+ }
2384+
2385+ if (r == bfd_reloc_ok)
2386+ r = perform_relocation (howto, rel, relocation, input_section,
2387+ input_bfd, contents);
2388+
2389+ switch (r)
2390+ {
2391+ case bfd_reloc_ok:
2392+ continue;
2393+
2394+ case bfd_reloc_overflow:
2395+ r = info->callbacks->reloc_overflow
2396+ (info, (h ? &h->root : NULL), name, howto->name,
2397+ (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
2398+ break;
2399+
2400+ case bfd_reloc_undefined:
2401+ r = info->callbacks->undefined_symbol
2402+ (info, name, input_bfd, input_section, rel->r_offset,
2403+ TRUE);
2404+ break;
2405+
2406+ case bfd_reloc_outofrange:
2407+ msg = _("internal error: out of range error");
2408+ break;
2409+
2410+ case bfd_reloc_notsupported:
2411+ msg = _("internal error: unsupported relocation error");
2412+ break;
2413+
2414+ case bfd_reloc_dangerous:
2415+ msg = _("internal error: dangerous relocation");
2416+ break;
2417+
2418+ default:
2419+ msg = _("internal error: unknown error");
2420+ break;
2421+ }
2422+
2423+ if (msg)
2424+ r = info->callbacks->warning
2425+ (info, msg, name, input_bfd, input_section, rel->r_offset);
2426+ goto out;
2427+ }
2428+
2429+ ret = riscv_resolve_pcrel_lo_relocs (&pcrel_relocs);
2430+out:
2431+ riscv_free_pcrel_relocs (&pcrel_relocs);
2432+ return ret;
2433+}
2434+
2435+/* Finish up dynamic symbol handling. We set the contents of various
2436+ dynamic sections here. */
2437+
2438+static bfd_boolean
2439+riscv_elf_finish_dynamic_symbol (bfd *output_bfd,
2440+ struct bfd_link_info *info,
2441+ struct elf_link_hash_entry *h,
2442+ Elf_Internal_Sym *sym)
2443+{
2444+ struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2445+ const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2446+
2447+ if (h->plt.offset != (bfd_vma) -1)
2448+ {
2449+ /* We've decided to create a PLT entry for this symbol. */
2450+ bfd_byte *loc;
2451+ bfd_vma i, header_address, plt_idx, got_address;
2452+ uint32_t plt_entry[PLT_ENTRY_INSNS];
2453+ Elf_Internal_Rela rela;
2454+
2455+ BFD_ASSERT (h->dynindx != -1);
2456+
2457+ /* Calculate the address of the PLT header. */
2458+ header_address = sec_addr (htab->elf.splt);
2459+
2460+ /* Calculate the index of the entry. */
2461+ plt_idx = (h->plt.offset - PLT_HEADER_SIZE) / PLT_ENTRY_SIZE;
2462+
2463+ /* Calculate the address of the .got.plt entry. */
2464+ got_address = riscv_elf_got_plt_val (plt_idx, info);
2465+
2466+ /* Find out where the .plt entry should go. */
2467+ loc = htab->elf.splt->contents + h->plt.offset;
2468+
2469+ /* Fill in the PLT entry itself. */
2470+ riscv_make_plt_entry (got_address, header_address + h->plt.offset,
2471+ plt_entry);
2472+ for (i = 0; i < PLT_ENTRY_INSNS; i++)
2473+ bfd_put_32 (output_bfd, plt_entry[i], loc + 4*i);
2474+
2475+ /* Fill in the initial value of the .got.plt entry. */
2476+ loc = htab->elf.sgotplt->contents
2477+ + (got_address - sec_addr (htab->elf.sgotplt));
2478+ bfd_put_NN (output_bfd, sec_addr (htab->elf.splt), loc);
2479+
2480+ /* Fill in the entry in the .rela.plt section. */
2481+ rela.r_offset = got_address;
2482+ rela.r_addend = 0;
2483+ rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_JUMP_SLOT);
2484+
2485+ loc = htab->elf.srelplt->contents + plt_idx * sizeof (ElfNN_External_Rela);
2486+ bed->s->swap_reloca_out (output_bfd, &rela, loc);
2487+
2488+ if (!h->def_regular)
2489+ {
2490+ /* Mark the symbol as undefined, rather than as defined in
2491+ the .plt section. Leave the value alone. */
2492+ sym->st_shndx = SHN_UNDEF;
2493+ /* If the symbol is weak, we do need to clear the value.
2494+ Otherwise, the PLT entry would provide a definition for
2495+ the symbol even if the symbol wasn't defined anywhere,
2496+ and so the symbol would never be NULL. */
2497+ if (!h->ref_regular_nonweak)
2498+ sym->st_value = 0;
2499+ }
2500+ }
2501+
2502+ if (h->got.offset != (bfd_vma) -1
2503+ && !(riscv_elf_hash_entry(h)->tls_type & (GOT_TLS_GD | GOT_TLS_IE)))
2504+ {
2505+ asection *sgot;
2506+ asection *srela;
2507+ Elf_Internal_Rela rela;
2508+
2509+ /* This symbol has an entry in the GOT. Set it up. */
2510+
2511+ sgot = htab->elf.sgot;
2512+ srela = htab->elf.srelgot;
2513+ BFD_ASSERT (sgot != NULL && srela != NULL);
2514+
2515+ rela.r_offset = sec_addr (sgot) + (h->got.offset &~ (bfd_vma) 1);
2516+
2517+ /* If this is a -Bsymbolic link, and the symbol is defined
2518+ locally, we just want to emit a RELATIVE reloc. Likewise if
2519+ the symbol was forced to be local because of a version file.
2520+ The entry in the global offset table will already have been
2521+ initialized in the relocate_section function. */
2522+ if (info->shared
2523+ && (info->symbolic || h->dynindx == -1)
2524+ && h->def_regular)
2525+ {
2526+ asection *sec = h->root.u.def.section;
2527+ rela.r_info = ELFNN_R_INFO (0, R_RISCV_RELATIVE);
2528+ rela.r_addend = (h->root.u.def.value
2529+ + sec->output_section->vma
2530+ + sec->output_offset);
2531+ }
2532+ else
2533+ {
2534+ BFD_ASSERT (h->dynindx != -1);
2535+ rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_NN);
2536+ rela.r_addend = 0;
2537+ }
2538+
2539+ bfd_put_NN (output_bfd, 0,
2540+ sgot->contents + (h->got.offset & ~(bfd_vma) 1));
2541+ riscv_elf_append_rela (output_bfd, srela, &rela);
2542+ }
2543+
2544+ if (h->needs_copy)
2545+ {
2546+ Elf_Internal_Rela rela;
2547+
2548+ /* This symbols needs a copy reloc. Set it up. */
2549+ BFD_ASSERT (h->dynindx != -1);
2550+
2551+ rela.r_offset = sec_addr (h->root.u.def.section) + h->root.u.def.value;
2552+ rela.r_info = ELFNN_R_INFO (h->dynindx, R_RISCV_COPY);
2553+ rela.r_addend = 0;
2554+ riscv_elf_append_rela (output_bfd, htab->srelbss, &rela);
2555+ }
2556+
2557+ /* Mark some specially defined symbols as absolute. */
2558+ if (h == htab->elf.hdynamic
2559+ || (h == htab->elf.hgot || h == htab->elf.hplt))
2560+ sym->st_shndx = SHN_ABS;
2561+
2562+ return TRUE;
2563+}
2564+
2565+/* Finish up the dynamic sections. */
2566+
2567+static bfd_boolean
2568+riscv_finish_dyn (bfd *output_bfd, struct bfd_link_info *info,
2569+ bfd *dynobj, asection *sdyn)
2570+{
2571+ struct riscv_elf_link_hash_table *htab = riscv_elf_hash_table (info);
2572+ const struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
2573+ size_t dynsize = bed->s->sizeof_dyn;
2574+ bfd_byte *dyncon, *dynconend;
2575+
2576+ dynconend = sdyn->contents + sdyn->size;
2577+ for (dyncon = sdyn->contents; dyncon < dynconend; dyncon += dynsize)
2578+ {
2579+ Elf_Internal_Dyn dyn;
2580+ asection *s;
2581+
2582+ bed->s->swap_dyn_in (dynobj, dyncon, &dyn);
2583+
2584+ switch (dyn.d_tag)
2585+ {
2586+ case DT_PLTGOT:
2587+ s = htab->elf.sgotplt;
2588+ dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2589+ break;
2590+ case DT_JMPREL:
2591+ s = htab->elf.srelplt;
2592+ dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2593+ break;
2594+ case DT_PLTRELSZ:
2595+ s = htab->elf.srelplt;
2596+ dyn.d_un.d_val = s->size;
2597+ break;
2598+ default:
2599+ continue;
2600+ }
2601+
2602+ bed->s->swap_dyn_out (output_bfd, &dyn, dyncon);
2603+ }
2604+ return TRUE;
2605+}
2606+
2607+static bfd_boolean
2608+riscv_elf_finish_dynamic_sections (bfd *output_bfd,
2609+ struct bfd_link_info *info)
2610+{
2611+ bfd *dynobj;
2612+ asection *sdyn;
2613+ struct riscv_elf_link_hash_table *htab;
2614+
2615+ htab = riscv_elf_hash_table (info);
2616+ BFD_ASSERT (htab != NULL);
2617+ dynobj = htab->elf.dynobj;
2618+
2619+ sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2620+
2621+ if (elf_hash_table (info)->dynamic_sections_created)
2622+ {
2623+ asection *splt;
2624+ bfd_boolean ret;
2625+
2626+ splt = htab->elf.splt;
2627+ BFD_ASSERT (splt != NULL && sdyn != NULL);
2628+
2629+ ret = riscv_finish_dyn (output_bfd, info, dynobj, sdyn);
2630+
2631+ if (ret != TRUE)
2632+ return ret;
2633+
2634+ /* Fill in the head and tail entries in the procedure linkage table. */
2635+ if (splt->size > 0)
2636+ {
2637+ int i;
2638+ uint32_t plt_header[PLT_HEADER_INSNS];
2639+ riscv_make_plt0_entry (sec_addr (htab->elf.sgotplt),
2640+ sec_addr (splt), plt_header);
2641+
2642+ for (i = 0; i < PLT_HEADER_INSNS; i++)
2643+ bfd_put_32 (output_bfd, plt_header[i], splt->contents + 4*i);
2644+ }
2645+
2646+ elf_section_data (splt->output_section)->this_hdr.sh_entsize
2647+ = PLT_ENTRY_SIZE;
2648+ }
2649+
2650+ if (htab->elf.sgotplt)
2651+ {
2652+ if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
2653+ {
2654+ (*_bfd_error_handler)
2655+ (_("discarded output section: `%A'"), htab->elf.sgotplt);
2656+ return FALSE;
2657+ }
2658+
2659+ if (htab->elf.sgotplt->size > 0)