Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Patrick Monette | 0196be2 | 2019-05-10 03:33:15 | [diff] [blame] | 5 | #ifndef CHROME_CHROME_ELF_HOOK_UTIL_HOOK_UTIL_H_ |
| 6 | #define CHROME_CHROME_ELF_HOOK_UTIL_HOOK_UTIL_H_ |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 7 | |
| 8 | #include <windows.h> |
| 9 | |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 10 | namespace elf_hook { |
| 11 | |
| 12 | //------------------------------------------------------------------------------ |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 13 | // Import Address Table hooking support |
| 14 | //------------------------------------------------------------------------------ |
| 15 | class IATHook { |
| 16 | public: |
| 17 | IATHook(); |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 18 | |
| 19 | IATHook(const IATHook&) = delete; |
| 20 | IATHook& operator=(const IATHook&) = delete; |
| 21 | |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 22 | ~IATHook(); |
| 23 | |
| 24 | // Intercept a function in an import table of a specific |
| 25 | // module. Saves everything needed to Unhook. |
| 26 | // |
| 27 | // NOTE: Hook can only be called once at a time, to enable Unhook(). |
| 28 | // |
| 29 | // Arguments: |
| 30 | // module Module to be intercepted |
| 31 | // imported_from_module Module that exports the 'function_name' |
| 32 | // function_name Name of the API to be intercepted |
| 33 | // new_function New function pointer |
| 34 | // |
| 35 | // Returns: Windows error code (winerror.h). NO_ERROR if successful. |
| 36 | DWORD Hook(HMODULE module, |
| 37 | const char* imported_from_module, |
| 38 | const char* function_name, |
| 39 | void* new_function); |
| 40 | |
| 41 | // Unhook the IAT entry. |
| 42 | // |
| 43 | // Returns: Windows error code (winerror.h). NO_ERROR if successful. |
| 44 | DWORD Unhook(); |
| 45 | |
| 46 | private: |
| 47 | void* intercept_function_; |
| 48 | void* original_function_; |
| 49 | IMAGE_THUNK_DATA* iat_thunk_; |
pennymac | 5446d89 | 2016-08-27 10:45:12 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | } // namespace elf_hook |
| 53 | |
Lei Zhang | 40a9bf9 | 2021-04-21 16:28:54 | [diff] [blame] | 54 | #endif // CHROME_CHROME_ELF_HOOK_UTIL_HOOK_UTIL_H_ |