diff options
author | Jose E. Marchesi <jose.marchesi@oracle.com> | 2023-07-30 22:39:30 +0200 |
---|---|---|
committer | Jose E. Marchesi <jose.marchesi@oracle.com> | 2023-07-30 22:39:30 +0200 |
commit | 1e18ffc9915bb9d3bbcd934d8b3e57ae9fe82f60 (patch) | |
tree | 92abdac23f41629b636c939591747e6fe5571ad9 | |
parent | 0346042938539324a5052cc09023c1fe426e8b31 (diff) | |
download | binutils-1e18ffc9915bb9d3bbcd934d8b3e57ae9fe82f60.zip binutils-1e18ffc9915bb9d3bbcd934d8b3e57ae9fe82f60.tar.gz binutils-1e18ffc9915bb9d3bbcd934d8b3e57ae9fe82f60.tar.bz2 |
bpf: include, bfd, opcodes: add EF_BPF_CPUVER ELF header flags
This patch adds support for EF_BPF_CPUVER bits in the ELF
machine-dependent header flags. These bits encode the BPF CPU
version for which the object file has been compiled for.
The BPF assembler is updated so it annotates the object files it
generates with these bits.
The BPF disassembler is updated so it honors EF_BPF_CPUVER to use the
appropriate ISA version if the user didn't specify an explicit ISA
version in the command line. Note that a value of zero in
EF_BPF_CPUVER is interpreted by the disassembler as "use the later
supported version" (the BPF CPU versions start with v1.)
The readelf utility is updated to pretty print EF_BPF_CPUVER when it
prints out the ELF header:
$ readelf -h a.out
ELF Header:
...
Flags: 0x4, CPU Version: 4
Tested in bpf-unknown-none.
include/ChangeLog:
2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com>
* elf/bpf.h (EF_BPF_CPUVER): Define.
* opcode/bpf.h (BPF_XBPF): Change from 0xf to 0xff so it fits in
EF_BPF_CPUVER.
binutils/ChangeLog:
2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com>
* readelf.c (get_machine_flags): Recognize and pretty print BPF
machine flags.
opcodes/ChangeLog:
2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com>
* bpf-dis.c: Initialize asm_bpf_version to -1.
(print_insn_bpf): Set BPF ISA version from the cpu version ELF
header flags if no explicit version set in the command line.
* disassemble.c (disassemble_init_for_target): Remove unused code.
gas/ChangeLog:
2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com>
* config/tc-bpf.h (elf_tc_final_processing): Define.
* config/tc-bpf.c (bpf_elf_final_processing): New function.
-rw-r--r-- | binutils/ChangeLog | 5 | ||||
-rw-r--r-- | binutils/readelf.c | 6 | ||||
-rw-r--r-- | gas/ChangeLog | 5 | ||||
-rw-r--r-- | gas/config/tc-bpf.c | 9 | ||||
-rw-r--r-- | gas/config/tc-bpf.h | 3 | ||||
-rw-r--r-- | gas/config/tc-sparc.c | 1 | ||||
-rw-r--r-- | include/ChangeLog | 6 | ||||
-rw-r--r-- | include/elf/bpf.h | 5 | ||||
-rw-r--r-- | include/opcode/bpf.h | 2 | ||||
-rw-r--r-- | opcodes/ChangeLog | 7 | ||||
-rw-r--r-- | opcodes/bpf-dis.c | 30 | ||||
-rw-r--r-- | opcodes/disassemble.c | 20 |
12 files changed, 77 insertions, 22 deletions
diff --git a/binutils/ChangeLog b/binutils/ChangeLog index cfd4d1a..65cff80 100644 --- a/binutils/ChangeLog +++ b/binutils/ChangeLog @@ -1,3 +1,8 @@ +2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com> + + * readelf.c (get_machine_flags): Recognize and pretty print BPF + machine flags. + 2023-07-24 Johannes Schauer Marin Rodrigues <josch@debian.org> * doc/binutils.texi (objcopy): Document change in behaviour of diff --git a/binutils/readelf.c b/binutils/readelf.c index bb488ef..4ecff4c 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -168,6 +168,7 @@ #include "elf/xtensa.h" #include "elf/z80.h" #include "elf/loongarch.h" +#include "elf/bpf.h" #include "getopt.h" #include "libiberty.h" @@ -4191,6 +4192,11 @@ get_machine_flags (Filedata * filedata, unsigned e_flags, unsigned e_machine) strcat (buf, ", no delay"); break; + case EM_BPF: + sprintf (buf + strlen (buf), ", CPU Version: %u", + e_flags & EF_BPF_CPUVER); + break; + case EM_SPARCV9: if (e_flags & EF_SPARC_32PLUS) strcat (buf, ", v8+"); diff --git a/gas/ChangeLog b/gas/ChangeLog index ab139a9..26d7dc1 100644 --- a/gas/ChangeLog +++ b/gas/ChangeLog @@ -1,5 +1,10 @@ 2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com> + * config/tc-bpf.h (elf_tc_final_processing): Define. + * config/tc-bpf.c (bpf_elf_final_processing): New function. + +2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com> + * config/tc-bpf.c (signed_overflow): Copy function from tc-aarch64.c. (encode_insn): Check for overflow in constant immediates. diff --git a/gas/config/tc-bpf.c b/gas/config/tc-bpf.c index 7e1bd5d..b4566d8 100644 --- a/gas/config/tc-bpf.c +++ b/gas/config/tc-bpf.c @@ -1679,3 +1679,12 @@ bpf_tc_equal_in_insn (int c ATTRIBUTE_UNUSED, char *str ATTRIBUTE_UNUSED) return 0; } + +/* Some special processing for a BPF ELF file. */ + +void +bpf_elf_final_processing (void) +{ + /* Annotate the BPF ISA version in the ELF flag bits. */ + elf_elfheader (stdoutput)->e_flags |= (isa_spec & EF_BPF_CPUVER); +} diff --git a/gas/config/tc-bpf.h b/gas/config/tc-bpf.h index d57a66f..9fb71ed 100644 --- a/gas/config/tc-bpf.h +++ b/gas/config/tc-bpf.h @@ -53,3 +53,6 @@ #define TC_EQUAL_IN_INSN(c, s) bpf_tc_equal_in_insn ((c), (s)) extern bool bpf_tc_equal_in_insn (int, char *); + +#define elf_tc_final_processing bpf_elf_final_processing +extern void bpf_elf_final_processing (void); diff --git a/gas/config/tc-sparc.c b/gas/config/tc-sparc.c index eada5e5..c273bd7 100644 --- a/gas/config/tc-sparc.c +++ b/gas/config/tc-sparc.c @@ -4992,3 +4992,4 @@ sparc_cfi_emit_pcrel_expr (expressionS *exp, unsigned int nbytes) emit_expr_with_reloc (exp, nbytes, "disp"); sparc_no_align_cons = 0; } + diff --git a/include/ChangeLog b/include/ChangeLog index 11c1c09..4d40740 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,9 @@ +2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com> + + * elf/bpf.h (EF_BPF_CPUVER): Define. + * opcode/bpf.h (BPF_XBPF): Change from 0xf to 0xff so it fits in + EF_BPF_CPUVER. + 2023-07-24 Jose E. Marchesi <jose.marchesi@oracle.com> * opcode/bpf.h (BPF_IMM32_BSWAP16): Define. diff --git a/include/elf/bpf.h b/include/elf/bpf.h index d0fad08..e4d4162 100644 --- a/include/elf/bpf.h +++ b/include/elf/bpf.h @@ -37,4 +37,9 @@ START_RELOC_NUMBERS (elf_bpf_reloc_type) RELOC_NUMBER (R_BPF_GNU_64_16, 256) END_RELOC_NUMBERS (R_BPF_max) +/* Processor specific flags for the ELF header e_flags field. */ + +/* Version of the BPF ISA used in the file. */ +#define EF_BPF_CPUVER 0x0000000f + #endif /* _ELF_BPF_H */ diff --git a/include/opcode/bpf.h b/include/opcode/bpf.h index 20e323a..6decae4 100644 --- a/include/opcode/bpf.h +++ b/include/opcode/bpf.h @@ -44,7 +44,7 @@ typedef uint64_t bpf_insn_word; #define BPF_V2 0x2 #define BPF_V3 0x3 #define BPF_V4 0x4 -#define BPF_XBPF 0xff +#define BPF_XBPF 0xf /* Masks for the several instruction fields in a BPF instruction. These assume big-endian BPF instructions. */ diff --git a/opcodes/ChangeLog b/opcodes/ChangeLog index f611ec5..9d33da2 100644 --- a/opcodes/ChangeLog +++ b/opcodes/ChangeLog @@ -1,3 +1,10 @@ +2023-07-30 Jose E. Marchesi <jose.marchesi@oracle.com> + + * bpf-dis.c: Initialize asm_bpf_version to -1. + (print_insn_bpf): Set BPF ISA version from the cpu version ELF + header flags if no explicit version set in the command line. + * disassemble.c (disassemble_init_for_target): Remove unused code. + 2023-07-26 Jose E. Marchesi <jose.marchesi@oracle.com> * bpf-opc.c (bpf_opcodes): Fix BPF_INSN_NEGR to not use a src diff --git a/opcodes/bpf-dis.c b/opcodes/bpf-dis.c index 4fe0efc..a8cb9e8 100644 --- a/opcodes/bpf-dis.c +++ b/opcodes/bpf-dis.c @@ -24,6 +24,8 @@ #include "libiberty.h" #include "opintl.h" #include "opcode/bpf.h" +#include "elf-bfd.h" +#include "elf/bpf.h" #include <string.h> #include <inttypes.h> @@ -42,7 +44,7 @@ enum bpf_dialect /* Global configuration for the disassembler. */ static enum bpf_dialect asm_dialect = BPF_DIALECT_NORMAL; -static int asm_bpf_version = BPF_V4; +static int asm_bpf_version = -1; static int asm_obase = 10; /* Print BPF specific command-line options. */ @@ -141,6 +143,32 @@ print_insn_bpf (bfd_vma pc, disassemble_info *info) info->disassembler_options = NULL; } + /* Determine what version of the BPF ISA to use when disassembling. + If the user didn't explicitly specify an ISA version, then derive + it from the CPU Version flag in the ELF header. A CPU version of + 0 in the header means "latest version". */ + if (asm_bpf_version == -1) + { + struct bfd *abfd = info->section->owner; + Elf_Internal_Ehdr *header = elf_elfheader (abfd); + int cpu_version = header->e_flags & EF_BPF_CPUVER; + + switch (cpu_version) + { + case 0: asm_bpf_version = BPF_V4; break; + case 1: asm_bpf_version = BPF_V1; break; + case 2: asm_bpf_version = BPF_V2; break; + case 3: asm_bpf_version = BPF_V3; break; + case 4: asm_bpf_version = BPF_V4; break; + case 0xf: asm_bpf_version = BPF_XBPF; break; + default: + /* xgettext:c-format */ + opcodes_error_handler (_("unknown BPF CPU version %u\n"), + cpu_version); + break; + } + } + /* Print eight bytes per line. */ info->bytes_per_chunk = 1; info->bytes_per_line = 8; diff --git a/opcodes/disassemble.c b/opcodes/disassemble.c index a5cb396..8aac42e 100644 --- a/opcodes/disassemble.c +++ b/opcodes/disassemble.c @@ -680,27 +680,7 @@ disassemble_init_for_target (struct disassemble_info * info) #endif #ifdef ARCH_bpf case bfd_arch_bpf: - /* XXX option for dialect */ info->created_styled_output = true; -#if 0 - info->endian_code = BFD_ENDIAN_LITTLE; - if (!info->private_data) - { - info->private_data = cgen_bitset_create (ISA_MAX); - if (info->endian == BFD_ENDIAN_BIG) - { - cgen_bitset_set (info->private_data, ISA_EBPFBE); - if (info->mach == bfd_mach_xbpf) - cgen_bitset_set (info->private_data, ISA_XBPFBE); - } - else - { - cgen_bitset_set (info->private_data, ISA_EBPFLE); - if (info->mach == bfd_mach_xbpf) - cgen_bitset_set (info->private_data, ISA_XBPFLE); - } - } -#endif break; #endif #ifdef ARCH_pru |