123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #ifndef BASE_WIN_PE_IMAGE_H_
- #define BASE_WIN_PE_IMAGE_H_
- #include <windows.h>
- #include <stdint.h>
- #if defined(_WIN32_WINNT_WIN8)
- #undef FACILITY_VISUALCPP
- #endif
- #include <DelayIMP.h>
- namespace base {
- namespace win {
- class PEImage {
- public:
-
-
-
- using EnumSectionsFunction =
- bool (*)(const PEImage&, PIMAGE_SECTION_HEADER, PVOID, DWORD, PVOID);
-
-
-
-
-
- using EnumExportsFunction =
- bool (*)(const PEImage&, DWORD, DWORD, LPCSTR, PVOID, LPCSTR, PVOID);
-
-
-
-
- using EnumImportChunksFunction = bool (*)(const PEImage&,
- LPCSTR,
- PIMAGE_THUNK_DATA,
- PIMAGE_THUNK_DATA,
- PVOID);
-
-
-
-
- using EnumImportsFunction = bool (*)(const PEImage&,
- LPCSTR,
- DWORD,
- LPCSTR,
- DWORD,
- PIMAGE_THUNK_DATA,
- PVOID);
-
-
-
-
- using EnumDelayImportChunksFunction = bool (*)(const PEImage&,
- PImgDelayDescr,
- LPCSTR,
- PIMAGE_THUNK_DATA,
- PIMAGE_THUNK_DATA,
- PVOID);
-
-
-
- using EnumRelocsFunction = bool (*)(const PEImage&, WORD, PVOID, PVOID);
- explicit PEImage(HMODULE module) : module_(module) {}
- explicit PEImage(const void* module) {
- module_ = reinterpret_cast<HMODULE>(const_cast<void*>(module));
- }
- virtual ~PEImage() = default;
-
- HMODULE module() const;
-
- void set_module(HMODULE module);
-
- static bool IsOrdinal(LPCSTR name);
-
- static WORD ToOrdinal(LPCSTR name);
-
- PIMAGE_DOS_HEADER GetDosHeader() const;
-
- PIMAGE_NT_HEADERS GetNTHeaders() const;
-
- WORD GetNumSections() const;
-
-
- PIMAGE_SECTION_HEADER GetSectionHeader(UINT section) const;
-
-
- DWORD GetImageDirectoryEntrySize(UINT directory) const;
-
-
- PVOID GetImageDirectoryEntryAddr(UINT directory) const;
-
-
-
-
- PIMAGE_SECTION_HEADER GetImageSectionFromAddr(PVOID address) const;
-
- PIMAGE_SECTION_HEADER GetImageSectionHeaderByName(LPCSTR section_name) const;
-
- PIMAGE_IMPORT_DESCRIPTOR GetFirstImportChunk() const;
-
- PIMAGE_EXPORT_DIRECTORY GetExportDirectory() const;
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool GetDebugId(LPGUID guid,
- LPDWORD age,
- LPCSTR* pdb_filename,
- size_t* pdb_filename_length) const;
-
-
-
-
-
-
- PDWORD GetExportEntry(LPCSTR name) const;
-
-
-
-
-
-
-
-
- FARPROC GetProcAddress(LPCSTR function_name) const;
-
-
- bool GetProcOrdinal(LPCSTR function_name, WORD* ordinal) const;
-
-
-
- bool EnumSections(EnumSectionsFunction callback, PVOID cookie) const;
-
-
-
- bool EnumExports(EnumExportsFunction callback, PVOID cookie) const;
-
-
-
-
-
- bool EnumAllImports(EnumImportsFunction callback,
- PVOID cookie,
- LPCSTR target_module_name) const;
-
-
-
-
-
- bool EnumImportChunks(EnumImportChunksFunction callback,
- PVOID cookie,
- LPCSTR target_module_name) const;
-
-
-
- bool EnumOneImportChunk(EnumImportsFunction callback,
- LPCSTR module_name,
- PIMAGE_THUNK_DATA name_table,
- PIMAGE_THUNK_DATA iat,
- PVOID cookie) const;
-
-
-
-
-
-
- bool EnumAllDelayImports(EnumImportsFunction callback,
- PVOID cookie,
- LPCSTR target_module_name) const;
-
-
-
-
-
-
- bool EnumDelayImportChunks(EnumDelayImportChunksFunction callback,
- PVOID cookie,
- LPCSTR target_module_name) const;
-
-
-
- bool EnumOneDelayImportChunk(EnumImportsFunction callback,
- PImgDelayDescr delay_descriptor,
- LPCSTR module_name,
- PIMAGE_THUNK_DATA name_table,
- PIMAGE_THUNK_DATA iat,
- PVOID cookie) const;
-
-
-
- bool EnumRelocs(EnumRelocsFunction callback, PVOID cookie) const;
-
-
- bool VerifyMagic() const;
-
- virtual PVOID RVAToAddr(uintptr_t rva) const;
-
-
- bool ImageRVAToOnDiskOffset(uintptr_t rva, DWORD* on_disk_offset) const;
-
-
- bool ImageAddrToOnDiskOffset(LPVOID address, DWORD* on_disk_offset) const;
- private:
-
-
- const IMAGE_DATA_DIRECTORY* GetDataDirectory(UINT directory) const;
- HMODULE module_;
- };
- class PEImageAsData : public PEImage {
- public:
- explicit PEImageAsData(HMODULE hModule) : PEImage(hModule) {}
- PVOID RVAToAddr(uintptr_t rva) const override;
- };
- inline bool PEImage::IsOrdinal(LPCSTR name) {
- return reinterpret_cast<uintptr_t>(name) <= 0xFFFF;
- }
- inline WORD PEImage::ToOrdinal(LPCSTR name) {
- return static_cast<WORD>(reinterpret_cast<intptr_t>(name));
- }
- inline HMODULE PEImage::module() const {
- return module_;
- }
- inline PIMAGE_IMPORT_DESCRIPTOR PEImage::GetFirstImportChunk() const {
- return reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(
- GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_IMPORT));
- }
- inline PIMAGE_EXPORT_DIRECTORY PEImage::GetExportDirectory() const {
- return reinterpret_cast<PIMAGE_EXPORT_DIRECTORY>(
- GetImageDirectoryEntryAddr(IMAGE_DIRECTORY_ENTRY_EXPORT));
- }
- }
- }
- #endif
|