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