summaryrefslogtreecommitdiff
path: root/src/arch/dalvik/link.c
blob: 694ec65523cabf2b60bff87aeba07c4ccdfeafef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310

/* Chrysalide - Outil d'analyse de fichiers binaires
 * link.c - édition des liens après la phase de désassemblage
 *
 * Copyright (C) 2016 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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "link.h"


#include <assert.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>


#include <i18n.h>


#include "pseudo/switch.h"
#include "../target.h"
#include "../../common/extstr.h"



/* Mémorisation des cas rencontrés */
typedef struct _case_comment
{
    bool valid;                             /* Entrée utilisable ?         */

    vmpa2t handler;                         /* Position du code associé    */

    bool is_default;                        /* Gestion par défaut ?        */
    union
    {
        int32_t key;                        /* Clef unique                 */
        int32_t *keys;                      /* Ensemble de clefs dynamique */
    };

    size_t count;                           /* Nombre de clefs conservées  */

} case_comment;



/******************************************************************************
*                                                                             *
*  Paramètres  : instr   = instruction ARMv7 à traiter.                       *
*                proc    = représentation de l'architecture utilisée.         *
*                context = contexte associé à la phase de désassemblage.      *
*                format  = acès aux données du binaire d'origine.             *
*                                                                             *
*  Description : Etablit tous les liens liés à un embranchement compressé.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void handle_dalvik_packed_switch_links(GArchInstruction *instr, GArchProcessor *proc, GProcContext *context, GExeFormat *format)
{
    GArchOperand *op;                       /* Opérande numérique en place */
    virt_t virt;                            /* Adresse virtuelle           */
    vmpa2t addr;                            /* Adresse de destination      */
    GArchInstruction *switch_ins;           /* Instruction de branchements */
    const mrange_t *range;                  /* Zone d'occupation           */
    const vmpa2t *start_addr;               /* Adresse de référentiel      */
    const int32_t *keys;                    /* Conditions de sauts         */
    const int32_t *targets;                 /* Positions relatives liées   */
    uint16_t count;                         /* Taille de ces tableaux      */
    case_comment *comments;                 /* Mémorisation progressive    */
    vmpa2t def_addr;                        /* Traitement par défaut       */
    GArchInstruction *target;               /* Ligne visée par la référence*/
    case_comment *comment;                  /* Commentaire à éditer        */
    uint16_t i;                             /* Boucle de parcours #1       */
    size_t j;                               /* Boucle de parcours #2       */
    int32_t tmp;                            /* Sauvegarde temporaire       */
    char *msg;                              /* Indication à imprimer       */
    size_t k;                               /* Boucle de parcours #3       */
    char *int_val;                          /* Valeur en chaîne de carac.  */
    GDbComment *item;                       /* Indication sur la condition */

    assert(g_arch_instruction_count_operands(instr) == 2);

    op = g_arch_instruction_get_operand(instr, 1);

    virt = VMPA_NO_VIRTUAL;

    if (G_IS_TARGET_OPERAND(op))
        virt = g_target_operand_get_addr(G_TARGET_OPERAND(op));

    else if (G_IS_IMM_OPERAND(op))
    {
        if (!g_imm_operand_to_virt_t(G_IMM_OPERAND(op), &virt))
            virt = VMPA_NO_VIRTUAL;
    }

    if (virt != VMPA_NO_VIRTUAL)
    {
        /* TODO : utiliser format pour contruire une adresse avec une position physique,
         * ce qui accélèrerait les recherches.
         */
        init_vmpa(&addr, VMPA_NO_PHYSICAL, virt);

        switch_ins = g_arch_processor_find_instr_by_address(proc, &addr);

        if (G_IS_DALVIK_SWITCH_INSTR(switch_ins))
        {
            range = g_arch_instruction_get_range(instr);

            start_addr = get_mrange_addr(range);

            /* Préparation de l'édition des commentaires */

            count = g_dalvik_switch_get_data(G_DALVIK_SWITCH_INSTR(switch_ins), &keys, &targets);

            comments = (case_comment *)calloc(1 + count, sizeof(case_comment));

            /* Cas par défaut */

            compute_mrange_end_addr(range, &def_addr);

            target = g_arch_processor_find_instr_by_address(proc, &def_addr);

            if (target != NULL)
            {
                comment = &comments[0];

                comment->valid = true;

                copy_vmpa(&comment->handler, &def_addr);

                comment->is_default = true;

                g_arch_instruction_link_with(instr, target, ILT_CASE_JUMP);

                g_object_unref(G_OBJECT(target));

            }

            /* Autres cas */

            for (i = 0; i < count; i++)
            {
                copy_vmpa(&addr, start_addr);
                advance_vmpa(&addr, targets[i] * sizeof(uint16_t));

                if (cmp_vmpa(&addr, &def_addr) == 0)
                    continue;

                target = g_arch_processor_find_instr_by_address(proc, &addr);

                if (target != NULL)
                {
                    for (j = 0; j < (1 + count); j++)
                    {
                        if (!comments[j].valid)
                            break;

                        if (cmp_vmpa(&addr, &comments[j].handler) == 0)
                            break;

                    }

                    assert(j < (1 + count));

                    comment = &comments[j];

                    if (!comment->valid)
                    {
                        comment->valid = true;

                        copy_vmpa(&comment->handler, &addr);

                        comment->key = keys[i];
                        comment->count = 1;

                    }
                    else
                    {
                        if (comment->count == 0)
                            comment->key = keys[i];

                        if (comment->count == 1)
                        {
                            tmp = comment->key;

                            comment->keys = (int32_t *)calloc(2, sizeof(int32_t));

                            comment->keys[0] = tmp;
                            comment->keys[1] = keys[i];

                            comment->count = 2;

                        }

                        else
                        {
                            comment->count++;

                            comment->keys = (int32_t *)realloc(comment->keys, comment->count * sizeof(int32_t));

                            comment->keys[comment->count - 1] = keys[i];

                        }

                    }

                    g_arch_instruction_link_with(instr, target, ILT_CASE_JUMP);

                    g_object_unref(G_OBJECT(target));

                }

            }

            /* Edition des commentaires et nettoyage */

            for (j = 0; j < (1 + count); j++)
            {
                comment = &comments[j];

                if (!comment->valid)
                    break;

                switch (comment->count)
                {
                    case 0:
                        msg = NULL;
                        break;

                    case 1:
                        asprintf(&msg, _("Case %d"), comment->key);
                        break;

                    default:

                        msg = NULL;

                        /**
                         * Les spécifications indiquent que les clefs sont triées.
                         * Donc nul besoin de s'occuper de leur ordre ici.
                         */

                        for (k = 0; k < comment->count; k++)
                        {
                            if (k > 0)
                                msg = stradd(msg, COMMENT_LINE_SEP);

                            asprintf(&int_val, _("Case %d:"), comment->keys[k]);
                            msg = stradd(msg, int_val);
                            free(int_val);

                        }

                        break;

                }

                if (comment->is_default)
                {
                    if (msg == NULL)
                        msg = strdup(_("Defaut case:"));
                    else
                    {
                        msg = stradd(msg, COMMENT_LINE_SEP);
                        msg = stradd(msg, _("Defaut case"));
                    }

                }

                item = g_db_comment_new_area(&comment->handler, BLF_NONE, msg, true);

                g_db_item_set_volatile(G_DB_ITEM(item), true);
                g_proc_context_add_db_item(context, G_DB_ITEM(item));

                free(msg);

                if (comment->count > 1)
                    free(comment->keys);

            }

            free(comments);

        }

        if (switch_ins != NULL)
            g_object_unref(G_OBJECT(switch_ins));

    }

}