1 /* Linux bpf specific support for 64-bit ELF
2 Copyright (C) 2019 Free Software Foundation, Inc.
3 Contributed by Oracle Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "libiberty.h"
29 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
30 #define MINUS_ONE (~ (bfd_vma) 0)
32 #define BASEADDR(SEC) ((SEC)->output_section->vma + (SEC)->output_offset)
34 /* Handler for PC-relative relocations, which must be handled in
37 static bfd_reloc_status_type
38 bpf_elf_insn_disp_reloc (bfd *abfd,
42 asection *input_section,
44 char **error_message ATTRIBUTE_UNUSED)
46 bfd_signed_vma relocation;
47 bfd_signed_vma addend;
48 reloc_howto_type *howto = reloc_entry->howto;
50 /* This part is from bfd_elf_generic_reloc. */
51 if (output_bfd != NULL
52 && (symbol->flags & BSF_SECTION_SYM) == 0
53 && (! reloc_entry->howto->partial_inplace
54 || reloc_entry->addend == 0))
56 reloc_entry->address += input_section->output_offset;
60 /* This works because partial_inplace is FALSE. */
61 if (output_bfd != NULL)
62 return bfd_reloc_continue;
64 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
65 return bfd_reloc_outofrange;
67 relocation = (symbol->value
68 + symbol->section->output_section->vma
69 + symbol->section->output_offset);
70 /* Make it PC relative. */
71 relocation -= (input_section->output_section->vma
72 + input_section->output_offset);
73 relocation -= reloc_entry->address;
74 /* Make it 64-bit words. */
75 relocation = relocation / 8;
77 /* Get the addend from the instruction and apply it. */
78 switch (howto->bitsize)
84 addend = bfd_get_16 (abfd, (bfd_byte *) data + reloc_entry->address + 2);
87 addend = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
91 if ((addend & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
92 addend -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
95 /* Write out the relocated value. */
96 switch (howto->bitsize)
102 bfd_put_16 (abfd, relocation, (bfd_byte *) data + reloc_entry->address + 2);
105 bfd_put_32 (abfd, relocation, (bfd_byte *) data + reloc_entry->address + 4);
109 /* Check for overflow. */
110 if (howto->complain_on_overflow == complain_overflow_signed)
112 bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
113 bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
115 if (relocation > reloc_signed_max || relocation < reloc_signed_min)
116 return bfd_reloc_overflow;
124 /* Relocation tables. */
125 static reloc_howto_type bpf_elf_howto_table [] =
127 /* This reloc does nothing. */
128 HOWTO (R_BPF_NONE, /* type */
130 3, /* size (0 = byte, 1 = short, 2 = long) */
132 FALSE, /* pc_relative */
134 complain_overflow_dont, /* complain_on_overflow */
135 bfd_elf_generic_reloc, /* special_function */
136 "R_BPF_NONE", /* name */
137 FALSE, /* partial_inplace */
140 FALSE), /* pcrel_offset */
142 /* 64-immediate in LDDW instruction. */
143 HOWTO (R_BPF_INSN_64, /* type */
145 4, /* size (0 = byte, 1 = short, 2 = long) */
147 FALSE, /* pc_relative */
149 complain_overflow_signed, /* complain_on_overflow */
150 bfd_elf_generic_reloc, /* special_function */
151 "R_BPF_INSN_64", /* name */
152 FALSE, /* partial_inplace */
154 MINUS_ONE, /* dst_mask */
155 TRUE), /* pcrel_offset */
157 /* 32-immediate in LDDW instruction. */
158 HOWTO (R_BPF_INSN_32, /* type */
160 2, /* size (0 = byte, 1 = short, 2 = long) */
162 FALSE, /* pc_relative */
164 complain_overflow_signed, /* complain_on_overflow */
165 bfd_elf_generic_reloc, /* special_function */
166 "R_BPF_INSN_32", /* name */
167 FALSE, /* partial_inplace */
169 0xffffffff, /* dst_mask */
170 TRUE), /* pcrel_offset */
172 /* 16-bit offsets in instructions. */
173 HOWTO (R_BPF_INSN_16, /* type */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
177 FALSE, /* pc_relative */
179 complain_overflow_signed, /* complain_on_overflow */
180 bfd_elf_generic_reloc, /* special_function */
181 "R_BPF_INSN_16", /* name */
182 FALSE, /* partial_inplace */
184 0x0000ffff, /* dst_mask */
185 TRUE), /* pcrel_offset */
187 /* 16-bit PC-relative address in jump instructions. */
188 HOWTO (R_BPF_INSN_DISP16, /* type */
190 1, /* size (0 = byte, 1 = short, 2 = long) */
192 TRUE, /* pc_relative */
194 complain_overflow_signed, /* complain_on_overflow */
195 bpf_elf_insn_disp_reloc, /* special_function */
196 "R_BPF_INSN_DISP16", /* name */
197 FALSE, /* partial_inplace */
198 0xffff, /* src_mask */
199 0xffff, /* dst_mask */
200 TRUE), /* pcrel_offset */
202 HOWTO (R_BPF_DATA_8_PCREL,
204 0, /* size (0 = byte, 1 = short, 2 = long) */
206 TRUE, /* pc_relative */
208 complain_overflow_signed, /* complain_on_overflow */
209 bfd_elf_generic_reloc, /* special_function */
210 "R_BPF_8_PCREL", /* name */
211 FALSE, /* partial_inplace */
214 TRUE), /* pcrel_offset */
216 HOWTO (R_BPF_DATA_16_PCREL,
218 1, /* size (0 = byte, 1 = short, 2 = long) */
220 TRUE, /* pc_relative */
222 complain_overflow_signed, /* complain_on_overflow */
223 bfd_elf_generic_reloc, /* special_function */
224 "R_BPF_16_PCREL", /* name */
225 FALSE, /* partial_inplace */
227 0xffff, /* dst_mask */
228 TRUE), /* pcrel_offset */
230 HOWTO (R_BPF_DATA_32_PCREL,
232 2, /* size (0 = byte, 1 = short, 2 = long) */
234 TRUE, /* pc_relative */
236 complain_overflow_signed, /* complain_on_overflow */
237 bfd_elf_generic_reloc, /* special_function */
238 "R_BPF_32_PCREL", /* name */
239 FALSE, /* partial_inplace */
241 0xffffffff, /* dst_mask */
242 TRUE), /* pcrel_offset */
246 0, /* size (0 = byte, 1 = short, 2 = long) */
248 FALSE, /* pc_relative */
250 complain_overflow_unsigned, /* complain_on_overflow */
251 bfd_elf_generic_reloc, /* special_function */
252 "R_BPF_DATA_8", /* name */
253 FALSE, /* partial_inplace */
256 FALSE), /* pcrel_offset */
258 HOWTO (R_BPF_DATA_16,
260 1, /* size (0 = byte, 1 = short, 2 = long) */
262 FALSE, /* pc_relative */
264 complain_overflow_unsigned, /* complain_on_overflow */
265 bfd_elf_generic_reloc, /* special_function */
266 "R_BPF_DATA_16", /* name */
267 FALSE, /* partial_inplace */
269 0xffff, /* dst_mask */
270 FALSE), /* pcrel_offset */
272 /* 32-bit PC-relative address in call instructions. */
273 HOWTO (R_BPF_INSN_DISP32, /* type */
275 2, /* size (0 = byte, 1 = short, 2 = long) */
277 TRUE, /* pc_relative */
279 complain_overflow_signed, /* complain_on_overflow */
280 bpf_elf_insn_disp_reloc, /* special_function */
281 "R_BPF_INSN_DISP32", /* name */
282 FALSE, /* partial_inplace */
283 0xffffffff, /* src_mask */
284 0xffffffff, /* dst_mask */
285 TRUE), /* pcrel_offset */
288 HOWTO (R_BPF_DATA_32, /* type */
290 2, /* size (0 = byte, 1 = short, 2 = long) */
292 FALSE, /* pc_relative */
294 complain_overflow_bitfield, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_BPF_DATA_32", /* name */
297 FALSE, /* partial_inplace */
299 0xffffffff, /* dst_mask */
300 TRUE), /* pcrel_offset */
303 HOWTO (R_BPF_DATA_64, /* type */
305 4, /* size (0 = byte, 1 = short, 2 = long) */
307 FALSE, /* pc_relative */
309 complain_overflow_bitfield, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_BPF_DATA_64", /* name */
312 FALSE, /* partial_inplace */
314 MINUS_ONE, /* dst_mask */
315 TRUE), /* pcrel_offset */
317 HOWTO (R_BPF_DATA_64_PCREL,
319 4, /* size (0 = byte, 1 = short, 2 = long) */
321 TRUE, /* pc_relative */
323 complain_overflow_signed, /* complain_on_overflow */
324 bfd_elf_generic_reloc, /* special_function */
325 "R_BPF_64_PCREL", /* name */
326 FALSE, /* partial_inplace */
328 MINUS_ONE, /* dst_mask */
329 TRUE), /* pcrel_offset */
333 /* Map BFD reloc types to bpf ELF reloc types. */
335 static reloc_howto_type *
336 bpf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
337 bfd_reloc_code_real_type code)
339 /* Note that the bpf_elf_howto_table is indxed by the R_
340 constants. Thus, the order that the howto records appear in the
341 table *must* match the order of the relocation types defined in
342 include/elf/bpf.h. */
347 return &bpf_elf_howto_table[ (int) R_BPF_NONE];
349 case BFD_RELOC_8_PCREL:
350 return &bpf_elf_howto_table[ (int) R_BPF_DATA_8_PCREL];
351 case BFD_RELOC_16_PCREL:
352 return &bpf_elf_howto_table[ (int) R_BPF_DATA_16_PCREL];
353 case BFD_RELOC_32_PCREL:
354 return &bpf_elf_howto_table[ (int) R_BPF_DATA_32_PCREL];
355 case BFD_RELOC_64_PCREL:
356 return &bpf_elf_howto_table[ (int) R_BPF_DATA_64_PCREL];
359 return &bpf_elf_howto_table[ (int) R_BPF_DATA_8];
361 return &bpf_elf_howto_table[ (int) R_BPF_DATA_16];
363 return &bpf_elf_howto_table[ (int) R_BPF_DATA_32];
365 return &bpf_elf_howto_table[ (int) R_BPF_DATA_64];
367 case BFD_RELOC_BPF_64:
368 return &bpf_elf_howto_table[ (int) R_BPF_INSN_64];
369 case BFD_RELOC_BPF_32:
370 return &bpf_elf_howto_table[ (int) R_BPF_INSN_32];
371 case BFD_RELOC_BPF_16:
372 return &bpf_elf_howto_table[ (int) R_BPF_INSN_16];
373 case BFD_RELOC_BPF_DISP16:
374 return &bpf_elf_howto_table[ (int) R_BPF_INSN_DISP16];
375 case BFD_RELOC_BPF_DISP32:
376 return &bpf_elf_howto_table[ (int) R_BPF_INSN_DISP32];
379 /* Pacify gcc -Wall. */
385 /* Map BFD reloc names to bpf ELF reloc names. */
387 static reloc_howto_type *
388 bpf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
392 for (i = 0; i < ARRAY_SIZE (bpf_elf_howto_table); i++)
393 if (bpf_elf_howto_table[i].name != NULL
394 && strcasecmp (bpf_elf_howto_table[i].name, r_name) == 0)
395 return &bpf_elf_howto_table[i];
400 /* Set the howto pointer for a bpf reloc. */
403 bpf_info_to_howto (bfd *abfd, arelent *bfd_reloc,
404 Elf_Internal_Rela *elf_reloc)
408 r_type = ELF64_R_TYPE (elf_reloc->r_info);
409 if (r_type >= (unsigned int) R_BPF_max)
411 /* xgettext:c-format */
412 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
414 bfd_set_error (bfd_error_bad_value);
418 bfd_reloc->howto = &bpf_elf_howto_table [r_type];
422 /* Merge backend specific data from an object file to the output
423 object file when linking. */
426 elf64_bpf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
428 /* Check if we have the same endianness. */
429 if (! _bfd_generic_verify_endian_match (ibfd, info))
435 /* The macros below configure the architecture. */
437 #define TARGET_LITTLE_SYM bpf_elf64_le_vec
438 #define TARGET_LITTLE_NAME "elf64-bpfle"
440 #define TARGET_BIG_SYM bpf_elf64_be_vec
441 #define TARGET_BIG_NAME "elf64-bpfbe"
443 #define ELF_ARCH bfd_arch_bpf
444 #define ELF_MACHINE_CODE EM_BPF
446 #define ELF_MAXPAGESIZE 0x100000
448 #define elf_info_to_howto_rel bpf_info_to_howto
449 #define elf_info_to_howto bpf_info_to_howto
451 #define elf_backend_may_use_rel_p 1
452 #define elf_backend_may_use_rela_p 0
453 #define elf_backend_default_use_rela_p 0
455 #define elf_backend_can_gc_sections 0
457 #define elf_symbol_leading_char '_'
458 #define bfd_elf64_bfd_reloc_type_lookup bpf_reloc_type_lookup
459 #define bfd_elf64_bfd_reloc_name_lookup bpf_reloc_name_lookup
461 #define bfd_elf64_bfd_merge_private_bfd_data elf64_bpf_merge_private_bfd_data
463 #include "elf64-target.h"