Object-nt.h

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 /************************************************************************
00032  * Windows NT/2000 object files.
00033  ************************************************************************/
00034 
00035 
00036 
00037 
00038 #if !defined(_Object_nt_h_)
00039 #define _Object_nt_h_
00040 
00041 
00042 
00043 /************************************************************************
00044  * header files.
00045 ************************************************************************/
00046 
00047 #include "common/h/Types.h"
00048 #include <vector>
00049 #include <string>
00050 #include <algorithm>
00051 #include "symtabAPI/h/symutil.h"
00052 
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055 #define WIN32_LEAN_AND_MEAN
00056 #include <windows.h>
00057 #include <assert.h>
00058 
00059 #if !defined(__out_ecount_opt)
00060 #define __out_ecount_opt(UNUSED)
00061 #endif
00062 #include <dbghelp.h>
00063 
00064 namespace Dyninst{
00065 namespace SymtabAPI{
00066 
00067 class ExceptionBlock;
00068 
00069 /************************************************************************
00070  * class Object
00071 ************************************************************************/
00072 
00073 class Object : public AObject
00074 {
00075     friend class Symtab;
00076  public:
00077     class intSymbol
00078     {
00079     private:
00080             std::string name;
00081             DWORD64 addr;
00082             DWORD type;
00083             DWORD linkage;
00084             DWORD size;
00085             Region *region;
00086 
00087     public:
00088             intSymbol( std::string _name,
00089                        DWORD64 _addr,
00090                        DWORD _type,
00091                        DWORD _linkage,
00092                        DWORD _size,
00093                        Region *_region)
00094                 : name(_name),
00095                 addr(_addr),
00096                 type(_type),
00097                 linkage(_linkage),
00098                 size(_size),
00099                 region(_region)
00100         {}
00101 
00102             std::string GetName( void ) const          { return name; }
00103             DWORD64 GetAddr( void ) const           { return addr; }
00104             DWORD   GetSize( void ) const               { return size; }
00105             DWORD   GetType( void ) const               { return type; }
00106             DWORD   GetLinkage( void ) const            { return linkage; }
00107             Region *GetRegion( void ) const        { return region; }
00108             void    SetSize( DWORD cb )                 { size = cb; }
00109 
00110             void DefineSymbol( dyn_hash_map<std::string, std::vector< Symbol *> >& syms,
00111                                std::map<Symbol *, std::string> &symsToMods,
00112                                const std::string& modName ) const;
00113     };
00114 
00115     class File
00116     {
00117     private:
00118             std::string name;
00119             std::vector<intSymbol*> syms;
00120 
00121     public:
00122             File( std::string _name = "" )
00123                 : name(_name)
00124         {}
00125 
00126             void AddSymbol( intSymbol* pSym )
00127         {
00128                     syms.push_back( pSym );
00129         }
00130 
00131             void DefineSymbols( dyn_hash_map<std::string, std::vector< Symbol *> >& syms,
00132                                 std::map<Symbol *, std::string> &symsToMods,
00133                                 const std::string& modName ) const;
00134             std::string GetName( void ) const       { return name; }
00135             const std::vector<intSymbol*>& GetSymbols( void )   const       { return syms; }
00136     };
00137 
00138     class Module
00139     {
00140     private:
00141             std::string name;
00142             DWORD64 baseAddr;
00143             DWORD64 extent;
00144             bool isDll;
00145 
00146             std::vector<File *> files;
00147             File* defFile;
00148 
00149             void PatchSymbolSizes( const Object* obj,
00150                                    const std::vector<intSymbol*>& allSyms ) const;
00151 
00152     public:
00153             Module( std::string name,
00154                     DWORD64 baseAddr,
00155                     DWORD64 extent = 0 );
00156 
00157             File* GetDefaultFile( void )            { return defFile; }
00158             File* FindFile( std::string name );
00159             void AddFile( File* pFile )             { files.push_back( pFile ); }
00160 
00161             void DefineSymbols( const Object* obj,
00162                                 dyn_hash_map<std::string, std::vector< Symbol *> > & syms,
00163                                 std::map<Symbol *, std::string> &symsToMods ) const;
00164             void BuildSymbolMap( const Object* obj ) const; 
00165 
00166             std::string GetName( void ) const            { return name; }
00167             bool IsDll( void ) const                { return isDll; }
00168             void SetIsDll( bool v )                 { isDll = v; }
00169     };
00170 
00171  private:
00172     Module* curModule;
00173 
00174  public:
00175     SYMTAB_EXPORT Object(MappedFile *, bool defensive, 
00176                          void (*)(const char *) = log_msg, bool alloc_syms = true);
00177     SYMTAB_EXPORT Object(){};
00178   
00179     SYMTAB_EXPORT virtual ~Object( void );
00180 
00181     SYMTAB_EXPORT bool isForwarded( Offset addr );
00182     SYMTAB_EXPORT bool isEEL() const { return false; }
00183     SYMTAB_EXPORT bool isText( const Offset addr ) const; 
00184     SYMTAB_EXPORT Offset get_base_addr() const { return (Offset)mf->base_addr();} 
00185     SYMTAB_EXPORT Module* GetCurrentModule( void )                  { return curModule; }
00186    
00187     SYMTAB_EXPORT bool getCatchBlock(ExceptionBlock &b, Offset addr, unsigned size = 0) const;
00188     SYMTAB_EXPORT unsigned int GetTextSectionId( void ) const         { return textSectionId;}
00189     SYMTAB_EXPORT PIMAGE_NT_HEADERS   GetImageHeader( void ) const    { return peHdr; }
00190     SYMTAB_EXPORT PVOID GetMapAddr( void ) const                      { return mf->base_addr(); }
00191     SYMTAB_EXPORT Offset getEntryPoint( void ) const                { if (peHdr) return peHdr->OptionalHeader.AddressOfEntryPoint; return 0;}
00192     //+ desc.loadAddr(); } //laodAddr is always zero in our fake address space.
00193     // TODO. Change these later.
00194     SYMTAB_EXPORT Offset getLoadAddress() const { return imageBase; }
00195     SYMTAB_EXPORT Offset getEntryAddress() const { return getEntryPoint(); }
00196     SYMTAB_EXPORT Offset getBaseAddress() const { return get_base_addr(); }
00197     SYMTAB_EXPORT Offset getTOCoffset(Offset /*ignored*/) const { return 0; }
00198     SYMTAB_EXPORT ObjectType objType() const;
00199     SYMTAB_EXPORT const char *interpreter_name() const { return NULL; }
00200     SYMTAB_EXPORT dyn_hash_map <std::string, LineInformation> &getLineInfo();
00201     SYMTAB_EXPORT void parseTypeInfo(Symtab *obj);
00202     SYMTAB_EXPORT virtual Dyninst::Architecture getArch();   
00203     SYMTAB_EXPORT void    ParseGlobalSymbol(PSYMBOL_INFO pSymInfo);
00204     SYMTAB_EXPORT const std::vector<Offset> &getPossibleMains() const   { return possible_mains; }
00205     SYMTAB_EXPORT void getModuleLanguageInfo(dyn_hash_map<std::string, supportedLanguages> *mod_langs);
00206     SYMTAB_EXPORT bool emitDriver(Symtab *obj, std::string fName, 
00207                                     std::vector<Symbol *>&allSymbols, unsigned flag);
00208     SYMTAB_EXPORT unsigned int getSecAlign() const {return SecAlignment;}
00209     SYMTAB_EXPORT void insertPrereqLibrary(std::string lib);
00210     virtual char *mem_image() const 
00211     {
00212         assert(mf);
00213         return (char *)mf->base_addr();
00214     }
00215     void setTrapHeader(Offset ptr);
00216     Offset trapHeader();
00217 
00218     SYMTAB_EXPORT DWORD ImageOffset2SectionNum(DWORD dwRO);
00219     SYMTAB_EXPORT PIMAGE_SECTION_HEADER ImageOffset2Section(DWORD dwRO);
00220     SYMTAB_EXPORT PIMAGE_SECTION_HEADER ImageRVA2Section(DWORD dwRVA);
00221     SYMTAB_EXPORT DWORD RVA2Offset(DWORD dwRVA);
00222     SYMTAB_EXPORT DWORD Offset2RVA(DWORD dwRO);
00223     SYMTAB_EXPORT void addReference(Offset, std::string, std::string);
00224     SYMTAB_EXPORT std::map<std::string, std::map<Offset, std::string> > & getRefs() { return ref; }
00225 
00226     std::vector<std::pair<std::string, IMAGE_IMPORT_DESCRIPTOR> > & getImportDescriptorTable();
00227     std::map<std::string, std::map<std::string, WORD> > & getHintNameTable();
00228     PIMAGE_NT_HEADERS getPEHdr() { return peHdr; }
00229     void setTOCoffset(Offset) {};
00230 private:
00231     SYMTAB_EXPORT void    ParseSymbolInfo( bool );
00232     SYMTAB_EXPORT void    parseFileLineInfo(Symtab *, dyn_hash_map<std::string, LineInformation> &);
00233     SYMTAB_EXPORT void    FindInterestingSections( bool, bool );
00234     Region *          findEnclosingRegion(const Offset where);
00235     void AddTLSFunctions();
00236 
00237     Offset baseAddr;     // location of this object in mutatee address space
00238 
00239     Offset imageBase; // Virtual Address at which the binary is loaded in its address space
00240 
00241     PIMAGE_NT_HEADERS   peHdr;      // PE file headers
00242     Offset trapHeaderPtr_; // address & size
00243     unsigned int SecAlignment; //Section Alignment
00244 
00245     //structure of import table
00246     std::vector<std::pair<std::string, IMAGE_IMPORT_DESCRIPTOR> > idt_;
00247     std::map<std::string, std::map<std::string, WORD> > hnt_;
00248 
00249     //external reference info
00250    std::map<std::string,std::map<Offset, std::string> > ref;
00251 
00252     unsigned int textSectionId;     // id of .text segment (section)
00253     unsigned int dataSectionId;     // id of .data segment (section)
00254    
00255     HANDLE  hProc;                  // Process Handle
00256     std::vector<Offset> possible_mains; //Addresses of functions that may be main
00257 };
00258 
00259 // In recent versions of the Platform SDK, the macros naming
00260 // the value for the Flags field of the SYMBOL_INFO struct have
00261 // names with a SYMFLAG_ prefix.  Older Platform SDKs, including
00262 // the version that shipped with the Visual Studio .NET product
00263 // (i.e., VC7), use names for these macros with a SYMF_ prefix.
00264 // If we find we are using these older headers, we define the
00265 // new-style names.
00266 #if !defined(SYMFLAG_FUNCTION)
00267 #  define SYMFLAG_FUNCTION      SYMF_FUNCTION
00268 #  define SYMFLAG_LOCAL         SYMF_LOCAL
00269 #  define SYMFLAG_PARAMETER     SYMF_PARAMETER
00270 #  define SYMFLAG_EXPORT        SYMF_EXPORT
00271 #endif // !defined(SYMFLAG_FUNCTION)
00272 
00273 }//namespace Dyninst
00274 }//namespace SymtabAPI
00275 
00276 #endif /* !defined(_Object_nt_h_) */
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1