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