Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

/users/u3/mtikir/PMaCInstrumentor_v1601/src/ReWriting.C

Go to the documentation of this file.
00001 #include <Instruction.h>
00002 #include <FileHeader.h>
00003 #include <AOutHeader.h>
00004 #include <SectHeader.h>
00005 #include <LoaderSection.h>
00006 #include <RawSection.h>
00007 #include <SymbolTable.h>
00008 #include <StringTable.h>
00009 #include <RelocationTable.h>
00010 #include <LineInfoTable.h>
00011 #include <XCoffFile.h>
00012 #include <Iterator.h>
00013 #include <BinaryFile.h>
00014 #include <Function.h>
00015 #include <Generate.h>
00016 #include <Auxilary.h>
00017 
00018 /**** ----------------------------------------------------------------- ****/
00019 uint32_t FileHeader32::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00020     PRINT_DEBUG("FileHeader  Instrumentation ");
00021 
00022     FILHDR newEntry = entry;
00023     newEntry.f_symptr = xCoffGen->getSymbolTableOffsetForInst();
00024 
00025     memcpy(buffer,&newEntry,sizeInBytes); 
00026     return sizeInBytes;
00027 }
00028 uint32_t FileHeader64::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00029     PRINT_DEBUG("FileHeader  Instrumentation ");
00030 
00031     FILHDR_64 newEntry = entry;
00032     newEntry.f_symptr = xCoffGen->getSymbolTableOffsetForInst();
00033 
00034     memcpy(buffer,&newEntry,sizeInBytes); 
00035     return sizeInBytes;
00036 }
00037 /**** ----------------------------------------------------------------- ****/
00038 /**** ----------------------------------------------------------------- ****/
00039 uint32_t AOutHeader32::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00040     PRINT_DEBUG("AOutHeader  Instrumentation ");
00041 
00042     AOUTHDR newEntry = entry;
00043     newEntry.o_tsize = xCoffGen->getRawSectionSizeForInst(GET(o_sntext));
00044     newEntry.o_dsize = xCoffGen->getRawSectionSizeForInst(GET(o_sndata));
00045 
00046     memcpy(buffer,&newEntry,sizeInBytes); 
00047     return sizeInBytes;
00048 }
00049 uint32_t AOutHeader64::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00050     PRINT_DEBUG("AOutHeader  Instrumentation ");
00051 
00052     AOUTHDR_64 newEntry = entry;
00053     newEntry.o_tsize = xCoffGen->getRawSectionSizeForInst(GET(o_sntext));
00054     newEntry.o_dsize = xCoffGen->getRawSectionSizeForInst(GET(o_sndata));
00055 
00056     memcpy(buffer,&newEntry,sizeInBytes); 
00057     return sizeInBytes;
00058 }
00059 /**** ----------------------------------------------------------------- ****/
00060 /**** ----------------------------------------------------------------- ****/
00061 uint32_t SectHeader32::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00062     PRINT_DEBUG("SectHeader  Instrumentation ");
00063 
00064     SCNHDR newEntry = entry;
00065     if(!IS_SECT_TYPE(OVRFLO)){
00066         newEntry.s_size = xCoffGen->getRawSectionSizeForInst(getIndex());
00067         newEntry.s_scnptr = xCoffGen->getRawSectionOffsetForInst(getIndex());
00068         newEntry.s_relptr = xCoffGen->getRelocOffsetForInst(getIndex());
00069         newEntry.s_lnnoptr = xCoffGen->getLineInfoOffsetForInst(getIndex());
00070     } else {
00071         newEntry.s_relptr = xCoffGen->getRelocOffsetForInst(GET(s_nreloc));
00072         newEntry.s_lnnoptr = xCoffGen->getLineInfoOffsetForInst(GET(s_nreloc));
00073     }
00074 
00075     if(IS_SECT_TYPE(BSS)){
00076         newEntry.s_size = GET(s_size);
00077         newEntry.s_paddr = xCoffGen->getNewBSSSectionVAddressForInst();
00078         newEntry.s_vaddr = newEntry.s_paddr;
00079     }
00080 
00081     if(IS_SECT_TYPE(OVRFLO)){
00082         ASSERT(!newEntry.s_size && "Fatal: Size of OVRFLO has to be 0");
00083         ASSERT(!newEntry.s_scnptr && "Fatal: Sect pointer of OVRFLO has to be 0");
00084     }
00085     memcpy(buffer,&newEntry,sizeInBytes); 
00086 
00087     return sizeInBytes;
00088 }
00089 uint32_t SectHeader64::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00090     PRINT_DEBUG("SectHeader  Instrumentation ");
00091 
00092     SCNHDR_64 newEntry = entry;
00093     if(!IS_SECT_TYPE(OVRFLO)){
00094         newEntry.s_size = xCoffGen->getRawSectionSizeForInst(getIndex());
00095         newEntry.s_scnptr = xCoffGen->getRawSectionOffsetForInst(getIndex());
00096         newEntry.s_relptr = xCoffGen->getRelocOffsetForInst(getIndex());
00097         newEntry.s_lnnoptr = xCoffGen->getLineInfoOffsetForInst(getIndex());
00098     } else {
00099         newEntry.s_relptr = xCoffGen->getRelocOffsetForInst(GET(s_nreloc));
00100         newEntry.s_lnnoptr = xCoffGen->getLineInfoOffsetForInst(GET(s_nreloc));
00101     }
00102 
00103     if(IS_SECT_TYPE(BSS)){
00104         newEntry.s_size = GET(s_size);
00105         newEntry.s_paddr = xCoffGen->getNewBSSSectionVAddressForInst();
00106         newEntry.s_vaddr = newEntry.s_paddr;
00107     }
00108 
00109     if(IS_SECT_TYPE(OVRFLO)){
00110         ASSERT(!newEntry.s_size && "Fatal: Size of OVRFLO has to be 0");
00111         ASSERT(!newEntry.s_scnptr && "Fatal: Sect pointer of OVRFLO has to be 0");
00112     }
00113     memcpy(buffer,&newEntry,sizeInBytes); 
00114 
00115     return sizeInBytes;
00116 }
00117 /**** ----------------------------------------------------------------- ****/
00118 /**** ----------------------------------------------------------------- ****/
00119 uint32_t LineInfoTable::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00120     PRINT_DEBUG("LineInfoTable  Instrumentation ");
00121 
00122     uint32_t totalBytesWritten = 0;
00123 
00124     uint32_t currSize = Size__32_bit_LineInfoTable_Entry;
00125     if(getXCoffFile()->is64Bit()){
00126         currSize = Size__64_bit_LineInfoTable_Entry;
00127     }
00128 
00129     char* currPtr = buffer;
00130     for(uint32_t i=0;i<numOfLineInfos;i++){
00131         memcpy(currPtr,lineInfos[i]->charStream(),currSize);
00132         currPtr += currSize;
00133         totalBytesWritten += currSize;
00134     }
00135 
00136     ASSERT((sizeInBytes == totalBytesWritten) && 
00137            "FATAL: The bytes written should match the size of the linenumbers");
00138 
00139     return sizeInBytes;
00140 }
00141 /**** ----------------------------------------------------------------- ****/
00142 /**** ----------------------------------------------------------------- ****/
00143 uint32_t RelocationTable::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00144     PRINT_DEBUG("RelocationTable  Instrumentation ");
00145 
00146     uint32_t totalBytesWritten = 0;
00147 
00148     uint32_t currSize = Size__32_bit_RelocationTable_Entry;
00149     if(getXCoffFile()->is64Bit()){
00150         currSize = Size__64_bit_RelocationTable_Entry;
00151     }
00152 
00153     char* currPtr = buffer;
00154     for(uint32_t i=0;i<numOfRelocations;i++){
00155         memcpy(currPtr,relocations[i]->charStream(),currSize);
00156         currPtr += currSize;
00157         totalBytesWritten += currSize;
00158     }
00159 
00160     ASSERT((sizeInBytes == totalBytesWritten) && 
00161            "FATAL: The bytes written should match the size of the relocs");
00162 
00163     return sizeInBytes;
00164 }
00165 /**** ----------------------------------------------------------------- ****/
00166 /**** ----------------------------------------------------------------- ****/
00167 int compareOriginalFileOffsets(const void* arg1,const void* arg2){
00168     BaseGen* gen1 = *((BaseGen**)arg1);
00169     BaseGen* gen2 = *((BaseGen**)arg2);
00170     Base* base1 = gen1->getParsedBase();
00171     Base* base2 = gen2->getParsedBase();
00172     uint32_t vl1 = base1->getFileOffset();
00173     uint32_t vl2 = base2->getFileOffset();
00174 
00175     if(vl1 < vl2)
00176         return -1;
00177     if(vl1 > vl2)
00178         return 1;
00179     return 0;
00180 }
00181 
00182 BaseGen* binarySearch(BaseGen** objects,uint32_t objectCount,uint32_t value){
00183 
00184     if(!objectCount)
00185         return NULL;
00186 
00187     BaseGen* ret = NULL;
00188 
00189     uint32_t beginIndex = 0;
00190     uint32_t endIndex = objectCount;
00191    
00192     uint32_t midIndex = 0;
00193     BaseGen* midObject = NULL;
00194     uint32_t midVal = 0;
00195 
00196     while(beginIndex < endIndex){
00197         midIndex = (beginIndex+endIndex) / 2;
00198         midObject = objects[midIndex];
00199         midVal = midObject->getParsedBase()->getFileOffset();
00200 
00201         if(midVal > value){
00202             endIndex = midIndex;
00203         } else if(midVal < value){
00204             beginIndex = midIndex+1;
00205         } else {
00206             ret = midObject;
00207             break;
00208         }
00209     }
00210 
00211     if(!ret){
00212         if(value < midVal){
00213             if(midIndex) {
00214                 midIndex--;
00215                 ret = objects[midIndex];
00216             }
00217         } else {
00218             if(midIndex < (objectCount-1)){
00219                 ret = midObject;
00220             }
00221         }
00222     }
00223 
00224     return ret;
00225 }
00226 
00227 uint32_t SymbolTable::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00228 
00229     uint16_t bssIndex = getXCoffFile()->getBSSSectionIndex();
00230     uint64_t oldBssVAddr = getXCoffFile()->getBSSSectionVAddr();
00231     uint64_t newBssVAddr = xCoffGen->getNewBSSSectionVAddressForInst();
00232     PRINT_DEBUG("BSS section index is %d",bssIndex);
00233 
00234     uint32_t numberOfAllObjects = xCoffGen->getNumberOfAllObjects();
00235     BaseGen** allObjects = new BaseGen*[numberOfAllObjects];
00236     numberOfAllObjects = xCoffGen->getAllObjects(allObjects,numberOfAllObjects);
00237     qsort(allObjects,numberOfAllObjects,sizeof(BaseGen*),compareOriginalFileOffsets);
00238 
00239     PRINT_DEBUG("SymbolTable  Instrumentation ");
00240 
00241     uint32_t totalBytesWritten = 0;
00242 
00243     uint32_t currSize = Size__NN_bit_SymbolTable_Entry;
00244     SYMENT newEntry;
00245 
00246     char* currPtr = buffer;
00247     for(uint32_t i=0;i<numberOfSymbols;i++){
00248         char* ptr = symbols[i]->charStream();
00249 
00250         SymbolBase* symBase = symbols[i];
00251         if(!symBase->isAuxilary()){
00252 
00253             Symbol* sym = (Symbol*)symBase;  
00254             if(sym->IS_SYMB_TYPE(BINCL) ||
00255                sym->IS_SYMB_TYPE(EINCL)){
00256 
00257                 PRINT_DEBUG("To update C_BINCL or C_EINCL");
00258 
00259                 uint64_t value = sym->GET(n_value);
00260                 if(value){
00261                     BaseGen* gen = binarySearch(allObjects,numberOfAllObjects,value);
00262                     ASSERT(gen && gen->getParsedBase()->includesFileOffset(value));
00263                     value = gen->convertFileOffset(value);
00264                 }
00265 
00266                 sym->changeValueCopy(value,(char*)&newEntry);
00267                 ptr = (char*)&newEntry;
00268             } else if((sym->GET(n_scnum) == bssIndex) &&
00269                      (sym->IS_SYMB_TYPE(EXT) ||
00270                       sym->IS_SYMB_TYPE(HIDEXT) ||
00271                       sym->IS_SYMB_TYPE(WEAKEXT))){
00272 
00273                 PRINT_DEBUG("To update C_EXT or C_HIDEXT or C_WEAKEXT");
00274 
00275                 uint64_t value = sym->GET(n_value);
00276                 if(value){
00277                     PRINT_DEBUG("Changing %#llx with %#llx",value,(value-oldBssVAddr) + newBssVAddr);
00278                     value = (value-oldBssVAddr) + newBssVAddr;
00279                 }
00280                 sym->changeValueCopy(value,(char*)&newEntry);
00281                 ptr = (char*)&newEntry;
00282             }
00283 
00284         } else if(symBase->isAuxilary()){
00285             Auxilary* aux = (Auxilary*)symBase;
00286             if(aux->getAuxilaryType() == Type__Auxilary_Symbol_Function){
00287 
00288                 PRINT_DEBUG("To update AUX Function");
00289 
00290                 uint64_t lnnoptr = aux->GET_A(x_lnnoptr,x_fcn);
00291                 if(lnnoptr){
00292                     BaseGen* gen = binarySearch(allObjects,numberOfAllObjects,lnnoptr);
00293                     ASSERT(gen && gen->getParsedBase()->includesFileOffset(lnnoptr));
00294                     lnnoptr = gen->convertFileOffset(lnnoptr);
00295                 }
00296 
00297                 uint32_t exptr = 0;
00298                 if(!getXCoffFile()->is64Bit()){
00299                     exptr = aux->GET_A(x_exptr,x_fcn);
00300                     if(exptr){
00301                         gen = binarySearch(allObjects,numberOfAllObjects,exptr);
00302                         ASSERT(gen && gen->getParsedBase()->includesFileOffset(exptr));
00303                         exptr = gen->convertFileOffset(exptr);
00304                     }
00305                 }
00306 
00307                 AuxilaryFunction* auxFun = (AuxilaryFunction*)aux;
00308                 auxFun->changeExptrLnnoptrCopy(exptr,lnnoptr,(char*)&newEntry);
00309                 ptr = (char*)&newEntry;
00310 
00311             } else if(aux->getAuxilaryType() == Type__Auxilary_Symbol_Exception){
00312 
00313                 PRINT_DEBUG("To update AUX EXCEPTION");
00314 
00315                 uint64_t exptr = aux->GET_A(x_exptr,x_except);
00316                 if(exptr){
00317                     BaseGen* gen = binarySearch(allObjects,numberOfAllObjects,exptr);
00318                     ASSERT(gen && gen->getParsedBase()->includesFileOffset(exptr));
00319                     exptr = gen->convertFileOffset(exptr);
00320                 }
00321 
00322                 AuxilaryException* auxExp = (AuxilaryException*)aux;
00323                 auxExp->changeExptrCopy(exptr,(char*)&newEntry);
00324                 ptr = (char*)&newEntry;
00325             }
00326         }
00327 
00328         memcpy(currPtr,ptr,currSize);
00329         currPtr += currSize;
00330         totalBytesWritten += currSize;
00331     }
00332 
00333     ASSERT((sizeInBytes == totalBytesWritten) &&
00334            "FATAL: The bytes written should match the size of the linenumbers");
00335 
00336     delete[] allObjects;
00337     return sizeInBytes;
00338 }
00339 /**** ----------------------------------------------------------------- ****/
00340 /**** ----------------------------------------------------------------- ****/
00341 uint32_t StringTable::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00342     PRINT_DEBUG("StringTable  Instrumentation ");
00343     memcpy(buffer,stringTablePtr,sizeInBytes);
00344     return sizeInBytes;
00345 }
00346 /**** ----------------------------------------------------------------- ****/
00347 
00348 /**** ----------------------------------------------------------------- ****/
00349 uint32_t RawSection::getInstrumentationSize(XCoffFileGen* xCoffGen){
00350     if(getRawDataPtr())
00351         return sizeInBytes;
00352     return 0;
00353 }
00354 uint32_t RawSection::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00355     if(getRawDataPtr()){
00356         PRINT_DEBUG("RawSection  Instrumentation ");
00357         memcpy(buffer,getRawDataPtr(),sizeInBytes); 
00358         return sizeInBytes;
00359     }
00360     PRINT_DEBUG("RawSection  Instrumentation  DO NOTHING");
00361     return 0;
00362 }
00363 uint32_t ExceptionSection::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00364     PRINT_DEBUG("ExceptionSection  Instrumentation ");
00365 
00366     uint32_t totalBytesWritten = 0;
00367 
00368     uint32_t currSize = Size__32_bit_ExceptionTable_Entry;
00369     if(getXCoffFile()->is64Bit()){
00370         currSize = Size__64_bit_ExceptionTable_Entry;
00371     }
00372 
00373     char* currPtr = buffer;
00374     for(uint32_t i=0;i<numberOfExceptions;i++){
00375         memcpy(currPtr,exceptions[i]->charStream(),currSize);
00376         currPtr += currSize;
00377         totalBytesWritten += currSize;
00378     }
00379 
00380     ASSERT((sizeInBytes == totalBytesWritten) &&
00381            "FATAL: The bytes written should match the size of the exceptions");
00382 
00383     return sizeInBytes;
00384 }
00385 /**** ----------------------------------------------------------------- ****/
00386 /*********************************************************************************/
00387 uint32_t LoaderSection::getInstrumentationSize(XCoffFileGen* xCoffGen){
00388 
00389     uint32_t ret = sizeInBytes;
00390 
00391     uint32_t numOfSharedLibs = xCoffGen->getNumOfSharedLibFuncs();
00392     if(numOfSharedLibs){
00393         uint32_t oneSymbolSize = Size__32_bit_Loader_Section_Symbol;
00394         uint32_t oneRelocSize  = Size__32_bit_Loader_Section_Relocation;
00395         if(getXCoffFile()->is64Bit()){
00396             oneSymbolSize = Size__64_bit_Loader_Section_Symbol;
00397             oneRelocSize  = Size__64_bit_Loader_Section_Relocation;
00398         }
00399 
00400         ret += (oneSymbolSize * numOfSharedLibs);
00401         ret += (oneRelocSize * numOfSharedLibs);
00402         char* impidpath = NULL;
00403         char* impidbase = NULL;
00404         char* impidmem = NULL;
00405         xCoffGen->getSharedLibraryPathAndObj(&impidpath,&impidbase,&impidmem);
00406         ret += (strlen(impidpath) + strlen(impidbase) + strlen(impidmem) + 3);
00407         for(uint32_t i=0;i<numOfSharedLibs;i++){
00408             char* newSymName = xCoffGen->getSharedLibFuncName(i);
00409             ret += (sizeof(uint16_t) + strlen(newSymName) + 1);
00410         }
00411     }
00412 
00413     return ret;
00414 }
00415 uint32_t LoaderSection::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen){
00416     PRINT_DEBUG("LoaderSection Frequency Instrumentation");
00417 
00418     uint32_t numOfSharedLibs = xCoffGen->getNumOfSharedLibFuncs();
00419 
00420     uint32_t oneHeaderSize = Size__32_bit_Loader_Section_Header;
00421     uint32_t oneSymbolSize = Size__32_bit_Loader_Section_Symbol;
00422     uint32_t oneRelocSize  = Size__32_bit_Loader_Section_Relocation;
00423     if (getXCoffFile()->is64Bit()){
00424         oneHeaderSize = Size__64_bit_Loader_Section_Header;
00425         oneSymbolSize = Size__64_bit_Loader_Section_Symbol;
00426         oneRelocSize  = Size__64_bit_Loader_Section_Relocation;
00427     }
00428 
00429     uint32_t totalBytesWritten = 0;
00430     char* currPtr = buffer;
00431 
00432     currPtr += oneHeaderSize;
00433     totalBytesWritten += oneHeaderSize;
00434 
00435     uint32_t newSymTableOffset = totalBytesWritten;
00436     uint32_t newSymTableSize = 0;
00437 
00438     for (uint32_t i = 0; i < numberOfSymbols; i++){
00439         memcpy(currPtr,symbolTable[i+IMPLICIT_SYM_COUNT]->charStream(),oneSymbolSize);
00440         currPtr += oneSymbolSize;
00441         totalBytesWritten += oneSymbolSize;
00442         newSymTableSize += oneSymbolSize;
00443     }
00444 
00445     for (uint32_t i = 0; i < numOfSharedLibs; i++){
00446         uint32_t newSymOff = stringTable->getStringTableSize();
00447         for(uint32_t j=0;j<i;j++){
00448             char* newSymName = xCoffGen->getSharedLibFuncName(j);
00449             newSymOff += (sizeof(uint16_t) + strlen(newSymName) + 1);
00450         }
00451         newSymOff += sizeof(uint16_t);
00452 
00453         LSSymbol* newSymbol = LSSymbol::newSymbol(getXCoffFile()->is64Bit(),
00454                                                    newSymOff,fileNameTable->getFileNameEntryCount());
00455         memcpy(currPtr,newSymbol->charStream(),oneSymbolSize);
00456         delete newSymbol;
00457 
00458         currPtr += oneSymbolSize;
00459         totalBytesWritten += oneSymbolSize;
00460         newSymTableSize += oneSymbolSize;
00461     }
00462 
00463     uint32_t newRelocTableOffset = totalBytesWritten;
00464     uint32_t newRelocTableSize = 0;
00465 
00466     for (uint32_t i = 0; i < numberOfRelocations; i++){
00467         memcpy(currPtr,relocationTable[i]->charStream(),oneRelocSize);
00468         currPtr += oneRelocSize;
00469         totalBytesWritten += oneRelocSize;
00470         newRelocTableSize += oneRelocSize;
00471     }
00472     for (uint32_t i = 0; i < numOfSharedLibs; i++){
00473         uint64_t newSymAddr = xCoffGen->getSharedLibFuncAddrLocation(i);
00474         LSRelocation* newReloc = LSRelocation::newRelocation(getXCoffFile()->is64Bit(),
00475                                                     newSymAddr,
00476                                                     numberOfSymbols + IMPLICIT_SYM_COUNT + i,
00477                                                     getXCoffFile()->getDataSectionIndex());
00478         memcpy(currPtr,newReloc->charStream(),oneRelocSize);
00479         delete newReloc;
00480         currPtr += oneRelocSize;
00481         totalBytesWritten += oneRelocSize;
00482         newRelocTableSize += oneRelocSize;
00483     }
00484 
00485     uint32_t newFileNameOffset = totalBytesWritten;
00486     uint32_t newFileNameSize = 0;
00487 
00488     uint32_t currSize = fileNameTable->getFileNameTableSize();
00489     memcpy(currPtr,fileNameTable->getFileNameTablePtr(),currSize);
00490     currPtr += currSize;
00491     totalBytesWritten += currSize;
00492     newFileNameSize += currSize;
00493     if (numOfSharedLibs){
00494         char* impidpath = NULL;
00495         char* impidbase = NULL;
00496         char* impidmem = NULL;
00497         xCoffGen->getSharedLibraryPathAndObj(&impidpath,&impidbase,&impidmem);
00498         ASSERT(impidpath && impidbase && impidmem);
00499         
00500         uint32_t ns = 0;
00501         memcpy(currPtr,impidpath,strlen(impidpath)+1);
00502         ns += strlen(impidpath) + 1;
00503         memcpy(currPtr + ns,impidbase,strlen(impidbase)+1);
00504         ns += strlen(impidbase) + 1;
00505         memcpy(currPtr + ns ,impidmem,strlen(impidmem)+1);
00506         ns += strlen(impidmem) + 1;
00507 
00508         currPtr += ns;
00509         totalBytesWritten += ns;
00510         newFileNameSize += ns;
00511     }
00512 
00513     uint32_t newStringTableOffset = totalBytesWritten;
00514     uint32_t newStringTableSize = 0;
00515 
00516     currSize = stringTable->getStringTableSize();
00517     memcpy(currPtr,stringTable->getStringTablePtr(),currSize);
00518     currPtr += currSize;
00519     totalBytesWritten += currSize;
00520     newStringTableSize += currSize;
00521 
00522     if (numOfSharedLibs){
00523         for(uint32_t i=0;i<numOfSharedLibs;i++){
00524             char* newSymName = xCoffGen->getSharedLibFuncName(i);
00525             uint32_t ns = 0;
00526             uint16_t symsz = strlen(newSymName) + 1;
00527             memcpy(currPtr,&symsz,sizeof(uint16_t));
00528             ns += sizeof(uint16_t);
00529             memcpy(currPtr+ns,newSymName,symsz);
00530             ns += symsz;
00531             currPtr += ns;
00532             totalBytesWritten += ns;
00533             newStringTableSize += ns;
00534         }
00535     }
00536 
00537     LSHeader* newHeader = LSHeader::newHeader(header,getXCoffFile()->is64Bit(),
00538                                               numberOfSymbols+numOfSharedLibs,
00539                                               numberOfRelocations + numOfSharedLibs,
00540                                               newFileNameSize,
00541                                               fileNameTable->getFileNameEntryCount() + (numOfSharedLibs ? 1 : 0),
00542                                               newFileNameOffset,newStringTableSize,newStringTableOffset,
00543                                               newSymTableOffset,newRelocTableOffset);
00544     memcpy(buffer,newHeader->charStream(),oneHeaderSize);
00545     delete newHeader;
00546 
00547     return totalBytesWritten;
00548 }
00549 
00550 uint32_t TextSection::getInstrumentationSize(XCoffFileGen* xCoffGen){
00551 
00552     uint32_t bufferWordPtr = sizeInBytes;
00553 
00554     if(xCoffGen->getNumberOfInstPoints(this) || xCoffGen->byteCountForSharedLibFuncWrappers()){
00555 
00556         bufferWordPtr = nextAlignAddressWord(bufferWordPtr);
00557 
00558         bufferWordPtr += xCoffGen->byteCountForSharedLibFuncWrappers();
00559 
00560         uint32_t instCount = xCoffGen->getNumberOfInstPoints(this);
00561 
00562         uint64_t textSectionStartAddr = getSectHeader()->GET(s_vaddr);
00563         for(uint32_t i = 0;i<instCount;i++){
00564             bufferWordPtr += xCoffGen->byteCountForInst(i,textSectionStartAddr + bufferWordPtr,this);
00565         }
00566     }
00567     return bufferWordPtr;
00568 }
00569 
00570 uint32_t TextSection::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen){
00571 
00572     uint32_t bufferWordPtr = sizeInBytes;
00573 
00574     PRINT_DEBUG("TextSection Instrumentation");
00575     memcpy(buffer,getRawDataPtr(),bufferWordPtr);
00576 
00577     if (xCoffGen->getNumberOfInstPoints(this) || xCoffGen->byteCountForSharedLibFuncWrappers()){
00578         bufferWordPtr = nextAlignAddressWord(bufferWordPtr);
00579 
00580         xCoffGen->generateStubForAllLibraryCalls(bufferWordPtr,gen);
00581         bufferWordPtr += xCoffGen->byteCountForSharedLibFuncWrappers();
00582 
00583         uint32_t instCount = xCoffGen->getNumberOfInstPoints(this);
00584 
00585         uint64_t textSectionStartAddr = getSectHeader()->GET(s_vaddr);
00586         for (uint32_t i = 0; i < instCount; i++){
00587             bufferWordPtr += xCoffGen->generateCodeForInst(i,textSectionStartAddr + bufferWordPtr,this,
00588                                                            gen,bufferWordPtr);
00589         }
00590     }
00591 
00592     DEBUG_MORE(
00593         Instruction::print(buffer,getSectHeader()->GET(s_vaddr),bufferWordPtr,true);
00594     );
00595 
00596     PRINT_DEBUG("Using bufferwordptr %d gensize %d", bufferWordPtr, gen->getSizeInBytes());
00597     ASSERT(bufferWordPtr == gen->getSizeInBytes());
00598 
00599     return bufferWordPtr;
00600 }
00601 
00602 uint32_t DataSection::getInstrumentationSize(XCoffFileGen* xCoffGen){
00603     return (sizeInBytes + xCoffGen->getExtendedDataSize());
00604 }
00605 uint32_t DataSection::instrument(char* buffer,XCoffFileGen* xCoffGen,BaseGen* gen) {
00606     PRINT_DEBUG("DataSection  Instrumentation ");
00607     memcpy(buffer,getRawDataPtr(),sizeInBytes);
00608     xCoffGen->initializeReservedData(this,gen);
00609     return getInstrumentationSize(xCoffGen);
00610 }
00611 /*********************************************************************************/

Generated on Mon Jan 28 11:08:32 2008 for PMaCInstrumentor by doxygen 1.3.5