void report_image_structure(IMG img, int depth){ UINT32 I1 = IMG_Id(img); string I2 = IMG_Name(img); int I3 = IMG_IsMainExecutable( img ); int I4 = IMG_IsStaticExecutable( img ); ADDRINT I5 = IMG_LoadOffset(img); ADDRINT I6 = IMG_LowAddress(img); ADDRINT I7 = IMG_HighAddress(img); ADDRINT I8 = IMG_LoadOffset(img); ADDRINT I9 = IMG_StartAddress(img); ADDRINT I10 = IMG_Entry(img); USIZE I11 = IMG_SizeMapped( img ); IMG_TYPE I12 = IMG_Type(img); char I13[128]; int k ; for ( k = 0; k< depth ; k ++ ) TraceFile << "\t" ; TraceFile << "<IMAGE-LOAD>" << " I1:" << I1 << " I2:" << I2 << " I3:" << I3 << " I4:" << I4 << " I5:" << hex<< I5 << " I6:"<< I6 << " I7:" << I7 << " I8:" << I8 << " I9:"<< I9 << " I10:"<< I10 << " I11:" << I11 ; switch ( I12 ){ case IMG_TYPE_STATIC: strcpy( I13 ,"static" ); break; case IMG_TYPE_SHARED: strcpy( I13 ,"shared" ); break; case IMG_TYPE_INVALID: strcpy( I13 ,"invalid" ); break; case IMG_TYPE_LAST: strcpy( I13 ,"last" ); break; case IMG_TYPE_SHAREDLIB: strcpy( I13 ,"shared-lib" ); break; case IMG_TYPE_RELOCATABLE: strcpy( I13 ,"relocatable" ); break; case IMG_TYPE_DYNAMIC_CODE: strcpy( I13 ,"dynamic-code" ); break; default: strcpy( I13 ,"UNKNOWN" ); break; } TraceFile << " I12:" << I12 << " I13:" << I13 << endl; for( SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec) ){ report_section_structure( sec, depth + 1 ); } /* */ for (SYM sym = IMG_RegsymHead(img); SYM_Valid(sym); sym = SYM_Next(sym)){ report_sym_structure( sym, depth +1 ); } for ( k = 0; k< depth ; k ++ ) TraceFile << "\t" ; TraceFile << "</IMAGE-LOAD>" << endl; }
// Writes the image load event to the file "imgLog" static void LogImageLoad(IMG img, void *v) { // Ensure that we can't overflow when we read it back. ASSERTX (IMG_Name(img).length() < MAX_FILENAME_LENGTH); ADDRESS_RANGE range = FindImageTextMargin(img); // Log the data needed to restore it fprintf(imgLog, "L '%s' %llx %lx %llx %d \n", IMG_Name(img).c_str(), (unsigned long long)range._low, (long)(range._high - range._low), (unsigned long long)IMG_LoadOffset(img), (int)IMG_IsMainExecutable(img)); for (SEC sec = IMG_SecHead(img); SEC_Valid(sec); sec = SEC_Next(sec)) { if (SEC_Type(sec) != SEC_TYPE_EXEC) { continue; } for (RTN rtn=SEC_RtnHead(sec); RTN_Valid(rtn); rtn = RTN_Next(rtn)) { if (RTN_IsArtificial(rtn)) { continue; } fprintf(imgLog, "\t'%s' %llx\n", RTN_Name(rtn).c_str(), (unsigned long long)RTN_Address(rtn)); } } fprintf(imgLog, "%s", END_RTN_LIST); }
/* Image instrumentation */ static void IMG_Instrumentation(IMG img, void *v) { /* Lock / Unlock the Analysis from a Entry point */ if (tracer::pintool::options::startAnalysisFromEntry) { tracer::pintool::options::startAnalysisFromEntry = false; /* IMG_LoadOffset(img) + IMG_Entry(img) for PIE binaries (see #524) */ tracer::pintool::options::startAnalysisFromAddress.insert(IMG_LoadOffset(img) + IMG_Entry(img)); } /* Lock / Unlock the Analysis from a symbol */ if (tracer::pintool::options::startAnalysisFromSymbol != nullptr){ RTN targetRTN = RTN_FindByName(img, tracer::pintool::options::startAnalysisFromSymbol); if (RTN_Valid(targetRTN)) { RTN_Open(targetRTN); RTN_InsertCall(targetRTN, IPOINT_BEFORE, (AFUNPTR) toggleWrapper, IARG_BOOL, true, IARG_END); RTN_InsertCall(targetRTN, IPOINT_AFTER, (AFUNPTR) toggleWrapper, IARG_BOOL, false, IARG_END); RTN_Close(targetRTN); } } /* Callback on routine entry */ std::map<const char *, PyObject *>::iterator it; for (it = tracer::pintool::options::callbackRoutineEntry.begin(); it != tracer::pintool::options::callbackRoutineEntry.end(); it++) { RTN targetRTN = RTN_FindByName(img, it->first); if (RTN_Valid(targetRTN)){ RTN_Open(targetRTN); RTN_InsertCall(targetRTN, IPOINT_BEFORE, (AFUNPTR)callbackRoutineEntry, IARG_CONTEXT, IARG_THREAD_ID, IARG_PTR, it->second, IARG_END); RTN_Close(targetRTN); } } /* Callback on routine exit */ for (it = tracer::pintool::options::callbackRoutineExit.begin(); it != tracer::pintool::options::callbackRoutineExit.end(); it++) { RTN targetRTN = RTN_FindByName(img, it->first); if (RTN_Valid(targetRTN)){ RTN_Open(targetRTN); RTN_InsertCall(targetRTN, IPOINT_AFTER, (AFUNPTR)callbackRoutineExit, IARG_CONTEXT, IARG_THREAD_ID, IARG_PTR, it->second, IARG_END); RTN_Close(targetRTN); } } /* * Callback when a new image is loaded. * This callback must be called even outside the range analysis. */ if (IMG_Valid(img)) tracer::pintool::callbackImageLoad(img); }
// Save the image load event static void LogImageLoad(IMG img, void *v) { // Ensure that we can't overflow when we read it back. ASSERTX (IMG_Name(img).length() < MAX_FILENAME_LENGTH); // Log the data needed to restore it fprintf (imgLog, "L '%s' 0x%lx \n", IMG_Name(img).c_str(), (unsigned long)IMG_LoadOffset(img)); }
VOID ImageLoad (IMG img, VOID *v) { uint32_t id = IMG_Id (img); std::string iname = IMG_Name(img); if (id==1) // this is the first image, extract the path and the name of the executable { string ename, epath; MIAMIU::ExtractNameAndPath(iname, epath, ename); MIAMI::MiamiOptions *mo = MIAMI::mdriver.getProgramOptions(); mo->addExecutableName(ename); mo->addExecutablePath(epath); } // print info about the sections in this image, for debugging // comment out in production runs #if DEBUG_CFG_COUNTS DEBUG_CFG(4, cerr << "Image: " << iname << ", id " << id << hex << " load offser=0x" << IMG_LoadOffset(img) << ", low addr=0x" << IMG_LowAddress(img) << ", high addr=0x" << IMG_HighAddress(img) << ", start addr=0x" << IMG_StartAddress(img) << ", mapped size=0x" << IMG_SizeMapped(img) << dec << ", has the following sections:" << endl; for (SEC sec= IMG_SecHead(img) ; SEC_Valid(sec) ; sec = SEC_Next(sec)) { cerr << "Section " << SEC_Name(sec) << " of type " << SEC_Type(sec) << " at address 0x" << hex << SEC_Address(sec) << " of size 0x" << SEC_Size(sec) << dec << "/" << SEC_Size(sec) << " bytes:" << " valid? " << SEC_Valid(sec) << ", mapped? " << SEC_Mapped(sec) << ", executable? " << SEC_IsExecutable(sec) << ", readable? " << SEC_IsReadable(sec) << ", writable? " << SEC_IsWriteable(sec) << endl; } )
// Print the list of images currently loaded, with some information about each. static VOID PrintImageList() { for (IMG img= APP_ImgHead(); IMG_Valid(img); img = IMG_Next(img)) { ADDRESS_RANGE range = FindImageTextMargin(img); fprintf (trace, " L %-40s %2d [0x%llx:0x%llx] offset 0x%llx %4d RTNs\n", IMG_Name(img).c_str(), (int)IMG_Id(img), (unsigned long long)range._low, (unsigned long long)range._high, (unsigned long long)IMG_LoadOffset(img), CountImageRtns (img)); } }
VOID ImageLoad (IMG img, VOID *v) { printf ("loaded image %s lowAddr %p highAddr %p loadOffset %p\n", IMG_Name(img).c_str(), (void *)IMG_LowAddress(img), (void *)IMG_HighAddress(img), (void *)IMG_LoadOffset(img)); }
<< ", start addr=0x" << IMG_StartAddress(img) << ", mapped size=0x" << IMG_SizeMapped(img) << dec << ", has the following sections:" << endl; for (SEC sec= IMG_SecHead(img) ; SEC_Valid(sec) ; sec = SEC_Next(sec)) { cerr << "Section " << SEC_Name(sec) << " of type " << SEC_Type(sec) << " at address 0x" << hex << SEC_Address(sec) << " of size 0x" << SEC_Size(sec) << dec << "/" << SEC_Size(sec) << " bytes:" << " valid? " << SEC_Valid(sec) << ", mapped? " << SEC_Mapped(sec) << ", executable? " << SEC_IsExecutable(sec) << ", readable? " << SEC_IsReadable(sec) << ", writable? " << SEC_IsWriteable(sec) << endl; } ) #endif MIAMI::addrtype load_offset = IMG_LoadOffset(img); MIAMI::mdriver.LoadImage(id, iname, load_offset, IMG_LowAddress(img)-load_offset); } /* ===================================================================== */ VOID Fini (int code, VOID * v) { MIAMI::mdriver.Finalize(KnobOutputFile.Value()); } /* ===================================================================== */ int main (int argc, char *argv[])
// Print the list of images currently loaded, with some information about each. static VOID PrintImageList() { for (IMG img= APP_ImgHead(); IMG_Valid(img); img = IMG_Next(img)) { int nSecs; int nRtns; CountImageSecsAndRtns (img, &nSecs, &nRtns); fprintf (trace, " L %-40s %2d [0x%lx:0x%lx] offset 0x%lx %2d SECs %4d RTNs\n", IMG_Name(img).c_str(), IMG_Id(img), (unsigned long)IMG_LowAddress(img), (unsigned long)IMG_HighAddress(img), (unsigned long)IMG_LoadOffset(img), nSecs, nRtns); } }