/* 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 "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 *); /* 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) { } /****************************************************************************** * * * 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 = 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(const GDexFormat *format, const encoded_method *seed, uleb128_t *last) { GDexMethod *result; /* Composant à retourner */ off_t offset; /* Tête de lecture */ code_item item; /* Corps de la méthode */ GBinRoutine *routine; vmpa2t addr; mrange_t range; offset = seed->code_off; if (!read_dex_code_item(format, &offset, &item)) return NULL; *last += seed->method_idx_diff; routine = get_routine_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; //printf(" ==== (%p) %s ====\n", routine, g_binary_routine_to_string(routine)); //printf(" try ? %d\n", item.tries_size); //printf(" method idx :: %d\n", seed->method_idx_diff); //printf(" code size :: %d\n", item.insns_size); /* printf(" code regs :: %d\n", item.registers_size); printf(" code ins :: %d\n", item.ins_size); printf(" code outs :: %d\n", item.outs_size); */ //printf(" method idx :: %lld\n", *last); result->offset = seed->code_off + 4 * sizeof(uint16_t) + 2 *sizeof(uint32_t);/* TODO : faire plus propre ! */ //printf(" method off :: 0x%08x\n", result->offset); 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 : 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. * * * * Description : Fournit la zone binaire correspondant à la méthode. * * * * Retour : Zone binaire à analyser. * * * * Remarques : - * * * ******************************************************************************/ #if 0 GBinPart *g_dex_method_as_part(const GDexMethod *method) { GBinPart *result; /* Instance à retourner */ off_t size; /* Taille du code associé */ result = g_binary_part_new(); g_binary_part_set_name(result, "name"); if (method->info.access_flags & ACC_NATIVE) size = 0; else size = method->body.insns_size * sizeof(uint16_t); g_binary_part_set_values(result, method->offset, size, method->offset); return result; } #endif /****************************************************************************** * * * 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); }