/* Chrysalide - Outil d'analyse de fichiers binaires
* modrm.c - opérandes de type mod/rm
*
* Copyright (C) 2012 Cyrille Bagard
*
* This file is part of Chrysalide.
*
* Chrysalide 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.
*
* Chrysalide 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 .
*/
#include "modrm.h"
#include
#include "register.h"
#include "../../operand-int.h"
/* Définition d'un opérande x86 de type ModRM (instance) */
struct _GX86ModRMOperand
{
GArchOperand parent; /* Instance parente */
uint8_t scale; /* Puissance de deux */
GX86Register *index; /* Registre servant d'indice */
GX86Register *base; /* Registre de base */
GImmOperand *displacement; /* Décallage supplémentaire */
};
/* Définition d'un opérande x86 de type ModRM (classe) */
struct _GX86ModRMOperandClass
{
GArchOperandClass parent; /* Classe parente */
};
/* Initialise la classe des opérandes x86 de type ModRM. */
static void g_x86_mod_rm_operand_class_init(GX86ModRMOperandClass *);
/* Initialise une instance d'opérande x86 de type ModRM. */
static void g_x86_mod_rm_operand_init(GX86ModRMOperand *);
/* Indique le type défini par la GLib pour un opérande x86 de type ModRM. */
G_DEFINE_TYPE(GX86ModRMOperand, g_x86_mod_rm_operand, G_TYPE_ARCH_OPERAND);
/******************************************************************************
* *
* Paramètres : klass = classe à initialiser. *
* *
* Description : Initialise la classe des opérandes x86 de type ModRM. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void g_x86_mod_rm_operand_class_init(GX86ModRMOperandClass *klass)
{
}
/******************************************************************************
* *
* Paramètres : operand = instance à initialiser. *
* *
* Description : Initialise une instance d'opérande x86 de type ModRM. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void g_x86_mod_rm_operand_init(GX86ModRMOperand *operand)
{
}
/******************************************************************************
* *
* Paramètres : data = flux de données à analyser. *
* pos = position courante dans ce flux. [OUT] *
* len = taille totale des données à analyser. *
* size = taille de l'opérande, et donc du registre. *
* *
* Description : Crée un opérande x86 de type ModRM. *
* *
* Retour : Opérande mis en place. *
* *
* Remarques : - *
* *
******************************************************************************/
GArchOperand *g_x86_mod_rm_operand_new(const bin_t *data, off_t *pos, off_t len, MemoryDataSize size)
{
GX86ModRMOperand *result; /* Structure à retourner */
uint8_t mod; /* Modificateur présent */
GX86Register *reg; /* Registre lu */
mod = (data[*pos] & 0xc0);
if (mod == 0xc0)
return g_x86_register_operand_new_from_mod_rm(data, pos, len, size, true);
reg = g_x86_register_new(size, data[*pos] & 0x07);
if (reg == NULL) return NULL;
(*pos)++;
/* Vieille astuce de l'emplacement mémoire fixe ? */
if (g_x86_register_is_base_pointer(reg) && mod == 0x00)
{
/* FIXME *///free_x86_register(reg);
assert(0);
return NULL;
//return g_imm_operand_new_from_data_old(MDS_32_BITS/* FIXME */, data, pos, len, SRE_LITTLE /*FIXME*/);
}
result = g_object_new(G_TYPE_X86_MOD_RM_OPERAND, NULL);
/* A la recherche d'un SIB */
if (g_x86_register_is_stack_pointer(reg))
{
/* FIXME *///free_x86_register(reg);
result->base = g_x86_register_new(size, data[*pos] & 0x07);
if (result->base == NULL) goto gxmron_error;
result->index = g_x86_register_new(size, (data[*pos] & 0x38) >> 3);
if (result->index == NULL) goto gxmron_error;
result->scale = ((data[*pos] & 0xc0) >> 6);
if (g_x86_register_is_stack_pointer(result->index))
{
/* FIXME *///free_x86_register(result->index);
result->index = result->base;
result->base = NULL;
}
(*pos)++;
}
else result->index = reg;
/* Décallage supplémentaire ? */
switch (mod)
{
case 0x00:
if (result->base != NULL && g_x86_register_is_base_pointer(result->base))
{
/* FIXME *///free_x86_register(result->base);
result->base = NULL;
assert(0);
//result->displacement = g_imm_operand_new_from_data_old(size/* FIXME : !convert mds/aos */, data, pos, len, SRE_LITTLE /* FIXME */);
if (result->displacement == NULL) goto gxmron_error;
}
break;
case 0x40:
assert(0);
//result->displacement = g_imm_operand_new_from_data_old(MDS_8_BITS_SIGNED, data, pos, len, SRE_LITTLE /* FIXME */);
if (result->displacement == NULL) goto gxmron_error;
break;
case 0x80:
assert(0);
//result->displacement = g_imm_operand_new_from_data_old(MDS_32_BITS_SIGNED/* FIXME ! 16/32 */, data, pos, len, SRE_LITTLE /* FIXME */);
if (result->displacement == NULL) goto gxmron_error;
break;
}
return G_ARCH_OPERAND(result);
gxmron_error:
/* FIXME free(result);*/
return NULL;
}
#if 0
/******************************************************************************
* *
* Paramètres : operand = opérande à transcrire. *
* options = options de rendu. *
* rendering = support effectif final des lignes de code. *
* stream = flux ouvert en écriture. *
* *
* Description : Ajoute du texte simple à un fichier ouvert en écriture. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void g_x86_mod_rm_operand_add_text(const GX86ModRMOperand *operand, GRenderingOptions *options, MainRendering rendering, FILE *stream)
{
GContentExporter *exporter; /* Autre vision de l'opérande */
char tmp[2]; /* Echelle en puissance de 2 */
exporter = G_CONTENT_EXPORTER(operand);
switch (g_rendering_options_get_syntax(options))
{
case ASX_INTEL:
g_content_exporter_insert_text(exporter, stream, "[", 1, RTT_HOOK);
if (operand->scale > 0)
{
snprintf(tmp, 2, "%d", (int)pow(2, operand->scale));
g_content_exporter_insert_text(exporter, stream, tmp, 1, RTT_IMMEDIATE);
g_content_exporter_insert_text(exporter, stream, "*", 1, RTT_SIGNS);
}
g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->index),
options, rendering, stream);
if (operand->base != NULL)
{
g_content_exporter_insert_text(exporter, stream, "+", 1, RTT_SIGNS);
g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->base),
options, rendering, stream);
}
if (operand->displacement != NULL)
{
if (g_imm_operand_is_negative(operand->displacement))
g_content_exporter_insert_text(exporter, stream, "-", 1, RTT_SIGNS);
else
g_content_exporter_insert_text(exporter, stream, "+", 1, RTT_SIGNS);
g_content_exporter_add_text(G_CONTENT_EXPORTER(operand->displacement),
options, rendering, stream);
}
g_content_exporter_insert_text(exporter, stream, "]", 1, RTT_HOOK);
break;
case ASX_ATT:
/* TODO */
g_content_exporter_insert_text(exporter, stream, "[ModRM]", 7, RTT_HOOK);
break;
}
}
/******************************************************************************
* *
* Paramètres : operand = opérande à transcrire. *
* buffer = espace où placer ledit contenu. *
* options = options de rendu. *
* *
* Description : Ajoute à un tampon GLib le contenu de l'instance spécifiée. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
static void g_x86_mod_rm_operand_to_buffer(const GX86ModRMOperand *operand, GBufferLine *buffer, GRenderingOptions *options)
{
GContentExporter *exporter; /* Autre vision de l'opérande */
char tmp[2]; /* Echelle en puissance de 2 */
exporter = G_CONTENT_EXPORTER(operand);
switch (g_rendering_options_get_syntax(options))
{
case ASX_INTEL:
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"[", 1, RTT_HOOK);
if (operand->scale > 0)
{
snprintf(tmp, 2, "%d", (int)pow(2, operand->scale));
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
tmp, 1, RTT_IMMEDIATE);
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"*", 1, RTT_SIGNS);
}
g_content_exporter_to_buffer(G_CONTENT_EXPORTER(operand->index), buffer, options);
if (operand->base != NULL)
{
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"+", 1, RTT_SIGNS);
g_content_exporter_to_buffer(G_CONTENT_EXPORTER(operand->base), buffer, options);
}
if (operand->displacement != NULL)
{
if (g_imm_operand_is_negative(operand->displacement))
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"-", 1, RTT_SIGNS);
else
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"+", 1, RTT_SIGNS);
g_content_exporter_to_buffer(G_CONTENT_EXPORTER(operand->displacement), buffer, options);
}
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"]", 1, RTT_HOOK);
break;
case ASX_ATT:
/* TODO */
g_content_exporter_insert_into_buffer(exporter, buffer, BLC_ASSEMBLY,
"[ModRM]", 7, RTT_HOOK);
break;
}
}
#endif
/******************************************************************************
* *
* Paramètres : operand = opérande à consulter. *
* scale = facteur sous forme de puissance de deux. [OUT *
* index = register principal de l'opérande. [OUT] *
* *
* Description : Fournit l'indice et l'échelle d'un opérande x86 ModRM. *
* *
* Retour : - *
* *
* Remarques : - *
* *
******************************************************************************/
void g_x86_mod_rm_operand_get_scale_and_index(const GX86ModRMOperand *operand, uint8_t *scale, const GX86Register **index)
{
*scale = operand->scale;
*index = operand->index;
}
/******************************************************************************
* *
* Paramètres : operand = opérande à consulter. *
* *
* Description : Fournit le registre de base d'un opérande x86 ModRM. *
* *
* Retour : Registre de base de l'opérande. *
* *
* Remarques : - *
* *
******************************************************************************/
const GX86Register *g_x86_mod_rm_operand_get_base(const GX86ModRMOperand *operand)
{
return operand->base;
}
/******************************************************************************
* *
* Paramètres : operand = opérande à consulter. *
* *
* Description : Fournit le décallage supplémentaire d'un opérande x86 ModRM. *
* *
* Retour : Décallage numérique pour l'opérande. *
* *
* Remarques : - *
* *
******************************************************************************/
const GImmOperand *g_x86_mod_rm_operand_get_displacement(const GX86ModRMOperand *operand)
{
return operand->displacement;
}