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