File indexing completed on 2025-01-18 09:16:08
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027 #ifdef _WIN32
0028
0029
0030 #pragma warning ( disable : 4273 )
0031 #define popen _popen
0032 #define pclose _pclose
0033 #define fileno _fileno
0034 #include <stdlib.h>
0035 #endif
0036
0037
0038 #include <vector>
0039 #include <string>
0040 #include <iostream>
0041 #include <fstream>
0042
0043
0044 #if !defined(AFX_LIBSYMBOLINFO_H__1A7003B4_BA53_11D1_AE46_1CFB51000000__INCLUDED_)
0045 #define AFX_LIBSYMBOLINFO_H__1A7003B4_BA53_11D1_AE46_1CFB51000000__INCLUDED_
0046
0047 #if _MSC_VER >= 1000
0048 #pragma once
0049 #endif
0050
0051 #include <string>
0052 #include <iostream>
0053
0054 #include <stdio.h>
0055 #include <assert.h>
0056 #include <windows.h>
0057
0058 class CLibSymbolInfo
0059 {
0060 public:
0061 CLibSymbolInfo();
0062 virtual ~CLibSymbolInfo();
0063 BOOL DumpSymbols(LPTSTR lpszLibPathName, std::ostream& pFile);
0064 std::string GetLastError() const;
0065
0066 protected:
0067 std::string m_strResultsString;
0068 std::string m_strErrorMsg;
0069
0070 BOOL Dump(LPTSTR lpszLibPathName, std::ostream& pFile);
0071 BOOL IsRegularLibSymbol( PSTR pszSymbolName );
0072 BOOL IsFiltedSymbol( std::string& pszSymbolName );
0073 DWORD ConvertBigEndian(DWORD bigEndian);
0074 };
0075
0076 enum errMMF { errMMF_NoError, errMMF_FileOpen,
0077 errMMF_FileMapping, errMMF_MapView };
0078
0079 class MEMORY_MAPPED_FILE
0080 {
0081 public:
0082 MEMORY_MAPPED_FILE( PSTR pszFileName );
0083 ~MEMORY_MAPPED_FILE(void);
0084
0085 PVOID GetBase( void ){ return m_pMemoryMappedFileBase; }
0086 DWORD GetFileSize( void ){ return m_cbFile; }
0087 BOOL IsValid( void ) { return errMMF_NoError == m_errCode; }
0088 errMMF GetErrorType(){ return m_errCode; }
0089
0090 private:
0091
0092 HANDLE m_hFile;
0093 HANDLE m_hFileMapping;
0094 PVOID m_pMemoryMappedFileBase;
0095 DWORD m_cbFile;
0096 errMMF m_errCode;
0097 };
0098
0099 typedef MEMORY_MAPPED_FILE* PMEMORY_MAPPED_FILE;
0100
0101 #endif
0102
0103 using namespace std;
0104
0105 #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD_PTR)(ptr) + (DWORD_PTR)(addValue))
0106
0107
0108
0109
0110 CLibSymbolInfo::CLibSymbolInfo()
0111 {
0112 }
0113
0114 CLibSymbolInfo::~CLibSymbolInfo()
0115 {
0116 }
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 BOOL CLibSymbolInfo::DumpSymbols(LPTSTR lpszLibPathName, ostream& pFile)
0131 {
0132 if(lpszLibPathName == NULL || pFile.bad() ) {
0133 assert(lpszLibPathName != NULL);
0134 assert(pFile.good());
0135 m_strErrorMsg.assign("NULL <lpszLibPathName> or Invalid file handle.");
0136 return FALSE;
0137 }
0138
0139 if(!Dump(lpszLibPathName, pFile)) return FALSE;
0140 return TRUE;
0141 }
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 BOOL CLibSymbolInfo::Dump(LPTSTR lpszLibPathName, ostream& pFile)
0155 {
0156 string sBuff;
0157 MEMORY_MAPPED_FILE libFile(lpszLibPathName);
0158
0159
0160 if( FALSE == libFile.IsValid() ) {
0161 m_strErrorMsg = "Unable to access file ";
0162 m_strErrorMsg+= lpszLibPathName;
0163 return FALSE;
0164 }
0165
0166
0167
0168 PSTR pArchiveStartString = (PSTR)libFile.GetBase();
0169
0170 if ( 0 != strncmp( pArchiveStartString, IMAGE_ARCHIVE_START,
0171 IMAGE_ARCHIVE_START_SIZE ) ) {
0172 m_strErrorMsg.assign("Not a valid COFF LIB file.");
0173 return FALSE;
0174 }
0175
0176
0177
0178 PIMAGE_ARCHIVE_MEMBER_HEADER pMbrHdr;
0179 pMbrHdr = MakePtr( PIMAGE_ARCHIVE_MEMBER_HEADER, pArchiveStartString,
0180 IMAGE_ARCHIVE_START_SIZE );
0181
0182
0183 PDWORD pcbSymbols = (PDWORD)(pMbrHdr + 1);
0184
0185
0186
0187 DWORD cSymbols = ConvertBigEndian( *pcbSymbols );
0188
0189
0190
0191 PDWORD pMemberOffsets = pcbSymbols + 1;
0192
0193
0194
0195 PSTR pszSymbolName = MakePtr( PSTR, pMemberOffsets, 4 * cSymbols );
0196
0197
0198
0199
0200 for ( unsigned i = 0; i < cSymbols; i++ )
0201 {
0202 DWORD offset;
0203
0204
0205
0206 offset = ConvertBigEndian( *pMemberOffsets );
0207
0208
0209
0210
0211 if ( IsRegularLibSymbol( pszSymbolName ) ) {
0212 string symbol(pszSymbolName);
0213 if (IsFiltedSymbol(symbol) ) {
0214 pFile << symbol << endl;
0215 }
0216 }
0217
0218
0219
0220 pMemberOffsets++;
0221 pszSymbolName += strlen(pszSymbolName) + 1;
0222 }
0223 return TRUE;
0224 }
0225
0226
0227
0228
0229
0230 BOOL CLibSymbolInfo::IsRegularLibSymbol( PSTR pszSymbolName )
0231 {
0232 if ( 0 == strncmp( pszSymbolName, "__IMPORT_DESCRIPTOR_", 20 ) )
0233 return FALSE;
0234
0235 if ( 0 == strncmp( pszSymbolName, "__NULL_IMPORT_DESCRIPTOR", 24 ) )
0236 return FALSE;
0237
0238 if ( strstr( pszSymbolName, "_NULL_THUNK_DATA" ) )
0239 return FALSE;
0240
0241 return TRUE;
0242 }
0243
0244
0245
0246 BOOL CLibSymbolInfo::IsFiltedSymbol( string& symbolName )
0247 {
0248
0249 if ( symbolName.substr(0,3) == "_CT" ) return FALSE;
0250 if ( symbolName.substr(0,3) == "_TI" ) return FALSE;
0251
0252 if ( symbolName.substr(0,2) == "__" )
0253 return FALSE;
0254 if ( symbolName.substr(0,3) == "??_" && symbolName[3] != '0')
0255 return FALSE;
0256 if( symbolName[0] == '_') {
0257 symbolName.erase(0, 1);
0258 }
0259
0260 if (symbolName.find ("detail@boost") != string::npos )
0261 return FALSE;
0262 if (symbolName.find ("details@boost") != string::npos )
0263 return FALSE;
0264 return TRUE;
0265 }
0266
0267
0268
0269
0270 DWORD CLibSymbolInfo::ConvertBigEndian(DWORD bigEndian)
0271 {
0272 DWORD temp = 0;
0273
0274 temp |= bigEndian >> 24;
0275 temp |= ((bigEndian & 0x00FF0000) >> 8);
0276 temp |= ((bigEndian & 0x0000FF00) << 8);
0277 temp |= ((bigEndian & 0x000000FF) << 24);
0278
0279 return temp;
0280 }
0281
0282 string CLibSymbolInfo::GetLastError() const
0283 {
0284 return m_strErrorMsg;
0285 }
0286
0287
0288 MEMORY_MAPPED_FILE::MEMORY_MAPPED_FILE( PSTR pszFileName ) {
0289
0290
0291
0292
0293
0294 m_hFile = INVALID_HANDLE_VALUE;
0295 m_hFileMapping = 0;
0296 m_pMemoryMappedFileBase = 0;
0297 m_cbFile = 0;
0298 m_errCode = errMMF_FileOpen;
0299
0300 m_hFile = CreateFile(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
0301 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)0);
0302
0303 if ( m_hFile == INVALID_HANDLE_VALUE )
0304 {
0305 m_errCode = errMMF_FileOpen;
0306 return;
0307 }
0308 m_cbFile = ::GetFileSize( m_hFile, 0 );
0309
0310 m_hFileMapping = CreateFileMapping(m_hFile,NULL, PAGE_READONLY, 0, 0,NULL);
0311 if ( m_hFileMapping == 0 )
0312 {
0313
0314 CloseHandle(m_hFile);
0315 m_hFile = INVALID_HANDLE_VALUE;
0316 m_errCode = errMMF_FileMapping;
0317 return;
0318 }
0319 m_pMemoryMappedFileBase = (PCHAR)MapViewOfFile( m_hFileMapping,
0320 FILE_MAP_READ, 0, 0, 0);
0321 if ( m_pMemoryMappedFileBase == 0 )
0322 {
0323
0324 CloseHandle(m_hFileMapping);
0325 m_hFileMapping = 0;
0326 CloseHandle(m_hFile);
0327 m_hFile = INVALID_HANDLE_VALUE;
0328 m_errCode = errMMF_MapView;
0329 return;
0330 }
0331 m_errCode = errMMF_NoError;
0332 }
0333
0334 MEMORY_MAPPED_FILE::~MEMORY_MAPPED_FILE(void)
0335 {
0336
0337 if ( m_pMemoryMappedFileBase )
0338 UnmapViewOfFile( m_pMemoryMappedFileBase );
0339
0340 if ( m_hFileMapping )
0341 CloseHandle( m_hFileMapping );
0342
0343 if ( m_hFile != INVALID_HANDLE_VALUE )
0344 CloseHandle( m_hFile );
0345
0346 m_errCode = errMMF_FileOpen;
0347 }
0348
0349 namespace windef {
0350 void usage(){
0351 cerr << "Usage: genwindef [-l <dllname>] [-o <output-file> | exports.def] <obj or lib filenames>" << endl;
0352 exit(1);
0353 }
0354 }
0355
0356
0357
0358 int main ( int argc, char** argv )
0359
0360 {
0361 string outfile("exports.def");
0362 string library("UnknownLib");
0363 string objfiles;
0364 bool debug(false);
0365
0366 int arg;
0367 if (argc < 3) windef::usage();
0368 arg = 1;
0369 while (argv[arg][0] == '-') {
0370 if (strcmp(argv[arg], "--") == 0) {
0371 windef::usage();
0372 }
0373 else if (strcmp(argv[arg], "-l") == 0) {
0374 arg++;
0375 if (arg == argc) windef::usage();
0376 library = argv[arg];
0377 }
0378 else if (strcmp(argv[arg], "-o") == 0) {
0379 arg++;
0380 if (arg == argc) windef::usage();
0381 outfile = argv[arg];
0382 }
0383 arg++;
0384 }
0385 if (arg == argc) windef::usage();
0386 for (arg; arg < argc; arg++) {
0387 objfiles += argv[arg];
0388 if( arg+1 < argc) objfiles += " ";
0389 }
0390
0391 CLibSymbolInfo libsymbols;
0392 ofstream out(outfile.c_str());
0393 if(out.fail()) {
0394 cerr << "windef: Error opening file " << outfile << endl;
0395 return 1;
0396 }
0397 out << "LIBRARY " << library << endl;
0398 out << "EXPORTS" << endl;
0399
0400 libsymbols.DumpSymbols(const_cast<char*>(objfiles.c_str()), out);
0401
0402 out.close();
0403
0404
0405 return 0;
0406 }