summaryrefslogtreecommitdiff
path: root/src/arch/x86/operand.h
blob: c9ade39ea5f93ca238eb5e92abc07d3cedb0ff76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

/* OpenIDA - Outil d'analyse de fichiers binaires
 * operand.h - prototypes pour la gestion des operandes de l'architecture x86
 *
 * Copyright (C) 2008 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#ifndef _ARCH_X86_OPERAND_H
#define _ARCH_X86_OPERAND_H


#include <stdbool.h>
#include <stdint.h>


#include "instruction.h"



/* Définition d'une opérande x86 */
typedef struct _asm_x86_operand asm_x86_operand;



/* Crée une opérande vierge pour x86. */
asm_x86_operand *create_new_x86_operand(void);



/* Crée une opérande renvoyant vers un registre 16 ou 32 bits. */
asm_x86_operand *x86_create_reg1632_operand(uint8_t, bool, uint8_t);

/* Crée une opérande renvoyant vers un registre 16 ou 32 bits. */
asm_x86_operand *x86_create_reg1632_operand_from_modrm(uint8_t, bool, bool);

/* Crée une opérande renvoyant vers un contenu 16 ou 32 bits. */
asm_x86_operand *x86_create_content1632_operand(const uint8_t *, off_t *, off_t, bool, bool);



/* Crée une opérande renvoyant vers un registre 8 bits. */
asm_x86_operand *x86_create_r8_operand(uint8_t, bool);

/* Crée une opérande à partir d'un registre/une mémoire 8 bits. */
asm_x86_operand *x86_create_rm8_operand(const uint8_t *, off_t *, off_t, ...);

/* Crée une opérande renvoyant vers un registre 16 ou 32 bits. */
asm_x86_operand *x86_create_r1632_operand(uint8_t, bool, bool);

/* Crée une opérande à partir d'un registre/une mémoire 16/32b. */
asm_x86_operand *x86_create_rm1632_operand(const uint8_t *, off_t *, off_t, bool, ...);



/* Traduit une opérande de registre en texte. */
void x86_print_reg_operand(const asm_x86_operand *, char *, size_t, AsmSyntax);



/* ------------------------ OPERANDES D'EMPLACEMENTS MEMOIRE ------------------------ */


/* Crée une opérande à partir d'un emplacement mémoire 8 bits. */
asm_x86_operand *x86_create_moffs8_operand(const uint8_t *, off_t *, off_t);

/* Crée une opérande à partir d'un emplacement mémoire 16/32b. */
asm_x86_operand *x86_create_moffs1632_operand(const uint8_t *, off_t *, off_t, bool);

/* Traduit une opérande d'emplacement mémoire en texte. */
void x86_print_moffs_operand(const asm_x86_operand *, char *, size_t, AsmSyntax);



/* ------------------------- OPERANDES D'ADRESSES RELATIVES ------------------------- */


/* Crée une opérande à partir d'une adresse relative (8 bits). */
asm_x86_operand *x86_create_rel8_operand_in_32b(uint64_t, const uint8_t *, off_t *, off_t);

/* Crée une opérande à partir d'une adresse relative (16/32b). */
asm_x86_operand *x86_create_rel1632_operand_in_32b(uint64_t, const uint8_t *, off_t *, off_t, bool);



/* ------------------------- AIDE A LA CREATION D'OPERANDES ------------------------- */


/* Construction d'identifiants typés */

#define X86_OTP_IMM_TYPE    0x8000
#define X86_OTP_REG_TYPE    0x4000
#define X86_OTP_RM_TYPE     0x2000

#define X86_OTP_IMM(b)  X86_OTP_IMM_TYPE | (1 << b)
#define X86_OTP_REG(b)  X86_OTP_REG_TYPE | (1 << b)
#define X86_OTP_RM(b)   X86_OTP_RM_TYPE  | (1 << b)

/* Types d'opérandes supportés */
typedef enum _X86OperandType
{
    X86_OTP_IMM8        = X86_OTP_IMM(1),   /* Valeur immédiate sur 8 bits */
    X86_OTP_IMM1632     = X86_OTP_IMM(2),   /* Valeur immédiate sur 16/32b */
    X86_OTP_MOFFS8      = X86_OTP_IMM(3),   /* Décallage immédiat 8 bits   */
    X86_OTP_MOFFS1632   = X86_OTP_IMM(4),   /* Décallage immédiat 16/32b   */
    X86_OTP_REL8        = X86_OTP_IMM(5),   /* Adresse relative 8 bits     */
    X86_OTP_REL1632     = X86_OTP_IMM(6),   /* Adresse relative 16/32 bits */

    X86_OTP_R8          = X86_OTP_REG(1),   /* Registre 8 bits             */
    X86_OTP_R1632       = X86_OTP_REG(2),   /* Registre 16 ou 32 bits      */
    X86_OTP_OP_R1632    = X86_OTP_REG(3),   /* Registre 16 ou 32 bits      */

    X86_OTP_RM8         = X86_OTP_RM(1),    /* Registre 8 bits ou mémoire  */
    X86_OTP_RM1632      = X86_OTP_RM(2),    /* Registre 16/32b ou mémoire  */

    X86_OTP_CL          = 0x0ffd,           /* Registre cl                 */
    X86_OTP_AL          = 0x0ffe,           /* Registre al                 */
    X86_OTP_E_AX        = 0x0fff            /* Registre eax ou ax          */

} X86OperandType;


/* Procède à la lecture d'un opérande donné. */
bool x86_read_one_operand(asm_x86_instr *, const uint8_t *, off_t *, off_t, X86OperandType, ...);

/* Procède à la lecture de deux opérandes donnés. */
bool x86_read_two_operands(asm_x86_instr *, const uint8_t *, off_t *, off_t, X86OperandType, X86OperandType, ...);



#endif  /* _ARCH_X86_OPERAND_H */