| 1 | #include <stdlib.h> |
|---|
| 2 | #include <stdio.h> |
|---|
| 3 | #include <stdint.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | #include <unistd.h> |
|---|
| 6 | |
|---|
| 7 | #define MAX_MATCHES (64) |
|---|
| 8 | |
|---|
| 9 | typedef struct { |
|---|
| 10 | uint32_t ptr; |
|---|
| 11 | uint32_t fail; |
|---|
| 12 | uint32_t success; |
|---|
| 13 | } Match; |
|---|
| 14 | |
|---|
| 15 | typedef struct { |
|---|
| 16 | uint32_t offs; |
|---|
| 17 | uint32_t value; |
|---|
| 18 | uint32_t mask; |
|---|
| 19 | } FuncSig; |
|---|
| 20 | |
|---|
| 21 | typedef struct { |
|---|
| 22 | const char *name; |
|---|
| 23 | FuncSig *sig; |
|---|
| 24 | } FuncsList; |
|---|
| 25 | |
|---|
| 26 | #if defined(PLATFORMOS_vxworks) |
|---|
| 27 | #include "signatures_vxworks.h" |
|---|
| 28 | #elif defined(PLATFORMOS_dryos) |
|---|
| 29 | #include "signatures_dryos.h" |
|---|
| 30 | #else |
|---|
| 31 | #error Undefined platform OS |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | int match_compare(const Match *p1, const Match *p2) |
|---|
| 35 | { |
|---|
| 36 | /* NOTE: If a function has *more* matches, it will be prefered, even if it has a lower percent matches */ |
|---|
| 37 | if (p1->success > p2->success){ |
|---|
| 38 | return -1; |
|---|
| 39 | } else |
|---|
| 40 | if (p1->success < p2->success){ |
|---|
| 41 | return 1; |
|---|
| 42 | } else { |
|---|
| 43 | if (p1->fail < p2->fail){ |
|---|
| 44 | return -1; |
|---|
| 45 | } else |
|---|
| 46 | if (p1->fail > p2->fail){ |
|---|
| 47 | return 1; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /* scores are equal. prefer lower address */ |
|---|
| 52 | |
|---|
| 53 | if (p1->ptr < p2->ptr){ |
|---|
| 54 | return -1; |
|---|
| 55 | } else |
|---|
| 56 | if (p1->ptr > p2->ptr){ |
|---|
| 57 | return 1; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | return 0; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void usage() |
|---|
| 64 | { |
|---|
| 65 | printf("finsig <primary> <base>\n"); |
|---|
| 66 | exit(1); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | int main(int argc, char **argv) |
|---|
| 70 | { |
|---|
| 71 | Match matches[MAX_MATCHES]; |
|---|
| 72 | uint32_t *buf; |
|---|
| 73 | FILE *f; |
|---|
| 74 | int size; |
|---|
| 75 | int i,j,k; |
|---|
| 76 | int fail, success; |
|---|
| 77 | uint32_t base; |
|---|
| 78 | FuncSig *sig; |
|---|
| 79 | int count; |
|---|
| 80 | int ret = 0; |
|---|
| 81 | const char *curr_name; |
|---|
| 82 | |
|---|
| 83 | if (argc != 3) |
|---|
| 84 | usage(); |
|---|
| 85 | |
|---|
| 86 | f = fopen(argv[1], "r+b"); |
|---|
| 87 | |
|---|
| 88 | if (f == NULL) |
|---|
| 89 | usage(); |
|---|
| 90 | |
|---|
| 91 | base = strtoul(argv[2], NULL, 0); |
|---|
| 92 | |
|---|
| 93 | printf("// !!! THIS FILE IS GENERATED. DO NOT EDIT. !!!\n"); |
|---|
| 94 | printf("#include \"stubs_asm.h\"\n\n"); |
|---|
| 95 | |
|---|
| 96 | fseek(f,0,SEEK_END); |
|---|
| 97 | size=ftell(f)/4; |
|---|
| 98 | fseek(f,0,SEEK_SET); |
|---|
| 99 | |
|---|
| 100 | buf=malloc(size*4); |
|---|
| 101 | fread(buf, 4, size, f); |
|---|
| 102 | |
|---|
| 103 | for (k=0;func_list[k].name;k++){ |
|---|
| 104 | |
|---|
| 105 | count = 0; |
|---|
| 106 | curr_name = func_list[k].name; |
|---|
| 107 | |
|---|
| 108 | while (1) { |
|---|
| 109 | sig = func_list[k].sig; |
|---|
| 110 | |
|---|
| 111 | for (i=0;i<size;i++){ |
|---|
| 112 | fail = 0; |
|---|
| 113 | success = 0; |
|---|
| 114 | for (j=0;sig[j].offs!=-1;j++){ |
|---|
| 115 | if ((i+sig[j].offs) >= size || (buf[i+sig[j].offs] & sig[j].mask) != sig[j].value){ |
|---|
| 116 | fail++; |
|---|
| 117 | } else { |
|---|
| 118 | success++; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | if (success > fail){ |
|---|
| 122 | matches[count].ptr = base+i*4; |
|---|
| 123 | matches[count].success = success; |
|---|
| 124 | matches[count].fail = fail; |
|---|
| 125 | count ++; |
|---|
| 126 | if (count >= MAX_MATCHES){ |
|---|
| 127 | printf("// WARNING: too many matches for %s!\n", func_list[k].name); |
|---|
| 128 | break; |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | // same name, so we have another version of the same function |
|---|
| 134 | if ((func_list[k+1].name == NULL) || (strcmp(curr_name, func_list[k+1].name) != 0)) { |
|---|
| 135 | break; |
|---|
| 136 | } |
|---|
| 137 | k++; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // find best match and report results |
|---|
| 141 | if (count == 0){ |
|---|
| 142 | printf("// ERROR: %s is not found!\n", curr_name); |
|---|
| 143 | ret = 1; |
|---|
| 144 | } else { |
|---|
| 145 | if (count > 1){ |
|---|
| 146 | qsort(matches, count, sizeof(Match), (void*)match_compare); |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | if (matches->fail > 0) |
|---|
| 150 | printf("// Best match: %d%%\n", matches->success*100/(matches->success+matches->fail)); |
|---|
| 151 | |
|---|
| 152 | printf("NSTUB(%s, 0x%x)\n", curr_name, matches->ptr); |
|---|
| 153 | |
|---|
| 154 | for (i=1;i<count && matches[i].fail==matches[0].fail;i++){ |
|---|
| 155 | printf("// ALT: NSTUB(%s, 0x%x) // %d/%d\n", curr_name, matches[i].ptr, matches[i].success, matches[i].fail); |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | return ret; |
|---|
| 161 | } |
|---|
| 162 | |
|---|