/* Chrysalide - Outil d'analyse de fichiers binaires * method.c - manipulation des methodes du format DEX * * Copyright (C) 2010-2012 Cyrille Bagard * * This file is part of Chrysalide. * * OpenIDA is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * OpenIDA is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Foobar. If not, see . */ #include "method.h" #include #include "dex-int.h" #include "pool.h" /* Methode issue du code source (instance) */ struct _GDexMethod { GObject parent; /* A laisser en premier */ GBinRoutine *routine; /* Représentation interne */ encoded_method info; /* Propriétés de la méthode */ code_item body; /* Corps de la méthode */ off_t offset; /* Position du code */ }; /* Methode issue du code source (classe) */ struct _GDexMethodClass { GObjectClass parent; /* A laisser en premier */ }; /* Procède à l'initialisation d'une methode issue du code. */ static void g_dex_method_class_init(GDexMethodClass *); /* Procède à l'initialisation d'une methode issue du code. */ static void g_dex_method_init(GDexMethod *); /* Supprime toutes les références externes. */ static void g_dex_method_dispose(GDexMethod *); /* Procède à la libération totale de la mémoire. */ static void g_dex_method_finalize(GDexMethod *); /* Détermine le type d'une methode issue du code source. */ G_DEFINE_TYPE(GDexMethod, g_dex_method, G_TYPE_OBJECT); /****************************************************************************** * * * Paramètres : class = classe de composant GLib à initialiser. * * * * Description : Procède à l'initialisation d'une methode issue du code. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_dex_method_class_init(GDexMethodClass *class) { GObjectClass *object; /* Autre version de la classe */ object = G_OBJECT_CLASS(class); object->dispose = (GObjectFinalizeFunc/* ! */)g_dex_method_dispose; object->finalize = (GObjectFinalizeFunc)g_dex_method_finalize; } /****************************************************************************** * * * Paramètres : method = composant GLib à initialiser. * * * * Description : Procède à l'initialisation d'une methode issue du code. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_dex_method_init(GDexMethod *method) { } /****************************************************************************** * * * Paramètres : format = instance d'objet GLib à traiter. * * * * Description : Supprime toutes les références externes. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_dex_method_dispose(GDexMethod *method) { if (method->routine != NULL) g_object_unref(G_OBJECT(method->routine)); G_OBJECT_CLASS(g_dex_method_parent_class)->dispose(G_OBJECT(method)); } /****************************************************************************** * * * Paramètres : method = instance d'objet GLib à traiter. * * * * Description : Procède à la libération totale de la mémoire. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ static void g_dex_method_finalize(GDexMethod *method) { G_OBJECT_CLASS(g_dex_method_parent_class)->finalize(G_OBJECT(method)); } /****************************************************************************** * * * Paramètres : format = représentation interne du format DEX à consulter. * * seed = graine des informations à extraire. * * last = dernier indice utilisé (à mettre à jour). [OUT] * * * * Description : Crée une nouvelle représentation de methode issue de code. * * * * Retour : Composant GLib créé. * * * * Remarques : - * * * ******************************************************************************/ GDexMethod *g_dex_method_new(GDexFormat *format, const encoded_method *seed, uleb128_t *last) { GDexMethod *result; /* Composant à retourner */ vmpa2t addr; /* Tête de lecture générique */ code_item item; /* Corps de la méthode */ GBinRoutine *routine; /* Routine représentée */ mrange_t range; /* Emplacement du code associé */ init_vmpa(&addr, seed->code_off, VMPA_NO_VIRTUAL); if (!read_dex_code_item(format, &addr, &item)) return NULL; *last += seed->method_idx_diff; routine = get_prototype_from_dex_pool(format, *last); if (routine == NULL) return NULL; result = g_object_new(G_TYPE_DEX_METHOD, NULL); result->info = *seed; result->body = item; result->offset = seed->code_off + 4 * sizeof(uint16_t) + 2 *sizeof(uint32_t);/* TODO : faire plus propre ! */ init_vmpa(&addr, result->offset, VMPA_NO_VIRTUAL); init_mrange(&range, &addr, item.insns_size * sizeof(uint16_t)); g_binary_routine_set_range(routine, &range); result->routine = routine; return result; } /****************************************************************************** * * * Paramètres : format = représentation interne du format DEX à consulter.* * method_id = informations de base quant à la méthode. * * * * Description : Crée une nouvelle représentation de methode vide. * * * * Retour : Composant GLib créé. * * * * Remarques : - * * * ******************************************************************************/ GDexMethod *g_dex_method_new_empty(const GDexFormat *format, const method_id_item *method_id) { GDexMethod *result; /* Composant à retourner */ result = g_object_new(G_TYPE_DEX_METHOD, NULL); return result; } /****************************************************************************** * * * Paramètres : method = représentation interne de la méthode à consulter. * * * * Description : Fournit les indications Dex concernant la méthode. * * * * Retour : Données brutes du binaire. * * * * Remarques : - * * * ******************************************************************************/ const encoded_method *g_dex_method_get_dex_info(const GDexMethod *method) { return &method->info; } /****************************************************************************** * * * Paramètres : method = représentation interne de la méthode à consulter. * * * * Description : Fournit les indications Dex relatives au corps de la méthode.* * * * Retour : Données brutes du binaire. * * * * Remarques : - * * * ******************************************************************************/ const code_item *g_dex_method_get_dex_body(const GDexMethod *method) { return &method->body; } /****************************************************************************** * * * Paramètres : method = représentation interne du format DEX à consulter. * * * * Description : Fournit la routine OpenIDA correspondant à la méthode. * * * * Retour : Instance de routine mise en place. * * * * Remarques : - * * * ******************************************************************************/ GBinRoutine *g_dex_method_get_routine(const GDexMethod *method) { return method->routine; } /****************************************************************************** * * * Paramètres : method = représentation interne du format DEX à consulter. * * raw = portion de binaire brut à raffiner. * * * * Description : Intègre la méthode en tant que portion de code. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_dex_method_include_as_portion(const GDexMethod *method, GBinPortion *raw) { GBinPortion *new; /* Nouvelle portion définie */ char *desc; /* Description d'une portion */ vmpa2t addr; /* Emplacement dans le binaire */ /* Si la taille est nulle, on ne fait rien */ if (method->info.access_flags & ACC_NATIVE) return; new = g_binary_portion_new(BPC_CODE); asprintf(&desc, _("Dalvik code")); g_binary_portion_set_desc(new, desc); free(desc); init_vmpa(&addr, method->offset, VMPA_NO_VIRTUAL); g_binary_portion_set_values(new, &addr, method->body.insns_size * sizeof(uint16_t)); g_binary_portion_set_rights(new, PAC_READ | PAC_EXEC); g_binary_portion_include(raw, new); } /****************************************************************************** * * * Paramètres : method = représentation interne du format DEX à consulter. * * * * Description : Indique la position de la méthode au sein du binaire. * * * * Retour : Localisation dans le contenu binaire. * * * * Remarques : - * * * ******************************************************************************/ off_t g_dex_method_get_offset(const GDexMethod *method) { return method->offset; } /****************************************************************************** * * * Paramètres : method = représentation interne du format DEX à consulter. * * index = indice de base comme seul indice. * * * * Description : Fournit des indications sur la nature d'une variable donnée. * * * * Retour : Indentifiant complet d'une variable utilisée. * * * * Remarques : - * * * ******************************************************************************/ DexVariableIndex g_dex_method_get_variable(const GDexMethod *method, uint32_t index) { const encoded_method *info; /* Propriétés de la méthode */ const code_item *body; /* Corps de la méthode */ uint32_t pivot; /* Bascule pour les arguments */ info = &method->info; body = &method->body; /* S'agit-il d'un argument ? */ pivot = body->registers_size - body->ins_size; if (!(info->access_flags & ACC_STATIC)) pivot++; if (index >= pivot) return (index - pivot) | DVI_ARGUMENT; /* S'agit-il de "this" ? */ if (!(info->access_flags & ACC_STATIC) && index == (body->registers_size - body->ins_size)) return DVI_THIS; /* Alors il s'agit d'une variable locale... */ return index | DVI_LOCAL; } /****************************************************************************** * * * Paramètres : method = informations chargées à consulter. * * lang = langage à utiliser pour la sortie humaine. * * buffer = tampon mis à disposition pour la sortie. * * * * Description : Procède à la décompilation complète d'une routine donnée. * * * * Retour : - * * * * Remarques : - * * * ******************************************************************************/ void g_dex_method_decompile(const GDexMethod *method, GLangOutput *lang, GCodeBuffer *buffer) { g_binary_routine_print_code(method->routine, lang, buffer, true); }