Yun Peng | 66437a0 | 2017-08-25 09:59:14 +0200 | [diff] [blame] | 1 | diff --git a/third_party/def_parser/def_parser.cc b/third_party/def_parser/def_parser.cc |
| 2 | index e96226a4b..07cb727b7 100644 |
| 3 | --- a/third_party/def_parser/def_parser.cc |
| 4 | +++ b/third_party/def_parser/def_parser.cc |
| 5 | @@ -61,19 +61,24 @@ |
| 6 | * Author: Valery Fine 16/09/96 (E-mail: fine@vxcern.cern.ch) |
| 7 | *---------------------------------------------------------------------- |
| 8 | */ |
| 9 | -#include "bindexplib.h" |
| 10 | +#include "src/main/cpp/util/file_platform.h" |
| 11 | +#include "third_party/def_parser/def_parser.h" |
| 12 | |
| 13 | -#include "cmsys/Encoding.hxx" |
| 14 | -#include "cmsys/FStream.hxx" |
| 15 | +#include <algorithm> |
| 16 | #include <iostream> |
| 17 | +#include <fstream> |
| 18 | +#include <sstream> |
| 19 | #include <windows.h> |
| 20 | |
| 21 | #ifndef IMAGE_FILE_MACHINE_ARMNT |
| 22 | #define IMAGE_FILE_MACHINE_ARMNT 0x01c4 |
| 23 | #endif |
| 24 | |
| 25 | -typedef struct cmANON_OBJECT_HEADER_BIGOBJ |
| 26 | -{ |
| 27 | +using std::string; |
| 28 | +using std::wstring; |
| 29 | +using std::stringstream; |
| 30 | + |
| 31 | +typedef struct cmANON_OBJECT_HEADER_BIGOBJ { |
| 32 | /* same as ANON_OBJECT_HEADER_V2 */ |
| 33 | WORD Sig1; // Must be IMAGE_FILE_MACHINE_UNKNOWN |
| 34 | WORD Sig2; // Must be 0xffff |
| 35 | @@ -92,13 +97,10 @@ typedef struct cmANON_OBJECT_HEADER_BIGOBJ |
| 36 | DWORD NumberOfSymbols; |
| 37 | } cmANON_OBJECT_HEADER_BIGOBJ; |
| 38 | |
| 39 | -typedef struct _cmIMAGE_SYMBOL_EX |
| 40 | -{ |
| 41 | - union |
| 42 | - { |
| 43 | +typedef struct _cmIMAGE_SYMBOL_EX { |
| 44 | + union { |
| 45 | BYTE ShortName[8]; |
| 46 | - struct |
| 47 | - { |
| 48 | + struct { |
| 49 | DWORD Short; // if 0, use LongName |
| 50 | DWORD Long; // offset into string table |
| 51 | } Name; |
| 52 | @@ -113,16 +115,14 @@ typedef struct _cmIMAGE_SYMBOL_EX |
| 53 | typedef cmIMAGE_SYMBOL_EX UNALIGNED* cmPIMAGE_SYMBOL_EX; |
| 54 | |
| 55 | PIMAGE_SECTION_HEADER GetSectionHeaderOffset( |
| 56 | - PIMAGE_FILE_HEADER pImageFileHeader) |
| 57 | -{ |
| 58 | + PIMAGE_FILE_HEADER pImageFileHeader) { |
| 59 | return (PIMAGE_SECTION_HEADER)((DWORD_PTR)pImageFileHeader + |
| 60 | IMAGE_SIZEOF_FILE_HEADER + |
| 61 | pImageFileHeader->SizeOfOptionalHeader); |
| 62 | } |
| 63 | |
| 64 | PIMAGE_SECTION_HEADER GetSectionHeaderOffset( |
| 65 | - cmANON_OBJECT_HEADER_BIGOBJ* pImageFileHeader) |
| 66 | -{ |
| 67 | + cmANON_OBJECT_HEADER_BIGOBJ* pImageFileHeader) { |
| 68 | return (PIMAGE_SECTION_HEADER)((DWORD_PTR)pImageFileHeader + |
| 69 | sizeof(cmANON_OBJECT_HEADER_BIGOBJ)); |
| 70 | } |
| 71 | @@ -130,8 +130,7 @@ PIMAGE_SECTION_HEADER GetSectionHeaderOffset( |
| 72 | /* |
| 73 | + * Utility func, strstr with size |
| 74 | + */ |
| 75 | -const char* StrNStr(const char* start, const char* find, size_t& size) |
| 76 | -{ |
| 77 | +const char* StrNStr(const char* start, const char* find, size_t& size) { |
| 78 | size_t len; |
| 79 | const char* hint; |
| 80 | |
| 81 | @@ -157,9 +156,8 @@ template < |
| 82 | class ObjectHeaderType, |
| 83 | // cmPIMAGE_SYMBOL_EX or PIMAGE_SYMBOL |
| 84 | class SymbolTableType> |
| 85 | -class DumpSymbols |
| 86 | -{ |
| 87 | -public: |
| 88 | +class DumpSymbols { |
| 89 | + public: |
| 90 | /* |
| 91 | *---------------------------------------------------------------------- |
| 92 | * Constructor -- |
| 93 | @@ -169,11 +167,10 @@ public: |
| 94 | *---------------------------------------------------------------------- |
| 95 | */ |
| 96 | |
| 97 | - DumpSymbols(ObjectHeaderType* ih, std::set<std::string>& symbols, |
| 98 | - std::set<std::string>& dataSymbols, bool isI386) |
| 99 | + DumpSymbols(ObjectHeaderType* ih, std::set<string>& symbols, |
| 100 | + std::set<string>& dataSymbols, bool isI386) |
| 101 | : Symbols(symbols) |
| 102 | - , DataSymbols(dataSymbols) |
| 103 | - { |
| 104 | + , DataSymbols(dataSymbols) { |
| 105 | this->ObjectImageHeader = ih; |
| 106 | this->SymbolTable = |
| 107 | (SymbolTableType*)((DWORD_PTR) this->ObjectImageHeader + |
| 108 | @@ -190,7 +187,9 @@ public: |
| 109 | * Dump an object file's exported symbols. |
| 110 | *---------------------------------------------------------------------- |
| 111 | */ |
| 112 | - void DumpObjFile() { this->DumpExternalsObjects(); } |
| 113 | + void DumpObjFile() { |
| 114 | + this->DumpExternalsObjects(); |
| 115 | + } |
| 116 | |
| 117 | /* |
| 118 | *---------------------------------------------------------------------- |
| 119 | @@ -199,11 +198,10 @@ public: |
| 120 | * Dumps a COFF symbol table from an OBJ. |
| 121 | *---------------------------------------------------------------------- |
| 122 | */ |
| 123 | - void DumpExternalsObjects() |
| 124 | - { |
| 125 | + void DumpExternalsObjects() { |
| 126 | unsigned i; |
| 127 | PSTR stringTable; |
| 128 | - std::string symbol; |
| 129 | + string symbol; |
| 130 | DWORD SectChar; |
| 131 | /* |
| 132 | * The string table apparently starts right after the symbol table |
| 133 | @@ -230,8 +228,8 @@ public: |
| 134 | // if it starts with _ and has an @ then it is a __cdecl |
| 135 | // so remove the @ stuff for the export |
| 136 | if (symbol[0] == '_') { |
| 137 | - std::string::size_type posAt = symbol.find('@'); |
| 138 | - if (posAt != std::string::npos) { |
| 139 | + string::size_type posAt = symbol.find('@'); |
| 140 | + if (posAt != string::npos) { |
| 141 | symbol.erase(posAt); |
| 142 | } |
| 143 | } |
| 144 | @@ -247,14 +245,14 @@ public: |
| 145 | const char* scalarPrefix = "??_G"; |
| 146 | const char* vectorPrefix = "??_E"; |
| 147 | // The original code had a check for |
| 148 | - // symbol.find("real@") == std::string::npos) |
| 149 | + // symbol.find("real@") == string::npos) |
| 150 | // but this disallows member functions with the name "real". |
| 151 | if (symbol.compare(0, 4, scalarPrefix) && |
| 152 | symbol.compare(0, 4, vectorPrefix)) { |
| 153 | SectChar = this->SectionHeaders[pSymbolTable->SectionNumber - 1] |
| 154 | .Characteristics; |
| 155 | // skip symbols containing a dot |
| 156 | - if (symbol.find('.') == std::string::npos) { |
| 157 | + if (symbol.find('.') == string::npos) { |
| 158 | if (!pSymbolTable->Type && (SectChar & IMAGE_SCN_MEM_WRITE)) { |
| 159 | // Read only (i.e. constants) must be excluded |
| 160 | this->DataSymbols.insert(symbol); |
| 161 | @@ -278,9 +276,9 @@ public: |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | -private: |
| 166 | - std::set<std::string>& Symbols; |
| 167 | - std::set<std::string>& DataSymbols; |
| 168 | + private: |
| 169 | + std::set<string>& Symbols; |
| 170 | + std::set<string>& DataSymbols; |
| 171 | DWORD_PTR SymbolCount; |
| 172 | PIMAGE_SECTION_HEADER SectionHeaders; |
| 173 | ObjectHeaderType* ObjectImageHeader; |
| 174 | @@ -288,35 +286,56 @@ private: |
| 175 | bool IsI386; |
| 176 | }; |
| 177 | |
| 178 | -bool DumpFile(const char* filename, std::set<std::string>& symbols, |
| 179 | - std::set<std::string>& dataSymbols) |
| 180 | -{ |
| 181 | +void PrintLastError() { |
| 182 | + DWORD last_error = GetLastError(); |
| 183 | + if (last_error == 0) { |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + char* message_buffer; |
| 188 | + size_t size = FormatMessageA( |
| 189 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 190 | + FORMAT_MESSAGE_IGNORE_INSERTS, |
| 191 | + NULL, last_error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 192 | + (LPSTR)&message_buffer, 0, NULL); |
| 193 | + |
| 194 | + std::cerr << "(error: " << last_error << "): " << message_buffer; |
| 195 | + LocalFree(message_buffer); |
| 196 | +} |
| 197 | + |
| 198 | +bool DumpFile(const char* filename, std::set<string>& symbols, |
| 199 | + std::set<string>& dataSymbols) { |
| 200 | HANDLE hFile; |
| 201 | HANDLE hFileMapping; |
| 202 | LPVOID lpFileBase; |
| 203 | PIMAGE_DOS_HEADER dosHeader; |
| 204 | |
| 205 | - hFile = CreateFileW(cmsys::Encoding::ToWide(filename).c_str(), GENERIC_READ, |
| 206 | + wstring filenameW; |
| 207 | + blaze_util::AsAbsoluteWindowsPath(filename, &filenameW); |
| 208 | + hFile = CreateFileW(filenameW.c_str(), GENERIC_READ, |
| 209 | FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 210 | FILE_ATTRIBUTE_NORMAL, 0); |
| 211 | |
| 212 | if (hFile == INVALID_HANDLE_VALUE) { |
| 213 | + PrintLastError(); |
| 214 | fprintf(stderr, "Couldn't open file '%s' with CreateFile()\n", filename); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | hFileMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); |
| 219 | if (hFileMapping == 0) { |
| 220 | - CloseHandle(hFile); |
| 221 | + PrintLastError(); |
| 222 | fprintf(stderr, "Couldn't open file mapping with CreateFileMapping()\n"); |
| 223 | + CloseHandle(hFile); |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | lpFileBase = MapViewOfFile(hFileMapping, FILE_MAP_READ, 0, 0, 0); |
| 228 | if (lpFileBase == 0) { |
| 229 | + PrintLastError(); |
| 230 | + fprintf(stderr, "Couldn't map view of file with MapViewOfFile()\n"); |
| 231 | CloseHandle(hFileMapping); |
| 232 | CloseHandle(hFile); |
| 233 | - fprintf(stderr, "Couldn't map view of file with MapViewOfFile()\n"); |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | @@ -348,7 +367,7 @@ bool DumpFile(const char* filename, std::set<std::string>& symbols, |
| 238 | (h->Machine == IMAGE_FILE_MACHINE_I386)); |
| 239 | symbolDumper.DumpObjFile(); |
| 240 | } else { |
| 241 | - printf("unrecognized file format in '%s'\n", filename); |
| 242 | + printf("Unrecognized file format in '%s'\n", filename); |
| 243 | return false; |
| 244 | } |
| 245 | } |
| 246 | @@ -358,19 +377,23 @@ bool DumpFile(const char* filename, std::set<std::string>& symbols, |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | -bool bindexplib::AddObjectFile(const char* filename) |
| 251 | -{ |
| 252 | + |
| 253 | +void DefParser::SetDLLName(const string& dllname) { |
| 254 | + this->DLLName = dllname; |
| 255 | +} |
| 256 | + |
| 257 | +bool DefParser::AddObjectFile(const char* filename) { |
| 258 | return DumpFile(filename, this->Symbols, this->DataSymbols); |
| 259 | } |
| 260 | |
| 261 | -bool bindexplib::AddDefinitionFile(const char* filename) |
| 262 | -{ |
| 263 | - cmsys::ifstream infile(filename); |
| 264 | +bool DefParser::AddDefinitionFile(const char* filename) { |
| 265 | + std::ifstream infile(filename); |
| 266 | if (!infile) { |
| 267 | + PrintLastError(); |
| 268 | fprintf(stderr, "Couldn't open definition file '%s'\n", filename); |
| 269 | return false; |
| 270 | } |
| 271 | - std::string str; |
| 272 | + string str; |
| 273 | while (std::getline(infile, str)) { |
| 274 | // skip the LIBRAY and EXPORTS lines (if any) |
| 275 | if ((str.compare(0, 7, "LIBRARY") == 0) || |
| 276 | @@ -380,8 +403,8 @@ bool bindexplib::AddDefinitionFile(const char* filename) |
| 277 | // remove leading tabs & spaces |
| 278 | str.erase(0, str.find_first_not_of(" \t")); |
| 279 | std::size_t found = str.find(" \t DATA"); |
| 280 | - if (found != std::string::npos) { |
| 281 | - str.erase(found, std::string::npos); |
| 282 | + if (found != string::npos) { |
| 283 | + str.erase(found, string::npos); |
| 284 | this->DataSymbols.insert(str); |
| 285 | } else { |
| 286 | this->Symbols.insert(str); |
| 287 | @@ -391,14 +414,37 @@ bool bindexplib::AddDefinitionFile(const char* filename) |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | -void bindexplib::WriteFile(FILE* file) |
| 292 | -{ |
| 293 | +bool DefParser::IsDefFile(const string& file) { |
| 294 | + // Get file extension and convert it to lower case. |
| 295 | + string ext = file.substr(file.find_last_of(".") + 1); |
| 296 | + std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower); |
| 297 | + return ext == "def"; |
| 298 | +} |
| 299 | + |
| 300 | +bool DefParser::AddFile(const string& file) { |
| 301 | + if (IsDefFile(file)) { |
| 302 | + if (!this->AddDefinitionFile(file.c_str())) { |
| 303 | + return false; |
| 304 | + } |
| 305 | + } else { |
| 306 | + if (!this->AddObjectFile(file.c_str())) { |
| 307 | + return false; |
| 308 | + } |
| 309 | + } |
| 310 | + return true; |
| 311 | +} |
| 312 | + |
| 313 | +void DefParser::WriteFile(FILE* file) { |
| 314 | + if (!this->DLLName.empty()) { |
| 315 | + fprintf(file, "LIBRARY %s\n", this->DLLName.c_str()); |
| 316 | + } |
| 317 | + |
| 318 | fprintf(file, "EXPORTS \n"); |
| 319 | - for (std::set<std::string>::const_iterator i = this->DataSymbols.begin(); |
| 320 | + for (std::set<string>::const_iterator i = this->DataSymbols.begin(); |
| 321 | i != this->DataSymbols.end(); ++i) { |
| 322 | fprintf(file, "\t%s \t DATA\n", i->c_str()); |
| 323 | } |
| 324 | - for (std::set<std::string>::const_iterator i = this->Symbols.begin(); |
| 325 | + for (std::set<string>::const_iterator i = this->Symbols.begin(); |
| 326 | i != this->Symbols.end(); ++i) { |
| 327 | fprintf(file, "\t%s\n", i->c_str()); |
| 328 | } |