Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Minimal BPF JIT image disassembler
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Disassembles BPF JIT compiler emitted opcodes back to asm insn's for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * debugging or verification purposes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * To get the disassembly of the JIT code, do the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *  1) `echo 2 > /proc/sys/net/core/bpf_jit_enable`
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  2) Load a BPF filter (e.g. `tcpdump -p -n -s 0 -i eth1 host 192.168.20.0/24`)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *  3) Run e.g. `bpf_jit_disasm -o` to read out the last JIT code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * Copyright 2013 Daniel Borkmann <borkmann@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <assert.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <bfd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <dis-asm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <regex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <sys/klog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define CMD_ACTION_SIZE_BUFFER		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define CMD_ACTION_READ_ALL		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static void get_exec_path(char *tpath, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	char *path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	snprintf(tpath, size, "/proc/%d/exe", (int) getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	tpath[size - 1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	path = strdup(tpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	assert(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	len = readlink(path, tpath, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	tpath[len] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	free(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static void get_asm_insns(uint8_t *image, size_t len, int opcodes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	int count, i, pc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	char tpath[PATH_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct disassemble_info info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	disassembler_ftype disassemble;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	bfd *bfdf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	memset(tpath, 0, sizeof(tpath));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	get_exec_path(tpath, sizeof(tpath));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	bfdf = bfd_openr(tpath, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	assert(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	assert(bfd_check_format(bfdf, bfd_object));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	init_disassemble_info(&info, stdout, (fprintf_ftype) fprintf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	info.arch = bfd_get_arch(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	info.mach = bfd_get_mach(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	info.buffer = image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	info.buffer_length = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	disassemble_init_for_target(&info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) #ifdef DISASM_FOUR_ARGS_SIGNATURE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	disassemble = disassembler(info.arch,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 				   bfd_big_endian(bfdf),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 				   info.mach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 				   bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	disassemble = disassembler(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	assert(disassemble);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		printf("%4x:\t", pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		count = disassemble(pc, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		if (opcodes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 			printf("\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			for (i = 0; i < count; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 				printf("%02x ", (uint8_t) image[pc + i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		pc += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	} while(count > 0 && pc < len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	bfd_close(bfdf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static char *get_klog_buff(unsigned int *klen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	int ret, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	char *buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	len = klogctl(CMD_ACTION_SIZE_BUFFER, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (len < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	buff = malloc(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (!buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ret = klogctl(CMD_ACTION_READ_ALL, buff, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		free(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	*klen = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static char *get_flog_buff(const char *file, unsigned int *klen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	int fd, ret, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	struct stat fi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	char *buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	fd = open(file, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	ret = fstat(fd, &fi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (ret < 0 || !S_ISREG(fi.st_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	len = fi.st_size + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	buff = malloc(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (!buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	memset(buff, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	ret = read(fd, buff, len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	*klen = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return buff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	free(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static char *get_log_buff(const char *file, unsigned int *klen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	return file ? get_flog_buff(file, klen) : get_klog_buff(klen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static void put_log_buff(char *buff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	free(buff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static uint8_t *get_last_jit_image(char *haystack, size_t hlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				   unsigned int *ilen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	char *ptr, *pptr, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	off_t off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	unsigned int proglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	int ret, flen, pass, ulen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	regmatch_t pmatch[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	unsigned long base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	regex_t regex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	uint8_t *image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (hlen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	ret = regcomp(&regex, "flen=[[:alnum:]]+ proglen=[[:digit:]]+ "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		      "pass=[[:digit:]]+ image=[[:xdigit:]]+", REG_EXTENDED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	assert(ret == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	ptr = haystack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	memset(pmatch, 0, sizeof(pmatch));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		ret = regexec(&regex, ptr, 1, pmatch, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			ptr += pmatch[0].rm_eo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 			off += pmatch[0].rm_eo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			assert(off < hlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	ptr = haystack + off - (pmatch[0].rm_eo - pmatch[0].rm_so);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	ret = sscanf(ptr, "flen=%d proglen=%u pass=%d image=%lx",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		     &flen, &proglen, &pass, &base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (ret != 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		regfree(&regex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (proglen > 1000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		printf("proglen of %d too big, stopping\n", proglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	image = malloc(proglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (!image) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		printf("Out of memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	memset(image, 0, proglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	tmp = ptr = haystack + off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	while ((ptr = strtok(tmp, "\n")) != NULL && ulen < proglen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		tmp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 		if (!strstr(ptr, "JIT code"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		pptr = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		while ((ptr = strstr(pptr, ":")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 			pptr = ptr + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		ptr = pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 		do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 			image[ulen++] = (uint8_t) strtoul(pptr, &pptr, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 			if (ptr == pptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 				ulen--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 			if (ulen >= proglen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 			ptr = pptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		} while (1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	assert(ulen == proglen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	printf("%u bytes emitted from JIT compiler (pass:%d, flen:%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	       proglen, pass, flen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	printf("%lx + <x>:\n", base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	regfree(&regex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	*ilen = ulen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	return image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	printf("Usage: bpf_jit_disasm [...]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	printf("       -o          Also display related opcodes (default: off).\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	printf("       -O <file>   Write binary image of code to file, don't disassemble to stdout.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	printf("       -f <file>   Read last image dump from file or stdin (default: klog).\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	printf("       -h          Display this help.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int main(int argc, char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	unsigned int len, klen, opt, opcodes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	char *kbuff, *file = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	char *ofile = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	int ofd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	ssize_t nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	uint8_t *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	uint8_t *image = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	while ((opt = getopt(argc, argv, "of:O:")) != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		switch (opt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		case 'o':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			opcodes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		case 'O':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			ofile = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		case 'f':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			file = optarg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	bfd_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	kbuff = get_log_buff(file, &klen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (!kbuff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		fprintf(stderr, "Could not retrieve log buffer!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	image = get_last_jit_image(kbuff, klen, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!image) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		fprintf(stderr, "No JIT image found!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	if (!ofile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		get_asm_insns(image, len, opcodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	ofd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC, DEFFILEMODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	if (ofd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		fprintf(stderr, "Could not open file %s for writing: ", ofile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		perror(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	pos = image;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		nr = write(ofd, pos, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		if (nr < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			fprintf(stderr, "Could not write data to %s: ", ofile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			perror(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		len -= nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		pos += nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	} while (len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	close(ofd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	put_log_buff(kbuff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	free(image);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }