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