summaryrefslogtreecommitdiff
path: root/src/arch/operand.c
blob: e24a3e0b0e0086c523cf93b7ab795e12e5065e6c (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

/* OpenIDA - Outil d'analyse de fichiers binaires
 * operand.c - gestion générique des opérandes
 *
 * 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/>.
 */


#include "operand.h"


#include <stdio.h>


#include "operand-int.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : operand = structure dont le contenu est à définir.           *
*                value   = valeur immédiate à renseigner.                     *
*                                                                             *
*  Description : Crée une opérande pour l'instruction 'db'.                   *
*                                                                             *
*  Retour      : true si l'opérande a été définie avec succès, false sinon.   *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool fill_db_operand(asm_operand *operand, uint8_t value)
{
    operand->type = AOT_NONE;
    operand->size = AOS_8_BITS;

    operand->value.val8 = value;

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = instruction à traiter.                             *
*                buffer  = tampon de sortie mis à disposition. [OUT]          *
*                len     = taille de ce tampon.                               *
*                syntax  = type de représentation demandée.                   *
*                                                                             *
*  Description : Traduit une opérande de type 'db' en texte.                  *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void print_db_operand(const asm_operand *operand, char *buffer, size_t len, AsmSyntax syntax)
{
    switch (syntax)
    {
        case ASX_INTEL:
            snprintf(buffer, len, "0x%02hhx", operand->value.val8);
            break;

        case ASX_ATT:
            snprintf(buffer, len, "$0x%02hhx", operand->value.val8);
            break;

    }

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = structure dont le contenu est à définir.           *
*                size    = taille de l'opérande souhaitée.                    *
*                data    = flux de données à analyser.                        *
*                pos     = position courante dans ce flux. [OUT]              *
*                len     = taille totale des données à analyser.              *
*                                                                             *
*  Description : Crée une opérande contenant une valeur sur x bits.           *
*                                                                             *
*  Retour      : true si l'opération s'est effectuée avec succès, false sinon.*
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool fill_imm_operand(asm_operand *operand, AsmOperandSize size, const uint8_t *data, off_t *pos, off_t len)
{
    /* Vérifications sanitaires */
    switch (size)
    {
        case AOS_8_BITS:
            if ((len - *pos) < 1) return false;
            break;
        case AOS_16_BITS:
            if ((len - *pos) < 2) return false;
            break;
        case AOS_32_BITS:
            if ((len - *pos) < 4) return false;
            break;
        case AOS_64_BITS:
            if ((len - *pos) < 8) return false;
            break;
    }

    operand->type = AOT_IMM;
    operand->size = size;

    switch (size)
    {
        case AOS_8_BITS:
            operand->value.val8 = data[*pos];
            *pos += 1;
            break;
        case AOS_16_BITS:
            operand->value.val16 = data[*pos] | (uint16_t)data[*pos + 1] << 8;
            *pos += 2;
            break;
        case AOS_32_BITS:
            operand->value.val32 = data[*pos] | (uint32_t)data[*pos + 1] << 8
                | (uint32_t)data[*pos + 2] << 16 | (uint32_t)data[*pos + 3] << 24;
            *pos += 4;
            break;
        case AOS_64_BITS:
            operand->value.val64 = data[*pos] | (uint64_t)data[*pos + 1] << 8 | (uint64_t)data[*pos + 2] << 16
                | (uint64_t)data[*pos + 3] << 24 | (uint64_t)data[*pos + 4] << 32 | (uint64_t)data[*pos + 5] << 40
                | (uint64_t)data[*pos + 6] << 48 | (uint64_t)data[*pos + 7] << 56;
            *pos += 8;
            break;
    }

    return true;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = structure dont le contenu est à définir.           *
*                size    = taille de l'opérande souhaitée.                    *
*                data    = flux de données à analyser.                        *
*                pos     = position courante dans ce flux. [OUT]              *
*                len     = taille totale des données à analyser.              *
*                ref     = adresse de référence.                              *
*                                                                             *
*  Description : Crée une opérande contenant une valeur relative sur x bits.  *
*                                                                             *
*  Retour      : true si l'opération s'est effectuée avec succès, false sinon.*
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool fill_relimm_operand(asm_operand *operand, AsmOperandSize size, const uint8_t *data, off_t *pos, off_t len, uint64_t ref)
{
    bool result;                            /* Bilan à retourner           */
    off_t old_pos;                          /* Sauvegarde de l'évolution   */
    int8_t val8;                            /* Valeur sur 8 bits           */
    int16_t val16;                          /* Valeur sur 16 bits          */
    uint32_t val32;                          /* Valeur sur 32 bits          */
    int64_t val64;                          /* Valeur sur 64 bits          */

    old_pos = *pos;
    result = fill_imm_operand(operand, size, data, pos, len);

    if (result)
        switch (size)
        {
            case AOS_8_BITS:
                if (operand->value.val8 & 0x80)
                {
                    val8 = operand->value.val8 - 1;
                    val8 = ~val8;
                    operand->value.val8 = ref + (*pos - old_pos);
                    operand->value.val8 -= val8;
                }
                else operand->value.val8 += ref + (*pos - old_pos);
                break;
            case AOS_16_BITS:
                if (operand->value.val16 & 0x8000)
                {
                    val16 = operand->value.val16 - 1;
                    val16 = ~val16;
                    operand->value.val16 = ref + (*pos - old_pos);
                    operand->value.val16 -= val16;
                }
                else operand->value.val16 += ref + (*pos - old_pos);
                break;
            case AOS_32_BITS:
                if (operand->value.val32 & 0x80000000)
                {
                    val32 = operand->value.val32 - 1;
                    val32 = ~val32;
                    operand->value.val32 = ref + (*pos - old_pos);
                    operand->value.val32 -= val32;
                }
                else operand->value.val32 += ref + (*pos - old_pos);
                break;
            case AOS_64_BITS:
                if (operand->value.val64 & 0x8000000000000000ull)
                {
                    val64 = operand->value.val64 - 1;
                    val64 = ~val64;
                    operand->value.val64 = ref + (*pos - old_pos);
                    operand->value.val64 -= val64;
                }
                else operand->value.val64 += ref + (*pos - old_pos);
                break;
        }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : operand = instruction à traiter.                             *
*                buffer  = tampon de sortie mis à disposition. [OUT]          *
*                len     = taille de ce tampon.                               *
*                syntax  = type de représentation demandée.                   *
*                                                                             *
*  Description : Traduit une opérande de valeur immédiate en texte.           *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void print_imm_operand(const asm_operand *operand, char *buffer, size_t len, AsmSyntax syntax)
{
    switch (syntax)
    {
        case ASX_INTEL:
            switch (operand->size)
            {
                case AOS_8_BITS:
                    snprintf(buffer, len, "0x%hhx", operand->value.val8);
                    break;
                case AOS_16_BITS:
                    snprintf(buffer, len, "0x%hx", operand->value.val16);
                    break;
                case AOS_32_BITS:
                    snprintf(buffer, len, "0x%x", operand->value.val32);
                    break;
                case AOS_64_BITS:
                    snprintf(buffer, len, "0x%llx", operand->value.val64);
                    break;
            }
            break;

        case ASX_ATT:
            switch (operand->size)
            {
                case AOS_8_BITS:
                    snprintf(buffer, len, "$0x%hhx", operand->value.val8);
                    break;
                case AOS_16_BITS:
                    snprintf(buffer, len, "$0x%hx", operand->value.val16);
                    break;
                case AOS_32_BITS:
                    snprintf(buffer, len, "$0x%x", operand->value.val32);
                    break;
                case AOS_64_BITS:
                    snprintf(buffer, len, "$0x%llx", operand->value.val64);
                    break;
            }
            break;

    }

}