1 /*
2 **
3 ** skelet.c in
4 **
5 ** Author : <sk at devhell dot org>
6 ** Started : Thu May 29 21:56:30 2003
7 ** Updated : Wed Jun 18 19:26:23 2003
8 */
9 #include "modflow.h"
10
11 /*****************************************************************************
12 * High level block analysis.
13 *
14 * this function builds a linked list of function
15 * which each contains a list linked of blocks
16 *
17 * for each block,
18 *
19 * until a new block starting a function, current blocks
20 * are supposed to be part of current function
21 *
22 ************************************************************************/
23
elfsh_init()24 void elfsh_init() {
25 puts(" [*] ELFsh modflow("__DATE__"/"__TIME__") fini -OK-\n");
26 puts(" Added commands:\n");
27 puts("\tgraph <file> : dump graphviz graph to file"
28 "\tinspect <vaddr> : inspect block at vaddr\n"
29 "\tflow <symbol> : build a .control section\n");
30 vm_addcmd(CMD_GRAPH, cmd_graph, vm_getoption, 1);
31 vm_addcmd(ELFSH_CMD_FLOW, modflow_cmd, vm_getoption, 1);
32 vm_addcmd(ELFSH_CMD_INSPECT, inspect_cmd, vm_getoption, 1);
33 }
34
35
elfsh_fini()36 void elfsh_fini() {
37 puts(" [*] ELFsh modflow init -OK-\n");
38 vm_delcmd(ELFSH_CMD_FLOW);
39 vm_delcmd(CMD_GRAPH);
40 vm_delcmd(ELFSH_CMD_INSPECT);
41 }
42
43
44 /*******************************************************************
45 * main
46 */
47
48 hash_t block_hash;
49
modflow_cmd(void)50 int modflow_cmd(void)
51 {
52 char *buffer;
53 asm_instr instr;
54 elfshobj_t *hdl;
55 Elf32_Sym *sym;
56 u_int disassembled;
57 u_int ilen;
58 u_int max_len;
59 u_int foff;
60 u_int e_point;
61 u_int main_addr;
62 u_int vaddr;
63 struct s_list *args;
64 struct s_iblock *binary_blk;
65 struct s_function *binary_functions;
66
67 /* char *str; */
68
69
70 /*
71 parse arguments
72 load binary and resolve symbol
73 */
74
75 hdl = world.current;
76
77 if (!(sym = elfsh_get_metasym_by_name(hdl, world.args.param[0])))
78 ELFSH_SETERROR("[elfsh] Cannont find symbol\n", -1);
79
80
81 foff = elfsh_get_symbol_foffset(hdl, sym);
82 max_len = elfsh_get_symbol_size(sym);
83 vaddr = sym->st_value;
84
85
86 printf("[MODFLOW] loading code... vaddr = %08x foffset = %i len = %i\n", vaddr, foff, max_len);
87 buffer = malloc(max_len);
88 elfsh_raw_read(hdl, foff, buffer, max_len);
89
90 hash_init(&block_hash, max_len);
91 binary_blk = 0;
92 binary_functions = 0;
93 args = 0;
94
95 /*
96 ** Fetch main from entry point
97 */
98
99 e_point = elfsh_get_entrypoint(elfsh_get_hdr(hdl));
100 printf(" [*] Entry point: %08x\n", e_point);
101 if (vaddr == e_point)
102 {
103 main_addr = trace_start(hdl, buffer, max_len, e_point,
104 &binary_blk, &binary_functions);
105 printf(" [*] main located at %8x\n", main_addr);
106 }
107
108 printf(" [*] starting disassembly\n");
109
110 /*
111 main loop
112 for each instruction disassembled, pass it to
113 the trace_control function which may build dynamically
114 a linked list of blocks, with additionnal relationship
115 informations in a linked list contained in the block
116 */
117
118 for (disassembled = 0; disassembled < max_len; disassembled += ilen)
119 {
120 if ((ilen = asm_read_instr(&instr, buffer + disassembled,
121 max_len - disassembled, &world.proc)))
122 {
123
124 /*
125 str = asm_display_instr_att(&instr, vaddr + disassembled);
126 printf("%8x:\t%s\n", vaddr + disassembled, str);
127 */
128 trace_control(hdl, &instr, vaddr + disassembled, &binary_blk);
129 }
130 else
131 ilen = 1;
132 }
133
134 /*
135 cleanup & ouput
136 save file.
137 */
138
139 puts("[MODFLOW] done\n");
140
141 store_blocks(hdl, binary_blk);
142 display_blocks(hdl, binary_blk, 1);
143
144
145 /*
146 puts(" - done\n[blocks path recursion]");
147
148
149 trace_functions(hdl, &binary_functions, binary_blk);
150
151 puts("-done\n[Functions]");
152
153 display_functions(hdl, binary_functions);
154 */
155
156
157 free(buffer);
158
159 return (0);
160 }
161
162
163
164