| 1 | /* Capstone Disassembly Engine */cxx . . $ `<file>/arch/BPF/BPFMapping.c`/ |
| 2 | /* BPF Backend by david942j <[email protected]>, 2019 */ |
| 3 | |
| 4 | #include <string.h> |
| 5 | |
| 6 | #include "BPFConstants.h"cxx . . $ `<file>/arch/BPF/BPFConstants.h`/ |
| 7 | #include "BPFMapping.h"cxx . . $ `<file>/arch/BPF/BPFMapping.h`/ |
| 8 | #include "../../utils.h"cxx . . $ `<file>/utils.h`/ |
| 9 | |
| 10 | #ifndef CAPSTONE_DIET |
| 11 | static const name_mapcxx . . $ name_map# group_name_mapscxx . . $ group_name_maps.[] = { |
| 12 | { BPF_GRP_INVALIDcxx . . $ BPF_GRP_INVALID., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 13 | |
| 14 | { BPF_GRP_LOADcxx . . $ BPF_GRP_LOAD., "load" }, |
| 15 | { BPF_GRP_STOREcxx . . $ BPF_GRP_STORE., "store" }, |
| 16 | { BPF_GRP_ALUcxx . . $ BPF_GRP_ALU., "alu" }, |
| 17 | { BPF_GRP_JUMPcxx . . $ BPF_GRP_JUMP., "jump" }, |
| 18 | { BPF_GRP_CALLcxx . . $ BPF_GRP_CALL., "call" }, |
| 19 | { BPF_GRP_RETURNcxx . . $ BPF_GRP_RETURN., "return" }, |
| 20 | { BPF_GRP_MISCcxx . . $ BPF_GRP_MISC., "misc" }, |
| 21 | }; |
| 22 | #endif |
| 23 | |
| 24 | const char *BPF_group_namecxx . . $ BPF_group_name(efd1348d9ae5172e).(cshcxx . . $ csh# handle, unsigned int id) |
| 25 | { |
| 26 | #ifndef CAPSTONE_DIET |
| 27 | return id2namecxx . . $ id2name(92d4aec78e87b576).(group_name_mapscxx . . $ group_name_maps., ARR_SIZEcxx . . $ `utils.h:24:9`!(group_name_maps), id); |
| 28 | #else |
| 29 | return NULL; |
| 30 | #endif |
| 31 | } |
| 32 | |
| 33 | #ifndef CAPSTONE_DIET |
| 34 | static const name_mapcxx . . $ name_map# insn_name_mapscxx . . $ insn_name_maps.[BPF_INS_ENDINGcxx . . $ BPF_INS_ENDING.] = { |
| 35 | { BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`! }, |
| 36 | |
| 37 | { BPF_INS_ADDcxx . . $ BPF_INS_ADD., "add" }, |
| 38 | { BPF_INS_SUBcxx . . $ BPF_INS_SUB., "sub" }, |
| 39 | { BPF_INS_MULcxx . . $ BPF_INS_MUL., "mul" }, |
| 40 | { BPF_INS_DIVcxx . . $ BPF_INS_DIV., "div" }, |
| 41 | { BPF_INS_ORcxx . . $ BPF_INS_OR., "or" }, |
| 42 | { BPF_INS_ANDcxx . . $ BPF_INS_AND., "and" }, |
| 43 | { BPF_INS_LSHcxx . . $ BPF_INS_LSH., "lsh" }, |
| 44 | { BPF_INS_RSHcxx . . $ BPF_INS_RSH., "rsh" }, |
| 45 | { BPF_INS_NEGcxx . . $ BPF_INS_NEG., "neg" }, |
| 46 | { BPF_INS_MODcxx . . $ BPF_INS_MOD., "mod" }, |
| 47 | { BPF_INS_XORcxx . . $ BPF_INS_XOR., "xor" }, |
| 48 | { BPF_INS_MOVcxx . . $ BPF_INS_MOV., "mov" }, |
| 49 | { BPF_INS_ARSHcxx . . $ BPF_INS_ARSH., "arsh" }, |
| 50 | |
| 51 | { BPF_INS_ADD64cxx . . $ BPF_INS_ADD64., "add64" }, |
| 52 | { BPF_INS_SUB64cxx . . $ BPF_INS_SUB64., "sub64" }, |
| 53 | { BPF_INS_MUL64cxx . . $ BPF_INS_MUL64., "mul64" }, |
| 54 | { BPF_INS_DIV64cxx . . $ BPF_INS_DIV64., "div64" }, |
| 55 | { BPF_INS_OR64cxx . . $ BPF_INS_OR64., "or64" }, |
| 56 | { BPF_INS_AND64cxx . . $ BPF_INS_AND64., "and64" }, |
| 57 | { BPF_INS_LSH64cxx . . $ BPF_INS_LSH64., "lsh64" }, |
| 58 | { BPF_INS_RSH64cxx . . $ BPF_INS_RSH64., "rsh64" }, |
| 59 | { BPF_INS_NEG64cxx . . $ BPF_INS_NEG64., "neg64" }, |
| 60 | { BPF_INS_MOD64cxx . . $ BPF_INS_MOD64., "mod64" }, |
| 61 | { BPF_INS_XOR64cxx . . $ BPF_INS_XOR64., "xor64" }, |
| 62 | { BPF_INS_MOV64cxx . . $ BPF_INS_MOV64., "mov64" }, |
| 63 | { BPF_INS_ARSH64cxx . . $ BPF_INS_ARSH64., "arsh64" }, |
| 64 | |
| 65 | { BPF_INS_LE16cxx . . $ BPF_INS_LE16., "le16" }, |
| 66 | { BPF_INS_LE32cxx . . $ BPF_INS_LE32., "le32" }, |
| 67 | { BPF_INS_LE64cxx . . $ BPF_INS_LE64., "le64" }, |
| 68 | { BPF_INS_BE16cxx . . $ BPF_INS_BE16., "be16" }, |
| 69 | { BPF_INS_BE32cxx . . $ BPF_INS_BE32., "be32" }, |
| 70 | { BPF_INS_BE64cxx . . $ BPF_INS_BE64., "be64" }, |
| 71 | |
| 72 | { BPF_INS_LDWcxx . . $ BPF_INS_LDW., "ldw" }, |
| 73 | { BPF_INS_LDHcxx . . $ BPF_INS_LDH., "ldh" }, |
| 74 | { BPF_INS_LDBcxx . . $ BPF_INS_LDB., "ldb" }, |
| 75 | { BPF_INS_LDDWcxx . . $ BPF_INS_LDDW., "lddw" }, |
| 76 | { BPF_INS_LDXWcxx . . $ BPF_INS_LDXW., "ldxw" }, |
| 77 | { BPF_INS_LDXHcxx . . $ BPF_INS_LDXH., "ldxh" }, |
| 78 | { BPF_INS_LDXBcxx . . $ BPF_INS_LDXB., "ldxb" }, |
| 79 | { BPF_INS_LDXDWcxx . . $ BPF_INS_LDXDW., "ldxdw" }, |
| 80 | |
| 81 | { BPF_INS_STWcxx . . $ BPF_INS_STW., "stw" }, |
| 82 | { BPF_INS_STHcxx . . $ BPF_INS_STH., "sth" }, |
| 83 | { BPF_INS_STBcxx . . $ BPF_INS_STB., "stb" }, |
| 84 | { BPF_INS_STDWcxx . . $ BPF_INS_STDW., "stdw" }, |
| 85 | { BPF_INS_STXWcxx . . $ BPF_INS_STXW., "stxw" }, |
| 86 | { BPF_INS_STXHcxx . . $ BPF_INS_STXH., "stxh" }, |
| 87 | { BPF_INS_STXBcxx . . $ BPF_INS_STXB., "stxb" }, |
| 88 | { BPF_INS_STXDWcxx . . $ BPF_INS_STXDW., "stxdw" }, |
| 89 | { BPF_INS_XADDWcxx . . $ BPF_INS_XADDW., "xaddw" }, |
| 90 | { BPF_INS_XADDDWcxx . . $ BPF_INS_XADDDW., "xadddw" }, |
| 91 | |
| 92 | { BPF_INS_JMPcxx . . $ BPF_INS_JMP., "jmp" }, |
| 93 | { BPF_INS_JEQcxx . . $ BPF_INS_JEQ., "jeq" }, |
| 94 | { BPF_INS_JGTcxx . . $ BPF_INS_JGT., "jgt" }, |
| 95 | { BPF_INS_JGEcxx . . $ BPF_INS_JGE., "jge" }, |
| 96 | { BPF_INS_JSETcxx . . $ BPF_INS_JSET., "jset" }, |
| 97 | { BPF_INS_JNEcxx . . $ BPF_INS_JNE., "jne" }, |
| 98 | { BPF_INS_JSGTcxx . . $ BPF_INS_JSGT., "jsgt" }, |
| 99 | { BPF_INS_JSGEcxx . . $ BPF_INS_JSGE., "jsge" }, |
| 100 | { BPF_INS_CALLcxx . . $ BPF_INS_CALL., "call" }, |
| 101 | { BPF_INS_CALLXcxx . . $ BPF_INS_CALLX., "callx" }, |
| 102 | { BPF_INS_EXITcxx . . $ BPF_INS_EXIT., "exit" }, |
| 103 | { BPF_INS_JLTcxx . . $ BPF_INS_JLT., "jlt" }, |
| 104 | { BPF_INS_JLEcxx . . $ BPF_INS_JLE., "jle" }, |
| 105 | { BPF_INS_JSLTcxx . . $ BPF_INS_JSLT., "jslt" }, |
| 106 | { BPF_INS_JSLEcxx . . $ BPF_INS_JSLE., "jsle" }, |
| 107 | |
| 108 | { BPF_INS_RETcxx . . $ BPF_INS_RET., "ret" }, |
| 109 | |
| 110 | { BPF_INS_TAXcxx . . $ BPF_INS_TAX., "tax" }, |
| 111 | { BPF_INS_TXAcxx . . $ BPF_INS_TXA., "txa" }, |
| 112 | }; |
| 113 | #endif |
| 114 | |
| 115 | const char *BPF_insn_namecxx . . $ BPF_insn_name(efd1348d9ae5172e).(cshcxx . . $ csh# handle, unsigned int id) |
| 116 | { |
| 117 | #ifndef CAPSTONE_DIET |
| 118 | /* We have some special cases because 'ld' in cBPF is equivalent to 'ldw' |
| 119 | * in eBPF, and we don't want to see 'ldw' appears in cBPF mode. |
| 120 | */ |
| 121 | if (!EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(handle)) { |
| 122 | switch (id) { |
| 123 | case BPF_INS_LDcxx . . $ BPF_INS_LD.: return "ld"; |
| 124 | case BPF_INS_LDXcxx . . $ BPF_INS_LDX.: return "ldx"; |
| 125 | case BPF_INS_STcxx . . $ BPF_INS_ST.: return "st"; |
| 126 | case BPF_INS_STXcxx . . $ BPF_INS_STX.: return "stx"; |
| 127 | } |
| 128 | } |
| 129 | return id2namecxx . . $ id2name(92d4aec78e87b576).(insn_name_mapscxx . . $ insn_name_maps., ARR_SIZEcxx . . $ `utils.h:24:9`!(insn_name_maps), id); |
| 130 | #else |
| 131 | return NULL; |
| 132 | #endif |
| 133 | } |
| 134 | |
| 135 | const char *BPF_reg_namecxx . . $ BPF_reg_name(efd1348d9ae5172e).(cshcxx . . $ csh# handle, unsigned int reg) |
| 136 | { |
| 137 | #ifndef CAPSTONE_DIET |
| 138 | if (EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(handle)) { |
| 139 | if (reg < BPF_REG_R0cxx . . $ BPF_REG_R0. || reg > BPF_REG_R10cxx . . $ BPF_REG_R10.) |
| 140 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 141 | static const char reg_names[11][4] = { |
| 142 | "r0", "r1", "r2", "r3", "r4", |
| 143 | "r5", "r6", "r7", "r8", "r9", |
| 144 | "r10" |
| 145 | }; |
| 146 | return reg_names[reg - BPF_REG_R0cxx . . $ BPF_REG_R0.]; |
| 147 | } |
| 148 | |
| 149 | /* cBPF mode */ |
| 150 | if (reg == BPF_REG_Acxx . . $ BPF_REG_A.) |
| 151 | return "a"; |
| 152 | else if (reg == BPF_REG_Xcxx . . $ BPF_REG_X.) |
| 153 | return "x"; |
| 154 | else |
| 155 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 156 | #else |
| 157 | return NULL; |
| 158 | #endif |
| 159 | } |
| 160 | |
| 161 | static bpf_insncxx . . $ bpf_insn# op2insn_ldcxx . . $ op2insn_ld(cf04ce91d110db35).(unsigned opcode) |
| 162 | { |
| 163 | #define CASEcxx . . $ `arch/BPF/BPFMapping.c:163:9`!(c) case BPF_SIZE_##c: \ |
| 164 | if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(opcode) == BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!) \ |
| 165 | return BPF_INS_LD##c; \ |
| 166 | else \ |
| 167 | return BPF_INS_LDX##c; |
| 168 | |
| 169 | switch (BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(opcode)) { |
| 170 | CASEcxx . . $ `arch/BPF/BPFMapping.c:163:9`!(W); |
| 171 | CASEcxx . . $ `arch/BPF/BPFMapping.c:163:9`!(H); |
| 172 | CASEcxx . . $ `arch/BPF/BPFMapping.c:163:9`!(B); |
| 173 | CASEcxx . . $ `arch/BPF/BPFMapping.c:163:9`!(DW); |
| 174 | } |
| 175 | #undef CASEcxx . . $ `arch/BPF/BPFMapping.c:163:9`! |
| 176 | |
| 177 | return BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.; |
| 178 | } |
| 179 | |
| 180 | static bpf_insncxx . . $ bpf_insn# op2insn_stcxx . . $ op2insn_st(cf04ce91d110db35).(unsigned opcode) |
| 181 | { |
| 182 | /* |
| 183 | * - BPF_STX | BPF_XADD | BPF_{W,DW} |
| 184 | * - BPF_ST* | BPF_MEM | BPF_{W,H,B,DW} |
| 185 | */ |
| 186 | |
| 187 | if (opcode == (BPF_CLASS_STXcxx . . $ `arch/BPF/BPFConstants.h:15:9`! | BPF_MODE_XADDcxx . . $ `arch/BPF/BPFConstants.h:81:9`! | BPF_SIZE_Wcxx . . $ `arch/BPF/BPFConstants.h:68:9`!)) |
| 188 | return BPF_INS_XADDWcxx . . $ BPF_INS_XADDW.; |
| 189 | if (opcode == (BPF_CLASS_STXcxx . . $ `arch/BPF/BPFConstants.h:15:9`! | BPF_MODE_XADDcxx . . $ `arch/BPF/BPFConstants.h:81:9`! | BPF_SIZE_DWcxx . . $ `arch/BPF/BPFConstants.h:71:9`!)) |
| 190 | return BPF_INS_XADDDWcxx . . $ BPF_INS_XADDDW.; |
| 191 | |
| 192 | /* should be BPF_MEM */ |
| 193 | #define CASEcxx . . $ `arch/BPF/BPFMapping.c:193:9`!(c) case BPF_SIZE_##c: \ |
| 194 | if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(opcode) == BPF_CLASS_STcxx . . $ `arch/BPF/BPFConstants.h:14:9`!) \ |
| 195 | return BPF_INS_ST##c; \ |
| 196 | else \ |
| 197 | return BPF_INS_STX##c; |
| 198 | switch (BPF_SIZEcxx . . $ `arch/BPF/BPFConstants.h:66:9`!(opcode)) { |
| 199 | CASEcxx . . $ `arch/BPF/BPFMapping.c:193:9`!(W); |
| 200 | CASEcxx . . $ `arch/BPF/BPFMapping.c:193:9`!(H); |
| 201 | CASEcxx . . $ `arch/BPF/BPFMapping.c:193:9`!(B); |
| 202 | CASEcxx . . $ `arch/BPF/BPFMapping.c:193:9`!(DW); |
| 203 | } |
| 204 | #undef CASEcxx . . $ `arch/BPF/BPFMapping.c:193:9`! |
| 205 | |
| 206 | return BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.; |
| 207 | } |
| 208 | |
| 209 | static bpf_insncxx . . $ bpf_insn# op2insn_alucxx . . $ op2insn_alu(cf04ce91d110db35).(unsigned opcode) |
| 210 | { |
| 211 | /* Endian is a special case */ |
| 212 | if (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(opcode) == BPF_ALU_ENDcxx . . $ `arch/BPF/BPFConstants.h:38:9`!) { |
| 213 | switch (opcode ^ BPF_CLASS_ALUcxx . . $ `arch/BPF/BPFConstants.h:16:9`! ^ BPF_ALU_ENDcxx . . $ `arch/BPF/BPFConstants.h:38:9`!) { |
| 214 | case BPF_SRC_LITTLEcxx . . $ `arch/BPF/BPFConstants.h:63:9`! | (16 << 4): |
| 215 | return BPF_INS_LE16cxx . . $ BPF_INS_LE16.; |
| 216 | case BPF_SRC_LITTLEcxx . . $ `arch/BPF/BPFConstants.h:63:9`! | (32 << 4): |
| 217 | return BPF_INS_LE32cxx . . $ BPF_INS_LE32.; |
| 218 | case BPF_SRC_LITTLEcxx . . $ `arch/BPF/BPFConstants.h:63:9`! | (64 << 4): |
| 219 | return BPF_INS_LE64cxx . . $ BPF_INS_LE64.; |
| 220 | case BPF_SRC_BIGcxx . . $ `arch/BPF/BPFConstants.h:64:9`! | (16 << 4): |
| 221 | return BPF_INS_BE16cxx . . $ BPF_INS_BE16.; |
| 222 | case BPF_SRC_BIGcxx . . $ `arch/BPF/BPFConstants.h:64:9`! | (32 << 4): |
| 223 | return BPF_INS_BE32cxx . . $ BPF_INS_BE32.; |
| 224 | case BPF_SRC_BIGcxx . . $ `arch/BPF/BPFConstants.h:64:9`! | (64 << 4): |
| 225 | return BPF_INS_BE64cxx . . $ BPF_INS_BE64.; |
| 226 | } |
| 227 | return BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.; |
| 228 | } |
| 229 | |
| 230 | #define CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(c) case BPF_ALU_##c: \ |
| 231 | if (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(opcode) == BPF_CLASS_ALUcxx . . $ `arch/BPF/BPFConstants.h:16:9`!) \ |
| 232 | return BPF_INS_##c; \ |
| 233 | else \ |
| 234 | return BPF_INS_##c##64; |
| 235 | |
| 236 | switch (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(opcode)) { |
| 237 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(ADD); |
| 238 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(SUB); |
| 239 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(MUL); |
| 240 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(DIV); |
| 241 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(OR); |
| 242 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(AND); |
| 243 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(LSH); |
| 244 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(RSH); |
| 245 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(NEG); |
| 246 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(MOD); |
| 247 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(XOR); |
| 248 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(MOV); |
| 249 | CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`!(ARSH); |
| 250 | } |
| 251 | #undef CASEcxx . . $ `arch/BPF/BPFMapping.c:230:9`! |
| 252 | |
| 253 | return BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.; |
| 254 | } |
| 255 | |
| 256 | static bpf_insncxx . . $ bpf_insn# op2insn_jmpcxx . . $ op2insn_jmp(cf04ce91d110db35).(unsigned opcode) |
| 257 | { |
| 258 | if (opcode == (BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`! | BPF_JUMP_CALLcxx . . $ `arch/BPF/BPFConstants.h:49:9`! | BPF_SRC_Xcxx . . $ `arch/BPF/BPFConstants.h:60:9`!)) { |
| 259 | return BPF_INS_CALLXcxx . . $ BPF_INS_CALLX.; |
| 260 | } |
| 261 | |
| 262 | #define CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(c) case BPF_JUMP_##c: return BPF_INS_##c |
| 263 | switch (BPF_OPcxx . . $ `arch/BPF/BPFConstants.h:22:9`!(opcode)) { |
| 264 | case BPF_JUMP_JAcxx . . $ `arch/BPF/BPFConstants.h:41:9`!: |
| 265 | return BPF_INS_JMPcxx . . $ BPF_INS_JMP.; |
| 266 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JEQ); |
| 267 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JGT); |
| 268 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JGE); |
| 269 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JSET); |
| 270 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JNE); |
| 271 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JSGT); |
| 272 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JSGE); |
| 273 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(CALL); |
| 274 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(EXIT); |
| 275 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JLT); |
| 276 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JLE); |
| 277 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JSLT); |
| 278 | CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`!(JSLE); |
| 279 | } |
| 280 | #undef CASEcxx . . $ `arch/BPF/BPFMapping.c:262:9`! |
| 281 | |
| 282 | return BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.; |
| 283 | } |
| 284 | |
| 285 | static void update_regs_accesscxx . . $ update_regs_access(8c29a82f0bb298fb).(cs_structcxx . . $ cs_struct# *ud, cs_detailcxx . . $ cs_detail# *detail, |
| 286 | bpf_insncxx . . $ bpf_insn# insn_id, unsigned int opcode) |
| 287 | { |
| 288 | if (insn_id == BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.) |
| 289 | return; |
| 290 | #define PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(r) do { \ |
| 291 | detail->regs_read[detail->regs_read_count] = r; \ |
| 292 | detail->regs_read_count++; \ |
| 293 | } while (0) |
| 294 | #define PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(r) do { \ |
| 295 | detail->regs_write[detail->regs_write_count] = r; \ |
| 296 | detail->regs_write_count++; \ |
| 297 | } while (0) |
| 298 | /* |
| 299 | * In eBPF mode, only these instructions have implicit registers access: |
| 300 | * - legacy ld{w,h,b,dw} * // w: r0 |
| 301 | * - exit // r: r0 |
| 302 | */ |
| 303 | if (EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud)) { |
| 304 | switch (insn_id) { |
| 305 | default: |
| 306 | break; |
| 307 | case BPF_INS_LDWcxx . . $ BPF_INS_LDW.: |
| 308 | case BPF_INS_LDHcxx . . $ BPF_INS_LDH.: |
| 309 | case BPF_INS_LDBcxx . . $ BPF_INS_LDB.: |
| 310 | case BPF_INS_LDDWcxx . . $ BPF_INS_LDDW.: |
| 311 | if (BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(opcode) == BPF_MODE_ABScxx . . $ `arch/BPF/BPFConstants.h:76:9`! || BPF_MODEcxx . . $ `arch/BPF/BPFConstants.h:73:9`!(opcode) == BPF_MODE_INDcxx . . $ `arch/BPF/BPFConstants.h:77:9`!) { |
| 312 | PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(BPF_REG_R0); |
| 313 | } |
| 314 | break; |
| 315 | case BPF_INS_EXITcxx . . $ BPF_INS_EXIT.: |
| 316 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_R0); |
| 317 | break; |
| 318 | } |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | /* cBPF mode */ |
| 323 | switch (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(opcode)) { |
| 324 | default: |
| 325 | break; |
| 326 | case BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!: |
| 327 | PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(BPF_REG_A); |
| 328 | break; |
| 329 | case BPF_CLASS_LDXcxx . . $ `arch/BPF/BPFConstants.h:13:9`!: |
| 330 | PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(BPF_REG_X); |
| 331 | break; |
| 332 | case BPF_CLASS_STcxx . . $ `arch/BPF/BPFConstants.h:14:9`!: |
| 333 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_A); |
| 334 | break; |
| 335 | case BPF_CLASS_STXcxx . . $ `arch/BPF/BPFConstants.h:15:9`!: |
| 336 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_X); |
| 337 | break; |
| 338 | case BPF_CLASS_ALUcxx . . $ `arch/BPF/BPFConstants.h:16:9`!: |
| 339 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_A); |
| 340 | PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(BPF_REG_A); |
| 341 | break; |
| 342 | case BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`!: |
| 343 | if (insn_id != BPF_INS_JMPcxx . . $ BPF_INS_JMP.) // except the unconditional jump |
| 344 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_A); |
| 345 | break; |
| 346 | /* case BPF_CLASS_RET: */ |
| 347 | case BPF_CLASS_MISCcxx . . $ `arch/BPF/BPFConstants.h:19:9`!: |
| 348 | if (insn_id == BPF_INS_TAXcxx . . $ BPF_INS_TAX.) { |
| 349 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_A); |
| 350 | PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(BPF_REG_X); |
| 351 | } |
| 352 | else { |
| 353 | PUSH_READcxx . . $ `arch/BPF/BPFMapping.c:290:9`!(BPF_REG_X); |
| 354 | PUSH_WRITEcxx . . $ `arch/BPF/BPFMapping.c:294:9`!(BPF_REG_A); |
| 355 | } |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * 1. Convert opcode(id) to BPF_INS_* |
| 362 | * 2. Set regs_read/regs_write/groups |
| 363 | */ |
| 364 | void BPF_get_insn_idcxx . . $ BPF_get_insn_id(b32a94ba649bfb5a).(cs_structcxx . . $ cs_struct# *ud, cs_insncxx . . $ cs_insn# *insn, unsigned int opcode) |
| 365 | { |
| 366 | // No need to care the mode (cBPF or eBPF) since all checks has be done in |
| 367 | // BPF_getInstruction, we can simply map opcode to BPF_INS_*. |
| 368 | cs_detailcxx . . $ cs_detail# *detail; |
| 369 | bpf_insncxx . . $ bpf_insn# id = BPF_INS_INVALIDcxx . . $ BPF_INS_INVALID.; |
| 370 | bpf_insn_groupcxx . . $ bpf_insn_group# grp; |
| 371 | |
| 372 | detail = insn->detailcxx . . $ cs_insn#detail.; |
| 373 | #ifndef CAPSTONE_DIET |
| 374 | #define PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(grp) do { \ |
| 375 | if (detail) { \ |
| 376 | detail->groups[detail->groups_count] = grp; \ |
| 377 | detail->groups_count++; \ |
| 378 | } \ |
| 379 | } while(0) |
| 380 | #else |
| 381 | #define PUSH_GROUP |
| 382 | #endif |
| 383 | |
| 384 | switch (BPF_CLASScxx . . $ `arch/BPF/BPFConstants.h:9:9`!(opcode)) { |
| 385 | default: // will never happen |
| 386 | break; |
| 387 | case BPF_CLASS_LDcxx . . $ `arch/BPF/BPFConstants.h:12:9`!: |
| 388 | case BPF_CLASS_LDXcxx . . $ `arch/BPF/BPFConstants.h:13:9`!: |
| 389 | id = op2insn_ldcxx . . $ op2insn_ld(cf04ce91d110db35).(opcode); |
| 390 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(BPF_GRP_LOAD); |
| 391 | break; |
| 392 | case BPF_CLASS_STcxx . . $ `arch/BPF/BPFConstants.h:14:9`!: |
| 393 | case BPF_CLASS_STXcxx . . $ `arch/BPF/BPFConstants.h:15:9`!: |
| 394 | id = op2insn_stcxx . . $ op2insn_st(cf04ce91d110db35).(opcode); |
| 395 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(BPF_GRP_STORE); |
| 396 | break; |
| 397 | case BPF_CLASS_ALUcxx . . $ `arch/BPF/BPFConstants.h:16:9`!: |
| 398 | id = op2insn_alucxx . . $ op2insn_alu(cf04ce91d110db35).(opcode); |
| 399 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(BPF_GRP_ALU); |
| 400 | break; |
| 401 | case BPF_CLASS_JMPcxx . . $ `arch/BPF/BPFConstants.h:17:9`!: |
| 402 | grp = BPF_GRP_JUMPcxx . . $ BPF_GRP_JUMP.; |
| 403 | id = op2insn_jmpcxx . . $ op2insn_jmp(cf04ce91d110db35).(opcode); |
| 404 | if (id == BPF_INS_CALLcxx . . $ BPF_INS_CALL. || id == BPF_INS_CALLXcxx . . $ BPF_INS_CALLX.) |
| 405 | grp = BPF_GRP_CALLcxx . . $ BPF_GRP_CALL.; |
| 406 | else if (id == BPF_INS_EXITcxx . . $ BPF_INS_EXIT.) |
| 407 | grp = BPF_GRP_RETURNcxx . . $ BPF_GRP_RETURN.; |
| 408 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(grp); |
| 409 | break; |
| 410 | case BPF_CLASS_RETcxx . . $ `arch/BPF/BPFConstants.h:18:9`!: |
| 411 | id = BPF_INS_RETcxx . . $ BPF_INS_RET.; |
| 412 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(BPF_GRP_RETURN); |
| 413 | break; |
| 414 | // BPF_CLASS_MISC and BPF_CLASS_ALU64 have exactly same value |
| 415 | case BPF_CLASS_MISCcxx . . $ `arch/BPF/BPFConstants.h:19:9`!: |
| 416 | /* case BPF_CLASS_ALU64: */ |
| 417 | if (EBPF_MODEcxx . . $ `arch/BPF/BPFMapping.h:11:9`!(ud)) { |
| 418 | // ALU64 in eBPF |
| 419 | id = op2insn_alucxx . . $ op2insn_alu(cf04ce91d110db35).(opcode); |
| 420 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(BPF_GRP_ALU); |
| 421 | } |
| 422 | else { |
| 423 | if (BPF_MISCOPcxx . . $ `arch/BPF/BPFConstants.h:83:9`!(opcode) == BPF_MISCOP_TXAcxx . . $ `arch/BPF/BPFConstants.h:86:9`!) |
| 424 | id = BPF_INS_TXAcxx . . $ BPF_INS_TXA.; |
| 425 | else |
| 426 | id = BPF_INS_TAXcxx . . $ BPF_INS_TAX.; |
| 427 | PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`!(BPF_GRP_MISC); |
| 428 | } |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | insn->idcxx . . $ cs_insn#id. = id; |
| 433 | #undef PUSH_GROUPcxx . . $ `arch/BPF/BPFMapping.c:374:10`! |
| 434 | |
| 435 | #ifndef CAPSTONE_DIET |
| 436 | if (detail) { |
| 437 | update_regs_accesscxx . . $ update_regs_access(8c29a82f0bb298fb).(ud, detail, id, opcode); |
| 438 | } |
| 439 | #endif |
| 440 | } |
| 441 | |
| 442 | static void sort_and_uniqcxx . . $ sort_and_uniq(3eef557f651eb958).(cs_regscxx . . $ cs_regs# arr, uint8_tcxx . . $ uint8_t# n, uint8_tcxx . . $ uint8_t# *new_n) |
| 443 | { |
| 444 | /* arr is always a tiny (usually n < 3) array, |
| 445 | * a simple O(n^2) sort is efficient enough. */ |
| 446 | int i; |
| 447 | int j; |
| 448 | int iMin; |
| 449 | int tmp; |
| 450 | |
| 451 | /* a modified selection sort for sorting and making unique */ |
| 452 | for (j = 0; j < n; j++) { |
| 453 | /* arr[iMin] will be min(arr[j .. n-1]) */ |
| 454 | iMin = j; |
| 455 | for (i = j + 1; i < n; i++) { |
| 456 | if (arr[i] < arr[iMin]) |
| 457 | iMin = i; |
| 458 | } |
| 459 | if (j != 0 && arr[iMin] == arr[j - 1]) { // duplicate ele found |
| 460 | arr[iMin] = arr[n - 1]; |
| 461 | --n; |
| 462 | } |
| 463 | else { |
| 464 | tmp = arr[iMin]; |
| 465 | arr[iMin] = arr[j]; |
| 466 | arr[j] = tmp; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | *new_n = n; |
| 471 | } |
| 472 | void BPF_reg_accesscxx . . $ BPF_reg_access(a4e53139cd2f126c).(const cs_insncxx . . $ cs_insn# *insn, |
| 473 | cs_regscxx . . $ cs_regs# regs_read, uint8_tcxx . . $ uint8_t# *regs_read_count, |
| 474 | cs_regscxx . . $ cs_regs# regs_write, uint8_tcxx . . $ uint8_t# *regs_write_count) |
| 475 | { |
| 476 | unsigned i; |
| 477 | uint8_tcxx . . $ uint8_t# read_count, write_count; |
| 478 | const cs_bpfcxx . . $ cs_bpf# *bpf = &(insn->detailcxx . . $ cs_insn#detail.->bpfcxx . . $ cs_detail#$anonymous_type_0#bpf.); |
| 479 | |
| 480 | read_count = insn->detailcxx . . $ cs_insn#detail.->regs_read_countcxx . . $ cs_detail#regs_read_count.; |
| 481 | write_count = insn->detailcxx . . $ cs_insn#detail.->regs_write_countcxx . . $ cs_detail#regs_write_count.; |
| 482 | |
| 483 | // implicit registers |
| 484 | memcpycxx . . $ memcpy(cc817cb175f282d).(regs_read, insn->detailcxx . . $ cs_insn#detail.->regs_readcxx . . $ cs_detail#regs_read., read_count * sizeof(insn->detailcxx . . $ cs_insn#detail.->regs_readcxx . . $ cs_detail#regs_read.[0])); |
| 485 | memcpycxx . . $ memcpy(cc817cb175f282d).(regs_write, insn->detailcxx . . $ cs_insn#detail.->regs_writecxx . . $ cs_detail#regs_write., write_count * sizeof(insn->detailcxx . . $ cs_insn#detail.->regs_writecxx . . $ cs_detail#regs_write.[0])); |
| 486 | |
| 487 | for (i = 0; i < bpf->op_countcxx . . $ cs_bpf#op_count.; i++) { |
| 488 | const cs_bpf_opcxx . . $ cs_bpf_op# *op = &(bpf->operandscxx . . $ cs_bpf#operands.[i]); |
| 489 | switch (op->typecxx . . $ cs_bpf_op#type.) { |
| 490 | default: |
| 491 | break; |
| 492 | case BPF_OP_REGcxx . . $ BPF_OP_REG.: |
| 493 | if (op->accesscxx . . $ cs_bpf_op#access. & CS_AC_READcxx . . $ CS_AC_READ.) { |
| 494 | regs_read[read_count] = op->regcxx . . $ cs_bpf_op#$anonymous_type_0#reg.; |
| 495 | read_count++; |
| 496 | } |
| 497 | if (op->accesscxx . . $ cs_bpf_op#access. & CS_AC_WRITEcxx . . $ CS_AC_WRITE.) { |
| 498 | regs_write[write_count] = op->regcxx . . $ cs_bpf_op#$anonymous_type_0#reg.; |
| 499 | write_count++; |
| 500 | } |
| 501 | break; |
| 502 | case BPF_OP_MEMcxx . . $ BPF_OP_MEM.: |
| 503 | if (op->memcxx . . $ cs_bpf_op#$anonymous_type_0#mem..basecxx . . $ bpf_op_mem#base. != BPF_REG_INVALIDcxx . . $ BPF_REG_INVALID.) { |
| 504 | regs_read[read_count] = op->memcxx . . $ cs_bpf_op#$anonymous_type_0#mem..basecxx . . $ bpf_op_mem#base.; |
| 505 | read_count++; |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | sort_and_uniqcxx . . $ sort_and_uniq(3eef557f651eb958).(regs_read, read_count, regs_read_count); |
| 512 | sort_and_uniqcxx . . $ sort_and_uniq(3eef557f651eb958).(regs_write, write_count, regs_write_count); |
| 513 | } |