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