From d6b2e6639698674cbdaf7dc2e5f5a637abcfadb0 Mon Sep 17 00:00:00 2001 From: Cyrille Bagard Date: Tue, 18 Feb 2020 23:24:55 +0100 Subject: Relocated the raw instructions. --- plugins/arm/v7/fetch.c | 2 +- plugins/arm/v7/processor.c | 3 - plugins/dalvik/context.c | 2 +- plugins/elf/strings.c | 2 +- plugins/elf/symbols.c | 2 +- plugins/fmtp/parser.c | 2 +- plugins/pychrysalide/analysis/constants.c | 20 +- plugins/pychrysalide/arch/Makefile.am | 1 - plugins/pychrysalide/arch/constants.c | 43 +- plugins/pychrysalide/arch/constants.h | 3 - plugins/pychrysalide/arch/instructions/Makefile.am | 1 + plugins/pychrysalide/arch/instructions/constants.c | 42 + plugins/pychrysalide/arch/instructions/constants.h | 3 + plugins/pychrysalide/arch/instructions/module.c | 2 + plugins/pychrysalide/arch/instructions/raw.c | 525 +++++++++++++ plugins/pychrysalide/arch/instructions/raw.h | 45 ++ plugins/pychrysalide/arch/instructions/undefined.c | 4 +- plugins/pychrysalide/arch/instructions/undefined.h | 2 +- plugins/pychrysalide/arch/module.c | 2 - plugins/pychrysalide/arch/raw.c | 331 -------- plugins/pychrysalide/arch/raw.h | 45 -- plugins/readdex/ids.c | 2 +- plugins/readelf/strtab.c | 2 +- src/analysis/disass/area.c | 2 +- src/analysis/disass/block.c | 2 +- src/analysis/disass/links.c | 2 +- src/analysis/routine.c | 2 +- src/arch/Makefile.am | 1 - src/arch/instructions/Makefile.am | 1 + src/arch/instructions/raw.c | 870 +++++++++++++++++++++ src/arch/instructions/raw.h | 91 +++ src/arch/processor.c | 1 - src/arch/raw.c | 870 --------------------- src/arch/raw.h | 91 --- src/core/processors.c | 2 +- tests/arch/raw.py | 32 + 36 files changed, 1638 insertions(+), 1415 deletions(-) create mode 100644 plugins/pychrysalide/arch/instructions/raw.c create mode 100644 plugins/pychrysalide/arch/instructions/raw.h delete mode 100644 plugins/pychrysalide/arch/raw.c delete mode 100644 plugins/pychrysalide/arch/raw.h create mode 100644 src/arch/instructions/raw.c create mode 100644 src/arch/instructions/raw.h delete mode 100644 src/arch/raw.c delete mode 100644 src/arch/raw.h create mode 100644 tests/arch/raw.py diff --git a/plugins/arm/v7/fetch.c b/plugins/arm/v7/fetch.c index 6a4e47e..a24f99a 100644 --- a/plugins/arm/v7/fetch.c +++ b/plugins/arm/v7/fetch.c @@ -30,7 +30,7 @@ #include #include -#include +#include #include #include #include diff --git a/plugins/arm/v7/processor.c b/plugins/arm/v7/processor.c index e990344..61de7b5 100644 --- a/plugins/arm/v7/processor.c +++ b/plugins/arm/v7/processor.c @@ -27,9 +27,6 @@ #include -#include - - #include "arm.h" #include "context.h" #include "thumb_16.h" diff --git a/plugins/dalvik/context.c b/plugins/dalvik/context.c index f16f90e..b08678c 100644 --- a/plugins/dalvik/context.c +++ b/plugins/dalvik/context.c @@ -32,7 +32,7 @@ #include #include -#include +#include #include #include diff --git a/plugins/elf/strings.c b/plugins/elf/strings.c index dafc25b..7a2b34a 100644 --- a/plugins/elf/strings.c +++ b/plugins/elf/strings.c @@ -33,7 +33,7 @@ #include -#include +#include #include #include #include diff --git a/plugins/elf/symbols.c b/plugins/elf/symbols.c index 80a9143..2a164a0 100644 --- a/plugins/elf/symbols.c +++ b/plugins/elf/symbols.c @@ -31,7 +31,7 @@ #include -#include +#include #include #include #include diff --git a/plugins/fmtp/parser.c b/plugins/fmtp/parser.c index 9026fe2..3379ac2 100644 --- a/plugins/fmtp/parser.c +++ b/plugins/fmtp/parser.c @@ -28,7 +28,7 @@ #include -#include +#include diff --git a/plugins/pychrysalide/analysis/constants.c b/plugins/pychrysalide/analysis/constants.c index 2b7c3b5..d014fb7 100644 --- a/plugins/pychrysalide/analysis/constants.c +++ b/plugins/pychrysalide/analysis/constants.c @@ -68,16 +68,16 @@ bool define_analysis_content_constants(PyTypeObject *type) values = PyDict_New(); result = add_const_to_group(values, "UNDEFINED", MDS_UNDEFINED); - if (result) result = add_const_to_group(values, "4_BITS_UNSIGNED", MDS_4_BITS_UNSIGNED); - if (result) result = add_const_to_group(values, "8_BITS_UNSIGNED", MDS_8_BITS_UNSIGNED); - if (result) result = add_const_to_group(values, "16_BITS_UNSIGNED", MDS_16_BITS_UNSIGNED); - if (result) result = add_const_to_group(values, "32_BITS_UNSIGNED", MDS_32_BITS_UNSIGNED); - if (result) result = add_const_to_group(values, "64_BITS_UNSIGNED", MDS_64_BITS_UNSIGNED); - if (result) result = add_const_to_group(values, "4_BITS_SIGNED", MDS_4_BITS_SIGNED); - if (result) result = add_const_to_group(values, "8_BITS_SIGNED", MDS_8_BITS_SIGNED); - if (result) result = add_const_to_group(values, "16_BITS_SIGNED", MDS_16_BITS_SIGNED); - if (result) result = add_const_to_group(values, "32_BITS_SIGNED", MDS_32_BITS_SIGNED); - if (result) result = add_const_to_group(values, "64_BITS_SIGNED", MDS_64_BITS_SIGNED); + if (result) result = add_const_to_group(values, "_4_BITS_UNSIGNED", MDS_4_BITS_UNSIGNED); + if (result) result = add_const_to_group(values, "_8_BITS_UNSIGNED", MDS_8_BITS_UNSIGNED); + if (result) result = add_const_to_group(values, "_16_BITS_UNSIGNED", MDS_16_BITS_UNSIGNED); + if (result) result = add_const_to_group(values, "_32_BITS_UNSIGNED", MDS_32_BITS_UNSIGNED); + if (result) result = add_const_to_group(values, "_64_BITS_UNSIGNED", MDS_64_BITS_UNSIGNED); + if (result) result = add_const_to_group(values, "_4_BITS_SIGNED", MDS_4_BITS_SIGNED); + if (result) result = add_const_to_group(values, "_8_BITS_SIGNED", MDS_8_BITS_SIGNED); + if (result) result = add_const_to_group(values, "_16_BITS_SIGNED", MDS_16_BITS_SIGNED); + if (result) result = add_const_to_group(values, "_32_BITS_SIGNED", MDS_32_BITS_SIGNED); + if (result) result = add_const_to_group(values, "_64_BITS_SIGNED", MDS_64_BITS_SIGNED); if (!result) { diff --git a/plugins/pychrysalide/arch/Makefile.am b/plugins/pychrysalide/arch/Makefile.am index da78572..45fd567 100644 --- a/plugins/pychrysalide/arch/Makefile.am +++ b/plugins/pychrysalide/arch/Makefile.am @@ -10,7 +10,6 @@ libpychrysaarch_la_SOURCES = \ module.h module.c \ operand.h operand.c \ processor.h processor.c \ - raw.h raw.c \ register.h register.c \ vmpa.h vmpa.c diff --git a/plugins/pychrysalide/arch/constants.c b/plugins/pychrysalide/arch/constants.c index b6b0e62..5e9ad37 100644 --- a/plugins/pychrysalide/arch/constants.c +++ b/plugins/pychrysalide/arch/constants.c @@ -25,7 +25,7 @@ #include "constants.h" -#include +#include #include @@ -149,44 +149,3 @@ bool define_arch_vmpa_constants(PyTypeObject *type) return result; } - - -/****************************************************************************** -* * -* Paramètres : type = type dont le dictionnaire est à compléter. * -* * -* Description : Définit les constantes relatives aux instructions brutes. * -* * -* Retour : true en cas de succès de l'opération, false sinon. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool define_raw_instruction_constants(PyTypeObject *type) -{ - bool result; /* Bilan à retourner */ - PyObject *values; /* Groupe de valeurs à établir */ - - values = PyDict_New(); - - result = add_const_to_group(values, "PADDING", RIF_PADDING); - if (result) result = add_const_to_group(values, "STRING", RIF_STRING); - - if (!result) - { - Py_DECREF(values); - goto exit; - } - - result = attach_constants_group_to_type(type, true, "RawInstrFlag", values, - "Flags for some instruction properties.\n" - "\n" - "They can be seen as an extension of" \ - " pychrysalide.arch.ArchInstruction.ArchInstrFlag"); - - exit: - - return result; - -} diff --git a/plugins/pychrysalide/arch/constants.h b/plugins/pychrysalide/arch/constants.h index 9488bc0..ecd6ce8 100644 --- a/plugins/pychrysalide/arch/constants.h +++ b/plugins/pychrysalide/arch/constants.h @@ -37,9 +37,6 @@ bool define_arch_instruction_constants(PyTypeObject *); /* Définit les constantes relatives aux emplacements. */ bool define_arch_vmpa_constants(PyTypeObject *); -/* Définit les constantes relatives aux instructions brutes. */ -bool define_raw_instruction_constants(PyTypeObject *); - #endif /* _PLUGINS_PYCHRYSALIDE_ARCH_CONSTANTS_H */ diff --git a/plugins/pychrysalide/arch/instructions/Makefile.am b/plugins/pychrysalide/arch/instructions/Makefile.am index 885f96e..be7c6d7 100644 --- a/plugins/pychrysalide/arch/instructions/Makefile.am +++ b/plugins/pychrysalide/arch/instructions/Makefile.am @@ -4,6 +4,7 @@ noinst_LTLIBRARIES = libpychrysaarchinstructions.la libpychrysaarchinstructions_la_SOURCES = \ constants.h constants.c \ module.h module.c \ + raw.h raw.c \ undefined.h undefined.c libpychrysaarchinstructions_la_LIBADD = diff --git a/plugins/pychrysalide/arch/instructions/constants.c b/plugins/pychrysalide/arch/instructions/constants.c index 1f6a1d2..b5e08e1 100644 --- a/plugins/pychrysalide/arch/instructions/constants.c +++ b/plugins/pychrysalide/arch/instructions/constants.c @@ -25,6 +25,7 @@ #include "constants.h" +#include #include @@ -36,6 +37,47 @@ * * * Paramètres : type = type dont le dictionnaire est à compléter. * * * +* Description : Définit les constantes relatives aux instructions brutes. * +* * +* Retour : true en cas de succès de l'opération, false sinon. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool define_raw_instruction_constants(PyTypeObject *type) +{ + bool result; /* Bilan à retourner */ + PyObject *values; /* Groupe de valeurs à établir */ + + values = PyDict_New(); + + result = add_const_to_group(values, "PADDING", RIF_PADDING); + if (result) result = add_const_to_group(values, "STRING", RIF_STRING); + + if (!result) + { + Py_DECREF(values); + goto exit; + } + + result = attach_constants_group_to_type(type, true, "RawInstrFlag", values, + "Flags for some instruction properties.\n" + "\n" + "They can be seen as an extension of" \ + " pychrysalide.arch.ArchInstruction.ArchInstrFlag"); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : type = type dont le dictionnaire est à compléter. * +* * * Description : Définit les constantes liées aux comportements erratiques. * * * * Retour : true en cas de succès de l'opération, false sinon. * diff --git a/plugins/pychrysalide/arch/instructions/constants.h b/plugins/pychrysalide/arch/instructions/constants.h index c25f3f8..2f0c587 100644 --- a/plugins/pychrysalide/arch/instructions/constants.h +++ b/plugins/pychrysalide/arch/instructions/constants.h @@ -31,6 +31,9 @@ +/* Définit les constantes relatives aux instructions brutes. */ +bool define_raw_instruction_constants(PyTypeObject *); + /* Définit les constantes liées aux comportements erratiques. */ bool define_undefined_instruction_constants(PyTypeObject *); diff --git a/plugins/pychrysalide/arch/instructions/module.c b/plugins/pychrysalide/arch/instructions/module.c index 13a9c8e..5280db8 100644 --- a/plugins/pychrysalide/arch/instructions/module.c +++ b/plugins/pychrysalide/arch/instructions/module.c @@ -28,6 +28,7 @@ #include +#include "raw.h" #include "undefined.h" #include "../../helpers.h" @@ -95,6 +96,7 @@ bool populate_arch_instructions_module(void) result = true; + if (result) result = ensure_python_raw_instruction_is_registered(); if (result) result = ensure_python_undefined_instruction_is_registered(); assert(result); diff --git a/plugins/pychrysalide/arch/instructions/raw.c b/plugins/pychrysalide/arch/instructions/raw.c new file mode 100644 index 0000000..10e5c8f --- /dev/null +++ b/plugins/pychrysalide/arch/instructions/raw.c @@ -0,0 +1,525 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * raw.c - équivalent Python du fichier "arch/instructions/raw.h" + * + * Copyright (C) 2018-2020 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#include "raw.h" + + +#include + + +#include +#include +#include + + +#include "constants.h" +#include "../instruction.h" +#include "../vmpa.h" +#include "../../access.h" +#include "../../helpers.h" +#include "../../analysis/content.h" + + + +/* Accompagne la création d'une instance dérivée en Python. */ +static PyObject *py_raw_instruction_new(PyTypeObject *, PyObject *, PyObject *); + +/* Initialise une instance sur la base du dérivé de GObject. */ +static int py_raw_instruction_init(PyObject *, PyObject *, PyObject *); + +/* Indique si le contenu de l'instruction est du bourrage. */ +static PyObject *py_raw_instruction_get_padding(PyObject *, void *); + +/* Marque l'instruction comme ne contenant que du bourrage. */ +static int py_raw_instruction_set_padding(PyObject *, PyObject *, void *); + +/* Indique si le contenu de l'instruction est un texte. */ +static PyObject *py_raw_instruction_get_string(PyObject *, void *); + +/* Marque l'instruction comme contenant une chaîne de texte. */ +static int py_raw_instruction_set_string(PyObject *, PyObject *, void *); + + + +/****************************************************************************** +* * +* Paramètres : type = type du nouvel objet à mettre en place. * +* args = éventuelle liste d'arguments. * +* kwds = éventuel dictionnaire de valeurs mises à disposition. * +* * +* Description : Accompagne la création d'une instance dérivée en Python. * +* * +* Retour : Nouvel objet Python mis en place ou NULL en cas d'échec. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_raw_instruction_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObject *result; /* Objet à retourner */ + PyTypeObject *base; /* Type de base à dériver */ + bool first_time; /* Evite les multiples passages*/ + GType gtype; /* Nouveau type de processeur */ + bool status; /* Bilan d'un enregistrement */ + + /* Validations diverses */ + + base = get_python_raw_instruction_type(); + + if (type == base) + goto simple_way; + + /* Mise en place d'un type dédié */ + + first_time = (g_type_from_name(type->tp_name) == 0); + + gtype = build_dynamic_type(G_TYPE_RAW_INSTRUCTION, type->tp_name, NULL, NULL, NULL); + + if (first_time) + { + status = register_class_for_dynamic_pygobject(gtype, type, base); + + if (!status) + { + result = NULL; + goto exit; + } + + } + + /* On crée, et on laisse ensuite la main à PyGObject_Type.tp_init() */ + + simple_way: + + result = PyType_GenericNew(type, args, kwds); + + exit: + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet à initialiser (théoriquement). * +* args = arguments fournis à l'appel. * +* kwds = arguments de type key=val fournis. * +* * +* Description : Initialise une instance sur la base du dérivé de GObject. * +* * +* Retour : 0. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_raw_instruction_init(PyObject *self, PyObject *args, PyObject *kwds) +{ + int result; /* Bilan à retourner */ + vmpa2t *addr; /* Texte de lecture */ + unsigned long mem_size; /* Taille de portion brute */ + unsigned long long value; /* Valeur brute à considérer */ + GBinContent *content; /* Contenu à lire au besoin */ + unsigned long count; /* Nombre d'éléments à lister */ + unsigned int endian; /* Type de boutisme impliqué */ + int ret; /* Bilan de lecture des args. */ + PyObject *new_args; /* Nouveaux arguments épurés */ + PyObject *new_kwds; /* Nouveau dictionnaire épuré */ + GArchInstruction *fake; /* Instruction à copier */ + GArchInstruction *instr; /* Instruction à manipuler */ + size_t op_count; /* Nombre d'opérande à copier */ + size_t i; /* Boucle de parcours */ + GArchOperand *op; /* Opérande à transférer */ + + static char *kwlist[] = { "addr", "mem_size", "value", "content", "count", "endian", NULL }; + +#define RAW_INSTRUCTION_DOC \ + "The RawInstruction object handles data which is not (yet?) disassembled" \ + " as code in a binary.\n" \ + "\n" \ + "Raw values pointed by this kind of instruction can be immediate values" \ + " or strings.\n" \ + "\n" \ + "Instances can be created using one of the following constructors:\n" \ + "\n" \ + " RawInstruction(addr, size, value=int)\n" \ + " RawInstruction(addr, size, content=object, count=int, endian=int)" \ + "\n" \ + "Where addr is always a location defined by a pychrysalide.arch.vmpa" \ + " object and size is a pychrysalide.analysis.BinContent.MemoryDataSize" \ + " constant defining the size of the read immediate value(s).\n" \ + "\n" \ + "In the first case, value is used to build an immediate operand for the" \ + " instruction.\n" \ + "\n" \ + "In the second case, content is a pychrysalide.analysis.BinContent" \ + " instance, count states how many items belong to the array and endian" \ + " is a pychrysalide.analysis.BinContent.SourceEndian constant defining" \ + " the byte order used to read values." + + result = -1; + + /* Récupération des paramètres */ + + value = 0; + content = NULL; + count = 0; + endian = 0; + + ret = PyArg_ParseTupleAndKeywords(args, kwds, "O&k|KO&kI", kwlist, + convert_any_to_vmpa, &addr, &mem_size, + &value, convert_to_binary_content, &content, &count, &endian); + if (!ret) return -1; + + /* Initialisation d'un objet GLib */ + + new_args = PyTuple_New(0); + new_kwds = PyDict_New(); + + ret = PyGObject_Type.tp_init(self, new_args, new_kwds); + + Py_DECREF(new_kwds); + Py_DECREF(new_args); + + if (ret == -1) goto clean_exit; + + /* Eléments de base */ + + if (content != NULL) + fake = g_raw_instruction_new_array(content, mem_size, count, addr, endian); + else + fake = g_raw_instruction_new_from_value(addr, mem_size, value); + + if (fake == NULL) + { + PyErr_SetString(PyExc_ValueError, _("Unable to build the object with the given parameters.")); + goto clean_exit; + } + + instr = G_ARCH_INSTRUCTION(pygobject_get(self)); + + g_arch_instruction_lock_operands(fake); + + op_count = _g_arch_instruction_count_operands(fake); + + for (i = 0; i < op_count; i++) + { + op = _g_arch_instruction_get_operand(fake, i); + g_arch_instruction_attach_extra_operand(instr, op); + } + + g_arch_instruction_unlock_operands(fake); + + g_arch_instruction_set_range(instr, g_arch_instruction_get_range(fake)); + + g_object_unref(G_OBJECT(fake)); + + result = 0; + + clean_exit: + + clean_vmpa_arg(addr); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une instruction. * +* closure = adresse non utilisée ici. * +* * +* Description : Indique si le contenu de l'instruction est du bourrage. * +* * +* Retour : Valeur associée à la propriété consultée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_raw_instruction_get_padding(PyObject *self, void *closure) +{ + PyObject *result; /* Conversion à retourner */ + GRawInstruction *instr; /* Version native */ + bool state; /* Etat courant à consulter */ + +#define RAW_INSTRUCTION_PADDING_ATTRIB PYTHON_GETSET_DEF_FULL \ +( \ + padding, py_raw_instruction, \ + "Report if the instruction is seen as padding." \ +) + + instr = G_RAW_INSTRUCTION(pygobject_get(self)); + + state = g_raw_instruction_is_padding(instr); + + result = state ? Py_True : Py_False; + Py_INCREF(result); + + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* value = valeur fournie à intégrer ou prendre en compte. * +* closure = adresse non utilisée ici. * +* * +* Description : Marque l'instruction comme ne contenant que du bourrage. * +* * +* Retour : Bilan de l'opération pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_raw_instruction_set_padding(PyObject *self, PyObject *value, void *closure) +{ + bool state; /* Nouvel état à définir */ + GRawInstruction *instr; /* Version native */ + + if (value != Py_True && value != Py_False) + return -1; + + state = (value == Py_True); + + instr = G_RAW_INSTRUCTION(pygobject_get(self)); + + g_raw_instruction_mark_as_padding(instr, state); + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : self = classe représentant une instruction. * +* closure = adresse non utilisée ici. * +* * +* Description : Indique si le contenu de l'instruction est un texte. * +* * +* Retour : Valeur associée à la propriété consultée. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static PyObject *py_raw_instruction_get_string(PyObject *self, void *closure) +{ + PyObject *result; /* Conversion à retourner */ + GRawInstruction *instr; /* Version native */ + bool state; /* Etat courant à consulter */ + +#define RAW_INSTRUCTION_STRING_ATTRIB PYTHON_GETSET_DEF_FULL \ +( \ + string, py_raw_instruction, \ + "Report if the instruction is seen as a string." \ +) + + instr = G_RAW_INSTRUCTION(pygobject_get(self)); + + state = g_raw_instruction_is_string(instr); + + result = state ? Py_True : Py_False; + Py_INCREF(result); + + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : self = objet Python concerné par l'appel. * +* value = valeur fournie à intégrer ou prendre en compte. * +* closure = adresse non utilisée ici. * +* * +* Description : Marque l'instruction comme contenant une chaîne de texte. * +* * +* Retour : Bilan de l'opération pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static int py_raw_instruction_set_string(PyObject *self, PyObject *value, void *closure) +{ + bool state; /* Nouvel état à définir */ + GRawInstruction *instr; /* Version native */ + + if (value != Py_True && value != Py_False) + return -1; + + state = (value == Py_True); + + instr = G_RAW_INSTRUCTION(pygobject_get(self)); + + g_raw_instruction_mark_as_string(instr, state); + + return 0; + +} + + +/****************************************************************************** +* * +* Paramètres : - * +* * +* Description : Fournit un accès à une définition de type à diffuser. * +* * +* Retour : Définition d'objet pour Python. * +* * +* Remarques : - * +* * +******************************************************************************/ + +PyTypeObject *get_python_raw_instruction_type(void) +{ + static PyMethodDef py_raw_instruction_methods[] = { + { NULL } + }; + + static PyGetSetDef py_raw_instruction_getseters[] = { + RAW_INSTRUCTION_PADDING_ATTRIB, + RAW_INSTRUCTION_STRING_ATTRIB, + { NULL } + }; + + static PyTypeObject py_raw_instruction_type = { + + PyVarObject_HEAD_INIT(NULL, 0) + + .tp_name = "pychrysalide.arch.instructions.RawInstruction", + .tp_basicsize = sizeof(PyGObject), + + .tp_flags = Py_TPFLAGS_DEFAULT, + + .tp_doc = RAW_INSTRUCTION_DOC, + + .tp_methods = py_raw_instruction_methods, + .tp_getset = py_raw_instruction_getseters, + + .tp_init = py_raw_instruction_init, + .tp_new = py_raw_instruction_new, + + }; + + return &py_raw_instruction_type; + +} + + +/****************************************************************************** +* * +* Paramètres : module = module dont la définition est à compléter. * +* * +* Description : Prend en charge l'objet 'pychrysalide.arch...RawInstruction'.* +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool ensure_python_raw_instruction_is_registered(void) +{ + PyTypeObject *type; /* Type Python 'RawInstruction'*/ + PyObject *module; /* Module à recompléter */ + PyObject *dict; /* Dictionnaire du module */ + + type = get_python_raw_instruction_type(); + + if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) + { + module = get_access_to_python_module("pychrysalide.arch.instructions"); + + dict = PyModule_GetDict(module); + + if (!ensure_python_arch_instruction_is_registered()) + return false; + + if (!register_class_for_pygobject(dict, G_TYPE_RAW_INSTRUCTION, type, get_python_arch_instruction_type())) + return false; + + if (!define_raw_instruction_constants(type)) + return false; + + } + + return true; + +} + + +/****************************************************************************** +* * +* Paramètres : arg = argument quelconque à tenter de convertir. * +* dst = destination des valeurs récupérées en cas de succès. * +* * +* Description : Tente de convertir en instruction brute. * +* * +* Retour : Bilan de l'opération, voire indications supplémentaires. * +* * +* Remarques : - * +* * +******************************************************************************/ + +int convert_to_raw_instruction(PyObject *arg, void *dst) +{ + int result; /* Bilan à retourner */ + + result = PyObject_IsInstance(arg, (PyObject *)get_python_raw_instruction_type()); + + switch (result) + { + case -1: + /* L'exception est déjà fixée par Python */ + result = 0; + break; + + case 0: + PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to raw instruction"); + break; + + case 1: + *((GRawInstruction **)dst) = G_RAW_INSTRUCTION(pygobject_get(arg)); + break; + + default: + assert(false); + break; + + } + + return result; + +} diff --git a/plugins/pychrysalide/arch/instructions/raw.h b/plugins/pychrysalide/arch/instructions/raw.h new file mode 100644 index 0000000..46518b5 --- /dev/null +++ b/plugins/pychrysalide/arch/instructions/raw.h @@ -0,0 +1,45 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * raw.h - prototypes pour l'équivalent Python du fichier "arch/instructions/raw.h" + * + * Copyright (C) 2018-2020 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 this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_INSTRUCTIONS_RAW_H +#define _PLUGINS_PYCHRYSALIDE_ARCH_INSTRUCTIONS_RAW_H + + +#include +#include + + + +/* Fournit un accès à une définition de type à diffuser. */ +PyTypeObject *get_python_raw_instruction_type(void); + +/* Prend en charge l'objet 'pychrysalide.arch.instructions.RawInstruction'. */ +bool ensure_python_raw_instruction_is_registered(void); + +/* Tente de convertir en instruction brute. */ +int convert_to_raw_instruction(PyObject *, void *); + + + +#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_INSTRUCTIONS_RAW_H */ diff --git a/plugins/pychrysalide/arch/instructions/undefined.c b/plugins/pychrysalide/arch/instructions/undefined.c index a2c989e..ee7dc51 100644 --- a/plugins/pychrysalide/arch/instructions/undefined.c +++ b/plugins/pychrysalide/arch/instructions/undefined.c @@ -269,7 +269,7 @@ PyTypeObject *get_python_undefined_instruction_type(void) * * * Paramètres : module = module dont la définition est à compléter. * * * -* Description : Prend en charge l'objet 'pychrysalide.arch.ArchInstruction'. * +* Description : Prend en charge l'objet 'pychrysalide.....UndefInstruction'. * * * * Retour : Bilan de l'opération. * * * @@ -279,7 +279,7 @@ PyTypeObject *get_python_undefined_instruction_type(void) bool ensure_python_undefined_instruction_is_registered(void) { - PyTypeObject *type; /* Type Python 'UndefinedInstruction'*/ + PyTypeObject *type; /* Type 'UndefinedInstruction' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ diff --git a/plugins/pychrysalide/arch/instructions/undefined.h b/plugins/pychrysalide/arch/instructions/undefined.h index 4e9090c..3fa0453 100644 --- a/plugins/pychrysalide/arch/instructions/undefined.h +++ b/plugins/pychrysalide/arch/instructions/undefined.h @@ -34,7 +34,7 @@ /* Fournit un accès à une définition de type à diffuser. */ PyTypeObject *get_python_undefined_instruction_type(void); -/* Prend en charge l'objet 'pychrysalide.arch.UndefinedInstruction'. */ +/* Prend en charge l'objet 'pychrysalide.arch.instructions.UndefInstruction'. */ bool ensure_python_undefined_instruction_is_registered(void); /* Tente de convertir en instruction non définie. */ diff --git a/plugins/pychrysalide/arch/module.c b/plugins/pychrysalide/arch/module.c index 05942a5..1fa017d 100644 --- a/plugins/pychrysalide/arch/module.c +++ b/plugins/pychrysalide/arch/module.c @@ -38,7 +38,6 @@ #include "instruction.h" #include "operand.h" #include "processor.h" -#include "raw.h" #include "register.h" #include "vmpa.h" #include "instructions/module.h" @@ -174,7 +173,6 @@ bool populate_arch_module(void) if (result) result = ensure_python_arch_instruction_is_registered(); if (result) result = ensure_python_arch_operand_is_registered(); if (result) result = ensure_python_arch_processor_is_registered(); - if (result) result = ensure_python_raw_instruction_is_registered(); if (result) result = ensure_python_arch_register_is_registered(); if (result) result = ensure_python_vmpa_is_registered(); if (result) result = ensure_python_mrange_is_registered(); diff --git a/plugins/pychrysalide/arch/raw.c b/plugins/pychrysalide/arch/raw.c deleted file mode 100644 index 43f2e5e..0000000 --- a/plugins/pychrysalide/arch/raw.c +++ /dev/null @@ -1,331 +0,0 @@ - -/* Chrysalide - Outil d'analyse de fichiers binaires - * raw.c - équivalent Python du fichier "arch/raw.h" - * - * Copyright (C) 2018-2020 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 this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - - -#include "raw.h" - - -#include - - -#include - - -#include "constants.h" -#include "instruction.h" -#include "../access.h" -#include "../helpers.h" - - - -#define RAW_INSTRUCTION_DOC \ - "The RawInstruction object handles data which is not (yet?) disassembled" \ - " as code in a binary." - - -/* Indique si le contenu de l'instruction est du bourrage. */ -static PyObject *py_raw_instruction_get_padding(PyObject *, void *); - -/* Marque l'instruction comme ne contenant que du bourrage. */ -static int py_raw_instruction_set_padding(PyObject *, PyObject *, void *); - -/* Indique si le contenu de l'instruction est un texte. */ -static PyObject *py_raw_instruction_get_string(PyObject *, void *); - -/* Marque l'instruction comme contenant une chaîne de texte. */ -static int py_raw_instruction_set_string(PyObject *, PyObject *, void *); - - - -/****************************************************************************** -* * -* Paramètres : self = classe représentant une instruction. * -* closure = adresse non utilisée ici. * -* * -* Description : Indique si le contenu de l'instruction est du bourrage. * -* * -* Retour : Valeur associée à la propriété consultée. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static PyObject *py_raw_instruction_get_padding(PyObject *self, void *closure) -{ - PyObject *result; /* Conversion à retourner */ - GRawInstruction *instr; /* Version native */ - bool state; /* Etat courant à consulter */ - -#define RAW_INSTRUCTION_PADDING_ATTRIB PYTHON_GETSET_DEF_FULL \ -( \ - padding, py_raw_instruction, \ - "Report if the instruction is seen as padding." \ -) - - instr = G_RAW_INSTRUCTION(pygobject_get(self)); - - state = g_raw_instruction_is_padding(instr); - - result = state ? Py_True : Py_False; - Py_INCREF(result); - - - return result; - -} - - -/****************************************************************************** -* * -* Paramètres : self = objet Python concerné par l'appel. * -* value = valeur fournie à intégrer ou prendre en compte. * -* closure = adresse non utilisée ici. * -* * -* Description : Marque l'instruction comme ne contenant que du bourrage. * -* * -* Retour : Bilan de l'opération pour Python. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static int py_raw_instruction_set_padding(PyObject *self, PyObject *value, void *closure) -{ - bool state; /* Nouvel état à définir */ - GRawInstruction *instr; /* Version native */ - - if (value != Py_True && value != Py_False) - return -1; - - state = (value == Py_True); - - instr = G_RAW_INSTRUCTION(pygobject_get(self)); - - g_raw_instruction_mark_as_padding(instr, state); - - return 0; - -} - - -/****************************************************************************** -* * -* Paramètres : self = classe représentant une instruction. * -* closure = adresse non utilisée ici. * -* * -* Description : Indique si le contenu de l'instruction est un texte. * -* * -* Retour : Valeur associée à la propriété consultée. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static PyObject *py_raw_instruction_get_string(PyObject *self, void *closure) -{ - PyObject *result; /* Conversion à retourner */ - GRawInstruction *instr; /* Version native */ - bool state; /* Etat courant à consulter */ - -#define RAW_INSTRUCTION_STRING_ATTRIB PYTHON_GETSET_DEF_FULL \ -( \ - string, py_raw_instruction, \ - "Report if the instruction is seen as a string." \ -) - - instr = G_RAW_INSTRUCTION(pygobject_get(self)); - - state = g_raw_instruction_is_string(instr); - - result = state ? Py_True : Py_False; - Py_INCREF(result); - - - return result; - -} - - -/****************************************************************************** -* * -* Paramètres : self = objet Python concerné par l'appel. * -* value = valeur fournie à intégrer ou prendre en compte. * -* closure = adresse non utilisée ici. * -* * -* Description : Marque l'instruction comme contenant une chaîne de texte. * -* * -* Retour : Bilan de l'opération pour Python. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static int py_raw_instruction_set_string(PyObject *self, PyObject *value, void *closure) -{ - bool state; /* Nouvel état à définir */ - GRawInstruction *instr; /* Version native */ - - if (value != Py_True && value != Py_False) - return -1; - - state = (value == Py_True); - - instr = G_RAW_INSTRUCTION(pygobject_get(self)); - - g_raw_instruction_mark_as_string(instr, state); - - return 0; - -} - - -/****************************************************************************** -* * -* Paramètres : - * -* * -* Description : Fournit un accès à une définition de type à diffuser. * -* * -* Retour : Définition d'objet pour Python. * -* * -* Remarques : - * -* * -******************************************************************************/ - -PyTypeObject *get_python_raw_instruction_type(void) -{ - static PyMethodDef py_raw_instruction_methods[] = { - { NULL } - }; - - static PyGetSetDef py_raw_instruction_getseters[] = { - RAW_INSTRUCTION_PADDING_ATTRIB, - RAW_INSTRUCTION_STRING_ATTRIB, - { NULL } - }; - - static PyTypeObject py_raw_instruction_type = { - - PyVarObject_HEAD_INIT(NULL, 0) - - .tp_name = "pychrysalide.arch.RawInstruction", - .tp_basicsize = sizeof(PyGObject), - - .tp_flags = Py_TPFLAGS_DEFAULT, - - .tp_doc = RAW_INSTRUCTION_DOC, - - .tp_methods = py_raw_instruction_methods, - .tp_getset = py_raw_instruction_getseters, - - }; - - return &py_raw_instruction_type; - -} - - -/****************************************************************************** -* * -* Paramètres : module = module dont la définition est à compléter. * -* * -* Description : Prend en charge l'objet 'pychrysalide.arch.ArchInstruction'. * -* * -* Retour : Bilan de l'opération. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool ensure_python_raw_instruction_is_registered(void) -{ - PyTypeObject *type; /* Type Python 'RawInstruction'*/ - PyObject *module; /* Module à recompléter */ - PyObject *dict; /* Dictionnaire du module */ - - type = get_python_raw_instruction_type(); - - if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) - { - module = get_access_to_python_module("pychrysalide.arch"); - - dict = PyModule_GetDict(module); - - if (!ensure_python_arch_instruction_is_registered()) - return false; - - if (!register_class_for_pygobject(dict, G_TYPE_RAW_INSTRUCTION, type, get_python_arch_instruction_type())) - return false; - - if (!define_raw_instruction_constants(type)) - return false; - - } - - return true; - -} - - -/****************************************************************************** -* * -* Paramètres : arg = argument quelconque à tenter de convertir. * -* dst = destination des valeurs récupérées en cas de succès. * -* * -* Description : Tente de convertir en instruction brute. * -* * -* Retour : Bilan de l'opération, voire indications supplémentaires. * -* * -* Remarques : - * -* * -******************************************************************************/ - -int convert_to_raw_instruction(PyObject *arg, void *dst) -{ - int result; /* Bilan à retourner */ - - result = PyObject_IsInstance(arg, (PyObject *)get_python_raw_instruction_type()); - - switch (result) - { - case -1: - /* L'exception est déjà fixée par Python */ - result = 0; - break; - - case 0: - PyErr_SetString(PyExc_TypeError, "unable to convert the provided argument to raw instruction"); - break; - - case 1: - *((GRawInstruction **)dst) = G_RAW_INSTRUCTION(pygobject_get(arg)); - break; - - default: - assert(false); - break; - - } - - return result; - -} diff --git a/plugins/pychrysalide/arch/raw.h b/plugins/pychrysalide/arch/raw.h deleted file mode 100644 index e05e773..0000000 --- a/plugins/pychrysalide/arch/raw.h +++ /dev/null @@ -1,45 +0,0 @@ - -/* Chrysalide - Outil d'analyse de fichiers binaires - * raw.h - prototypes pour l'équivalent Python du fichier "arch/raw.h" - * - * Copyright (C) 2018-2020 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 this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - - -#ifndef _PLUGINS_PYCHRYSALIDE_ARCH_RAW_H -#define _PLUGINS_PYCHRYSALIDE_ARCH_RAW_H - - -#include -#include - - - -/* Fournit un accès à une définition de type à diffuser. */ -PyTypeObject *get_python_raw_instruction_type(void); - -/* Prend en charge l'objet 'pychrysalide.arch.RawInstruction'. */ -bool ensure_python_raw_instruction_is_registered(void); - -/* Tente de convertir en instruction brute. */ -int convert_to_raw_instruction(PyObject *, void *); - - - -#endif /* _PLUGINS_PYCHRYSALIDE_ARCH_RAW_H */ diff --git a/plugins/readdex/ids.c b/plugins/readdex/ids.c index 5c812a9..72c4072 100644 --- a/plugins/readdex/ids.c +++ b/plugins/readdex/ids.c @@ -29,7 +29,7 @@ #include -#include +#include #include #include #include diff --git a/plugins/readelf/strtab.c b/plugins/readelf/strtab.c index 141c868..2420da6 100644 --- a/plugins/readelf/strtab.c +++ b/plugins/readelf/strtab.c @@ -28,7 +28,7 @@ #include -#include +#include #include #include diff --git a/src/analysis/disass/area.c b/src/analysis/disass/area.c index f010fa9..eaec25d 100644 --- a/src/analysis/disass/area.c +++ b/src/analysis/disass/area.c @@ -34,7 +34,7 @@ #include "../routine.h" #include "../contents/restricted.h" -#include "../../arch/raw.h" +#include "../../arch/instructions/raw.h" #include "../../common/bits.h" #include "../../common/sort.h" #include "../../core/global.h" diff --git a/src/analysis/disass/block.c b/src/analysis/disass/block.c index e3f19d3..c80d464 100644 --- a/src/analysis/disass/block.c +++ b/src/analysis/disass/block.c @@ -32,7 +32,7 @@ #include "../block-int.h" -#include "../../arch/raw.h" +#include "../../arch/instructions/raw.h" #include "../../common/extstr.h" #include "../../core/params.h" #include "../../glibext/gbinarycursor.h" diff --git a/src/analysis/disass/links.c b/src/analysis/disass/links.c index e22fb05..c4e7076 100644 --- a/src/analysis/disass/links.c +++ b/src/analysis/disass/links.c @@ -28,7 +28,7 @@ #include "../../arch/instruction.h" -#include "../../arch/raw.h" +#include "../../arch/instructions/raw.h" #include "../../arch/target.h" #include "../../arch/operands/immediate.h" #include "../../arch/operands/targetable.h" diff --git a/src/analysis/routine.c b/src/analysis/routine.c index b9106d9..02bd167 100644 --- a/src/analysis/routine.c +++ b/src/analysis/routine.c @@ -35,7 +35,7 @@ #include "routine-int.h" -#include "../arch/raw.h" +#include "../arch/instructions/raw.h" #include "../common/extstr.h" #include "../core/params.h" #include "../glibext/gbinarycursor.h" diff --git a/src/arch/Makefile.am b/src/arch/Makefile.am index 46f78e8..af0c79a 100644 --- a/src/arch/Makefile.am +++ b/src/arch/Makefile.am @@ -18,7 +18,6 @@ libarch_la_SOURCES = \ processor-int.h \ processor.h processor.c \ proxy.h proxy.c \ - raw.h raw.c \ register-int.h \ register.h register.c \ storage.h storage.c \ diff --git a/src/arch/instructions/Makefile.am b/src/arch/instructions/Makefile.am index c060c7e..584173f 100644 --- a/src/arch/instructions/Makefile.am +++ b/src/arch/instructions/Makefile.am @@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libarchinstructions.la libarchinstructions_la_SOURCES = \ + raw.h raw.c \ undefined-int.h \ undefined.h undefined.c diff --git a/src/arch/instructions/raw.c b/src/arch/instructions/raw.c new file mode 100644 index 0000000..7c2535c --- /dev/null +++ b/src/arch/instructions/raw.c @@ -0,0 +1,870 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * artificial.c - instructions pures vues de l'esprit + * + * Copyright (C) 2014-2020 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 Chrysalide. If not, see . + */ + + +#include "raw.h" + + +#include +#include +#include + + +#include + + +#include "../instruction-int.h" +#include "../target.h" +#include "../operands/immediate.h" + + + +/* ------------------------- INSTRUCTION INCONNUE / DONNEES ------------------------- */ + + +/* Définition générique d'une instruction brute d'architecture (instance) */ +struct _GRawInstruction +{ + GArchInstruction parent; /* A laisser en premier */ + +}; + +/* Définition générique d'une instruction brute d'architecture (classe) */ +struct _GRawInstructionClass +{ + GArchInstructionClass parent; /* A laisser en premier */ + +}; + + +/* Initialise la classe des instructions brutes d'architecture. */ +static void g_raw_instruction_class_init(GRawInstructionClass *); + +/* Initialise une instance d'instruction brute d'architecture. */ +static void g_raw_instruction_init(GRawInstruction *); + +/* Supprime toutes les références externes. */ +static void g_raw_instruction_dispose(GRawInstruction *); + +/* Procède à la libération totale de la mémoire. */ +static void g_raw_instruction_finalize(GRawInstruction *); + +/* Indique l'encodage d'une instruction de façon détaillée. */ +static const char *g_raw_instruction_get_encoding(const GRawInstruction *); + +/* Fournit le nom humain de l'instruction manipulée. */ +static const char *g_raw_instruction_get_keyword(const GRawInstruction *); + + + +/* -------------------- CONSERVATION SUR DISQUE DES INSTRUCTIONS -------------------- */ + + +/* Charge une instruction depuis une mémoire tampon. */ +static bool g_raw_instruction_unserialize(GRawInstruction *, GAsmStorage *, GBinFormat *, packed_buffer *); + +/* Sauvegarde une instruction dans une mémoire tampon. */ +static bool g_raw_instruction_serialize(GRawInstruction *, GAsmStorage *, packed_buffer *); + + + +/* ------------------------ OFFRE DE CAPACITES DE GENERATION ------------------------ */ + + +/* Ajoute à un tampon GLib le contenu de l'instance spécifiée. */ +static void g_raw_instruction_print(GRawInstruction *, GBufferLine *, size_t, size_t, const GBinContent *); + + + +/* ---------------------------------------------------------------------------------- */ +/* INSTRUCTION INCONNUE / DONNEES */ +/* ---------------------------------------------------------------------------------- */ + + +/* Indique le type défini pour une instruction inconnue d'architecture. */ +G_DEFINE_TYPE(GRawInstruction, g_raw_instruction, G_TYPE_ARCH_INSTRUCTION); + + +/****************************************************************************** +* * +* Paramètres : klass = classe à initialiser. * +* * +* Description : Initialise la classe des instructions brutes d'architecture. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_raw_instruction_class_init(GRawInstructionClass *klass) +{ + GObjectClass *object; /* Autre version de la classe */ + GArchInstructionClass *instr; /* Encore une autre vision... */ + + object = G_OBJECT_CLASS(klass); + + object->dispose = (GObjectFinalizeFunc/* ! */)g_raw_instruction_dispose; + object->finalize = (GObjectFinalizeFunc)g_raw_instruction_finalize; + + instr = G_ARCH_INSTRUCTION_CLASS(klass); + + instr->get_encoding = (get_instruction_encoding_fc)g_raw_instruction_get_encoding; + instr->get_keyword = (get_instruction_keyword_fc)g_raw_instruction_get_keyword; + + instr->unserialize = (unserialize_instruction_fc)g_raw_instruction_unserialize; + instr->serialize = (serialize_instruction_fc)g_raw_instruction_serialize; + + instr->print = (print_instruction_fc)g_raw_instruction_print; + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instance à initialiser. * +* * +* Description : Initialise une instance d'instruction brute d'architecture. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_raw_instruction_init(GRawInstruction *instr) +{ + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instance d'objet GLib à traiter. * +* * +* Description : Supprime toutes les références externes. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_raw_instruction_dispose(GRawInstruction *instr) +{ + G_OBJECT_CLASS(g_raw_instruction_parent_class)->dispose(G_OBJECT(instr)); + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instance d'objet GLib à traiter. * +* * +* Description : Procède à la libération totale de la mémoire. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_raw_instruction_finalize(GRawInstruction *instr) +{ + G_OBJECT_CLASS(g_raw_instruction_parent_class)->finalize(G_OBJECT(instr)); + +} + + +/****************************************************************************** +* * +* Paramètres : addr = position à associer à l'instruction. * +* size = taille de l'opérande souhaitée. * +* value = valeur sur x bits à venir récupérer. * +* * +* Description : Crée une instruction de type 'db/dw/etc' simple. * +* * +* Retour : Instruction mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GArchInstruction *g_raw_instruction_new_from_value(const vmpa2t *addr, MemoryDataSize size, uint64_t value) +{ + GArchInstruction *result; /* Instruction à retourner */ + GImmOperand *operand; /* Octet non décodé à afficher */ + mrange_t range; /* Couverture de l'instruction */ + + result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); + + operand = G_IMM_OPERAND(g_imm_operand_new_from_value(size, value)); + if (operand == NULL) goto grinfv_error; + + g_imm_operand_pad(operand, true); + + g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); + + switch (size) + { + case MDS_8_BITS_UNSIGNED: + case MDS_8_BITS_SIGNED: + init_mrange(&range, addr, 1); + break; + + case MDS_16_BITS_UNSIGNED: + case MDS_16_BITS_SIGNED: + init_mrange(&range, addr, 2); + break; + + case MDS_32_BITS_UNSIGNED: + case MDS_32_BITS_SIGNED: + init_mrange(&range, addr, 4); + break; + + case MDS_64_BITS_UNSIGNED: + case MDS_64_BITS_SIGNED: + init_mrange(&range, addr, 8); + break; + + default: + assert(false); + goto grinfv_error; + break; + + } + + g_arch_instruction_set_range(result, &range); + + return result; + + grinfv_error: + + g_object_unref(G_OBJECT(result)); + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : content = flux de données à analyser. * +* addr = position courante dans ce flux. [OUT] * +* * +* Description : Crée une instruction de type 'db/dw/etc' pour un uleb128. * +* * +* Retour : Instruction mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GArchInstruction *g_raw_instruction_new_uleb128(const GBinContent *content, vmpa2t *addr) +{ + GArchInstruction *result; /* Instruction à retourner */ + vmpa2t start; /* Départ original de lecture */ + uleb128_t value; /* Valeur uleb128 à représenter*/ + phys_t diff; /* Couverture de la lecture */ + MemoryDataSize leb_size; /* Taille de la valeur */ + GImmOperand *operand; /* Octet non décodé à afficher */ + mrange_t range; /* Couverture de l'instruction */ + + copy_vmpa(&start, addr); + + if (!g_binary_content_read_uleb128(content, addr, &value)) + goto grinu_error; + + diff = compute_vmpa_diff(&start, addr); + + leb_size = MDS_FROM_BYTES(diff); + assert(leb_size != MDS_UNDEFINED); + + result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); + + init_mrange(&range, &start, diff); + g_arch_instruction_set_range(result, &range); + + operand = G_IMM_OPERAND(g_imm_operand_new_from_value(leb_size, (uint64_t)value)); + if (operand == NULL) goto grinu_error; + + g_imm_operand_pad(operand, true); + + g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); + + return result; + + grinu_error: + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : content = flux de données à analyser. * +* addr = position courante dans ce flux. [OUT] * +* * +* Description : Crée une instruction de type 'db/dw/etc' pour un sleb128. * +* * +* Retour : Instruction mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GArchInstruction *g_raw_instruction_new_sleb128(const GBinContent *content, vmpa2t *addr) +{ + GArchInstruction *result; /* Instruction à retourner */ + vmpa2t start; /* Départ original de lecture */ + uleb128_t value; /* Valeur uleb128 à représenter*/ + phys_t diff; /* Couverture de la lecture */ + MemoryDataSize leb_size; /* Taille de la valeur */ + GImmOperand *operand; /* Octet non décodé à afficher */ + mrange_t range; /* Couverture de l'instruction */ + + copy_vmpa(&start, addr); + + if (!g_binary_content_read_uleb128(content, addr, &value)) + goto grins_error; + + diff = compute_vmpa_diff(&start, addr); + + leb_size = MDS_FROM_BYTES(diff) | MDS_SIGN; + assert(leb_size != MDS_SIGN); + + result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); + + init_mrange(&range, &start, diff); + g_arch_instruction_set_range(result, &range); + + operand = G_IMM_OPERAND(g_imm_operand_new_from_value(leb_size, (uint64_t)value)); + if (operand == NULL) goto grins_error; + + g_imm_operand_pad(operand, true); + + g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); + + return result; + + grins_error: + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : content = flux de données à analyser. * +* size = taille de chacun des éléments à représenter. * +* count = nombre de ces éléments. * +* addr = position courante dans ce flux. [OUT] * +* endian = ordre des bits dans la source. * +* * +* Description : Crée une instruction de type 'db/dw/etc' étendue. * +* * +* Retour : Instruction mise en place. * +* * +* Remarques : - * +* * +******************************************************************************/ + +GArchInstruction *g_raw_instruction_new_array(const GBinContent *content, MemoryDataSize size, size_t count, vmpa2t *addr, SourceEndian endian) +{ + GArchInstruction *result; /* Instruction à retourner */ + vmpa2t old; /* Sauvegarde de la position */ + size_t i; /* Boucle de parcours */ + GImmOperand *operand; /* Octet non décodé à afficher */ + mrange_t range; /* Couverture de l'instruction */ + + /* Par soucis de cohérence */ + if (count == 0) return NULL; + + result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); + + copy_vmpa(&old, addr); + + for (i = 0; i < count; i++) + { + operand = G_IMM_OPERAND(g_imm_operand_new_from_data(size, content, addr, endian)); + if (operand == NULL) goto grina_error; + + g_imm_operand_pad(operand, true); + + g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); + + } + + init_mrange(&range, &old, compute_vmpa_diff(addr, &old)); + + g_arch_instruction_set_range(result, &range); + + return result; + + grina_error: + + g_object_unref(G_OBJECT(result)); + + return NULL; + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction quelconque à consulter. * +* * +* Description : Indique l'encodage d'une instruction de façon détaillée. * +* * +* Retour : Description humaine de l'encodage utilisé. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static const char *g_raw_instruction_get_encoding(const GRawInstruction *instr) +{ + const char *result; /* Description à retourner */ + + if (g_raw_instruction_is_string(instr)) + result = _("String"); + else + result = _("Raw"); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction d'assemblage à consulter. * +* * +* Description : Fournit le nom humain de l'instruction manipulée. * +* * +* Retour : Mot clef de bas niveau. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static const char *g_raw_instruction_get_keyword(const GRawInstruction *instr) +{ + GArchOperand *operand; /* Octet décodé à afficher */ + MemoryDataSize size; /* Taille de valeur associée */ + + static char *defines[] = { "dn", "db", "dw", "dd", "dq" }; + + operand = g_arch_instruction_get_operand(G_ARCH_INSTRUCTION(instr), 0); + + if (G_IS_TARGET_OPERAND(operand)) + size = g_target_operand_get_size(G_TARGET_OPERAND(operand)); + else + size = g_imm_operand_get_size(G_IMM_OPERAND(operand)); + + g_object_unref(G_OBJECT(operand)); + + return defines[MDS_RANGE(size)]; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* CONSERVATION SUR DISQUE DES INSTRUCTIONS */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : instr = instruction d'assemblage à consulter. * +* storage = mécanisme de sauvegarde à manipuler. * +* format = format binaire chargé associé à l'architecture. * +* pbuf = zone tampon à remplir. * +* * +* Description : Charge une instruction depuis une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_raw_instruction_unserialize(GRawInstruction *instr, GAsmStorage *storage, GBinFormat *format, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + GArchInstructionClass *parent; /* Classe parente à consulter */ + uint8_t boolean; /* Valeur booléenne */ + + parent = G_ARCH_INSTRUCTION_CLASS(g_raw_instruction_parent_class); + + result = parent->unserialize(G_ARCH_INSTRUCTION(instr), storage, format, pbuf); + + if (result) + { + result = extract_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); + + if (result) + g_raw_instruction_mark_as_padding(instr, (boolean == 1)); + + } + + if (result) + { + result = extract_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); + + if (result) + g_raw_instruction_mark_as_string(instr, (boolean == 1)); + + } + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction d'assemblage à consulter. * +* storage = mécanisme de sauvegarde à manipuler. * +* pbuf = zone tampon à remplir. * +* * +* Description : Sauvegarde une instruction dans une mémoire tampon. * +* * +* Retour : Bilan de l'opération. * +* * +* Remarques : - * +* * +******************************************************************************/ + +static bool g_raw_instruction_serialize(GRawInstruction *instr, GAsmStorage *storage, packed_buffer *pbuf) +{ + bool result; /* Bilan à retourner */ + GArchInstructionClass *parent; /* Classe parente à consulter */ + uint8_t boolean; /* Valeur booléenne */ + + parent = G_ARCH_INSTRUCTION_CLASS(g_raw_instruction_parent_class); + + result = parent->serialize(G_ARCH_INSTRUCTION(instr), storage, pbuf); + + if (result) + { + boolean = (g_raw_instruction_is_padding(instr) ? 1 : 0); + result = extend_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); + } + + if (result) + { + boolean = (g_raw_instruction_is_string(instr) ? 1 : 0); + result = extend_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); + } + + return result; + +} + + + +/* ---------------------------------------------------------------------------------- */ +/* OFFRE DE CAPACITES DE GENERATION */ +/* ---------------------------------------------------------------------------------- */ + + +/****************************************************************************** +* * +* Paramètres : instr = instruction d'assemblage à représenter. * +* line = ligne de rendu à compléter. * +* index = indice de cette même ligne dans le tampon global. * +* repeat = indice d'utilisations successives du générateur. * +* content = éventuel contenu binaire brut à imprimer. * +* * +* Description : Ajoute à un tampon GLib le contenu de l'instance spécifiée. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +static void g_raw_instruction_print(GRawInstruction *instr, GBufferLine *line, size_t index, size_t repeat, const GBinContent *content) +{ + GArchInstruction *base; /* Autre version de l'instance */ + phys_t max_displayed_len; /* Quantité de code affichée */ + const char *key; /* Mot clef principal */ + size_t klen; /* Taille de ce mot clef */ + char *string; /* Chaîne reconstituée */ + size_t iter; /* Tête d'écriture */ + bool first; /* Mémorise une énumération */ + size_t count; /* Nombre d'opérandes en place */ + size_t i; /* Boucle de parcours */ + GArchOperand *op; /* Opérande à manipuler */ + GImmOperand *imm; /* Version opérande de valeur */ + char byte; /* Octet à afficher (ou pas) */ +#ifndef NDEBUG + bool status; /* Bilan d'une récupération */ +#endif + + base = G_ARCH_INSTRUCTION(instr); + + /* Localisation */ + + g_buffer_line_fill_vmpa(line, get_mrange_addr(&base->range), MDS_32_BITS_UNSIGNED, MDS_32_BITS_UNSIGNED); + + /* Contenu */ + + if (g_raw_instruction_is_padding(instr)) + max_displayed_len = 0; + + else if (g_raw_instruction_is_string(instr)) + max_displayed_len = 1; + + else + { + max_displayed_len = get_mrange_length(&base->range); + max_displayed_len /= g_arch_instruction_count_operands(base); + } + + g_buffer_line_fill_content(line, content, &base->range, max_displayed_len); + + /* Zone du code d'assemblage */ + + key = g_arch_instruction_get_keyword(base); + klen = strlen(key); + + g_buffer_line_append_text(line, BLC_ASSEMBLY_HEAD, key, klen, RTT_INSTRUCTION, NULL); + + if (g_raw_instruction_is_padding(instr)) + g_buffer_line_append_text(line, BLC_ASSEMBLY, "...", 3, RTT_RAW, NULL); + + else + { + string = NULL; + iter = 0; + + first = true; + + g_arch_instruction_lock_operands(base); + + count = _g_arch_instruction_count_operands(base); + + for (i = 0; i < count; i++) + { + op = _g_arch_instruction_get_operand(base, i); + + if (!G_IS_IMM_OPERAND(op)) + goto grip_fallback; + + imm = G_IMM_OPERAND(op); + + if (g_imm_operand_get_size(imm) != MDS_8_BITS) + goto grip_fallback; + + if (!g_raw_instruction_is_string(instr) && g_imm_operand_get_display(imm) != IOD_CHAR) + goto grip_fallback; + +#ifndef NDEBUG + status = g_imm_operand_get_value(imm, MDS_8_BITS, &byte); + assert(status); +#else + g_imm_operand_get_value(imm, MDS_8_BITS, &byte); +#endif + + /* Si le caractère doit apparaître en hexadécimal... */ + + if (!isprint(byte)) + goto grip_fallback; + + /* Impression de l'octet */ + + if (string == NULL) + { + string = (char *)calloc(count + 3, sizeof(char)); + + strcpy(string, "\""); + iter = 1; + + } + + string[iter++] = byte; + + g_object_unref(G_OBJECT(op)); + + continue; + + grip_fallback: + + /* Si une chaîne précède */ + + if (string != NULL && iter > 1) + { + if (!first) + { + g_buffer_line_append_text(line, BLC_ASSEMBLY, ",", 1, RTT_PUNCT, NULL); + g_buffer_line_append_text(line, BLC_ASSEMBLY, " ", 1, RTT_RAW, NULL); + } + else + first = false; + + string[iter++] = '"'; + + g_buffer_line_append_text(line, BLC_ASSEMBLY, string, iter, RTT_STRING, NULL); + + iter = 1; + + } + + /* Intégration en tant qu'opérande classique */ + + if (!first) + { + g_buffer_line_append_text(line, BLC_ASSEMBLY, ",", 1, RTT_PUNCT, NULL); + g_buffer_line_append_text(line, BLC_ASSEMBLY, " ", 1, RTT_RAW, NULL); + } + else + first = false; + + g_arch_operand_print(op, line); + + g_object_unref(G_OBJECT(op)); + + } + + /* Si au final une chaîne traine encore */ + + if (string != NULL && iter > 1) + { + if (!first) + { + g_buffer_line_append_text(line, BLC_ASSEMBLY, ",", 1, RTT_PUNCT, NULL); + g_buffer_line_append_text(line, BLC_ASSEMBLY, " ", 1, RTT_RAW, NULL); + } + + string[iter++] = '"'; + + g_buffer_line_append_text(line, BLC_ASSEMBLY, string, iter, RTT_STRING, NULL); + + } + + g_arch_instruction_unlock_operands(base); + + if (string != NULL) + free(string); + + } + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction à traiter. * +* is_padding = nouveau statut à associer au contenu. * +* * +* Description : Marque l'instruction comme ne contenant que du bourrage. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_raw_instruction_mark_as_padding(GRawInstruction *instr, bool is_padding) +{ + if (is_padding) + g_arch_instruction_set_flag(G_ARCH_INSTRUCTION(instr), RIF_PADDING); + else + g_arch_instruction_unset_flag(G_ARCH_INSTRUCTION(instr), RIF_PADDING); + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction à traiter. * +* is_padding = nouveau statut à associer au contenu. * +* * +* Description : Indique si le contenu de l'instruction est du bourrage. * +* * +* Retour : Statut du contenu de l'instruction. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_raw_instruction_is_padding(const GRawInstruction *instr) +{ + bool result; /* Indication à retourner */ + + result = g_arch_instruction_has_flag(G_ARCH_INSTRUCTION(instr), RIF_PADDING); + + return result; + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction à traiter. * +* is_string = nouveau statut à associer au contenu. * +* * +* Description : Marque l'instruction comme contenant une chaîne de texte. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void g_raw_instruction_mark_as_string(GRawInstruction *instr, bool is_string) +{ + if (is_string) + g_arch_instruction_set_flag(G_ARCH_INSTRUCTION(instr), RIF_STRING); + else + g_arch_instruction_unset_flag(G_ARCH_INSTRUCTION(instr), RIF_STRING); + +} + + +/****************************************************************************** +* * +* Paramètres : instr = instruction à traiter. * +* is_string = nouveau statut à associer au contenu. * +* * +* Description : Indique si le contenu de l'instruction est un texte. * +* * +* Retour : Statut du contenu de l'instruction. * +* * +* Remarques : - * +* * +******************************************************************************/ + +bool g_raw_instruction_is_string(const GRawInstruction *instr) +{ + bool result; /* Indication à retourner */ + + result = g_arch_instruction_has_flag(G_ARCH_INSTRUCTION(instr), RIF_STRING); + + return result; + +} diff --git a/src/arch/instructions/raw.h b/src/arch/instructions/raw.h new file mode 100644 index 0000000..4e92cd4 --- /dev/null +++ b/src/arch/instructions/raw.h @@ -0,0 +1,91 @@ + +/* Chrysalide - Outil d'analyse de fichiers binaires + * raw.h - prototypes pour les instructions pures vues de l'esprit + * + * Copyright (C) 2014-2020 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 Chrysalide. If not, see . + */ + + +#ifndef _ARCH_INSTRUCTIONS_RAW_H +#define _ARCH_INSTRUCTIONS_RAW_H + + +#include + + +#include "../instruction.h" +#include "../vmpa.h" + + + +/* ------------------------- INSTRUCTION INCONNUE / DONNEES ------------------------- */ + + +#define G_TYPE_RAW_INSTRUCTION g_raw_instruction_get_type() +#define G_RAW_INSTRUCTION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_RAW_INSTRUCTION, GRawInstruction)) +#define G_IS_RAW_INSTRUCTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_RAW_INSTRUCTION)) +#define G_RAW_INSTRUCTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_RAW_INSTRUCTION, GRawInstructionClass)) +#define G_IS_RAW_INSTRUCTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_RAW_INSTRUCTION)) +#define G_RAW_INSTRUCTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_RAW_INSTRUCTION, GRawInstructionClass)) + + +/* Définition générique d'une instruction brute d'architecture (instance) */ +typedef struct _GRawInstruction GRawInstruction; + +/* Définition générique d'une instruction brute d'architecture (classe) */ +typedef struct _GRawInstructionClass GRawInstructionClass; + + +/* Indique le type défini pour une instruction inconnue d'architecture. */ +GType g_raw_instruction_get_type(void); + +/* Crée une instruction de type 'db/dw/etc' simple. */ +GArchInstruction *g_raw_instruction_new_from_value(const vmpa2t *, MemoryDataSize, uint64_t); + +/* Crée une instruction de type 'db/dw/etc' pour un uleb128. */ +GArchInstruction *g_raw_instruction_new_uleb128(const GBinContent *content, vmpa2t *); + +/* Crée une instruction de type 'db/dw/etc' pour un sleb128. */ +GArchInstruction *g_raw_instruction_new_sleb128(const GBinContent *content, vmpa2t *); + +/* Crée une instruction de type 'db/dw/etc' étendue. */ +GArchInstruction *g_raw_instruction_new_array(const GBinContent *, MemoryDataSize, size_t, vmpa2t *, SourceEndian); + +/* Drapeaux pour informations complémentaires */ +typedef enum _RawInstrFlag +{ + RIF_PADDING = (1 << (AIF_USER_BIT + 0)),/* Données de bourrage */ + RIF_STRING = (1 << (AIF_USER_BIT + 1)),/* Impression en chaîne */ + +} RawInstrFlag; + +/* Marque l'instruction comme ne contenant que du bourrage. */ +void g_raw_instruction_mark_as_padding(GRawInstruction *, bool); + +/* Indique si le contenu de l'instruction est du bourrage. */ +bool g_raw_instruction_is_padding(const GRawInstruction *); + +/* Marque l'instruction comme contenant une chaîne de texte. */ +void g_raw_instruction_mark_as_string(GRawInstruction *, bool); + +/* Indique si le contenu de l'instruction est un texte. */ +bool g_raw_instruction_is_string(const GRawInstruction *); + + + +#endif /* _ARCH_INSTRUCTIONS_RAW_H */ diff --git a/src/arch/processor.c b/src/arch/processor.c index 96007fb..777b01e 100644 --- a/src/arch/processor.c +++ b/src/arch/processor.c @@ -35,7 +35,6 @@ #include "instruction-int.h" #include "processor-int.h" -#include "raw.h" #include "../common/sort.h" #include "../core/logs.h" #include "../glibext/chrysamarshal.h" diff --git a/src/arch/raw.c b/src/arch/raw.c deleted file mode 100644 index 285e168..0000000 --- a/src/arch/raw.c +++ /dev/null @@ -1,870 +0,0 @@ - -/* Chrysalide - Outil d'analyse de fichiers binaires - * artificial.c - instructions pures vues de l'esprit - * - * Copyright (C) 2014-2020 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 Chrysalide. If not, see . - */ - - -#include "raw.h" - - -#include -#include -#include - - -#include - - -#include "instruction-int.h" -#include "target.h" -#include "operands/immediate.h" - - - -/* ------------------------- INSTRUCTION INCONNUE / DONNEES ------------------------- */ - - -/* Définition générique d'une instruction brute d'architecture (instance) */ -struct _GRawInstruction -{ - GArchInstruction parent; /* A laisser en premier */ - -}; - -/* Définition générique d'une instruction brute d'architecture (classe) */ -struct _GRawInstructionClass -{ - GArchInstructionClass parent; /* A laisser en premier */ - -}; - - -/* Initialise la classe des instructions brutes d'architecture. */ -static void g_raw_instruction_class_init(GRawInstructionClass *); - -/* Initialise une instance d'instruction brute d'architecture. */ -static void g_raw_instruction_init(GRawInstruction *); - -/* Supprime toutes les références externes. */ -static void g_raw_instruction_dispose(GRawInstruction *); - -/* Procède à la libération totale de la mémoire. */ -static void g_raw_instruction_finalize(GRawInstruction *); - -/* Indique l'encodage d'une instruction de façon détaillée. */ -static const char *g_raw_instruction_get_encoding(const GRawInstruction *); - -/* Fournit le nom humain de l'instruction manipulée. */ -static const char *g_raw_instruction_get_keyword(const GRawInstruction *); - - - -/* -------------------- CONSERVATION SUR DISQUE DES INSTRUCTIONS -------------------- */ - - -/* Charge une instruction depuis une mémoire tampon. */ -static bool g_raw_instruction_unserialize(GRawInstruction *, GAsmStorage *, GBinFormat *, packed_buffer *); - -/* Sauvegarde une instruction dans une mémoire tampon. */ -static bool g_raw_instruction_serialize(GRawInstruction *, GAsmStorage *, packed_buffer *); - - - -/* ------------------------ OFFRE DE CAPACITES DE GENERATION ------------------------ */ - - -/* Ajoute à un tampon GLib le contenu de l'instance spécifiée. */ -static void g_raw_instruction_print(GRawInstruction *, GBufferLine *, size_t, size_t, const GBinContent *); - - - -/* ---------------------------------------------------------------------------------- */ -/* INSTRUCTION INCONNUE / DONNEES */ -/* ---------------------------------------------------------------------------------- */ - - -/* Indique le type défini pour une instruction inconnue d'architecture. */ -G_DEFINE_TYPE(GRawInstruction, g_raw_instruction, G_TYPE_ARCH_INSTRUCTION); - - -/****************************************************************************** -* * -* Paramètres : klass = classe à initialiser. * -* * -* Description : Initialise la classe des instructions brutes d'architecture. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_raw_instruction_class_init(GRawInstructionClass *klass) -{ - GObjectClass *object; /* Autre version de la classe */ - GArchInstructionClass *instr; /* Encore une autre vision... */ - - object = G_OBJECT_CLASS(klass); - - object->dispose = (GObjectFinalizeFunc/* ! */)g_raw_instruction_dispose; - object->finalize = (GObjectFinalizeFunc)g_raw_instruction_finalize; - - instr = G_ARCH_INSTRUCTION_CLASS(klass); - - instr->get_encoding = (get_instruction_encoding_fc)g_raw_instruction_get_encoding; - instr->get_keyword = (get_instruction_keyword_fc)g_raw_instruction_get_keyword; - - instr->unserialize = (unserialize_instruction_fc)g_raw_instruction_unserialize; - instr->serialize = (serialize_instruction_fc)g_raw_instruction_serialize; - - instr->print = (print_instruction_fc)g_raw_instruction_print; - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instance à initialiser. * -* * -* Description : Initialise une instance d'instruction brute d'architecture. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_raw_instruction_init(GRawInstruction *instr) -{ - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instance d'objet GLib à traiter. * -* * -* Description : Supprime toutes les références externes. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_raw_instruction_dispose(GRawInstruction *instr) -{ - G_OBJECT_CLASS(g_raw_instruction_parent_class)->dispose(G_OBJECT(instr)); - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instance d'objet GLib à traiter. * -* * -* Description : Procède à la libération totale de la mémoire. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_raw_instruction_finalize(GRawInstruction *instr) -{ - G_OBJECT_CLASS(g_raw_instruction_parent_class)->finalize(G_OBJECT(instr)); - -} - - -/****************************************************************************** -* * -* Paramètres : addr = position à associer à l'instruction. * -* size = taille de l'opérande souhaitée. * -* value = valeur sur x bits à venir récupérer. * -* * -* Description : Crée une instruction de type 'db/dw/etc' simple. * -* * -* Retour : Instruction mise en place. * -* * -* Remarques : - * -* * -******************************************************************************/ - -GArchInstruction *g_raw_instruction_new_from_value(const vmpa2t *addr, MemoryDataSize size, uint64_t value) -{ - GArchInstruction *result; /* Instruction à retourner */ - GImmOperand *operand; /* Octet non décodé à afficher */ - mrange_t range; /* Couverture de l'instruction */ - - result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); - - operand = G_IMM_OPERAND(g_imm_operand_new_from_value(size, value)); - if (operand == NULL) goto grinfv_error; - - g_imm_operand_pad(operand, true); - - g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); - - switch (size) - { - case MDS_8_BITS_UNSIGNED: - case MDS_8_BITS_SIGNED: - init_mrange(&range, addr, 1); - break; - - case MDS_16_BITS_UNSIGNED: - case MDS_16_BITS_SIGNED: - init_mrange(&range, addr, 2); - break; - - case MDS_32_BITS_UNSIGNED: - case MDS_32_BITS_SIGNED: - init_mrange(&range, addr, 4); - break; - - case MDS_64_BITS_UNSIGNED: - case MDS_64_BITS_SIGNED: - init_mrange(&range, addr, 8); - break; - - default: - assert(false); - goto grinfv_error; - break; - - } - - g_arch_instruction_set_range(result, &range); - - return result; - - grinfv_error: - - g_object_unref(G_OBJECT(result)); - - return NULL; - -} - - -/****************************************************************************** -* * -* Paramètres : content = flux de données à analyser. * -* addr = position courante dans ce flux. [OUT] * -* * -* Description : Crée une instruction de type 'db/dw/etc' pour un uleb128. * -* * -* Retour : Instruction mise en place. * -* * -* Remarques : - * -* * -******************************************************************************/ - -GArchInstruction *g_raw_instruction_new_uleb128(const GBinContent *content, vmpa2t *addr) -{ - GArchInstruction *result; /* Instruction à retourner */ - vmpa2t start; /* Départ original de lecture */ - uleb128_t value; /* Valeur uleb128 à représenter*/ - phys_t diff; /* Couverture de la lecture */ - MemoryDataSize leb_size; /* Taille de la valeur */ - GImmOperand *operand; /* Octet non décodé à afficher */ - mrange_t range; /* Couverture de l'instruction */ - - copy_vmpa(&start, addr); - - if (!g_binary_content_read_uleb128(content, addr, &value)) - goto grinu_error; - - diff = compute_vmpa_diff(&start, addr); - - leb_size = MDS_FROM_BYTES(diff); - assert(leb_size != MDS_UNDEFINED); - - result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); - - init_mrange(&range, &start, diff); - g_arch_instruction_set_range(result, &range); - - operand = G_IMM_OPERAND(g_imm_operand_new_from_value(leb_size, (uint64_t)value)); - if (operand == NULL) goto grinu_error; - - g_imm_operand_pad(operand, true); - - g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); - - return result; - - grinu_error: - - return NULL; - -} - - -/****************************************************************************** -* * -* Paramètres : content = flux de données à analyser. * -* addr = position courante dans ce flux. [OUT] * -* * -* Description : Crée une instruction de type 'db/dw/etc' pour un sleb128. * -* * -* Retour : Instruction mise en place. * -* * -* Remarques : - * -* * -******************************************************************************/ - -GArchInstruction *g_raw_instruction_new_sleb128(const GBinContent *content, vmpa2t *addr) -{ - GArchInstruction *result; /* Instruction à retourner */ - vmpa2t start; /* Départ original de lecture */ - uleb128_t value; /* Valeur uleb128 à représenter*/ - phys_t diff; /* Couverture de la lecture */ - MemoryDataSize leb_size; /* Taille de la valeur */ - GImmOperand *operand; /* Octet non décodé à afficher */ - mrange_t range; /* Couverture de l'instruction */ - - copy_vmpa(&start, addr); - - if (!g_binary_content_read_uleb128(content, addr, &value)) - goto grins_error; - - diff = compute_vmpa_diff(&start, addr); - - leb_size = MDS_FROM_BYTES(diff) | MDS_SIGN; - assert(leb_size != MDS_SIGN); - - result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); - - init_mrange(&range, &start, diff); - g_arch_instruction_set_range(result, &range); - - operand = G_IMM_OPERAND(g_imm_operand_new_from_value(leb_size, (uint64_t)value)); - if (operand == NULL) goto grins_error; - - g_imm_operand_pad(operand, true); - - g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); - - return result; - - grins_error: - - return NULL; - -} - - -/****************************************************************************** -* * -* Paramètres : content = flux de données à analyser. * -* size = taille de chacun des éléments à représenter. * -* count = nombre de ces éléments. * -* addr = position courante dans ce flux. [OUT] * -* endian = ordre des bits dans la source. * -* * -* Description : Crée une instruction de type 'db/dw/etc' étendue. * -* * -* Retour : Instruction mise en place. * -* * -* Remarques : - * -* * -******************************************************************************/ - -GArchInstruction *g_raw_instruction_new_array(const GBinContent *content, MemoryDataSize size, size_t count, vmpa2t *addr, SourceEndian endian) -{ - GArchInstruction *result; /* Instruction à retourner */ - vmpa2t old; /* Sauvegarde de la position */ - size_t i; /* Boucle de parcours */ - GImmOperand *operand; /* Octet non décodé à afficher */ - mrange_t range; /* Couverture de l'instruction */ - - /* Par soucis de cohérence */ - if (count == 0) return NULL; - - result = g_object_new(G_TYPE_RAW_INSTRUCTION, NULL); - - copy_vmpa(&old, addr); - - for (i = 0; i < count; i++) - { - operand = G_IMM_OPERAND(g_imm_operand_new_from_data(size, content, addr, endian)); - if (operand == NULL) goto grina_error; - - g_imm_operand_pad(operand, true); - - g_arch_instruction_attach_extra_operand(result, G_ARCH_OPERAND(operand)); - - } - - init_mrange(&range, &old, compute_vmpa_diff(addr, &old)); - - g_arch_instruction_set_range(result, &range); - - return result; - - grina_error: - - g_object_unref(G_OBJECT(result)); - - return NULL; - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction quelconque à consulter. * -* * -* Description : Indique l'encodage d'une instruction de façon détaillée. * -* * -* Retour : Description humaine de l'encodage utilisé. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static const char *g_raw_instruction_get_encoding(const GRawInstruction *instr) -{ - const char *result; /* Description à retourner */ - - if (g_raw_instruction_is_string(instr)) - result = _("String"); - else - result = _("Raw"); - - return result; - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction d'assemblage à consulter. * -* * -* Description : Fournit le nom humain de l'instruction manipulée. * -* * -* Retour : Mot clef de bas niveau. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static const char *g_raw_instruction_get_keyword(const GRawInstruction *instr) -{ - GArchOperand *operand; /* Octet décodé à afficher */ - MemoryDataSize size; /* Taille de valeur associée */ - - static char *defines[] = { "dn", "db", "dw", "dd", "dq" }; - - operand = g_arch_instruction_get_operand(G_ARCH_INSTRUCTION(instr), 0); - - if (G_IS_TARGET_OPERAND(operand)) - size = g_target_operand_get_size(G_TARGET_OPERAND(operand)); - else - size = g_imm_operand_get_size(G_IMM_OPERAND(operand)); - - g_object_unref(G_OBJECT(operand)); - - return defines[MDS_RANGE(size)]; - -} - - - -/* ---------------------------------------------------------------------------------- */ -/* CONSERVATION SUR DISQUE DES INSTRUCTIONS */ -/* ---------------------------------------------------------------------------------- */ - - -/****************************************************************************** -* * -* Paramètres : instr = instruction d'assemblage à consulter. * -* storage = mécanisme de sauvegarde à manipuler. * -* format = format binaire chargé associé à l'architecture. * -* pbuf = zone tampon à remplir. * -* * -* Description : Charge une instruction depuis une mémoire tampon. * -* * -* Retour : Bilan de l'opération. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static bool g_raw_instruction_unserialize(GRawInstruction *instr, GAsmStorage *storage, GBinFormat *format, packed_buffer *pbuf) -{ - bool result; /* Bilan à retourner */ - GArchInstructionClass *parent; /* Classe parente à consulter */ - uint8_t boolean; /* Valeur booléenne */ - - parent = G_ARCH_INSTRUCTION_CLASS(g_raw_instruction_parent_class); - - result = parent->unserialize(G_ARCH_INSTRUCTION(instr), storage, format, pbuf); - - if (result) - { - result = extract_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); - - if (result) - g_raw_instruction_mark_as_padding(instr, (boolean == 1)); - - } - - if (result) - { - result = extract_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); - - if (result) - g_raw_instruction_mark_as_string(instr, (boolean == 1)); - - } - - return result; - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction d'assemblage à consulter. * -* storage = mécanisme de sauvegarde à manipuler. * -* pbuf = zone tampon à remplir. * -* * -* Description : Sauvegarde une instruction dans une mémoire tampon. * -* * -* Retour : Bilan de l'opération. * -* * -* Remarques : - * -* * -******************************************************************************/ - -static bool g_raw_instruction_serialize(GRawInstruction *instr, GAsmStorage *storage, packed_buffer *pbuf) -{ - bool result; /* Bilan à retourner */ - GArchInstructionClass *parent; /* Classe parente à consulter */ - uint8_t boolean; /* Valeur booléenne */ - - parent = G_ARCH_INSTRUCTION_CLASS(g_raw_instruction_parent_class); - - result = parent->serialize(G_ARCH_INSTRUCTION(instr), storage, pbuf); - - if (result) - { - boolean = (g_raw_instruction_is_padding(instr) ? 1 : 0); - result = extend_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); - } - - if (result) - { - boolean = (g_raw_instruction_is_string(instr) ? 1 : 0); - result = extend_packed_buffer(pbuf, &boolean, sizeof(uint8_t), false); - } - - return result; - -} - - - -/* ---------------------------------------------------------------------------------- */ -/* OFFRE DE CAPACITES DE GENERATION */ -/* ---------------------------------------------------------------------------------- */ - - -/****************************************************************************** -* * -* Paramètres : instr = instruction d'assemblage à représenter. * -* line = ligne de rendu à compléter. * -* index = indice de cette même ligne dans le tampon global. * -* repeat = indice d'utilisations successives du générateur. * -* content = éventuel contenu binaire brut à imprimer. * -* * -* Description : Ajoute à un tampon GLib le contenu de l'instance spécifiée. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -static void g_raw_instruction_print(GRawInstruction *instr, GBufferLine *line, size_t index, size_t repeat, const GBinContent *content) -{ - GArchInstruction *base; /* Autre version de l'instance */ - phys_t max_displayed_len; /* Quantité de code affichée */ - const char *key; /* Mot clef principal */ - size_t klen; /* Taille de ce mot clef */ - char *string; /* Chaîne reconstituée */ - size_t iter; /* Tête d'écriture */ - bool first; /* Mémorise une énumération */ - size_t count; /* Nombre d'opérandes en place */ - size_t i; /* Boucle de parcours */ - GArchOperand *op; /* Opérande à manipuler */ - GImmOperand *imm; /* Version opérande de valeur */ - char byte; /* Octet à afficher (ou pas) */ -#ifndef NDEBUG - bool status; /* Bilan d'une récupération */ -#endif - - base = G_ARCH_INSTRUCTION(instr); - - /* Localisation */ - - g_buffer_line_fill_vmpa(line, get_mrange_addr(&base->range), MDS_32_BITS_UNSIGNED, MDS_32_BITS_UNSIGNED); - - /* Contenu */ - - if (g_raw_instruction_is_padding(instr)) - max_displayed_len = 0; - - else if (g_raw_instruction_is_string(instr)) - max_displayed_len = 1; - - else - { - max_displayed_len = get_mrange_length(&base->range); - max_displayed_len /= g_arch_instruction_count_operands(base); - } - - g_buffer_line_fill_content(line, content, &base->range, max_displayed_len); - - /* Zone du code d'assemblage */ - - key = g_arch_instruction_get_keyword(base); - klen = strlen(key); - - g_buffer_line_append_text(line, BLC_ASSEMBLY_HEAD, key, klen, RTT_INSTRUCTION, NULL); - - if (g_raw_instruction_is_padding(instr)) - g_buffer_line_append_text(line, BLC_ASSEMBLY, "...", 3, RTT_RAW, NULL); - - else - { - string = NULL; - iter = 0; - - first = true; - - g_arch_instruction_lock_operands(base); - - count = _g_arch_instruction_count_operands(base); - - for (i = 0; i < count; i++) - { - op = _g_arch_instruction_get_operand(base, i); - - if (!G_IS_IMM_OPERAND(op)) - goto grip_fallback; - - imm = G_IMM_OPERAND(op); - - if (g_imm_operand_get_size(imm) != MDS_8_BITS) - goto grip_fallback; - - if (!g_raw_instruction_is_string(instr) && g_imm_operand_get_display(imm) != IOD_CHAR) - goto grip_fallback; - -#ifndef NDEBUG - status = g_imm_operand_get_value(imm, MDS_8_BITS, &byte); - assert(status); -#else - g_imm_operand_get_value(imm, MDS_8_BITS, &byte); -#endif - - /* Si le caractère doit apparaître en hexadécimal... */ - - if (!isprint(byte)) - goto grip_fallback; - - /* Impression de l'octet */ - - if (string == NULL) - { - string = (char *)calloc(count + 3, sizeof(char)); - - strcpy(string, "\""); - iter = 1; - - } - - string[iter++] = byte; - - g_object_unref(G_OBJECT(op)); - - continue; - - grip_fallback: - - /* Si une chaîne précède */ - - if (string != NULL && iter > 1) - { - if (!first) - { - g_buffer_line_append_text(line, BLC_ASSEMBLY, ",", 1, RTT_PUNCT, NULL); - g_buffer_line_append_text(line, BLC_ASSEMBLY, " ", 1, RTT_RAW, NULL); - } - else - first = false; - - string[iter++] = '"'; - - g_buffer_line_append_text(line, BLC_ASSEMBLY, string, iter, RTT_STRING, NULL); - - iter = 1; - - } - - /* Intégration en tant qu'opérande classique */ - - if (!first) - { - g_buffer_line_append_text(line, BLC_ASSEMBLY, ",", 1, RTT_PUNCT, NULL); - g_buffer_line_append_text(line, BLC_ASSEMBLY, " ", 1, RTT_RAW, NULL); - } - else - first = false; - - g_arch_operand_print(op, line); - - g_object_unref(G_OBJECT(op)); - - } - - /* Si au final une chaîne traine encore */ - - if (string != NULL && iter > 1) - { - if (!first) - { - g_buffer_line_append_text(line, BLC_ASSEMBLY, ",", 1, RTT_PUNCT, NULL); - g_buffer_line_append_text(line, BLC_ASSEMBLY, " ", 1, RTT_RAW, NULL); - } - - string[iter++] = '"'; - - g_buffer_line_append_text(line, BLC_ASSEMBLY, string, iter, RTT_STRING, NULL); - - } - - g_arch_instruction_unlock_operands(base); - - if (string != NULL) - free(string); - - } - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction à traiter. * -* is_padding = nouveau statut à associer au contenu. * -* * -* Description : Marque l'instruction comme ne contenant que du bourrage. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -void g_raw_instruction_mark_as_padding(GRawInstruction *instr, bool is_padding) -{ - if (is_padding) - g_arch_instruction_set_flag(G_ARCH_INSTRUCTION(instr), RIF_PADDING); - else - g_arch_instruction_unset_flag(G_ARCH_INSTRUCTION(instr), RIF_PADDING); - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction à traiter. * -* is_padding = nouveau statut à associer au contenu. * -* * -* Description : Indique si le contenu de l'instruction est du bourrage. * -* * -* Retour : Statut du contenu de l'instruction. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool g_raw_instruction_is_padding(const GRawInstruction *instr) -{ - bool result; /* Indication à retourner */ - - result = g_arch_instruction_has_flag(G_ARCH_INSTRUCTION(instr), RIF_PADDING); - - return result; - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction à traiter. * -* is_string = nouveau statut à associer au contenu. * -* * -* Description : Marque l'instruction comme contenant une chaîne de texte. * -* * -* Retour : - * -* * -* Remarques : - * -* * -******************************************************************************/ - -void g_raw_instruction_mark_as_string(GRawInstruction *instr, bool is_string) -{ - if (is_string) - g_arch_instruction_set_flag(G_ARCH_INSTRUCTION(instr), RIF_STRING); - else - g_arch_instruction_unset_flag(G_ARCH_INSTRUCTION(instr), RIF_STRING); - -} - - -/****************************************************************************** -* * -* Paramètres : instr = instruction à traiter. * -* is_string = nouveau statut à associer au contenu. * -* * -* Description : Indique si le contenu de l'instruction est un texte. * -* * -* Retour : Statut du contenu de l'instruction. * -* * -* Remarques : - * -* * -******************************************************************************/ - -bool g_raw_instruction_is_string(const GRawInstruction *instr) -{ - bool result; /* Indication à retourner */ - - result = g_arch_instruction_has_flag(G_ARCH_INSTRUCTION(instr), RIF_STRING); - - return result; - -} diff --git a/src/arch/raw.h b/src/arch/raw.h deleted file mode 100644 index c745f4a..0000000 --- a/src/arch/raw.h +++ /dev/null @@ -1,91 +0,0 @@ - -/* Chrysalide - Outil d'analyse de fichiers binaires - * raw.h - prototypes pour les instructions pures vues de l'esprit - * - * Copyright (C) 2014-2020 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 Chrysalide. If not, see . - */ - - -#ifndef _ARCH_RAW_H -#define _ARCH_RAW_H - - -#include - - -#include "instruction.h" -#include "vmpa.h" - - - -/* ------------------------- INSTRUCTION INCONNUE / DONNEES ------------------------- */ - - -#define G_TYPE_RAW_INSTRUCTION g_raw_instruction_get_type() -#define G_RAW_INSTRUCTION(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), G_TYPE_RAW_INSTRUCTION, GRawInstruction)) -#define G_IS_RAW_INSTRUCTION(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), G_TYPE_RAW_INSTRUCTION)) -#define G_RAW_INSTRUCTION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), G_TYPE_RAW_INSTRUCTION, GRawInstructionClass)) -#define G_IS_RAW_INSTRUCTION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), G_TYPE_RAW_INSTRUCTION)) -#define G_RAW_INSTRUCTION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), G_TYPE_RAW_INSTRUCTION, GRawInstructionClass)) - - -/* Définition générique d'une instruction brute d'architecture (instance) */ -typedef struct _GRawInstruction GRawInstruction; - -/* Définition générique d'une instruction brute d'architecture (classe) */ -typedef struct _GRawInstructionClass GRawInstructionClass; - - -/* Indique le type défini pour une instruction inconnue d'architecture. */ -GType g_raw_instruction_get_type(void); - -/* Crée une instruction de type 'db/dw/etc' simple. */ -GArchInstruction *g_raw_instruction_new_from_value(const vmpa2t *, MemoryDataSize, uint64_t); - -/* Crée une instruction de type 'db/dw/etc' pour un uleb128. */ -GArchInstruction *g_raw_instruction_new_uleb128(const GBinContent *content, vmpa2t *); - -/* Crée une instruction de type 'db/dw/etc' pour un sleb128. */ -GArchInstruction *g_raw_instruction_new_sleb128(const GBinContent *content, vmpa2t *); - -/* Crée une instruction de type 'db/dw/etc' étendue. */ -GArchInstruction *g_raw_instruction_new_array(const GBinContent *, MemoryDataSize, size_t, vmpa2t *, SourceEndian); - -/* Drapeaux pour informations complémentaires */ -typedef enum _RawInstrFlag -{ - RIF_PADDING = (1 << (AIF_USER_BIT + 0)),/* Données de bourrage */ - RIF_STRING = (1 << (AIF_USER_BIT + 1)),/* Impression en chaîne */ - -} RawInstrFlag; - -/* Marque l'instruction comme ne contenant que du bourrage. */ -void g_raw_instruction_mark_as_padding(GRawInstruction *, bool); - -/* Indique si le contenu de l'instruction est du bourrage. */ -bool g_raw_instruction_is_padding(const GRawInstruction *); - -/* Marque l'instruction comme contenant une chaîne de texte. */ -void g_raw_instruction_mark_as_string(GRawInstruction *, bool); - -/* Indique si le contenu de l'instruction est un texte. */ -bool g_raw_instruction_is_string(const GRawInstruction *); - - - -#endif /* _ARCH_RAW_H */ diff --git a/src/core/processors.c b/src/core/processors.c index c03b98f..224e9f7 100644 --- a/src/core/processors.c +++ b/src/core/processors.c @@ -29,8 +29,8 @@ #include -#include "../arch/raw.h" #include "../arch/target.h" +#include "../arch/instructions/raw.h" #include "../arch/instructions/undefined.h" #include "../arch/operands/immediate.h" #include "../arch/operands/register.h" diff --git a/tests/arch/raw.py b/tests/arch/raw.py new file mode 100644 index 0000000..05d7435 --- /dev/null +++ b/tests/arch/raw.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3-dbg +# -*- coding: utf-8 -*- + + +import pychrysalide +from chrysacase import ChrysalideTestCase +from pychrysalide.analysis import BinContent +from pychrysalide.analysis.contents import MemoryContent +from pychrysalide.arch import vmpa +from pychrysalide.arch.instructions import RawInstruction + + +class TestRawInstruction(ChrysalideTestCase): + """TestCase for arch.instructions.RawInstruction.""" + + def testConstructors(self): + """Build some raw instructions to check their constructors.""" + + instr = RawInstruction(vmpa(0), BinContent.MemoryDataSize._32_BITS_UNSIGNED, value=123) + self.assertIsNotNone(instr) + + data = b'\x01\x02\x03\x04\x05\x06\x07\x08' + cnt = MemoryContent(data) + + instr = RawInstruction(vmpa(0), BinContent.MemoryDataSize._32_BITS_UNSIGNED, + content=cnt, count=2, endian=BinContent.SourceEndian.LITTLE) + self.assertIsNotNone(instr) + + with self.assertRaisesRegex(Exception, 'Unable to build the object with the given parameters.'): + + instr = RawInstruction(vmpa(0), BinContent.MemoryDataSize._32_BITS_UNSIGNED, + content=cnt, count=3, endian=BinContent.SourceEndian.LITTLE) -- cgit v0.11.2-87-g4458