/* Chrysalide - Outil d'analyse de fichiers binaires * processor.c - équivalent Python du fichier "arch/processor.c" * * Copyright (C) 2012-2017 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 "processor.h" #include #include #include #include #include #include "context.h" #include "instriter.h" #include "instruction.h" #include "vmpa.h" #include "../access.h" #include "../helpers.h" #include "../analysis/content.h" #include "../format/executable.h" /* ------------------------ GLUE POUR CREATION DEPUIS PYTHON ------------------------ */ /* Accompagne la création d'une instance dérivée en Python. */ static PyObject *py_arch_processor_new(PyTypeObject *, PyObject *, PyObject *); /* Initialise la classe des descriptions de fichier binaire. */ static void py_arch_processor_init_gclass(GArchProcessorClass *, gpointer); /* Initialise une instance de processeur d'architecture. */ static void py_arch_processor_init_ginstance(GArchProcessor *, GArchProcessor *); /* Initialise une instance sur la base du dérivé de GObject. */ static int py_arch_processor_init(PyObject *, PyObject *, PyObject *); /* Fournit un contexte propre au processeur d'une architecture. */ static GProcContext *py_arch_processor_get_context_wrapper(const GArchProcessor *); /* Désassemble une instruction dans un flux de données. */ static GArchInstruction *py_arch_processor_disassemble_wrapper(const GArchProcessor *, GProcContext *, const GBinContent *, vmpa2t *, GExeFormat *); /* ---------------------------- DEFINITION DE PROCESSEUR ---------------------------- */ /* Fournit le boustime du processeur d'une architecture. */ static PyObject *py_arch_processor_get_endianness(PyObject *, void *); /* Fournit la taille de l'espace mémoire d'une architecture. */ static PyObject *py_arch_processor_get_memory_size(PyObject *, void *); /* Fournit la taille min. des instructions d'une architecture. */ static PyObject *py_arch_processor_get_instruction_min_size(PyObject *, void *); /* Indique si l'architecture possède un espace virtuel ou non. */ static PyObject *py_arch_processor_has_virtual_space(PyObject *, void *); /* Fournit un contexte propre au processeur d'une architecture. */ static PyObject *py_arch_processor_get_context(PyObject *, PyObject *); /* Désassemble une instruction dans un flux de données. */ static PyObject *py_arch_processor_disassemble(PyObject *, PyObject *); /* ------------------ CONSERVATION DES SOUCIS DURANT LE CHARGEMENT ------------------ */ /* Etend la liste des soucis détectés avec de nouvelles infos. */ static PyObject *py_arch_processor_add_error(PyObject *, PyObject *); /* Fournit les éléments concernant tous les soucis détectés. */ static PyObject *py_arch_processor_get_errors(PyObject *, void *); /* ------------------ MANIPULATIONS DES INSTRUCTIONS DESASSEMBLEES ------------------ */ /* Fournit les instructions désassemblées pour une architecture. */ static PyObject *py_arch_processor_get_instrs(PyObject *, void *); /* Note les instructions désassemblées avec une architecture. */ static int py_arch_processor_set_instrs(PyObject *, PyObject *, void *); /* Recherche une instruction d'après son adresse. */ static PyObject *py_arch_processor_find_instr_by_addr(PyObject *, PyObject *); /* ---------------------------- DEFINITION DE PROCESSEUR ---------------------------- */ /* Définit les constantes pour les types d'erreurs. */ static bool define_python_arch_processor_constants(PyTypeObject *); /* ---------------------------------------------------------------------------------- */ /* GLUE POUR CREATION DEPUIS PYTHON */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * 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_arch_processor_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_arch_processor_type(); if (type == base) { result = NULL; PyErr_Format(PyExc_RuntimeError, _("%s is an abstract class"), type->tp_name); goto exit; } /* Mise en place d'un type dédié */ first_time = (g_type_from_name(type->tp_name) == 0); gtype = build_dynamic_type(G_TYPE_ARCH_PROCESSOR, type->tp_name, (GClassInitFunc)py_arch_processor_init_gclass, NULL, (GInstanceInitFunc)py_arch_processor_init_ginstance); 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() */ result = PyType_GenericNew(type, args, kwds); exit: return result; } /****************************************************************************** * * * Paramètres : class = classe à initialiser. * * unused = données non utilisées ici. * * * * Description : Initialise la classe générique des processeurs. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void py_arch_processor_init_gclass(GArchProcessorClass *class, gpointer unused) { GType type; /* Type d'instances concerné */ GArchProcessor *pattern; /* Patron de données à copier */ type = G_TYPE_FROM_CLASS(class); pattern = get_dynamic_type_pattern(type); if (pattern != NULL) { /* class->endianness = pattern->endianness; class->memsize = pattern->memsize; class->inssize = pattern->inssize; class->virt_space = pattern->virt_space; */ } class->get_ctx = py_arch_processor_get_context_wrapper; class->disassemble = py_arch_processor_disassemble_wrapper; } /****************************************************************************** * * * Paramètres : proc = instance à initialiser. * * class = classe du type correspondant. * * * * Description : Initialise une instance de processeur d'architecture. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void py_arch_processor_init_ginstance(GArchProcessor *proc, GArchProcessor *class) { } /****************************************************************************** * * * 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_arch_processor_init(PyObject *self, PyObject *args, PyObject *kwds) { unsigned long endianness; /* Boutisme du processeur */ unsigned long mem_size; /* Taille d'adressage */ unsigned long ins_min_size; /* Taille minimale d'instruct° */ int vspace; /* Support d'un espace virtuel */ int ret; /* Bilan de lecture des args. */ PyObject *new_kwds; /* Nouveau dictionnaire épuré */ GArchProcessor *proc; /* Processeur à manipuler */ static char *kwlist[] = { "endianness", "mem_size", "ins_min_size", "vspace", NULL }; /* Récupération des paramètres */ ret = PyArg_ParseTupleAndKeywords(args, kwds, "kkkp", kwlist, &endianness, &mem_size, &ins_min_size, &vspace); if (!ret) return -1; /* Initialisation d'un objet GLib */ new_kwds = PyDict_New(); ret = PyGObject_Type.tp_init(self, args, new_kwds); Py_DECREF(new_kwds); if (ret == -1) return -1; /* Eléments de base */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); /* proc->endianness = endianness; proc->memsize = mem_size; proc->inssize = ins_min_size; proc->virt_space = vspace; */ register_dynamic_type_pattern(G_OBJECT(proc)); return 0; } /****************************************************************************** * * * Paramètres : proc = architecture visée par la procédure. * * * * Description : Fournit un contexte propre au processeur d'une architecture. * * * * Retour : Nouveau contexte mis à disposition. * * * * Remarques : - * * * ******************************************************************************/ static GProcContext *py_arch_processor_get_context_wrapper(const GArchProcessor *proc) { GProcContext *result; /* Instance à retourner */ PyGILState_STATE gstate; /* Sauvegarde d'environnement */ PyObject *pyobj; /* Objet Python concerné */ PyObject *pyctx; /* Contexte en objet Python */ int ret; /* Bilan d'une conversion */ GArchProcessorClass *class; /* Classe de l'objet courant */ GArchProcessorClass *parent; /* Classe parente */ gstate = PyGILState_Ensure(); pyobj = pygobject_new(G_OBJECT(proc)); if (has_python_method(pyobj, "_get_context")) { pyctx = run_python_method(pyobj, "_get_context", NULL); if (pyctx == NULL) result = NULL; else { ret = convert_to_proc_context(pyctx, &result); if (ret == 1) g_object_ref(G_OBJECT(result)); else { PyErr_Clear(); result = NULL; } Py_DECREF(pyctx); } } else { class = G_ARCH_PROCESSOR_GET_CLASS(proc); parent = g_type_class_peek_parent(class); result = parent->get_ctx(proc); } PyGILState_Release(gstate); return result; } /****************************************************************************** * * * Paramètres : proc = architecture visée par la procédure. * * ctx = contexte lié à l'exécution du processeur. * * content = flux de données à analyser. * * pos = position courante dans ce flux. [OUT] * * format = format du fichier contenant le code. * * * * Description : Désassemble une instruction dans un flux de données. * * * * Retour : Instruction mise en place ou NULL en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static GArchInstruction *py_arch_processor_disassemble_wrapper(const GArchProcessor *proc, GProcContext *ctx, const GBinContent *content, vmpa2t *pos, GExeFormat *format) { GArchInstruction *result; /* Instance à retourner */ PyGILState_STATE gstate; /* Sauvegarde d'environnement */ PyObject *pyobj; /* Objet Python concerné */ PyObject *pypos; /* Position en objet Python */ PyObject *args; /* Arguments pour l'appel */ PyObject *pyins; /* Instruction en objet Python */ int ret; /* Bilan d'une conversion */ gstate = PyGILState_Ensure(); pyobj = pygobject_new(G_OBJECT(proc)); if (has_python_method(pyobj, "_disassemble")) { pypos = build_from_internal_vmpa(pos); Py_INCREF(pypos); args = PyTuple_New(4); PyTuple_SetItem(args, 0, pygobject_new(G_OBJECT(ctx))); PyTuple_SetItem(args, 1, pygobject_new(G_OBJECT(content))); PyTuple_SetItem(args, 2, pypos); PyTuple_SetItem(args, 3, pygobject_new(G_OBJECT(format))); pyins = run_python_method(pyobj, "_disassemble", args); Py_DECREF(args); if (pyins == NULL) result = NULL; else { ret = convert_to_arch_instruction(pyins, &result); if (ret == 1) g_object_ref(G_OBJECT(result)); else { PyErr_Clear(); result = NULL; } Py_DECREF(pyins); copy_vmpa(pos, get_internal_vmpa(pypos)); } Py_DECREF(pypos); } else result = NULL; PyGILState_Release(gstate); return result; } /* ---------------------------------------------------------------------------------- */ /* DEFINITION DE PROCESSEUR */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit le boustime du processeur d'une architecture. * * * * Retour : Boutisme associé au processeur. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_get_endianness(PyObject *self, void *closure) { PyObject *result; /* Instance Python à retourner */ GArchProcessor *proc; /* Version GLib de l'opérande */ SourceEndian endianness; /* Boutisme du processeur */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); assert(proc != NULL); endianness = g_arch_processor_get_endianness(proc); result = Py_BuildValue("I", endianness); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit la taille de l'espace mémoire d'une architecture. * * * * Retour : Taille de l'espace mémoire. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_get_memory_size(PyObject *self, void *closure) { PyObject *result; /* Instance Python à retourner */ GArchProcessor *proc; /* Version GLib de l'opérande */ MemoryDataSize size; /* Type de donnée représentée */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); assert(proc != NULL); size = g_arch_processor_get_memory_size(proc); result = Py_BuildValue("I", size); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit la taille min. des instructions d'une architecture. * * * * Retour : Taille d'encodage des instructions. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_get_instruction_min_size(PyObject *self, void *closure) { PyObject *result; /* Instance Python à retourner */ GArchProcessor *proc; /* Version GLib de l'opérande */ MemoryDataSize size; /* Type de donnée représentée */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); assert(proc != NULL); size = g_arch_processor_get_instruction_min_size(proc); result = Py_BuildValue("I", size); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Indique si l'architecture possède un espace virtuel ou non. * * * * Retour : True si un espace virtuel existe, False sinon. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_has_virtual_space(PyObject *self, void *closure) { PyObject *result; /* Instance Python à retourner */ GArchProcessor *proc; /* Architecture visée */ bool status; /* Bilan de consultation */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); status = g_arch_processor_has_virtual_space(proc); result = status ? Py_True : Py_False; Py_INCREF(result); return result; } /****************************************************************************** * * * Paramètres : self = architecture concernée par la procédure. * * args = instruction représentant le point de départ. * * * * Description : Fournit un contexte propre au processeur d'une architecture. * * * * Retour : Nouveau contexte mis à disposition. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_get_context(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ GArchProcessor *proc; /* Processeur manipulé */ GProcContext *ctx; /* Nouveau contexte en place */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); ctx = g_arch_processor_get_context(proc); if (ctx != NULL) { result = pygobject_new(G_OBJECT(ctx)); g_object_unref(G_OBJECT(ctx)); } else { result = Py_None; Py_INCREF(result); } return result; } /****************************************************************************** * * * Paramètres : self = architecture concernée par la procédure. * * args = instruction représentant le point de départ. * * * * Description : Désassemble une instruction dans un flux de données. * * * * Retour : Instruction mise en place ou None en cas d'échec. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_disassemble(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ GProcContext *ctx; /* Contexte de désassemblage */ GBinContent *content; /* Contenu binaire à parcourir */ vmpa2t *addr; /* Position d'analyse courante */ GExeFormat *format; /* Format de fichier associé */ int ret; /* Bilan de lecture des args. */ GArchProcessor *proc; /* Processeur manipulé */ GArchInstruction *instr; /* Instruction mise en place */ ret = PyArg_ParseTuple(args, "O&O&OO&", convert_to_proc_context, &ctx, convert_to_binary_content, &content, convert_any_to_vmpa, &addr, convert_to_executable_format, &format); if (!ret) return NULL; proc = G_ARCH_PROCESSOR(pygobject_get(self)); instr = g_arch_processor_disassemble(proc, ctx, content, addr, format); if (instr != NULL) { result = pygobject_new(G_OBJECT(instr)); g_object_unref(G_OBJECT(instr)); } else { result = Py_None; Py_INCREF(result); } clean_vmpa_arg(addr); return result; } /* ---------------------------------------------------------------------------------- */ /* CONSERVATION DES SOUCIS DURANT LE CHARGEMENT */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = architecture concernée par la procédure. * * args = instruction représentant le point de départ. * * * * Description : Etend la liste des soucis détectés avec de nouvelles infos. * * * * Retour : None. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_add_error(PyObject *self, PyObject *args) { ArchProcessingError type; /* Type d'erreur détectée */ vmpa2t *addr; /* Position d'une erreur */ const char *desc; /* Description d'une erreur */ int ret; /* Bilan de lecture des args. */ GArchProcessor *proc; /* Processeur manipulé */ ret = PyArg_ParseTuple(args, "IO&s", &type, convert_any_to_vmpa, &addr, &desc); if (!ret) return NULL; proc = G_ARCH_PROCESSOR(pygobject_get(self)); g_arch_processor_add_error(proc, type, addr, desc); clean_vmpa_arg(addr); Py_RETURN_NONE; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit les éléments concernant tous les soucis détectés. * * * * Retour : Liste des erreurs relevées au niveau de l'assembleur. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_get_errors(PyObject *self, void *closure) { PyObject *result; /* Instance Python à retourner */ GArchProcessor *proc; /* Architecture visée */ size_t count; /* Nombre d'éléments à traiter */ size_t i; /* Boucle de parcours */ #ifndef NDEBUG bool status; /* Bilan d'un appel */ #endif ArchProcessingError type; /* Type d'erreur détectée */ vmpa2t addr; /* Position d'une erreur */ char *desc; /* Description d'une erreur */ PyObject *error; /* Nouvelle erreur à rajouter */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); g_arch_processor_lock_errors(proc); count = g_arch_processor_count_errors(proc); result = PyTuple_New(count); for (i = 0; i < count; i++) { #ifndef NDEBUG status = g_arch_processor_get_error(proc, i, &type, &addr, &desc); assert(status); #else g_arch_processor_get_error(proc, i, &type, &addr, &desc); #endif error = Py_BuildValue("IO&s", type, build_from_internal_vmpa, &addr, desc); PyTuple_SetItem(result, i, error); } g_arch_processor_unlock_errors(proc); return result; } /* ---------------------------------------------------------------------------------- */ /* MANIPULATIONS DES INSTRUCTIONS DESASSEMBLEES */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * closure = non utilisé ici. * * * * Description : Fournit les instructions désassemblées pour une architecture.* * * * Retour : Liste des instructions désassemblées ou None si aucune. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_get_instrs(PyObject *self, void *closure) { PyObject *result; /* Instance Python à retourner */ PyTypeObject *iterator_type; /* Type Python de l'itérateur */ PyObject *args; /* Liste des arguments d'appel */ iterator_type = get_python_instr_iterator_type(); args = Py_BuildValue("On", self, 0); result = PyObject_CallObject((PyObject *)iterator_type, args); Py_DECREF(args); return result; } /****************************************************************************** * * * Paramètres : self = objet Python concerné par l'appel. * * value = valeur fournie à intégrer ou prendre en compte. * * closure = non utilisé ici. * * * * Description : Note les instructions désassemblées avec une architecture. * * * * Retour : Bilan de l'opération pour Python. * * * * Remarques : - * * * ******************************************************************************/ static int py_arch_processor_set_instrs(PyObject *self, PyObject *value, void *closure) { size_t count; /* Nombre d'instructions */ GArchInstruction **list; /* Liste d'instructions */ size_t i; /* Boucle de parcours */ PyObject *instr; /* Instruction en Python */ GArchProcessor *proc; /* Architecture visée */ if (!PyTuple_Check(value)) { PyErr_SetString(PyExc_TypeError, _("The attribute value must be a tuple of instructions.")); return -1; } count = PyTuple_Size(value); list = (GArchInstruction **)calloc(count, sizeof(GArchInstruction *)); for (i = 0; i < count; i++) { instr = PyTuple_GetItem(value, i); if (!PyObject_TypeCheck(value, get_python_arch_instruction_type())) { PyErr_SetString(PyExc_TypeError, _("The attribute value must be a tuple of instructions.")); count = i; goto papsi_error; } list[i] = G_ARCH_INSTRUCTION(pygobject_get(instr)); g_object_ref(G_OBJECT(list[i])); } proc = G_ARCH_PROCESSOR(pygobject_get(self)); g_arch_processor_set_instructions(proc, list, count); return 0; papsi_error: for (i = 0; i < count; i++) g_object_unref(G_OBJECT(list[i])); free(list); return -1; } /****************************************************************************** * * * Paramètres : self = processeur d'architecture à manipuler. * * args = instruction représentant le point de départ. * * * * Description : Recherche une instruction d'après son adresse. * * * * Retour : Instruction trouvée à l'adresse donnée, None si aucune. * * * * Remarques : - * * * ******************************************************************************/ static PyObject *py_arch_processor_find_instr_by_addr(PyObject *self, PyObject *args) { PyObject *result; /* Instance à retourner */ GArchProcessor *proc; /* Processeur manipulé */ proc_cv_info_t conv; /* Informations de conversion */ int ret; /* Bilan de lecture des args. */ GArchInstruction *found; /* Instruction liée trouvée */ proc = G_ARCH_PROCESSOR(pygobject_get(self)); conv.proc = proc; ret = PyArg_ParseTuple(args, "O&", convert_to_vmpa_using_processor, &conv); if (!ret) return NULL; found = g_arch_processor_find_instr_by_address(proc, conv.vmpa); if (found != NULL) { result = pygobject_new(G_OBJECT(found)); g_object_unref(G_OBJECT(found)); } else { result = Py_None; Py_INCREF(result); } return result; } /* ---------------------------------------------------------------------------------- */ /* DEFINITION DE PROCESSEUR */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : obj_type = type dont le dictionnaire est à compléter. * * * * Description : Définit les constantes pour les types d'erreurs. * * * * Retour : true en cas de succès de l'opération, false sinon. * * * * Remarques : - * * * ******************************************************************************/ static bool define_python_arch_processor_constants(PyTypeObject *obj_type) { bool result; /* Bilan à retourner */ result = true; result &= PyDict_AddULongMacro(obj_type, APE_DISASSEMBLY); result &= PyDict_AddULongMacro(obj_type, APE_LABEL); return result; } /****************************************************************************** * * * Paramètres : - * * * * Description : Fournit un accès à une définition de type à diffuser. * * * * Retour : Définition d'objet pour Python. * * * * Remarques : - * * * ******************************************************************************/ PyTypeObject *get_python_arch_processor_type(void) { static PyMethodDef py_arch_processor_methods[] = { { "get_context", py_arch_processor_get_context, METH_NOARGS, "get_context($self, /)\n--\n\nProvide a new disassembly context." }, { "disassemble", py_arch_processor_disassemble, METH_VARARGS, "disassemble($self, context, content, pos, format, /)\n--\n\nDisassemble a portion of binary content into one instruction." }, { "add_error", py_arch_processor_add_error, METH_VARARGS, "add_error($self, type, addr, desc, /)\n--\n\nExtend the list of detected disassembling errors." }, { "find_instr_by_addr", py_arch_processor_find_instr_by_addr, METH_VARARGS, "find_instr_by_addr($self, addr, /)\n--\n\nLook for an instruction located at a given address." }, { NULL } }; static PyGetSetDef py_arch_processor_getseters[] = { { "endianness", py_arch_processor_get_endianness, NULL, "Provide the processor endianness.", NULL }, { "mem_size", py_arch_processor_get_memory_size, NULL, "Provide the size of memory addresses for the given architecture.", NULL }, { "ins_min_size", py_arch_processor_get_instruction_min_size, NULL, "Provide the minimal size of one processor instruction.", NULL }, { "virtual_space", py_arch_processor_has_virtual_space, NULL, "Tell if the processor provides a virtual address space.", NULL }, { "errors", py_arch_processor_get_errors, NULL, "List of all detected errors which occurred during the disassembling process.", NULL }, { "instrs", py_arch_processor_get_instrs, py_arch_processor_set_instrs, "Give access to the disassembled instructions run by the current processor.", NULL }, { NULL } }; static PyTypeObject py_arch_processor_type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "pychrysalide.arch.ArchProcessor", .tp_basicsize = sizeof(PyGObject), .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IS_ABSTRACT | Py_TPFLAGS_BASETYPE, .tp_doc = "PyChrysalide processor for a given architecture.", .tp_methods = py_arch_processor_methods, .tp_getset = py_arch_processor_getseters, .tp_init = py_arch_processor_init, .tp_new = py_arch_processor_new, }; return &py_arch_processor_type; } /****************************************************************************** * * * Paramètres : module = module dont la définition est à compléter. * * * * Description : Prend en charge l'objet 'pychrysalide.arch.ArchProcessor'. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ bool ensure_python_arch_processor_is_registered(void) { PyTypeObject *type; /* Type Python 'ArchProcessor' */ PyObject *module; /* Module à recompléter */ PyObject *dict; /* Dictionnaire du module */ type = get_python_arch_processor_type(); if (!PyType_HasFeature(type, Py_TPFLAGS_READY)) { module = get_access_to_python_module("pychrysalide.arch"); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_ARCH_PROCESSOR, type, &PyGObject_Type)) return false; if (!define_python_arch_processor_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 processeur d'architecture. * * * * Retour : Bilan de l'opération, voire indications supplémentaires. * * * * Remarques : - * * * ******************************************************************************/ int convert_to_arch_processor(PyObject *arg, void *dst) { int result; /* Bilan à retourner */ result = PyObject_IsInstance(arg, (PyObject *)get_python_arch_processor_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 arch processor"); break; case 1: *((GArchProcessor **)dst) = G_ARCH_PROCESSOR(pygobject_get(arg)); break; default: assert(false); break; } return result; } /* ---------------------------------------------------------------------------------- */ /* TRADUCTION D'EMPLACEMENT */ /* ---------------------------------------------------------------------------------- */ /****************************************************************************** * * * Paramètres : obj = objet Python à convertir en emplacement. * * info = informations utiles à l'opération. * * * * Description : Réalise une conversion d'un objet Python en localisation. * * * * Retour : Bilan de l'opération. * * * * Remarques : - * * * ******************************************************************************/ int convert_to_vmpa_using_processor(PyObject *obj, proc_cv_info_t *info) { int result; /* Bilan à retourner */ int ret; /* Bilan d'une consultation */ ret = PyObject_IsInstance(obj, (PyObject *)get_python_vmpa_type()); if (ret) { info->vmpa = get_internal_vmpa(obj); result = 1; } else { ret = PyObject_IsInstance(obj, (PyObject *)&PyLong_Type); if (ret) { info->vmpa = &info->tmp; if (g_arch_processor_has_virtual_space(info->proc)) init_vmpa(info->vmpa, VMPA_NO_PHYSICAL, PyLong_AsUnsignedLongLong(obj)); else init_vmpa(info->vmpa, PyLong_AsUnsignedLongLong(obj), VMPA_NO_VIRTUAL); result = 1; } else result = 0; } if (result == 0) PyErr_Format(PyExc_TypeError, _("unable to convert object to VMPA location")); return result; }