00001 #include <Instruction.h>
00002 #include <SectHeader.h>
00003 #include <SymbolTable.h>
00004 #include <DemangleWrapper.h>
00005 #include <LoaderSection.h>
00006 #include <RawSection.h>
00007 #include <Iterator.h>
00008 #include <Function.h>
00009 #include <Instruction.h>
00010 #include <XCoffFile.h>
00011 #include <BinaryFile.h>
00012 #include <LineInfoFinder.h>
00013 #include <Loop.h>
00014
00015
00016 void TextSection::buildLoops(){
00017 PRINT_INFOR("Building Loops for section %d", header->getIndex());
00018
00019 uint32_t idx = header->getIndex();
00020
00021 if (!getNumberOfFunctions()){
00022 PRINT_INFOR("Cannot build loops for section %d -- no Functions found", idx);
00023 return;
00024 }
00025
00026 for (uint32_t i = 0; i < getNumberOfFunctions(); i++){
00027 getFunction(i)->buildLoops();
00028 }
00029 }
00030
00031 RawSection::RawSection(SectHeader* h,XCoffFile* xcoff)
00032 : Base(XCoffClassTypes_sect_rawdata),header(h),xCoffSymbolTable(NULL),xCoffFile(xcoff)
00033 {
00034 rawDataPtr = header->getRawDataPtr();
00035 sizeInBytes = (uint32_t)header->getRawDataSize();
00036 hashCode = HashCode((uint32_t)header->getIndex());
00037 ASSERT(hashCode.isSection() && "FATAL : the has code for the section is not right");
00038 }
00039
00040 uint32_t RawSection::read(BinaryInputFile* binaryInputFile) {
00041
00042 if(rawDataPtr){
00043 binaryInputFile->setInPointer(rawDataPtr);
00044 setFileOffset(binaryInputFile->currentOffset());
00045 }
00046 return sizeInBytes;
00047 }
00048
00049 void RawSection::print(){
00050 if(!rawDataPtr)
00051 return;
00052
00053 PRINT_INFOR("SECTRAWDATA %s %d",header->getTypeName(),header->getIndex());
00054 PRINT_INFOR("\tBase : %#llx",header->GET(s_scnptr));
00055 PRINT_INFOR("\tSize : %d",sizeInBytes);
00056 }
00057
00058 RawSection* RawSection::newRawSection(SectHeader* h,XCoffFile* xcoff){
00059 RawSection* ret = NULL;
00060 if(h->IS_SECT_TYPE(DEBUG)){
00061 ret = new DebugSection(h,xcoff);
00062 } else if(h->IS_SECT_TYPE(TYPCHK) ||
00063 h->IS_SECT_TYPE(INFO)){
00064 ret = new TypeCommSection(h,xcoff);
00065 } else if(h->IS_SECT_TYPE(EXCEPT)){
00066 ret = new ExceptionSection(h,xcoff);
00067 } else if(h->IS_SECT_TYPE(LOADER)){
00068 ret = new LoaderSection(h,xcoff);
00069 } else if(h->IS_SECT_TYPE(TEXT)){
00070 ret = new TextSection(h,xcoff);
00071 } else if(h->IS_SECT_TYPE(DATA)){
00072 ret = new DataSection(h,xcoff);
00073 } else if(h->IS_SECT_TYPE(BSS)){
00074 ret = new BSSSection(h,xcoff);
00075 } else if(h->IS_SECT_TYPE(OVRFLO) ||
00076 h->IS_SECT_TYPE(PAD))
00077 {
00078 ret = new RawSection(h,xcoff);
00079 }
00080 ASSERT(ret && "There is a section type which we do not know");
00081 return ret;
00082 }
00083
00084 void DebugSection::print(){
00085
00086 RawSection::print();
00087
00088 DEBUG (
00089 PRINT_INFOR("\tDEBUGSTRINGS:");
00090 PRINT_INFOR("\tStrs :");
00091
00092 for(uint32_t currByte=0;currByte<sizeInBytes;){
00093
00094 uint16_t length32 = 0;
00095 uint32_t length64 = 0;
00096 uint32_t length = 0;
00097
00098 char* ptr = (char*)(rawDataPtr+currByte);
00099 if(getXCoffFile()->is64Bit()){
00100 memcpy(&length64,ptr,sizeof(uint32_t));
00101 length = length64;
00102 currByte += sizeof(uint32_t);
00103 ptr += sizeof(uint32_t);
00104 } else {
00105 memcpy(&length32,ptr,sizeof(uint16_t));
00106 length = length32;
00107 currByte += sizeof(uint16_t);
00108 ptr += sizeof(uint16_t);
00109 }
00110
00111 DemangleWrapper wrapper;
00112 char* demangled = wrapper.demangle_combined(ptr);
00113 ASSERT(demangled && "FATAL : demangling should always return non-null pointer");
00114
00115 PRINT_INFOR("\tDBG %9d %s --- %s",currByte,ptr,demangled);
00116
00117 ASSERT(((length - 1) == strlen(ptr)) && "FATAL : Somehow the string size does not match in debug");
00118 currByte += length;
00119 }
00120 );
00121 }
00122
00123 char* DebugSection::getString(uint32_t offset){
00124 ASSERT(offset < sizeInBytes);
00125 if(!offset)
00126 return "";
00127 return rawDataPtr+offset;
00128 }
00129
00130
00131 void TypeCommSection::print(){
00132
00133 RawSection::print();
00134
00135 PRINT_INFOR("\tStrs :");
00136
00137 for(uint32_t currByte=0;currByte<sizeInBytes;){
00138
00139 char* ptr = (char*)(rawDataPtr+currByte);
00140
00141 uint16_t length16 = 0;
00142 uint32_t length32 = 0;
00143 uint32_t length = 0;
00144 if(IS_SECT_TYPE(TYPCHK)){
00145 memcpy(&length16,ptr,sizeof(uint16_t));
00146 length = length16;
00147 currByte += sizeof(uint16_t);
00148 ptr += sizeof(uint16_t);
00149 } else if(IS_SECT_TYPE(INFO)) {
00150 memcpy(&length32,ptr,sizeof(uint32_t));
00151 length = length32;
00152 currByte += sizeof(uint32_t);
00153 ptr += sizeof(uint32_t);
00154 } else {
00155 ASSERT(0 && "FATAL: The section to call this can only be typecheck or comment (info)");
00156 }
00157
00158 char* tmpStr = new char[length+1];
00159 strncpy(tmpStr,ptr,length);
00160 tmpStr[length] = '\0';
00161 PRINT_INFOR("%9d %s %d %d",currByte,tmpStr,length,length-(uint32_t)strlen(tmpStr));
00162 delete[] tmpStr;
00163
00164 currByte += length;
00165 }
00166 }
00167
00168 char* TypeCommSection::getString(uint32_t offset){
00169 ASSERT(offset < sizeInBytes);
00170 if(!offset)
00171 return "";
00172 return rawDataPtr+offset;
00173 }
00174
00175 ExceptionSection::ExceptionSection(SectHeader* h,XCoffFile* xcoff)
00176 : RawSection(h,xcoff),exceptions(NULL)
00177 {
00178 if(getXCoffFile()->is64Bit()){
00179 numberOfExceptions = sizeInBytes/Size__64_bit_ExceptionTable_Entry;
00180 } else {
00181 numberOfExceptions = sizeInBytes/Size__32_bit_ExceptionTable_Entry;
00182 }
00183 }
00184
00185 uint32_t ExceptionSection::read(BinaryInputFile* binaryInputFile){
00186
00187 exceptions = new Exception*[numberOfExceptions];
00188
00189 PRINT_DEBUG("Reading the EXCEPTION table\n");
00190
00191 binaryInputFile->setInPointer(rawDataPtr);
00192 setFileOffset(binaryInputFile->currentOffset());
00193
00194 uint32_t currSize = Size__32_bit_ExceptionTable_Entry;
00195 if(getXCoffFile()->is64Bit()){
00196 currSize = Size__64_bit_ExceptionTable_Entry;
00197 }
00198
00199 uint32_t ret = 0;
00200 for(uint32_t i = 0;i<numberOfExceptions;i++){
00201 if(getXCoffFile()->is64Bit()){
00202 exceptions[i] = new Exception64();
00203 } else {
00204 exceptions[i] = new Exception32();
00205 }
00206 binaryInputFile->copyBytesIterate(exceptions[i]->charStream(),currSize);
00207 ret += currSize;
00208 }
00209
00210 ASSERT((sizeInBytes == ret) && "FATAL : Somehow the number of read does not match");
00211
00212 return sizeInBytes;
00213 }
00214
00215 void Exception::print(SymbolTable* symbolTable,uint32_t index){
00216 uint8_t reason = GET(e_reason);
00217 uint8_t lang = GET(e_lang);
00218
00219 if(!reason){
00220 uint32_t symbolTableIndex = GET(e_symndx);
00221 PRINT_INFOR("\tEXP_S [%3d] (rsn %3d) (lng %3d) (sym %9d)",index,reason,lang,symbolTableIndex);
00222 if(symbolTable){
00223 symbolTable->printSymbol(symbolTableIndex);
00224 }
00225 } else {
00226 uint64_t trapAddress = GET(e_paddr);
00227 PRINT_INFOR("\tEXP_A [%3d] (rsn %3d) (lng %3d) (adr %#llx)",index,reason,lang,trapAddress);
00228 }
00229 }
00230
00231 void ExceptionSection::print(){
00232 if(!xCoffSymbolTable || !exceptions){
00233 RawSection::print();
00234 return;
00235 }
00236
00237 PRINT_INFOR("EXCEPTIONTABLE");
00238 PRINT_INFOR("\tSize : %d",sizeInBytes);
00239 PRINT_INFOR("\tCoun : %d",numberOfExceptions);
00240
00241 PRINT_INFOR("\tExps :");
00242 for(uint32_t i=0;i<numberOfExceptions;i++){
00243 exceptions[i]->print(xCoffSymbolTable,i);
00244 }
00245 }
00246
00247 uint32_t LoaderSection::read(BinaryInputFile* binaryInputFile){
00248
00249 binaryInputFile->setInPointer(rawDataPtr);
00250 setFileOffset(binaryInputFile->currentOffset());
00251
00252 uint32_t currSize = 0;
00253 if(getXCoffFile()->is64Bit()){
00254 header = new LSHeader64();
00255 currSize = Size__64_bit_Loader_Section_Header;
00256 } else {
00257 header = new LSHeader32();
00258 currSize = Size__32_bit_Loader_Section_Header;
00259 }
00260
00261 binaryInputFile->copyBytesIterate(header->charStream(),currSize);
00262
00263 numberOfSymbols = header->GET(l_nsyms);
00264 if(numberOfSymbols){
00265 symbolTable = new LSSymbol*[numberOfSymbols+IMPLICIT_SYM_COUNT];
00266 for(uint32_t i=0;i<IMPLICIT_SYM_COUNT;i++)
00267 symbolTable[i] = NULL;
00268 }
00269
00270 numberOfRelocations = header->GET(l_nreloc);
00271 if(numberOfRelocations){
00272 relocationTable = new LSRelocation*[numberOfRelocations];
00273 }
00274
00275 if(getXCoffFile()->is64Bit()){
00276 symbolTablePtr = rawDataPtr + header->GET(l_symoff);
00277 relocationTablePtr = rawDataPtr + header->GET(l_rldoff);
00278 } else {
00279 symbolTablePtr = rawDataPtr + currSize;
00280 relocationTablePtr = symbolTablePtr + (numberOfSymbols * Size__32_bit_Loader_Section_Symbol);
00281 }
00282
00283 binaryInputFile->setInPointer(symbolTablePtr);
00284 for(uint32_t i=0;i<numberOfSymbols;i++){
00285 if(getXCoffFile()->is64Bit()){
00286 symbolTable[i+IMPLICIT_SYM_COUNT] = new LSSymbol64();
00287 currSize = Size__64_bit_Loader_Section_Symbol;
00288 } else {
00289 symbolTable[i+IMPLICIT_SYM_COUNT] = new LSSymbol32();
00290 currSize = Size__32_bit_Loader_Section_Symbol;
00291 }
00292 binaryInputFile->copyBytesIterate(symbolTable[i+IMPLICIT_SYM_COUNT]->charStream(),currSize);
00293 }
00294
00295 binaryInputFile->setInPointer(relocationTablePtr);
00296 for(uint32_t i=0;i<numberOfRelocations;i++){
00297 if(getXCoffFile()->is64Bit()){
00298 relocationTable[i] = new LSRelocation64();
00299 currSize = Size__64_bit_Loader_Section_Relocation;
00300 } else {
00301 relocationTable[i] = new LSRelocation32();
00302 currSize = Size__32_bit_Loader_Section_Relocation;
00303 }
00304 binaryInputFile->copyBytesIterate(relocationTable[i]->charStream(),currSize);
00305 }
00306
00307 fileNameTable = new LSFileNameTable(header,rawDataPtr);
00308 stringTable = new LSStringTable(header,rawDataPtr);
00309
00310 return sizeInBytes;
00311 }
00312
00313 void LoaderSection::print(){
00314 RawSection::print();
00315
00316 PRINT_INFOR("\tLSHeader");
00317 header->print();
00318
00319 PRINT_INFOR("\tLSSymbols");
00320 for(uint32_t i=0;i<numberOfSymbols;i++){
00321 symbolTable[i+IMPLICIT_SYM_COUNT]->print(i+IMPLICIT_SYM_COUNT,fileNameTable,stringTable);
00322 }
00323
00324 PRINT_INFOR("\tLSRelocations");
00325 for(uint32_t i=0;i<numberOfRelocations;i++){
00326 relocationTable[i]->print(i,symbolTable,stringTable);
00327 }
00328
00329 PRINT_INFOR("\tLSFileNameTable");
00330 fileNameTable->print();
00331
00332
00333 PRINT_INFOR("\tLSStringTable");
00334 stringTable->print();
00335 }
00336
00337 uint32_t LoaderSection::getBSSRelocations(uint64_t* addrs){
00338 #define BSS_SYMBOL_IN_LOADER 2
00339 uint32_t rlcReturn = 0;
00340 for(uint32_t i=0;i<numberOfRelocations;i++){
00341 LSRelocation* rlc = relocationTable[i];
00342 uint16_t secno = rlc->GET(l_symndx);
00343 if(secno == BSS_SYMBOL_IN_LOADER){
00344 addrs[rlcReturn] = rlc->GET(l_vaddr);
00345 rlcReturn++;
00346 }
00347 }
00348 return rlcReturn;
00349 }
00350
00351 uint32_t TextSection::getNumberOfFunctions(){
00352 return numOfFunctions;
00353 }
00354
00355 uint32_t TextSection::getNumberOfBlocks(){
00356 uint32_t ret = 0;
00357 for(uint32_t i=0;i<numOfFunctions;i++){
00358 Function* f = functions[i];
00359 FlowGraph* cfg = f->getFlowGraph();
00360 ret += (cfg ? cfg->getNumOfBasicBlocks() : 0);
00361 }
00362 return ret;
00363 }
00364
00365 uint32_t TextSection::getNumberOfMemoryOps(){
00366 uint32_t ret = 0;
00367 for(uint32_t i=0;i<numOfFunctions;i++){
00368 Function* f = functions[i];
00369 FlowGraph* cfg = f->getFlowGraph();
00370 ret += (cfg ? cfg->getNumOfMemoryOps() : 0);
00371 }
00372 return ret;
00373 }
00374
00375 uint32_t TextSection::getNumberOfFloatPOps(){
00376 uint32_t ret = 0;
00377 for(uint32_t i=0;i<numOfFunctions;i++){
00378 Function* f = functions[i];
00379 FlowGraph* cfg = f->getFlowGraph();
00380 ret += (cfg ? cfg->getNumOfFloatPOps() : 0);
00381 }
00382 return ret;
00383 }
00384
00385 void TextSection::buildLineInfoFinder(){
00386 PRINT_INFOR("Building LineInfoFinder for section %d", header->getIndex());
00387
00388 uint32_t idx = header->getIndex();
00389
00390 if (!xCoffFile->getLineInfoTable(idx)){
00391 PRINT_INFOR("Cannot build LineInfoFinder for section %d -- no LineInfoTable present", idx);
00392 lineInfoFinder = NULL;
00393 return;
00394 }
00395 if (!xCoffFile->getSymbolTable()){
00396 PRINT_INFOR("Cannot build LineInfoFinder for section %d -- no SymbolTable present", idx);
00397 lineInfoFinder = NULL;
00398 return;
00399 }
00400 if (!xCoffFile->getStringTable()){
00401 PRINT_INFOR("Cannot build LineInfoFinder for section %d -- no StringTable present", idx);
00402 lineInfoFinder = NULL;
00403 return;
00404 }
00405
00406 if(!lineInfoFinder){
00407 lineInfoFinder = new LineInfoFinder(header->getIndex(), xCoffFile);
00408 }
00409
00410 #ifdef LINE_INFO_TEST
00411 lineInfoFinder->commandLineTest();
00412 lineInfoFinder->testLineInfoFinder();
00413 #endif
00414
00415 }
00416
00417 void TextSection::print(){
00418 RawSection::print();
00419 for(uint32_t i=0;i<numOfFunctions;i++){
00420 functions[i]->print();
00421 }
00422 }
00423
00424 uint64_t RawSection::readBytes(AddressIterator* ait){
00425
00426 ASSERT(rawDataPtr && "FATAL : The section does not contain any valid data");
00427
00428 uint64_t address = *(*ait);
00429 ASSERT(inRange(address) && "FATAL: the section does not conatin the address");
00430
00431 ASSERT(header->GET(s_vaddr) && "FATAL : the section does not have any valid virtual address");
00432
00433 uint64_t ret = 0;
00434 uint64_t baseAddr = header->GET(s_vaddr);
00435 uint64_t offset = address - baseAddr;
00436
00437 char* ptr = rawDataPtr + offset;
00438 ret = ait->readBytes(ptr);
00439
00440 return ret;
00441 }
00442
00443 Instruction RawSection::readInstruction(AddressIterator* ait){
00444 uint32_t bits = (uint32_t)readBytes(ait);
00445 return Instruction(bits);
00446 }
00447
00448 char* RawSection::getContentVisually(Symbol** symbols,uint32_t symbolCount,uint64_t content){
00449
00450 return NULL;
00451 }
00452
00453 char* TextSection::getContentVisually(Symbol** symbols,uint32_t symbolCount,uint64_t content){
00454 return strdup("instruction");
00455 }
00456
00457 char* DataSection::getContentVisually(Symbol** symbols,uint32_t symbolCount,uint64_t content){
00458 char ret[1024+16];
00459 ret[1023] = '\0';
00460 strcpy(ret,"");
00461
00462 if(content){
00463
00464 Symbol* targetSym = Symbol::findSymbol(symbols,symbolCount,content);
00465 if(targetSym){
00466 char* symName = xCoffSymbolTable->getSymbolName(targetSym);
00467
00468 DemangleWrapper wrapper;
00469 char* demangled = wrapper.demangle_combined(symName);
00470 if(strlen(demangled) > 1023)
00471 strncpy(ret,demangled,1023);
00472 else
00473 strcpy(ret,demangled);
00474
00475 uint64_t symVal = targetSym->GET(n_value);
00476 if(content != symVal){
00477 sprintf(ret+strlen(ret)," + %lld ",(content-symVal));
00478 }
00479
00480 free(symName);
00481 }
00482 }
00483 return strdup(ret);
00484 }
00485
00486 AddressIterator RawSection::getAddressIterator(){
00487 ASSERT(NULL && "FATAL : No Section other than Text and Data can have an address iterator");
00488 return AddressIterator::invalidIterator();
00489 }
00490
00491 AddressIterator TextSection::getAddressIterator(){
00492 return AddressIterator::newAddressIteratorWord(header->GET(s_vaddr),header->GET(s_size));
00493 }
00494
00495 AddressIterator DataSection::getAddressIterator(){
00496 if(getXCoffFile()->is64Bit())
00497 return AddressIterator::newAddressIteratorDouble(header->GET(s_vaddr),header->GET(s_size));
00498 return AddressIterator::newAddressIteratorWord(header->GET(s_vaddr),header->GET(s_size));
00499 }
00500
00501 void RawSection::displaySymbols(Symbol** symbols,uint32_t symbolCount){
00502
00503 header->print();
00504
00505 ASSERT(Symbol::isSorted(symbols,symbolCount));
00506
00507 int64_t beginIndex = -1;
00508 for(uint32_t i=0;i<symbolCount;i++){
00509 Symbol* sym = symbols[i];
00510 if(sym->GET(n_scnum) == header->getIndex()){
00511 beginIndex = i;
00512 break;
00513 }
00514 }
00515 if((beginIndex < 0) || (beginIndex >= symbolCount))
00516 return;
00517
00518 AddressIterator ait = getAddressIterator();
00519 if(ait.isInvalid())
00520 return;
00521
00522 while(ait.hasMore() && (beginIndex < symbolCount)){
00523
00524 Symbol* sym = symbols[beginIndex];
00525 if(sym->GET(n_scnum) != header->getIndex())
00526 break;
00527
00528 bool printOnlyFirst = false;
00529 uint64_t symVal = sym->GET(n_value);
00530 while(*ait < symVal){
00531 uint64_t content = readBytes(&ait);
00532 if(!printOnlyFirst){
00533 char* contentVisual = getContentVisually(symbols,symbolCount,content);
00534 PRINT_INFOR("\t%#18llx: %#18llx\t%s",*ait,content,contentVisual);
00535 free(contentVisual);
00536 printOnlyFirst = true;
00537 }
00538 ++ait;
00539 }
00540
00541 if(*ait == symVal){
00542 while(*ait == symVal){
00543 uint64_t symLength = xCoffSymbolTable->getSymbolLength(sym);
00544 char* symName = xCoffSymbolTable->getSymbolName(sym);
00545
00546 DemangleWrapper wrapper;
00547 char* demangled = wrapper.demangle_combined(symName);
00548
00549 if(symLength){
00550 PRINT_INFOR("<%s>: %lld",
00551 demangled,symLength);
00552 } else {
00553 PRINT_INFOR("<%s>:",demangled);
00554 }
00555 free(symName);
00556
00557 beginIndex++;
00558 if(beginIndex >= symbolCount)
00559 break;
00560
00561 sym = symbols[beginIndex];
00562 symVal = sym->GET(n_value);
00563 }
00564
00565 --beginIndex;
00566
00567 } else {
00568 uint64_t symLength = xCoffSymbolTable->getSymbolLength(sym);
00569 char* symName = xCoffSymbolTable->getSymbolName(sym);
00570
00571 DemangleWrapper wrapper;
00572 char* demangled = wrapper.demangle_combined(symName);
00573
00574 if(symLength){
00575 PRINT_INFOR("\t\t<%s>: %#18llx (UN_ALGN) %lld ",
00576 demangled,symVal,symLength);
00577 } else {
00578 PRINT_INFOR("\t\t<%s>: %#18llx (UN_ALGN)",demangled,symVal);
00579 }
00580 free(symName);
00581 }
00582
00583 beginIndex++;
00584 }
00585
00586 bool printOnlyFirst = false;
00587 while(ait.hasMore()){
00588 uint64_t content = readBytes(&ait);
00589 if(!printOnlyFirst){
00590 char* contentVisual = getContentVisually(symbols,symbolCount,content);
00591 PRINT_INFOR("\t%#18llx: %#18llx\t%s",*ait,content,contentVisual);
00592 free(contentVisual);
00593 printOnlyFirst = true;
00594 }
00595 ++ait;
00596 }
00597 }
00598
00599 void BSSSection::displaySymbols(Symbol** symbols,uint32_t symbolCount){
00600
00601 header->print();
00602
00603 ASSERT(Symbol::isSorted(symbols,symbolCount));
00604
00605 int64_t beginIndex = -1;
00606 for(uint32_t i=0;i<symbolCount;i++){
00607 Symbol* sym = symbols[i];
00608 if(sym->GET(n_scnum) == header->getIndex()){
00609 beginIndex = i;
00610 break;
00611 }
00612 }
00613 if((beginIndex < 0) || (beginIndex >= symbolCount))
00614 return;
00615
00616
00617 while(beginIndex < symbolCount){
00618
00619 Symbol* sym = symbols[beginIndex];
00620 if(sym->GET(n_scnum) != header->getIndex())
00621 break;
00622
00623 uint64_t symVal = sym->GET(n_value);
00624 uint64_t currSymVal = symVal;
00625
00626 while(currSymVal == symVal){
00627 uint64_t symLength = xCoffSymbolTable->getSymbolLength(sym);
00628 char* symName = xCoffSymbolTable->getSymbolName(sym);
00629
00630 DemangleWrapper wrapper;
00631 char* demangled = wrapper.demangle_combined(symName);
00632
00633 if(symLength){
00634 PRINT_INFOR("<%s>: %lld",demangled,symLength);
00635 } else {
00636 PRINT_INFOR("<%s>:",demangled);
00637 }
00638 free(symName);
00639 beginIndex++;
00640 if(beginIndex >= symbolCount)
00641 break;
00642
00643 sym = symbols[beginIndex];
00644 symVal = sym->GET(n_value);
00645 }
00646 PRINT_INFOR("\t%#18llx:",currSymVal);
00647 }
00648 }
00649
00650 void RawSection::findFunctions(){
00651 PRINT_DEBUG("For section %d there is no need to find functions", header->getIndex());
00652 }
00653 void RawSection::generateCFGs(){
00654 PRINT_DEBUG("For section %d there is no need to generate CFGs", header->getIndex());
00655 }
00656
00657 void RawSection::findMemoryFloatOps(){
00658 PRINT_DEBUG("For section %d there is no need to find memory operations", header->getIndex());
00659 }
00660
00661 void TextSection::findFunctions(){
00662
00663 PRINT_INFOR("For section %d finding the functions", header->getIndex());
00664
00665 uint32_t numberOfSymbols = xCoffSymbolTable->getNumberOfSymbols();
00666 Symbol** symbols = new Symbol*[numberOfSymbols];
00667 numberOfSymbols = xCoffSymbolTable->filterSortFuncSymbols(symbols,numberOfSymbols,header);
00668
00669 ASSERT(Symbol::isSorted(symbols,numberOfSymbols));
00670
00671 if(!numberOfSymbols)
00672 return;
00673
00674 uint32_t functionIndex = 0;
00675 Function** candidateFunctions = new Function*[numberOfSymbols];
00676
00677 Function* candidateFunction = NULL;
00678
00679 uint64_t prevBeginAddress = header->GET(s_vaddr);
00680 uint64_t prevEndAddress = header->GET(s_vaddr);
00681
00682 for(uint32_t lastSymIndex = 0;lastSymIndex<numberOfSymbols;lastSymIndex++){
00683
00684 Symbol* candidateSym = symbols[lastSymIndex];
00685 uint64_t candidateAddr = candidateSym->GET(n_value);
00686
00687 ASSERT(candidateSym->GET(n_scnum) == header->getIndex());
00688
00689 prevEndAddress = candidateAddr;
00690
00691 if(candidateFunction){
00692 candidateFunction->updateSize(prevEndAddress-prevBeginAddress);
00693 }
00694
00695 uint32_t howManySameAddr = 0;
00696 for(uint32_t i=(lastSymIndex+1);i<numberOfSymbols;i++){
00697 Symbol* nextSym = symbols[i];
00698 if(candidateAddr == nextSym->GET(n_value)){
00699 howManySameAddr++;
00700 } else {
00701 break;
00702 }
00703 }
00704
00705 candidateFunction = new Function(functionIndex,howManySameAddr+1,symbols+lastSymIndex,(RawSection*)this);
00706
00707 lastSymIndex += howManySameAddr;
00708
00709 prevBeginAddress = prevEndAddress;
00710
00711 uint8_t candidateStorageMapping = xCoffSymbolTable->getStorageMapping(candidateSym);
00712 if((candidateStorageMapping != XMC_RO) && (candidateStorageMapping != XMC_DB)) {
00713 candidateFunctions[functionIndex++] = candidateFunction;
00714 } else {
00715 delete candidateFunction;
00716 candidateFunction = NULL;
00717 }
00718 }
00719
00720 prevEndAddress = header->GET(s_vaddr) + header->GET(s_size);
00721
00722 if(candidateFunction){
00723 candidateFunction->updateSize(prevEndAddress-prevBeginAddress);
00724 }
00725
00726 PRINT_INFOR("Total number of functions is %d",functionIndex);
00727
00728 numOfFunctions = functionIndex;
00729 functions = new Function*[functionIndex];
00730 for(uint32_t i=0;i<functionIndex;i++){
00731 Function* function = candidateFunctions[i];
00732 function->updateInstructionSize();
00733 functions[i] = function;
00734 }
00735
00736 delete[] candidateFunctions;
00737 delete[] symbols;
00738 }
00739
00740 void TextSection::generateCFGs(){
00741 PRINT_INFOR("For section %d generating the CFGs", header->getIndex());
00742 for(uint32_t i=0;i<numOfFunctions;i++){
00743 Function* function = functions[i];
00744 function->generateCFG();
00745 }
00746 }
00747
00748 void TextSection::findMemoryFloatOps(){
00749 PRINT_INFOR("For section %d finding the memory operations", header->getIndex());
00750 for(uint32_t i=0;i<numOfFunctions;i++){
00751 Function* function = functions[i];
00752 function->findMemoryFloatOps();
00753 }
00754 }
00755
00756 uint32_t TextSection::getAllBlocks(BasicBlock** arr){
00757 uint32_t ret = 0;
00758 for(uint32_t i=0;i<numOfFunctions;i++){
00759 Function* function = functions[i];
00760 FlowGraph* cfg = function->getFlowGraph();
00761 uint32_t n = cfg->getAllBlocks(arr);
00762 arr += n;
00763 ret += n;
00764 }
00765 return ret;
00766 }