Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:16:08

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 
0027 #ifdef _WIN32
0028   // Disable a warning in Boost program_options headers:
0029   // inconsistent linkage in program_options/variables_map.hpp
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 // Include files----------------------------------------------------------------
0038 #include <vector>
0039 #include <string>
0040 #include <iostream>
0041 #include <fstream>
0042 
0043 // LibSymbolinfo----------------------------------------------------------------
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 // _MSC_VER >= 1000
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; // Handle of memory mapped file
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 // !defined(AFX_LIBSYMBOLINFO_H__1A7003B4_BA53_11D1_AE46_1CFB51000000__INCLUDED_)
0102 
0103 using namespace std;
0104 
0105 #define MakePtr( cast, ptr, addValue ) (cast)( (DWORD_PTR)(ptr) + (DWORD_PTR)(addValue))
0106 
0107 /////////////////////////////////////////////////////////////////////////////
0108 // CLibSymbolInfo
0109 
0110 CLibSymbolInfo::CLibSymbolInfo()
0111 {
0112 }
0113 
0114 CLibSymbolInfo::~CLibSymbolInfo()
0115 {
0116 }
0117 
0118 //=============================================================================
0119 // Function: DumpSymbols
0120 //
0121 // Parameters:
0122 //      LPTSTR lpszLibPathName    - The library file path name
0123 //      CStdioFile* pFile         - Address of the file in which to dump the symbols
0124 //
0125 // Description:
0126 //
0127 // Given a library file path name, the function dumps the symbol info into the file
0128 // pointed to by pFile.
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 // Function: Dump
0145 //
0146 // Parameters:
0147 //      As mentioned above.
0148 //
0149 // Description:
0150 //
0151 // Depending on the value specified in <m_bDump>/<m_bGrep> the routine dumps/greps
0152 // the symbo info.
0153 //=============================================================================
0154 BOOL CLibSymbolInfo::Dump(LPTSTR lpszLibPathName, ostream& pFile) 
0155 {
0156   string sBuff;
0157   MEMORY_MAPPED_FILE libFile(lpszLibPathName);
0158         
0159   // Ensure that the file mapping worked
0160   if( FALSE == libFile.IsValid() ) {
0161         m_strErrorMsg = "Unable to access file ";
0162     m_strErrorMsg+= lpszLibPathName;
0163     return FALSE;
0164   }
0165   // All COFF libraries start with the string "!<arch>\n".  Verify that this
0166   // string is at the beginning of the mapped file
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   // Point to the first archive member.  This entry contains the LIB symbols,
0177   // and immediately follows the archive start string ("!<arch>\n")
0178   PIMAGE_ARCHIVE_MEMBER_HEADER pMbrHdr;
0179   pMbrHdr = MakePtr( PIMAGE_ARCHIVE_MEMBER_HEADER, pArchiveStartString,
0180                      IMAGE_ARCHIVE_START_SIZE );
0181 
0182   // First DWORD after this member header is a symbol count
0183   PDWORD pcbSymbols = (PDWORD)(pMbrHdr + 1);  // Pointer math!
0184 
0185   // The symbol count is stored in big endian format, so adjust as
0186   // appropriate for the target architecture
0187   DWORD cSymbols = ConvertBigEndian( *pcbSymbols );
0188 
0189   // Following the symbol count is an array of offsets to archive members
0190   // (essentially, embedded .OBJ files)
0191   PDWORD pMemberOffsets = pcbSymbols + 1;     // Pointer math!
0192 
0193   // Following the array of member offsets is an array of offsets to symbol
0194   // names.
0195   PSTR pszSymbolName = MakePtr( PSTR, pMemberOffsets, 4 * cSymbols );
0196 
0197   //
0198   // Loop through every symbol in the first archive member
0199   //      
0200   for ( unsigned i = 0; i < cSymbols; i++ )
0201   {
0202     DWORD offset;
0203 
0204     // The offsets to the archive member that contains the symbol is stored
0205     // in big endian format, so convert it appropriately.        
0206     offset = ConvertBigEndian( *pMemberOffsets );
0207 
0208     // Call DisplayLibInfoForSymbol, which figures out what kind of symbol
0209     // it is.  The "IsRegularLibSymbol" filters out symbols that are
0210     // internal to the linking process
0211     if ( IsRegularLibSymbol( pszSymbolName ) ) {
0212       string symbol(pszSymbolName);
0213       if (IsFiltedSymbol(symbol) ) {
0214         pFile << symbol << endl;
0215       }
0216     }            
0217     // Advanced to the next symbol offset and name.  The MemberOffsets
0218     // array has fixed length entries, while the symbol names are
0219     // sequential null-terminated strings
0220     pMemberOffsets++;
0221     pszSymbolName += strlen(pszSymbolName) + 1;
0222   }
0223   return TRUE;
0224 }
0225 
0226 //=============================================================================
0227 // Filters out symbols that are internal to the linking process, and which
0228 // the programmer never explicitly sees.
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 // Filters out symbols that are not needed....
0245 //=============================================================================
0246 BOOL CLibSymbolInfo::IsFiltedSymbol( string& symbolName )
0247 { 
0248   // Filter problematic symbols for Win64  
0249   if ( symbolName.substr(0,3) == "_CT" ) return FALSE;
0250   if ( symbolName.substr(0,3) == "_TI" ) return FALSE;
0251   // Filter other symbols
0252   if ( symbolName.substr(0,2) == "__" ) 
0253     return FALSE;
0254   if ( symbolName.substr(0,3) == "??_" && symbolName[3] != '0') // Keep 'operator/='  [??_0]
0255     return FALSE;
0256   if( symbolName[0] == '_') {
0257     symbolName.erase(0, 1);  // C functions ...
0258   }
0259   // Filter the internal Boost symbols
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 // Converts from big endian to little endian numbers.
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    // Given a filename, the constructor opens a file handle, creates a file
0292    // mapping, and maps the entire file into memory.
0293    //
0294    m_hFile = INVALID_HANDLE_VALUE;
0295    m_hFileMapping = 0;
0296    m_pMemoryMappedFileBase = 0;
0297    m_cbFile = 0;
0298    m_errCode = errMMF_FileOpen;    // Initial error code: not found
0299     // First get a file handle      
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     // Now, create a file mapping   
0310    m_hFileMapping = CreateFileMapping(m_hFile,NULL, PAGE_READONLY, 0, 0,NULL);
0311    if ( m_hFileMapping == 0 )
0312    {
0313        // Oops.  Something went wrong.  Clean up.
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        // Oops.  Something went wrong.  Clean up.
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     // Clean up everything that was created by the constructor
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 //--- Command main program-----------------------------------------------------
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 }