00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "analy.h"
00022 #include "analy_names.h"
00023 #include "analy_ppc.h"
00024 #include "analy_register.h"
00025 #include "global.h"
00026 #include "pef_analy.h"
00027
00028 #include "htctrl.h"
00029 #include "htdebug.h"
00030 #include "htiobox.h"
00031 #include "htpef.h"
00032 #include "htstring.h"
00033 #include "pestruct.h"
00034 #include "snprintf.h"
00035 #include "x86asm.h"
00036
00037 extern "C" {
00038 #include "demangle.h"
00039 }
00040
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044
00045
00046
00047
00048
00049 void PEFAnalyser::init(ht_pef_shared_data *Pef_shared, ht_streamfile *File)
00050 {
00051 pef_shared = Pef_shared;
00052 file = File;
00053
00054 validarea = new Area();
00055 validarea->init();
00056
00057 Analyser::init();
00058 }
00059
00060 void PEFAnalyser::beginAnalysis()
00061 {
00062
00063
00064
00065
00066 setLocationTreeOptimizeThreshold(100);
00067 setSymbolTreeOptimizeThreshold(100);
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 PEF_SECTION_HEADER *s32=pef_shared->sheaders.sheaders;
00079 char blub[100];
00080 for (UINT i=0; i < pef_shared->sheaders.count; i++) {
00081 Address *secaddr;
00082 secaddr = createAddress32(s32->defaultAddress);
00083 if (validAddress(secaddr, scvalid)) {
00084 ht_snprintf(blub, sizeof blub, "; section %d <%s>", i, getSegmentNameByAddress(secaddr));
00085 addComment(secaddr, 0, "");
00086 addComment(secaddr, 0, ";******************************************************************");
00087 addComment(secaddr, 0, blub);
00088 ht_snprintf(blub, sizeof blub, "; virtual address %08x virtual size %08x", s32->defaultAddress, s32->totalSize);
00089 addComment(secaddr, 0, blub);
00090 ht_snprintf(blub, sizeof blub, "; file offset %08x file size %08x", s32->containerOffset, s32->packedSize);
00091 addComment(secaddr, 0, blub);
00092 addComment(secaddr, 0, ";******************************************************************");
00093
00094
00095 ht_snprintf(blub, sizeof blub, "; end of section <%s>", getSegmentNameByAddress(secaddr));
00096 Address *secend_addr = (Address *)secaddr->duplicate();
00097 secend_addr->add(s32->totalSize);
00098 newLocation(secend_addr)->flags |= AF_FUNCTION_END;
00099 addComment(secend_addr, 0, "");
00100 addComment(secend_addr, 0, ";******************************************************************");
00101 addComment(secend_addr, 0, blub);
00102 addComment(secend_addr, 0, ";******************************************************************");
00103
00104 validarea->add(secaddr, secend_addr);
00105
00106 delete secend_addr;
00107 }
00108 delete secaddr;
00109 s32++;
00110 }
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 setLocationTreeOptimizeThreshold(1000);
00125 setSymbolTreeOptimizeThreshold(1000);
00126
00127
00128 Analyser::beginAnalysis();
00129 }
00130
00131
00132
00133
00134 void PEFAnalyser::initInsertSymbols(int shidx)
00135 {
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 }
00346
00347
00348
00349
00350 int PEFAnalyser::load(ht_object_stream *f)
00351 {
00352 GET_OBJECT(f, validarea);
00353 return Analyser::load(f);
00354 }
00355
00356
00357
00358
00359 void PEFAnalyser::done()
00360 {
00361 validarea->done();
00362 delete validarea;
00363 Analyser::done();
00364 }
00365
00366 OBJECT_ID PEFAnalyser::object_id() const
00367 {
00368 return ATOM_PEF_ANALYSER;
00369 }
00370
00371
00372
00373
00374 UINT PEFAnalyser::bufPtr(Address *Addr, byte *buf, int size)
00375 {
00376 FILEOFS ofs = addressToFileofs(Addr);
00377
00378
00379
00380 assert(ofs != INVALID_FILE_OFS);
00381 file->seek(ofs);
00382 return file->read(buf, size);;
00383 }
00384
00385 bool PEFAnalyser::convertAddressToPEFAddress(Address *addr, PEFAddress *r)
00386 {
00387 if (addr->object_id()==ATOM_ADDRESS_FLAT_32) {
00388 r->a32 = ((AddressFlat32*)addr)->addr;
00389 return true;
00390 } else {
00391 return false;
00392 }
00393 }
00394
00395 Address *PEFAnalyser::createAddress()
00396 {
00397 return new AddressFlat32();
00398 }
00399
00400 Address *PEFAnalyser::createAddress32(dword addr)
00401 {
00402 return new AddressFlat32(addr);
00403 }
00404
00405 Address *PEFAnalyser::createAddress64(qword addr)
00406 {
00407 return new AddressFlat64(addr);
00408 }
00409
00410
00411
00412
00413 Assembler *PEFAnalyser::createAssembler()
00414 {
00415 return NULL;
00416 }
00417
00418
00419
00420
00421 FILEOFS PEFAnalyser::addressToFileofs(Address *Addr)
00422 {
00423 if (validAddress(Addr, scinitialized)) {
00424 dword ofs;
00425 PEFAddress ea;
00426 if (!convertAddressToPEFAddress(Addr, &ea)) return INVALID_FILE_OFS;
00427 if (!pef_addr_to_ofs(&pef_shared->sheaders, ea, &ofs)) return INVALID_FILE_OFS;
00428 return ofs;
00429 } else {
00430 return INVALID_FILE_OFS;
00431 }
00432 }
00433
00434
00435
00436
00437 char *PEFAnalyser::getSegmentNameByAddress(Address *Addr)
00438 {
00439 static char pef_sectionname[33];
00440 pef_section_headers *sections=&pef_shared->sheaders;
00441 int i;
00442 PEFAddress ea;
00443 if (!convertAddressToPEFAddress(Addr, &ea)) return NULL;
00444 if (!pef_addr_to_section(sections, ea, &i)) return NULL;
00445 if ((uint32)pef_shared->sheaders.sheaders[i].nameOffset == 0xffffffff) {
00446 ht_snprintf(pef_sectionname, sizeof pef_sectionname, "unnamed%d", i);
00447 } else {
00448
00449
00450 strcpy(pef_sectionname, "nyi");
00451 }
00452 return pef_sectionname;
00453 }
00454
00455
00456
00457
00458 const char *PEFAnalyser::getName()
00459 {
00460 return file->get_desc();
00461 }
00462
00463
00464
00465
00466 const char *PEFAnalyser::getType()
00467 {
00468 return "PEF/Analyser";
00469 }
00470
00471
00472
00473
00474 void PEFAnalyser::initCodeAnalyser()
00475 {
00476 Analyser::initCodeAnalyser();
00477 }
00478
00479
00480
00481
00482 void PEFAnalyser::initUnasm()
00483 {
00484 DPRINTF("pef_analy: ");
00485 switch (pef_shared->arch) {
00486 case PEFARCH_PowerPC:
00487 DPRINTF("initing analy_ppc_disassembler\n");
00488 analy_disasm = new AnalyPPCDisassembler();
00489 ((AnalyPPCDisassembler*)analy_disasm)->init(this);
00490 break;
00491 case PEFARCH_M68K:
00492 DPRINTF("no disassembler for machine 'm68k'\n");
00493 warnbox("No disassembler for machine 'm68k'!");
00494 break;
00495 default:
00496 break;
00497 }
00498 }
00499
00500
00501
00502
00503 void PEFAnalyser::log(const char *msg)
00504 {
00505
00506
00507
00508
00509
00510
00511 }
00512
00513
00514
00515
00516 Address *PEFAnalyser::nextValid(Address *Addr)
00517 {
00518 return (Address *)validarea->findNext(Addr);
00519 }
00520
00521
00522
00523
00524 void PEFAnalyser::store(ht_object_stream *f)
00525 {
00526 PUT_OBJECT(f, validarea);
00527 Analyser::store(f);
00528 }
00529
00530
00531
00532
00533 int PEFAnalyser::queryConfig(int mode)
00534 {
00535 switch (mode) {
00536 case Q_DO_ANALYSIS:
00537 case Q_ENGAGE_CODE_ANALYSER:
00538 case Q_ENGAGE_DATA_ANALYSER:
00539 return true;
00540 default:
00541 return 0;
00542 }
00543 }
00544
00545
00546
00547
00548 Address *PEFAnalyser::fileofsToAddress(FILEOFS fileofs)
00549 {
00550 PEFAddress ea;
00551 if (pef_ofs_to_addr(&pef_shared->sheaders, fileofs, &ea)) {
00552 return createAddress32(ea.a32);
00553 } else {
00554 return new InvalidAddress();
00555 }
00556 }
00557
00558
00559
00560
00561 bool PEFAnalyser::validAddress(Address *Addr, tsectype action)
00562 {
00563 pef_section_headers *sections=&pef_shared->sheaders;
00564 int sec;
00565 PEFAddress ea;
00566 if (!convertAddressToPEFAddress(Addr, &ea)) return false;
00567 if (!pef_addr_to_section(sections, ea, &sec)) return false;
00568 PEF_SECTION_HEADER *s = sections->sheaders + sec;
00569 switch (action) {
00570 case scvalid:
00571 return true;
00572 case scread:
00573 return true;
00574 case scwrite:
00575 case screadwrite:
00576 return (s->sectionKind == 1) ||
00577 (s->sectionKind == 2) ||
00578 (s->sectionKind == 6);
00579 case sccode:
00580 return (s->sectionKind == 0);
00581 case scinitialized:
00582 return (s->sectionKind == 0) ||
00583 (s->sectionKind == 1) ||
00584 (s->sectionKind == 3) ||
00585 (s->sectionKind == 6) ;
00586 }
00587 return false;
00588 }