xref: /haiku/headers/libs/zydis/Zydis/Utils.h (revision caed67a8cba83913b9c21ac2b06ebc6bd1cb3111)
1 /***************************************************************************************************
2 
3   Zyan Disassembler Library (Zydis)
4 
5   Original Author : Florian Bernd
6 
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24 
25 ***************************************************************************************************/
26 
27 /**
28  * @file
29  * Other utility functions.
30  */
31 
32 #ifndef ZYDIS_UTILS_H
33 #define ZYDIS_UTILS_H
34 
35 #include <Zycore/Defines.h>
36 #include <Zydis/DecoderTypes.h>
37 #include <Zydis/Status.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /* ============================================================================================== */
44 /* Exported functions                                                                             */
45 /* ============================================================================================== */
46 
47 /**
48  * @addtogroup utils Utils
49  * Miscellaneous utility functions. Address translation and other helpers.
50  * @{
51  */
52 
53 /* ---------------------------------------------------------------------------------------------- */
54 /* Address calculation                                                                            */
55 /* ---------------------------------------------------------------------------------------------- */
56 
57 // TODO: Provide a function that works in minimal-mode and does not require a operand parameter
58 
59 /**
60  * Calculates the absolute address value for the given instruction operand.
61  *
62  * @param   instruction     A pointer to the `ZydisDecodedInstruction` struct.
63  * @param   operand         A pointer to the `ZydisDecodedOperand` struct.
64  * @param   runtime_address The runtime address of the instruction.
65  * @param   result_address  A pointer to the memory that receives the absolute address.
66  *
67  * @return  A zyan status code.
68  *
69  * You should use this function in the following cases:
70  * - `IMM` operands with relative address (e.g. `JMP`, `CALL`, ...)
71  * - `MEM` operands with `RIP`/`EIP`-relative address (e.g. `MOV RAX, [RIP+0x12345678]`)
72  * - `MEM` operands with absolute address (e.g. `MOV RAX, [0x12345678]`)
73  *   - The displacement needs to get truncated and zero extended
74  */
75 ZYDIS_EXPORT ZyanStatus ZydisCalcAbsoluteAddress(const ZydisDecodedInstruction* instruction,
76     const ZydisDecodedOperand* operand, ZyanU64 runtime_address, ZyanU64* result_address);
77 
78 /**
79  * Calculates the absolute address value for the given instruction operand.
80  *
81  * @param   instruction         A pointer to the `ZydisDecodedInstruction` struct.
82  * @param   operand             A pointer to the `ZydisDecodedOperand` struct.
83  * @param   runtime_address     The runtime address of the instruction.
84  * @param   register_context    A pointer to the `ZydisRegisterContext` struct.
85  * @param   result_address      A pointer to the memory that receives the absolute target-address.
86  *
87  * @return  A zyan status code.
88  *
89  * This function behaves like `ZydisCalcAbsoluteAddress` but takes an additional register-context
90  * argument to allow calculation of addresses depending on runtime register values.
91  *
92  * Note that `IP/EIP/RIP` from the register-context will be ignored in favor of the passed
93  * runtime-address.
94  */
95 ZYDIS_EXPORT ZyanStatus ZydisCalcAbsoluteAddressEx(const ZydisDecodedInstruction* instruction,
96     const ZydisDecodedOperand* operand, ZyanU64 runtime_address,
97     const ZydisRegisterContext* register_context, ZyanU64* result_address);
98 
99 /* ---------------------------------------------------------------------------------------------- */
100 
101 /**
102  * @}
103  */
104 
105 /* ============================================================================================== */
106 
107 #ifdef __cplusplus
108 }
109 #endif
110 
111 #endif /* ZYDIS_UTILS_H */
112