1 // Copyright 2022 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 #ifndef CLIENT_LINUX_MINIDUMP_WRITER_PE_FILE_H_ 30 #define CLIENT_LINUX_MINIDUMP_WRITER_PE_FILE_H_ 31 32 #include "client/linux/minidump_writer/pe_structs.h" 33 34 namespace google_breakpad { 35 36 typedef enum { 37 notPeCoff = 0, 38 peWithoutBuildId = 1, 39 peWithBuildId = 2 40 } PEFileFormat; 41 42 class PEFile { 43 public: 44 /** 45 * Attempts to parse RSDS_DEBUG_FORMAT record from a PE (Portable 46 * Executable) file. To do this we check whether the loaded file is a PE 47 * file, and if it is - try to find IMAGE_DEBUG_DIRECTORY structure with 48 * its type set to IMAGE_DEBUG_TYPE_CODEVIEW. 49 * 50 * @param filename Filename for the module to parse. 51 * @param debug_info RSDS_DEBUG_FORMAT struct to be populated with PE debug 52 * info (GUID and age). 53 * @return 54 * notPeCoff: not PE/COFF file; 55 * peWithoutBuildId: a PE/COFF file but build-id is not set; 56 * peWithBuildId: a PE/COFF file and build-id is set. 57 */ 58 static PEFileFormat TryGetDebugInfo(const char* filename, 59 PRSDS_DEBUG_FORMAT debug_info); 60 61 private: 62 template <class TStruct> TryReadStruct(const void * base,const DWORD position,const size_t file_size)63 static const TStruct* TryReadStruct(const void* base, 64 const DWORD position, 65 const size_t file_size) { 66 if (position + sizeof(TStruct) >= file_size){ 67 return nullptr; 68 } 69 70 const void* ptr = static_cast<const char*>(base) + position; 71 return reinterpret_cast<const TStruct*>(ptr); 72 } 73 }; 74 75 } // namespace google_breakpad 76 #endif // CLIENT_LINUX_MINIDUMP_WRITER_PE_FILE_H_