00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "analy_names.h"
00023 #include "htdebug.h"
00024 #include "htinfo.h"
00025 #include "out_html.h"
00026 #include "tools.h"
00027 #include "x86dis.h"
00028 #include "string.h"
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 struct ImageSymHeader {
00072 dword file_size HTPACKED;
00073 word entry_seg HTPACKED;
00074 word u0 HTPACKED;
00075 word u1 HTPACKED;
00076 word seg_count HTPACKED;
00077 dword u2 HTPACKED;
00078 byte module_name[16] HTPACKED;
00079 };
00080
00081 struct ImageSymSegHeader {
00082 word next_rec_ofs HTPACKED;
00083 word sym_count HTPACKED;
00084 word sym_ptr_table_ptr HTPACKED;
00085 word seg_idx HTPACKED;
00086 dword seg_start HTPACKED;
00087 dword seg_size HTPACKED;
00088 };
00089
00090 struct ImageSymDescriptor {
00091 dword address HTPACKED;
00092
00093
00094
00095 };
00096
00097
00098
00099
00100
00101 #define MAX_BYTES_PER_SEGMENT 0xff00
00102 #define MAX_SYMBOLS_PER_SEGMENT 0x4000
00103
00104 static void write_sym(ht_stream *stream, dword addr, char *name, UINT *bytes_written)
00105 {
00106 ImageSymDescriptor desc;
00107 desc.address = addr;
00108 stream->write(&desc, sizeof desc);
00109 putstrp(stream, name);
00110 *bytes_written += sizeof desc + 1 + strlen(name);
00111 }
00112
00113 static void g(ht_stream *stream, Symbol *s, UINT *bytes_written, UINT *symbols_written, word *ptr_table)
00114 {
00115 if (*bytes_written >= MAX_BYTES_PER_SEGMENT) return;
00116 if (*symbols_written >= MAX_SYMBOLS_PER_SEGMENT) return;
00117
00118 dword addr;
00119 if (s->location->addr->byteSize() == sizeof addr) {
00120 s->location->addr->putIntoArray((byte*)&addr);
00121
00122 } else {
00123 addr = 0;
00124 }
00125
00126 ptr_table[*symbols_written] = *bytes_written;
00127 write_sym(stream, addr, s->name, bytes_written);
00128
00129 (*symbols_written) ++;
00130 }
00131
00132 static void align16(ht_streamfile *file, UINT *bytes_written)
00133 {
00134 byte c = 0;
00135 while (*bytes_written % 16) {
00136 file->write(&c, 1);
00137 (*bytes_written) ++;
00138 }
00139 }
00140
00141 int export_to_sym(Analyser *analy, ht_streamfile *file)
00142 {
00143 if ((!analy) || (!file)) return 1;
00144 if (analy->active) return 1;
00145
00146 char *module_name = "TEST";
00147 ImageSymHeader head;
00148 ImageSymSegHeader seg_head;
00149
00150
00151 memset(&head, 0, sizeof head);
00152 file->write(&head, sizeof head);
00153
00154
00155
00156
00157
00158 memset(&seg_head, 0, sizeof seg_head);
00159 file->write(&seg_head, sizeof seg_head);
00160
00161 UINT bytes_written = sizeof seg_head, symbols_written = 0;
00162
00163 write_sym(file, 0xff000000, "_TEXT", &bytes_written);
00164
00165 word *ptr_table = (word*)malloc(MAX_SYMBOLS_PER_SEGMENT * sizeof *ptr_table);
00166
00167 Symbol *sym = NULL;
00168 while ((sym = analy->enumSymbols(sym))) {
00169 g(file, sym, &bytes_written, &symbols_written, ptr_table);
00170 }
00171
00172 UINT sym_ptr_table_ptr = bytes_written;
00173
00174
00175 file->write(ptr_table, sizeof *ptr_table * symbols_written);
00176 bytes_written += sizeof *ptr_table * symbols_written;
00177 free(ptr_table);
00178
00179 align16(file, &bytes_written);
00180
00181
00182 dword terminator = 0x04000000;
00183 file->write(&terminator, sizeof terminator);
00184 bytes_written += 4;
00185
00186 file->seek(0x0020);
00187
00188 seg_head.next_rec_ofs = 0x0002;
00189 seg_head.sym_count = symbols_written;
00190 seg_head.sym_ptr_table_ptr = sym_ptr_table_ptr;
00191 seg_head.seg_idx = 1;
00192 seg_head.seg_start = 0;
00193 seg_head.seg_size = 0x0000ffff;
00194
00195 file->write(&seg_head, sizeof seg_head);
00196
00197
00198
00199
00200 bytes_written += sizeof head;
00201 file->seek(0);
00202
00203 head.file_size = (bytes_written-1) / 16;
00204 head.entry_seg = 0;
00205 head.u0 = 0;
00206 head.u1 = 0x0015;
00207 head.seg_count = 1;
00208 head.u2 = 0x04020002;
00209 memcpy(head.module_name, module_name, MIN(strlen(module_name), sizeof head.module_name));
00210
00211 file->write(&head, sizeof head);
00212
00213 return 0;
00214 }
00215