/* Chrysalide - Outil d'analyse de fichiers binaires * processor.c - équivalent Python du fichier "arch/processor.c" * * Copyright (C) 2015 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 "instriter.h" #include "instruction.h" #include "vmpa.h" #include "../helpers.h" /* ------------------ MANIPULATIONS DES INSTRUCTIONS DESASSEMBLEES ------------------ */ /* Fournit les instructions désassemblées pour une architecture. */ static PyObject *py_arch_processor_get_instrs(PyObject *, void *); /* Recherche une instruction d'après son adresse. */ static PyObject *py_arch_processor_find_instr_by_addr(PyObject *, PyObject *); /* ---------------------------------------------------------------------------------- */ /* 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_disass_instrs(PyObject *self, PyObject *value, void *closure) { GArchProcessor *proc; /* Architecture visée */ GArchInstruction *instrs; /* Série d'instructions liées */ if (!PyObject_TypeCheck(value, get_python_arch_instruction_type())) { PyErr_SetString(PyExc_TypeError, _("The attribute value must be an instruction.")); return -1; } proc = G_ARCH_PROCESSOR(pygobject_get(self)); instrs = G_ARCH_INSTRUCTION(pygobject_get(value)); g_arch_processor_set_disassembled_instructions(proc, instrs); return 0; } /****************************************************************************** * * * 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 */ PyObject *addr_obj; /* Objet pour une localisation */ int ret; /* Bilan de lecture des args. */ GArchProcessor *proc; /* Processeur manipulé */ vmpa2t *addr; /* Localisation à retrouver */ GArchInstruction *found; /* Instruction liée trouvée */ ret = PyArg_ParseTuple(args, "O", &addr_obj); if (!ret) return NULL; ret = PyObject_IsInstance(addr_obj, (PyObject *)get_python_vmpa_type()); if (!ret) return NULL; proc = G_ARCH_PROCESSOR(pygobject_get(self)); addr = get_internal_vmpa(addr_obj); found = g_arch_processor_find_instr_by_address(proc, addr); if (found != NULL) { result = pygobject_new(G_OBJECT(found)); g_object_unref(G_OBJECT(found)); } else { result = Py_None; Py_INCREF(result); } 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[] = { { "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[] = { { "instrs", py_arch_processor_get_instrs, py_arch_processor_set_disass_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, }; 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 register_python_arch_processor(PyObject *module) { PyTypeObject *py_arch_processor_type; /* Type Python 'BinContent' */ PyObject *dict; /* Dictionnaire du module */ py_arch_processor_type = get_python_arch_processor_type(); dict = PyModule_GetDict(module); if (!register_class_for_pygobject(dict, G_TYPE_ARCH_PROCESSOR, py_arch_processor_type, &PyGObject_Type)) return false; return true; }