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