|
| 1 | +/* Controlflow Graph Driver for libvmcu */ |
| 2 | + |
| 3 | +// C Headers |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | +#include <string.h> |
| 7 | + |
| 8 | +// libvmcu |
| 9 | +#include "libvmcu_analyzer.h" |
| 10 | +#include "libvmcu_system.h" |
| 11 | + |
| 12 | +#define COLOR_RED "\x1b[31m" |
| 13 | +#define COLOR_GREEN "\x1b[32m" |
| 14 | +#define COLOR_YELLOW "\x1b[33m" |
| 15 | +#define COLOR_BLUE "\x1b[34m" |
| 16 | +#define COLOR_MAGENTA "\x1b[35m" |
| 17 | +#define COLOR_CYAN "\x1b[36m" |
| 18 | +#define COLOR_RESET "\x1b[0m" |
| 19 | + |
| 20 | +#define mnemlen(mnem) strlen(mnem->base) \ |
| 21 | + + strlen(mnem->src) \ |
| 22 | + + strlen(mnem->dest) |
| 23 | + |
| 24 | +/* libvmcu Structures */ |
| 25 | + |
| 26 | +vmcu_model_t *m328p = NULL; |
| 27 | +vmcu_report_t *report = NULL; |
| 28 | + |
| 29 | +/* Forward Declaration of static Functions */ |
| 30 | + |
| 31 | +static void print_instruction(vmcu_instr_t *instr); |
| 32 | +static void print_instruction_details(vmcu_instr_t *instr); |
| 33 | +static void print_colored_base(const char *basestr, VMCU_GROUP group); |
| 34 | +static void print_colored_operands(const char *opstr, VMCU_OPTYPE optype); |
| 35 | +static void add_padding(const size_t length, const size_t max); |
| 36 | +static void cleanup(void); |
| 37 | + |
| 38 | +/* --- Extern --- */ |
| 39 | + |
| 40 | +int main(const int argc, const char **argv) { |
| 41 | + |
| 42 | + if(argc != 2) { |
| 43 | + |
| 44 | + printf("Usage: ./skeleton <hexfile>\n"); |
| 45 | + return EXIT_FAILURE; |
| 46 | + } |
| 47 | + |
| 48 | + atexit(cleanup); |
| 49 | + |
| 50 | + m328p = vmcu_model_ctor(VMCU_DEVICE_M328P); |
| 51 | + report = vmcu_analyze_ihex(argv[1], m328p); |
| 52 | + |
| 53 | + if(report == NULL) |
| 54 | + return EXIT_FAILURE; |
| 55 | + |
| 56 | + vmcu_cfg_t *cfg = report->cfg; |
| 57 | + |
| 58 | + for(int32_t i = 0; i < cfg->used; i++) { |
| 59 | + |
| 60 | + vmcu_cfg_node_t *node = &cfg->node[i]; |
| 61 | + |
| 62 | + printf(" "); |
| 63 | + print_instruction(node->xto.i); |
| 64 | + |
| 65 | + if(node->t == NULL && node->f == NULL) { |
| 66 | + |
| 67 | + printf(" "); |
| 68 | + printf("leaf instruction. no branches found.\n"); |
| 69 | + } |
| 70 | + |
| 71 | + if(node->t != NULL) { |
| 72 | + |
| 73 | + printf("true branch: "); |
| 74 | + print_instruction(node->t->xto.i); |
| 75 | + } |
| 76 | + |
| 77 | + if(node->f != NULL) { |
| 78 | + |
| 79 | + printf("false branch: "); |
| 80 | + print_instruction(node->f->xto.i); |
| 81 | + } |
| 82 | + |
| 83 | + printf("----------------------------------------\n"); |
| 84 | + } |
| 85 | + |
| 86 | + return EXIT_SUCCESS; |
| 87 | +} |
| 88 | + |
| 89 | +/* --- Static --- */ |
| 90 | + |
| 91 | +static void print_instruction(vmcu_instr_t *instr) { |
| 92 | + |
| 93 | + vmcu_mnemonic_t *mnem = &instr->mnem; |
| 94 | + |
| 95 | + print_instruction_details(instr); |
| 96 | + print_colored_base(mnem->base, instr->group); |
| 97 | + |
| 98 | + size_t sz = 0; |
| 99 | + |
| 100 | + if(instr->dest.type != VMCU_OPTYPE_NONE) { |
| 101 | + |
| 102 | + print_colored_operands(mnem->dest, instr->dest.type); |
| 103 | + sz += printf(", "); |
| 104 | + } |
| 105 | + |
| 106 | + if(instr->src.type != VMCU_OPTYPE_NONE) { |
| 107 | + |
| 108 | + print_colored_operands(mnem->src, instr->src.type); |
| 109 | + sz += printf(" "); |
| 110 | + } |
| 111 | + |
| 112 | + add_padding(mnemlen(mnem) + sz, 26); |
| 113 | + printf("%s%s%s\n", COLOR_RED, mnem->comment, COLOR_RESET); |
| 114 | +} |
| 115 | + |
| 116 | +static void print_instruction_details(vmcu_instr_t *instr) { |
| 117 | + |
| 118 | + printf("0x%04x", instr->addr); |
| 119 | + |
| 120 | + const uint16_t opcl = (instr->opcode & 0x0000ffff); |
| 121 | + const uint16_t swpl = (opcl >> 8) | (opcl << 8); |
| 122 | + |
| 123 | + const uint16_t opch = ((instr->opcode & 0xffff0000) >> 16); |
| 124 | + const uint16_t swph = (opch >> 8) | (opch << 8); |
| 125 | + |
| 126 | + if(instr->dword == false) |
| 127 | + printf(" %s....%s ", COLOR_GREEN, COLOR_RESET); |
| 128 | + else |
| 129 | + printf(" %s%04x%s ", COLOR_GREEN, swph, COLOR_RESET); |
| 130 | + |
| 131 | + printf("%s%04x%s ", COLOR_YELLOW, swpl, COLOR_RESET); |
| 132 | +} |
| 133 | + |
| 134 | +static void print_colored_base(const char *basestr, VMCU_GROUP group) { |
| 135 | + |
| 136 | + switch(group) { |
| 137 | + |
| 138 | + case VMCU_GROUP_NONE: printf("%s", COLOR_YELLOW); break; |
| 139 | + case VMCU_GROUP_MATH_LOGIC: printf("%s", COLOR_RED); break; |
| 140 | + case VMCU_GROUP_SYS_CTRL: printf("%s", COLOR_CYAN); break; |
| 141 | + case VMCU_GROUP_TRANSFER: printf("%s", COLOR_RESET); break; |
| 142 | + case VMCU_GROUP_FLOW: printf("%s", COLOR_GREEN); break; |
| 143 | + case VMCU_GROUP_BIT: printf("%s", COLOR_MAGENTA); break; |
| 144 | + |
| 145 | + default: break; |
| 146 | + } |
| 147 | + |
| 148 | + printf("%s%s ", basestr, COLOR_RESET); |
| 149 | +} |
| 150 | + |
| 151 | +static void print_colored_operands(const char *opstr, VMCU_OPTYPE optype) { |
| 152 | + |
| 153 | + switch(optype) { |
| 154 | + |
| 155 | + case VMCU_OPTYPE_B: |
| 156 | + case VMCU_OPTYPE_K4: |
| 157 | + case VMCU_OPTYPE_K6: |
| 158 | + case VMCU_OPTYPE_K8: |
| 159 | + case VMCU_OPTYPE_IO5: |
| 160 | + case VMCU_OPTYPE_IO6: |
| 161 | + case VMCU_OPTYPE_D7: |
| 162 | + case VMCU_OPTYPE_D16: |
| 163 | + case VMCU_OPTYPE_P22: |
| 164 | + case VMCU_OPTYPE_S7: |
| 165 | + case VMCU_OPTYPE_S12: |
| 166 | + |
| 167 | + printf("%s", COLOR_BLUE); |
| 168 | + |
| 169 | + break; |
| 170 | + |
| 171 | + case VMCU_OPTYPE_R: |
| 172 | + case VMCU_OPTYPE_RP: |
| 173 | + case VMCU_OPTYPE_X: |
| 174 | + case VMCU_OPTYPE_Y: |
| 175 | + case VMCU_OPTYPE_Z: |
| 176 | + |
| 177 | + printf("%s", COLOR_CYAN); |
| 178 | + |
| 179 | + break; |
| 180 | + |
| 181 | + default: break; |
| 182 | + } |
| 183 | + |
| 184 | + printf("%s%s", opstr, COLOR_RESET); |
| 185 | +} |
| 186 | + |
| 187 | +static void add_padding(const size_t length, const size_t max) { |
| 188 | + |
| 189 | + if(length >= max) |
| 190 | + return; |
| 191 | + |
| 192 | + char pad[max - length + 1]; |
| 193 | + |
| 194 | + memset(pad, ' ', (max - length)); |
| 195 | + pad[max - length] = '\0'; |
| 196 | + |
| 197 | + printf("%s", pad); |
| 198 | +} |
| 199 | + |
| 200 | +static void cleanup(void) { |
| 201 | + |
| 202 | + if(report != NULL) |
| 203 | + vmcu_report_dtor(report); |
| 204 | + |
| 205 | + if(m328p != NULL) |
| 206 | + vmcu_model_dtor(m328p); |
| 207 | +} |
0 commit comments