Remove path name from test case
[binutils-gdb.git] / bfd / elf64-bpf.c
1 /* Linux bpf specific support for 64-bit ELF
2 Copyright (C) 2019-2023 Free Software Foundation, Inc.
3 Contributed by Oracle Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/bpf.h"
27 #include "libiberty.h"
28
29 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
30 #define MINUS_ONE (~ (bfd_vma) 0)
31
32 #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
33
34 static bfd_reloc_status_type bpf_elf_generic_reloc
35 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
36
37 #undef BPF_HOWTO
38 #define BPF_HOWTO(type, right, size, bits, pcrel, left, ovf, func, name, \
39 inplace, src_mask, dst_mask, pcrel_off) \
40 type##_IDX,
41 enum bpf_reloc_index {
42 R_BPF_INVALID_IDX = -1,
43 #include "bpf-reloc.def"
44 R_BPF_SIZE
45 };
46 #undef BPF_HOWTO
47
48 /* Relocation tables. */
49 #define BPF_HOWTO(...) HOWTO(__VA_ARGS__),
50 static reloc_howto_type bpf_elf_howto_table [] =
51 {
52 #include "bpf-reloc.def"
53 };
54 #undef AHOW
55 #undef BPF_HOWTO
56
57 #define BPF_HOWTO(type, right, size, bits, pcrel, left, ovf, func, name, \
58 inplace, src_mask, dst_mask, pcrel_off) \
59 case type: { return type##_IDX; }
60 static enum bpf_reloc_index
61 bpf_index_for_rtype(unsigned int r_type)
62 {
63 switch(r_type) {
64 #include "bpf-reloc.def"
65 default:
66 /* Unreachable code. */
67 BFD_ASSERT(0);
68 return -1;
69 };
70 }
71
72 /* Map BFD reloc types to bpf ELF reloc types. */
73
74 static reloc_howto_type *
75 bpf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
76 bfd_reloc_code_real_type code)
77 {
78 switch (code)
79 {
80 case BFD_RELOC_NONE:
81 return &bpf_elf_howto_table[ (int) R_BPF_NONE_IDX];
82
83 case BFD_RELOC_32:
84 return &bpf_elf_howto_table[ (int) R_BPF_64_ABS32_IDX];
85 case BFD_RELOC_64:
86 return &bpf_elf_howto_table[ (int) R_BPF_64_ABS64_IDX];
87
88 case BFD_RELOC_BPF_64:
89 return &bpf_elf_howto_table[ (int) R_BPF_64_64_IDX];
90 case BFD_RELOC_BPF_DISP32:
91 case BFD_RELOC_BPF_DISPCALL32:
92 return &bpf_elf_howto_table[ (int) R_BPF_64_32_IDX];
93 case BFD_RELOC_BPF_DISP16:
94 return &bpf_elf_howto_table[ (int) R_BPF_GNU_64_16_IDX];
95
96 default:
97 /* Pacify gcc -Wall. */
98 return NULL;
99 }
100 return NULL;
101 }
102
103 /* Map BFD reloc names to bpf ELF reloc names. */
104
105 static reloc_howto_type *
106 bpf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
107 {
108 unsigned int i;
109
110 for (i = 0; i < R_BPF_SIZE; i++)
111 if (bpf_elf_howto_table[i].name != NULL
112 && strcasecmp (bpf_elf_howto_table[i].name, r_name) == 0)
113 return &bpf_elf_howto_table[i];
114
115 return NULL;
116 }
117
118 /* Set the howto pointer for a bpf reloc. */
119
120 static bool
121 bpf_info_to_howto (bfd *abfd, arelent *bfd_reloc,
122 Elf_Internal_Rela *elf_reloc)
123 {
124 unsigned int r_type;
125 unsigned int i;
126 r_type = ELF64_R_TYPE (elf_reloc->r_info);
127
128 i = bpf_index_for_rtype(r_type);
129 if (i == (unsigned int) -1)
130 {
131 /* xgettext:c-format */
132 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
133 abfd, r_type);
134 bfd_set_error (bfd_error_bad_value);
135 return false;
136 }
137
138 bfd_reloc->howto = &bpf_elf_howto_table [i];
139 return true;
140 }
141
142 /* Relocate an eBPF ELF section.
143
144 The RELOCATE_SECTION function is called by the new ELF backend linker
145 to handle the relocations for a section.
146
147 The relocs are always passed as Rela structures; if the section
148 actually uses Rel structures, the r_addend field will always be
149 zero.
150
151 This function is responsible for adjusting the section contents as
152 necessary, and (if using Rela relocs and generating a relocatable
153 output file) adjusting the reloc addend as necessary.
154
155 This function does not have to worry about setting the reloc
156 address or the reloc symbol index.
157
158 LOCAL_SYMS is a pointer to the swapped in local symbols.
159
160 LOCAL_SECTIONS is an array giving the section in the input file
161 corresponding to the st_shndx field of each local symbol.
162
163 The global hash table entry for the global symbols can be found
164 via elf_sym_hashes (input_bfd).
165
166 When generating relocatable output, this function must handle
167 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
168 going to be the section symbol corresponding to the output
169 section, which means that the addend must be adjusted
170 accordingly. */
171
172 #define sec_addr(sec) ((sec)->output_section->vma + (sec)->output_offset)
173
174 static int
175 bpf_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
176 struct bfd_link_info *info,
177 bfd *input_bfd,
178 asection *input_section,
179 bfd_byte *contents,
180 Elf_Internal_Rela *relocs,
181 Elf_Internal_Sym *local_syms,
182 asection **local_sections)
183 {
184 Elf_Internal_Shdr *symtab_hdr;
185 struct elf_link_hash_entry **sym_hashes;
186 Elf_Internal_Rela *rel;
187 Elf_Internal_Rela *relend;
188
189 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
190 sym_hashes = elf_sym_hashes (input_bfd);
191 relend = relocs + input_section->reloc_count;
192
193 for (rel = relocs; rel < relend; rel ++)
194 {
195 reloc_howto_type * howto;
196 unsigned int howto_index;
197 unsigned long r_symndx;
198 Elf_Internal_Sym * sym;
199 asection * sec;
200 struct elf_link_hash_entry * h;
201 bfd_vma relocation;
202 bfd_reloc_status_type r;
203 const char * name = NULL;
204 int r_type ATTRIBUTE_UNUSED;
205 bfd_signed_vma addend;
206 bfd_byte * where;
207
208 r_type = ELF64_R_TYPE (rel->r_info);
209 r_symndx = ELF64_R_SYM (rel->r_info);
210
211 howto_index = bpf_index_for_rtype (ELF64_R_TYPE (rel->r_info));
212 howto = &bpf_elf_howto_table[howto_index];
213 h = NULL;
214 sym = NULL;
215 sec = NULL;
216 where = contents + rel->r_offset;
217
218 if (r_symndx < symtab_hdr->sh_info)
219 {
220 sym = local_syms + r_symndx;
221 sec = local_sections [r_symndx];
222 relocation = BASEADDR (sec) + sym->st_value;
223
224 name = bfd_elf_string_from_elf_section
225 (input_bfd, symtab_hdr->sh_link, sym->st_name);
226 name = name == NULL ? bfd_section_name (sec) : name;
227 }
228 else
229 {
230 bool warned ATTRIBUTE_UNUSED;
231 bool unresolved_reloc ATTRIBUTE_UNUSED;
232 bool ignored ATTRIBUTE_UNUSED;
233
234 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
235 r_symndx, symtab_hdr, sym_hashes,
236 h, sec, relocation,
237 unresolved_reloc, warned, ignored);
238
239 name = h->root.root.string;
240 }
241
242 if (sec != NULL && discarded_section (sec))
243 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
244 rel, 1, relend, howto, 0, contents);
245
246 if (bfd_link_relocatable (info))
247 continue;
248
249 switch (howto->type)
250 {
251 case R_BPF_64_32:
252 {
253 /* Make the relocation PC-relative, and change its unit to
254 64-bit words. Note we need *signed* arithmetic
255 here. */
256 relocation = ((bfd_signed_vma) relocation
257 - (sec_addr (input_section) + rel->r_offset));
258 relocation = (bfd_signed_vma) relocation / 8;
259
260 /* Get the addend from the instruction and apply it. */
261 addend = bfd_get (howto->bitsize, input_bfd,
262 contents + rel->r_offset
263 + (howto->bitsize == 16 ? 2 : 4));
264
265 if ((addend & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
266 addend -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
267 relocation += addend;
268
269 /* Write out the relocated value. */
270 bfd_put (howto->bitsize, input_bfd, relocation,
271 contents + rel->r_offset
272 + (howto->bitsize == 16 ? 2 : 4));
273
274 r = bfd_reloc_ok;
275 break;
276 }
277 case R_BPF_64_ABS64:
278 case R_BPF_64_ABS32:
279 {
280 addend = bfd_get (howto->bitsize, input_bfd, where);
281 relocation += addend;
282 bfd_put (howto->bitsize, input_bfd, relocation, where);
283
284 r = bfd_reloc_ok;
285 break;
286 }
287 case R_BPF_64_64:
288 {
289 /*
290 LDDW instructions are 128 bits long, with a 64-bit immediate.
291 The lower 32 bits of the immediate are in the same position
292 as the imm32 field of other instructions.
293 The upper 32 bits of the immediate are stored at the end of
294 the instruction.
295 */
296
297
298 /* Get the addend. The upper and lower 32 bits are split.
299 'where' is the beginning of the 16-byte instruction. */
300 addend = bfd_get_32 (input_bfd, where + 4);
301 addend |= (bfd_get_32 (input_bfd, where + 12) << 32);
302
303 relocation += addend;
304
305 bfd_put_32 (input_bfd, (relocation & 0xFFFFFFFF), where + 4);
306 bfd_put_32 (input_bfd, (relocation >> 32), where + 12);
307 r = bfd_reloc_ok;
308 break;
309 }
310 default:
311 r = bfd_reloc_notsupported;
312 }
313
314 if (r == bfd_reloc_ok)
315 r = bfd_check_overflow (howto->complain_on_overflow,
316 howto->bitsize,
317 howto->rightshift,
318 64, relocation);
319
320 if (r != bfd_reloc_ok)
321 {
322 const char * msg = NULL;
323
324 switch (r)
325 {
326 case bfd_reloc_overflow:
327 (*info->callbacks->reloc_overflow)
328 (info, (h ? &h->root : NULL), name, howto->name,
329 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
330 break;
331
332 case bfd_reloc_undefined:
333 (*info->callbacks->undefined_symbol)
334 (info, name, input_bfd, input_section, rel->r_offset, true);
335 break;
336
337 case bfd_reloc_outofrange:
338 msg = _("internal error: out of range error");
339 break;
340
341 case bfd_reloc_notsupported:
342 if (sym != NULL) /* Only if it's not an unresolved symbol. */
343 msg = _("internal error: relocation not supported");
344 break;
345
346 case bfd_reloc_dangerous:
347 msg = _("internal error: dangerous relocation");
348 break;
349
350 default:
351 msg = _("internal error: unknown error");
352 break;
353 }
354
355 if (msg)
356 (*info->callbacks->warning) (info, msg, name, input_bfd,
357 input_section, rel->r_offset);
358 }
359 }
360
361 return true;
362 }
363
364 /* Merge backend specific data from an object file to the output
365 object file when linking. */
366
367 static bool
368 elf64_bpf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
369 {
370 /* Check if we have the same endianness. */
371 if (! _bfd_generic_verify_endian_match (ibfd, info))
372 return false;
373
374 return true;
375 }
376
377 /* A generic howto special function for installing BPF relocations.
378 This function will be called by the assembler (via bfd_install_relocation),
379 and by various get_relocated_section_contents functions.
380 At link time, bpf_elf_relocate_section will resolve the final relocations.
381
382 BPF instructions are always big endian, and this approach avoids problems in
383 bfd_install_relocation. */
384
385 static bfd_reloc_status_type
386 bpf_elf_generic_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
387 void *data, asection *input_section,
388 bfd *output_bfd ATTRIBUTE_UNUSED,
389 char **error_message ATTRIBUTE_UNUSED)
390 {
391
392 bfd_signed_vma relocation;
393 bfd_reloc_status_type status;
394 bfd_byte *where;
395
396 /* Sanity check that the address is in range. */
397 bfd_size_type end = bfd_get_section_limit_octets (abfd, input_section);
398 bfd_size_type reloc_size;
399 if (reloc_entry->howto->type == R_BPF_64_64)
400 reloc_size = 16;
401 else
402 reloc_size = (reloc_entry->howto->bitsize
403 + reloc_entry->howto->bitpos) / 8;
404
405 if (reloc_entry->address > end
406 || end - reloc_entry->address < reloc_size)
407 return bfd_reloc_outofrange;
408
409 /* Get the symbol value. */
410 if (bfd_is_com_section (symbol->section))
411 relocation = 0;
412 else
413 relocation = symbol->value;
414
415 if (symbol->flags & BSF_SECTION_SYM)
416 /* Relocation against a section symbol: add in the section base address. */
417 relocation += BASEADDR (symbol->section);
418
419 relocation += reloc_entry->addend;
420
421 where = (bfd_byte *) data + reloc_entry->address;
422
423 status = bfd_check_overflow (reloc_entry->howto->complain_on_overflow,
424 reloc_entry->howto->bitsize,
425 reloc_entry->howto->rightshift, 64, relocation);
426
427 if (status != bfd_reloc_ok)
428 return status;
429
430 /* Now finally install the relocation. */
431 if (reloc_entry->howto->type == R_BPF_64_64)
432 {
433 /* lddw is a 128-bit (!) instruction that allows loading a 64-bit
434 immediate into a register. the immediate is split in half, with the
435 lower 32 bits in the same position as the imm32 field of other
436 instructions, and the upper 32 bits placed at the very end of the
437 instruction. that is, there are 32 unused bits between them. */
438
439 bfd_put_32 (abfd, (relocation & 0xFFFFFFFF), where + 4);
440 bfd_put_32 (abfd, (relocation >> 32), where + 12);
441 }
442 else
443 {
444 /* For other kinds of relocations, the relocated value simply goes
445 BITPOS bits from the start of the entry. This is always a multiple
446 of 8, i.e. whole bytes. */
447 bfd_put (reloc_entry->howto->bitsize, abfd, relocation,
448 where + reloc_entry->howto->bitpos / 8);
449 }
450
451 reloc_entry->addend = relocation;
452 reloc_entry->address += input_section->output_offset;
453
454 return bfd_reloc_ok;
455 }
456
457
458 /* The macros below configure the architecture. */
459
460 #define TARGET_LITTLE_SYM bpf_elf64_le_vec
461 #define TARGET_LITTLE_NAME "elf64-bpfle"
462
463 #define TARGET_BIG_SYM bpf_elf64_be_vec
464 #define TARGET_BIG_NAME "elf64-bpfbe"
465
466 #define ELF_ARCH bfd_arch_bpf
467 #define ELF_MACHINE_CODE EM_BPF
468
469 #define ELF_MAXPAGESIZE 0x100000
470
471 #define elf_info_to_howto_rel bpf_info_to_howto
472 #define elf_info_to_howto bpf_info_to_howto
473
474 #define elf_backend_may_use_rel_p 1
475 #define elf_backend_may_use_rela_p 0
476 #define elf_backend_default_use_rela_p 0
477 #define elf_backend_relocate_section bpf_elf_relocate_section
478
479 #define elf_backend_can_gc_sections 0
480
481 #define elf_symbol_leading_char '_'
482 #define bfd_elf64_bfd_reloc_type_lookup bpf_reloc_type_lookup
483 #define bfd_elf64_bfd_reloc_name_lookup bpf_reloc_name_lookup
484
485 #define bfd_elf64_bfd_merge_private_bfd_data elf64_bpf_merge_private_bfd_data
486
487 #include "elf64-target.h"