Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LibSymbolInfo.cpp
Go to the documentation of this file.
1 //==========================================
2 // Matt Pietrek
3 // Microsoft Systems Journal, April 1998
4 // Program: LibDump
5 // FILE: LibSymbolInfo.CPP
6 //==========================================
7 
8 // LibSymbolInfo.cpp: implementation of the CLibSymbolInfo class.
9 //
11 
12 
13 #include "LibSymbolInfo.h"
14 
15 using namespace std;
16 
17 
18 #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD_PTR)(ptr) + (DWORD_PTR)(addValue))
19 
21 // CLibSymbolInfo
22 
24 {
25 }
26 
28 {
29 }
30 
31 //=============================================================================
32 // Function: DumpSymbols
33 //
34 // Parameters:
35 // LPTSTR lpszLibPathName - The library file path name
36 // CStdioFile* pFile - Address of the file in which to dump the symbols
37 //
38 // Description:
39 //
40 // Given a library file path name, the function dumps the symbol info into the file
41 // pointed to by pFile.
42 //=============================================================================
43 BOOL CLibSymbolInfo::DumpSymbols(LPTSTR lpszLibPathName, ostream& pFile)
44 {
45  if(lpszLibPathName == NULL || pFile.bad() ) {
46  assert(lpszLibPathName != NULL);
47  assert(pFile.good());
48  m_strErrorMsg.assign("NULL <lpszLibPathName> or Invalid file handle.");
49  return FALSE;
50  }
51 
52  if(!Dump(lpszLibPathName, pFile)) return FALSE;
53  return TRUE;
54 }
55 
56 //=============================================================================
57 // Function: Dump
58 //
59 // Parameters:
60 // As mentioned above.
61 //
62 // Description:
63 //
64 // Depending on the value specified in <m_bDump>/<m_bGrep> the routine dumps/greps
65 // the symbo info.
66 //=============================================================================
67 BOOL CLibSymbolInfo::Dump(LPTSTR lpszLibPathName, ostream& pFile)
68 {
69  string sBuff;
70  MEMORY_MAPPED_FILE libFile(lpszLibPathName);
71 
72  // Ensure that the file mapping worked
73  if( FALSE == libFile.IsValid() ) {
74  m_strErrorMsg = "Unable to access file ";
75  m_strErrorMsg+= lpszLibPathName;
76  return FALSE;
77  }
78  // All COFF libraries start with the string "!<arch>\n". Verify that this
79  // string is at the beginning of the mapped file
80 
81  PSTR pArchiveStartString = (PSTR)libFile.GetBase();
82 
83  if ( 0 != strncmp( pArchiveStartString, IMAGE_ARCHIVE_START,
84  IMAGE_ARCHIVE_START_SIZE ) ) {
85  m_strErrorMsg.assign("Not a valid COFF LIB file.");
86  return FALSE;
87  }
88 
89  // Point to the first archive member. This entry contains the LIB symbols,
90  // and immediately follows the archive start string ("!<arch>\n")
91  PIMAGE_ARCHIVE_MEMBER_HEADER pMbrHdr;
92  pMbrHdr = MakePtr( PIMAGE_ARCHIVE_MEMBER_HEADER, pArchiveStartString,
93  IMAGE_ARCHIVE_START_SIZE );
94 
95  // First DWORD after this member header is a symbol count
96  PDWORD pcbSymbols = (PDWORD)(pMbrHdr + 1); // Pointer math!
97 
98  // The symbol count is stored in big endian format, so adjust as
99  // appropriate for the target architecture
100  DWORD cSymbols = ConvertBigEndian( *pcbSymbols );
101 
102  // Following the symbol count is an array of offsets to archive members
103  // (essentially, embedded .OBJ files)
104  PDWORD pMemberOffsets = pcbSymbols + 1; // Pointer math!
105 
106  // Following the array of member offsets is an array of offsets to symbol
107  // names.
108  PSTR pszSymbolName = MakePtr( PSTR, pMemberOffsets, 4 * cSymbols );
109 
110  //
111  // Loop through every symbol in the first archive member
112  //
113  for ( unsigned i = 0; i < cSymbols; i++ )
114  {
115  DWORD offset;
116 
117  // The offsets to the archive member that contains the symbol is stored
118  // in big endian format, so convert it appropriately.
119  offset = ConvertBigEndian( *pMemberOffsets );
120 
121  // Call DisplayLibInfoForSymbol, which figures out what kind of symbol
122  // it is. The "IsRegularLibSymbol" filters out symbols that are
123  // internal to the linking process
124  if ( IsRegularLibSymbol( pszSymbolName ) ) {
125  string symbol(pszSymbolName);
126  if (IsFiltedSymbol(symbol) ) {
127  pFile << symbol << endl;
128  }
129  }
130  // Advanced to the next symbol offset and name. The MemberOffsets
131  // array has fixed length entries, while the symbol names are
132  // sequential null-terminated strings
133  pMemberOffsets++;
134  pszSymbolName += strlen(pszSymbolName) + 1;
135  }
136  return TRUE;
137 }
138 
139 //=============================================================================
140 // Filters out symbols that are internal to the linking process, and which
141 // the programmer never explicitly sees.
142 //=============================================================================
143 BOOL CLibSymbolInfo::IsRegularLibSymbol( PSTR pszSymbolName )
144 {
145  if ( 0 == strncmp( pszSymbolName, "__IMPORT_DESCRIPTOR_", 20 ) )
146  return FALSE;
147 
148  if ( 0 == strncmp( pszSymbolName, "__NULL_IMPORT_DESCRIPTOR", 24 ) )
149  return FALSE;
150 
151  if ( strstr( pszSymbolName, "_NULL_THUNK_DATA" ) )
152  return FALSE;
153 
154  return TRUE;
155 }
156 //=============================================================================
157 // Filters out symbols that are not needed....
158 //=============================================================================
159 BOOL CLibSymbolInfo::IsFiltedSymbol( string& symbolName )
160 {
161  // Filter problematic symbols for Win64
162  if ( symbolName.substr(0,3) == "_CT" ) return FALSE;
163  if ( symbolName.substr(0,3) == "_TI" ) return FALSE;
164  // Filter other symbols
165  if ( symbolName.substr(0,2) == "__" )
166  return FALSE;
167  if ( symbolName.substr(0,3) == "??_" && symbolName[3] != '0') // Keep 'operator/=' [??_0]
168  return FALSE;
169  if( symbolName[0] == '_') {
170  symbolName.erase(0, 1); // C functions ...
171  }
172  // Filter the internal Boost symbols
173  if (symbolName.find ("detail@boost") != string::npos )
174  return FALSE;
175  if (symbolName.find ("details@boost") != string::npos )
176  return FALSE;
177  return TRUE;
178 }
179 
180 //=============================================================================
181 // Converts from big endian to little endian numbers.
182 //=============================================================================
183 DWORD CLibSymbolInfo::ConvertBigEndian(DWORD bigEndian)
184 {
185  DWORD temp = 0;
186 
187  temp |= bigEndian >> 24;
188  temp |= ((bigEndian & 0x00FF0000) >> 8);
189  temp |= ((bigEndian & 0x0000FF00) << 8);
190  temp |= ((bigEndian & 0x000000FF) << 24);
191 
192  return temp;
193 }
194 
196 {
197  return m_strErrorMsg;
198 }
199 
200 
202 
203  //
204  // Given a filename, the constructor opens a file handle, creates a file
205  // mapping, and maps the entire file into memory.
206  //
207  m_hFile = INVALID_HANDLE_VALUE;
208  m_hFileMapping = 0;
209  m_pMemoryMappedFileBase = 0;
210  m_cbFile = 0;
211  m_errCode = errMMF_FileOpen; // Initial error code: not found
212  // First get a file handle
213  m_hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
214  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)0);
215 
216  if ( m_hFile == INVALID_HANDLE_VALUE )
217  {
218  m_errCode = errMMF_FileOpen;
219  return;
220  }
221  m_cbFile = ::GetFileSize( m_hFile, 0 );
222  // Now, create a file mapping
223  m_hFileMapping = CreateFileMapping(m_hFile,NULL, PAGE_READONLY, 0, 0,NULL);
224  if ( m_hFileMapping == 0 )
225  {
226  // Oops. Something went wrong. Clean up.
227  CloseHandle(m_hFile);
228  m_hFile = INVALID_HANDLE_VALUE;
229  m_errCode = errMMF_FileMapping;
230  return;
231  }
232  m_pMemoryMappedFileBase = (PCHAR)MapViewOfFile( m_hFileMapping,
233  FILE_MAP_READ, 0, 0, 0);
234  if ( m_pMemoryMappedFileBase == 0 )
235  {
236  // Oops. Something went wrong. Clean up.
237  CloseHandle(m_hFileMapping);
238  m_hFileMapping = 0;
239  CloseHandle(m_hFile);
240  m_hFile = INVALID_HANDLE_VALUE;
241  m_errCode = errMMF_MapView;
242  return;
243  }
244  m_errCode = errMMF_NoError;
245 }
246 
248 {
249  // Clean up everything that was created by the constructor
250  if ( m_pMemoryMappedFileBase )
251  UnmapViewOfFile( m_pMemoryMappedFileBase );
252 
253  if ( m_hFileMapping )
254  CloseHandle( m_hFileMapping );
255 
256  if ( m_hFile != INVALID_HANDLE_VALUE )
257  CloseHandle( m_hFile );
258 
259  m_errCode = errMMF_FileOpen;
260 }
PVOID GetBase(void)
Definition: LibSymbolInfo.h:71
std::string GetLastError() const
#define MakePtr(cast, ptr, addValue)
#define FALSE
Definition: globals.hh:52
BOOL Dump(LPTSTR lpszLibPathName, std::ostream &pFile)
#define TRUE
Definition: globals.hh:55
BOOL IsValid(void)
Definition: LibSymbolInfo.h:73
BOOL IsFiltedSymbol(std::string &pszSymbolName)
DWORD ConvertBigEndian(DWORD bigEndian)
MEMORY_MAPPED_FILE(PSTR pszFileName)
BOOL DumpSymbols(LPTSTR lpszLibPathName, std::ostream &pFile)
virtual ~CLibSymbolInfo()
BOOL IsRegularLibSymbol(PSTR pszSymbolName)