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