diff -r 000000000000 -r 638b9c697799 apicompatanamdw/compatanalysercmd/libraryanalyser/src/la_otherfunc.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/apicompatanamdw/compatanalysercmd/libraryanalyser/src/la_otherfunc.cpp Tue Jan 12 14:52:39 2010 +0530 @@ -0,0 +1,945 @@ +/* +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: Other non-categorized LibraryAnalyser functionality +* +*/ + + +#include "la.hpp" + +// ---------------------------------------------------------------------------------------------------------- + +void ParseCommandLineParameters(int argc, char* argv[]) +{ + //for (int i=1; i::iterator i = _lib_dirs_baseline.begin(); + for( ; i != _lib_dirs_baseline.end(); i++) + MakeSureTrailingDirectoryMarkerExists(*i); + + for( i = _lib_dirs_current.begin(); i != _lib_dirs_current.end(); i++) + MakeSureTrailingDirectoryMarkerExists(*i); + + MakeSureTrailingDirectoryMarkerExists(_cl_toolsdir); + MakeSureTrailingDirectoryMarkerExists(_cl_tempdir); + + // check if baselinedir & currentdir exists + vector::const_iterator j = _lib_dirs_baseline.begin(); + for( ; j != _lib_dirs_baseline.end(); j++) + if (!DirectoryExists(*j)) + { + cerr << "ERROR: " << *j << " not found!" << endl; + cerr << "Check -baselinelibdir parameter" << endl; + exit(EXIT_BASELINEDIR_NOT_FOUND); + } + for( j = _lib_dirs_current.begin(); j != _lib_dirs_current.end(); j++) + if (!DirectoryExists(*j)) + { + cerr << "ERROR: " << *j << " not found!" << endl; + cerr << "Check -currentlibdir parameter" << endl; + exit(EXIT_CURRENTDIR_NOT_FOUND); + } + + //if both -baselinedlldir & -currentdlldir are given, check the validity of their value + if (!_cl_baselinedlldir.empty() && !_cl_currentdlldir.empty()) + { + if( !FileExists(_cl_baselinedlldir) ) + { + //create a vector from the ';' seperated string + _dll_dirs_baseline = splitString( _cl_baselinedlldir, ';'); + for( i = _dll_dirs_baseline.begin(); i != _dll_dirs_baseline.end(); i++) + { + MakeSureTrailingDirectoryMarkerExists(*i); + if ( !DirectoryExists(*i) ) + { + cerr << "ERROR: " << *i << " not found!" << endl; + cerr << "Check -baselinedlldir parameter" << endl; + exit(EXIT_BASELINEDLLDIR_NOT_FOUND); + } + } + } + else + { + _baselinedllfile = _cl_baselinedlldir; + } + if( !FileExists(_cl_currentdlldir) ) + { + //create a vector from the ';' seperated string + _dll_dirs_current = splitString( _cl_currentdlldir, ';'); + for( i = _dll_dirs_current.begin(); i != _dll_dirs_current.end(); i++) + { + MakeSureTrailingDirectoryMarkerExists(*i); + if ( !DirectoryExists(*i) ) + { + cerr << "ERROR: " << *i << " not found!" << endl; + cerr << "Check -currentdlldir parameter" << endl; + exit(EXIT_CURRENTDLLDIR_NOT_FOUND); + } + } + } + else + { + _currentdllfile = _cl_currentdlldir; + } + } + + // check if user given filter set exists + if (!_cl_set.empty() && !FileExists(_cl_set)) + { + cerr << "ERROR: " << _cl_set << " not found!" << endl; + cerr << "Check -set parameter" << endl; + exit(EXIT_SET_FILE_NOT_FOUND); + } + + // if tools parameter is empty, try get it from path, otherwise check given parameter + if (_cl_toolsdir.empty()) + { + GetToolsPathFromEnvironmentVariable(); + } + else + { + // check the given tools directory parameter is valid + if (_cl_use_gcc) + { + _gcc_nm_location = _cl_toolsdir + GCC_NM_EXE; + + if (!FileExists(_gcc_nm_location)) + { + cerr << "ERROR: Unable to find " + _gcc_nm_location << ", check -tools parameter" << endl; + cerr << "Tip: GCC is typically installed to \\epoc32\\gcc\\bin directory" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND); + } + + // extra quotes needed to prevent white space problems while executing the tool + InsertQuotesToFilePath(_gcc_nm_location); + } + + else if (_cl_use_gcce) + { + _gcce_nm_location = _cl_toolsdir + GCCE_NM_EXE; + _gcce_readelf_location = _cl_toolsdir + GCCE_READELF_EXE; + _gcce_cfilt_location = _cl_toolsdir + GCCE_CFILT_EXE; + + if (!FileExists(_gcce_nm_location)) + { + cerr << "ERROR: Unable to find " + _gcce_nm_location << ", check -tools param" << endl; + cerr << "Tip: GCCE is typically installed to C:\\Program Files\\CSL Arm Toolchain\\bin" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND); + } + + if (!FileExists(_gcce_readelf_location)) + { + cerr << "ERROR: Unable to find " + _gcce_readelf_location << ", check -tools param" << endl; + cerr << "Tip: GCCE is typically installed to C:\\Program Files\\CSL Arm Toolchain\\bin" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND); + } + + if (!FileExists(_gcce_cfilt_location)) + { + cerr << "ERROR: Unable to find " + _gcce_cfilt_location << ", check -tools param" << endl; + cerr << "Tip: GCCE is typically installed to C:\\Program Files\\CSL Arm Toolchain\\bin" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND); + } + + // extra quotes needed to prevent white space problems while executing the tool + InsertQuotesToFilePath(_gcce_nm_location); + InsertQuotesToFilePath(_gcce_readelf_location); + InsertQuotesToFilePath(_gcce_cfilt_location); + + } + + else if (_cl_use_rvct) + { + _rvct_armar_location = _cl_toolsdir + RVCT_ARMAR_EXE; + _rvct_fromelf_location = _cl_toolsdir + RVCT_FROMELF_EXE; + + if (!FileExists(_rvct_armar_location)) + { + cerr << "ERROR: Unable to find " + _rvct_armar_location << ", check -tools param" << endl; + cerr << "Tip: Check your environment variables to see the directory where RVCT has been installed" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND); + } + + if (!FileExists(_rvct_fromelf_location)) + { + cerr << "ERROR: Unable to find " + _rvct_fromelf_location << ", check -tools param" << endl; + cerr << "Tip: Check your environment variables to see the directory where RVCT has been installed" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND); + } + + // extra quotes needed to prevent white space problems while executing the tool + InsertQuotesToFilePath(_rvct_armar_location); + InsertQuotesToFilePath(_rvct_fromelf_location); + } + } // else of if (!cl_toolsdir) + + // check path of RVCT's cfilt is correct + if (_cl_use_rvct) + { + _rvct_cfilt_location = _cl_cfiltloc; + + if (!FileExists(_rvct_cfilt_location)) + { + cerr << "ERROR: Unable to find " + _rvct_cfilt_location << ", check -cfilt param" << endl; + cerr << "Tip: Working cfilt.exe can be found from the same directory where LibraryAnalyser has been installed" << endl; + exit(EXIT_CFILT_NOT_FOUND); + } + + // extra quotes needed to prevent white space problems while executing the tool + InsertQuotesToFilePath(_rvct_cfilt_location); + } + + + // get temporary directory from environment variable if not specified by the user + if (_cl_tempdir.empty()) + { + GetTempPathFromEnvironmentVariable(); + } + // otherwise create path to user given parameter + else + { + MkDirAll(_cl_tempdir); + } + + // check that it is possible to create the temporary file under the temp directory + _tempfile_location = _cl_tempdir + TEMP_FILENAME; + ofstream tempf(_tempfile_location.c_str(), ios::trunc); + if (!tempf.is_open()) + { + tempf.close(); + cerr << "ERROR: Cannot open " << _tempfile_location << " for writing!" << endl; + cerr << "Please check that are no write permission problems" << endl; + exit(EXIT_CANNOT_WRITE_TO_TEMP_FILE); + } + else + tempf.close(); + + + //if -baselinedlldir or -currentdlldir or both not given, throw warning + if (_cl_baselinedlldir.empty() || _cl_currentdlldir.empty() ) + { + cout << "WARNING: Required parameters for checking dll properties not specified. Breaks in dlls will not be checked!" << endl; + } + + // check if analysing .dso or .lib files + if (_cl_use_gcc || _cl_use_libs) // for GCC use always .lib files + _lib_extension = ".lib"; + else + _lib_extension = ".dso"; +} + +// ---------------------------------------------------------------------------------------------------------- + +void GetToolsPathFromEnvironmentVariable() +{ + #ifdef WIN32 + // get path environment variable + const char* env_path = getenv("PATH"); + + if (env_path != NULL) + { + string env_path_str = env_path; + + string::size_type previous_delimpos = 0; + bool match_found(false); + string test_path; + + for (unsigned int i=0; i5000) + break; // something went wrong.. + } + + if (!match_found) + { + cerr << "ERROR: Cannot find specified compiler toolset from PATH environment" << endl; + cerr << "Fix PATH environment variable or specify -tools parameter" << endl; + exit(EXIT_TOOLCHAIN_NOT_FOUND_FROM_PATH); + } + + } + else + { + cerr << "ERROR: PATH environment variable not available, please specify -tools parameter" << endl; + exit(EXIT_PATH_VARIABLE_NOT_FOUND); + } + + #else + + cerr << "ERROR: Optional -tools parameter is only supported in Windows environment " << endl; + exit(EXIT_ONLY_SUPPORTED_IN_WIN32); + + #endif + +} + +// ---------------------------------------------------------------------------------------------------------- + +void GetTempPathFromEnvironmentVariable() +{ + #ifdef WIN32 + // get path environment variable + const char* env_path = getenv("TEMP"); + + if (env_path != NULL) + { + string env_path_str = env_path; + MakeSureTrailingDirectoryMarkerExists(env_path_str); + + // check if the directory exists + if (DirectoryExists(env_path_str)) + { + _cl_tempdir = env_path_str; + } + else + { + cerr << "ERROR: Directory specified in TEMP environment variable does not exist" << endl; + cerr << "Fix TEMP environment variable or specify -temp parameter" << endl; + exit(EXIT_TEMP_DIRECTORY_DOES_NOT_EXIST); + } + } + else + { + cerr << "ERROR: TEMP environment variable not available, please specify -temp parameter" << endl; + exit(EXIT_TEMP_VARIABLE_NOT_FOUND); + } + + #else + + cerr << "ERROR: Optional -temp parameter is only supported in Windows environment " << endl; + exit(EXIT_ONLY_SUPPORTED_IN_WIN32); + + #endif + +} + +// ---------------------------------------------------------------------------------------------------------- + +void GetListsOfImportLibraries() +{ + string lib_filter = "*" + _lib_extension; + + GetFileNamesFromDirectory(_lib_dirs_baseline, lib_filter, _lib_files_baseline); + GetFileNamesFromDirectory(_lib_dirs_current, lib_filter, _lib_files_current); +} + +// ---------------------------------------------------------------------------------------------------------- + +void GetFileNamesFromDirectory(const vector& directories, const string& filter, vector& resultset) +{ + // get list of files from the directory by executing OS specific dir/ls command + string dir_command = DIR_COMMAND; + vector tempVector; + vector localVector; + unsigned int i = 0; + unsigned int j = 0; + string cmd; + + // Get filter extension to compare with libname extension + string::size_type dot_loc = filter.find_last_of("."); + string filterExtensn; + if (dot_loc != string::npos) + filterExtensn = filter.substr(dot_loc,string::npos); + + for(; i < directories.size(); i++) + { + cmd = dir_command + " \"" + directories.at(i) + "\"" + filter + " " + CERR_TO_NULL; + ExecuteCommand(cmd, localVector); + string fileExtn; + //append the list of files, along with directory names, to a common vector + for(j = 0;j < localVector.size(); j++) + { + // get lib name extensiion and compare with filter extension. + dot_loc = localVector.at(j).find_last_of("."); + if (dot_loc != string::npos) + fileExtn = localVector.at(j).substr(dot_loc,string::npos); + // if extensions are not matching , ignore the libname + if(stricmp(fileExtn.c_str(),filterExtensn.c_str()) == 0 ) + { + #ifdef WIN32 + tempVector.push_back( directories.at(i) + localVector.at(j) ); + #else + tempVector.push_back( localVector.at(j) ); + #endif + } + } + } + + // loop through all returned files + for (unsigned int i=0; i lib_files_filter; + string item_str; + int i=0; + + // read all names from the lib + while(!set_f.eof() && i<50000) + { + getline(set_f, item_str); + item_str = TrimAll(item_str); + + if (!item_str.empty()) + { + // strip any directory path, both Windows and Unix style + string::size_type sep_pos = item_str.find_last_of("\\"); + if (sep_pos != string::npos) + item_str = item_str.substr(sep_pos+1, item_str.length()-sep_pos-1); + sep_pos = item_str.find_last_of("/"); + if (sep_pos != string::npos) + item_str = item_str.substr(sep_pos+1, item_str.length()-sep_pos-1); + + //cerr << "Set filter: " << item_str << endl; + + // append the file name to the vector + lib_files_filter.push_back(item_str); + } + + i++; + } + set_f.close(); + + + // remove from _lib_files_baseline those strings which aren't in lib_files_filter + for (unsigned int j=0; j<_lib_files_baseline.size(); ) + { + bool found = false; + + for (unsigned int k=0; k& dllPath, string type) +{ + string filter = "*.dll"; + vector dlls_done; + vector resultset; + + + ofstream outputfile( dllDataFile.c_str()); + if(!outputfile.is_open()) + { + cerr << "ERROR: Cannot open " << dllDataFile << " for writing!" << endl; + exit(EXIT_CANNOT_OPEN_FILE); + } + + WriteXMLLineOpeningTag(outputfile,0,"dll_list"); + + GetFileNamesFromDirectory(dllPath,filter,resultset); + vector::iterator dll = resultset.begin(); + for( ; dll != resultset.end(); dll++) + { + string dllname = getFilename(*dll); + //consider the dll only if the corresponding library is to be analysed + vector::const_iterator i = _lib_files_baseline.begin(); + for(; i != _lib_files_baseline.end(); ++i) + { + if(StringICmpFileNamesWithoutExtension(getFilename(*i),dllname)==0) + break; + } + + if (i == _lib_files_baseline.end()) + continue; + + //consider the dll file only once + if(count(dlls_done.begin(),dlls_done.end(),dllname)==1) + { + cout<<"Warning: More than one files matches for the given filename '"<1) + continue; + + dlls_done.push_back(dllname); + + + WriteXMLLineOpeningTag(outputfile,0,"dll"); + WriteXMLLineOpenClosed(outputfile,1,"dllname",dllname); + + fstream dllFile((*dll).c_str(),ios::binary | ios::in); + + if(!dllFile.is_open()) + { + cerr << "ERROR: Cannot open " << *dll << " for reading!" << endl; + exit(EXIT_CANNOT_OPEN_FILE); + } + + string value; + + value=HexValueAt(dllFile, UID1_OFFSET); // 0x00 offset of UID1 + WriteXMLLineOpenClosed(outputfile,1,"uid1",value); + + value=HexValueAt(dllFile, UID2_OFFSET); // 0x04 offset of UID2 + WriteXMLLineOpenClosed(outputfile,1,"uid2",value); + + value=HexValueAt(dllFile, UID3_OFFSET); // 0x08 offset of UID3 + WriteXMLLineOpenClosed(outputfile,1,"uid3",value); + + value=HexValueAt(dllFile, SID_OFFSET); // 0x80 offset of Secure ID + WriteXMLLineOpenClosed(outputfile,1,"sid",value); + + value=HexValueAt(dllFile, CAPABILITY_OFFSET); // 0x88 offset of Capabilities + WriteXMLLineOpenClosed(outputfile,1,"capability",value); + + WriteXMLLineClosingTag(outputfile,0,"dll"); + + dllFile.close(); + } + + WriteXMLLineClosingTag(outputfile,0,"dll_list"); + outputfile.close(); + +} + +// ---------------------------------------------------------------------------------------------------------- +void GetDllDataFiles() +{ + if (!_cl_baselinedlldir.empty() && !_cl_currentdlldir.empty() ) + { + //if -baselinedlldir value is path, construct the dll data xml file + if( _baselinedllfile.empty() ) + { + _baselinedllfile = _cl_tempdir + "baselinedlldata.xml"; + CreateDllDataFile(_baselinedllfile,_dll_dirs_baseline, "baseline"); + } + //if -currentdlldir value is path, construct the dll data xml file + if( _currentdllfile.empty() ) + { + _currentdllfile = _cl_tempdir + "currentdlldata.xml"; + CreateDllDataFile(_currentdllfile,_dll_dirs_current, "current"); + } + } +}