summaryrefslogtreecommitdiff
path: root/plugins/elf/helper_arm.c
blob: 111e34bb02f24ffd37f538b7fe486097446f8a1d (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * helper_x86.c - gestion auxiliaire de l'architecture x86
 *
 * Copyright (C) 2014-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 Chrysalide.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "helper_arm.h"


#include "elf_def_arm.h"
#include "elf-int.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : p_type = type associé à un en-tête de programme.             *
*                                                                             *
*  Description : Fournit la description humaine d'un type de segment ELF.     *
*                                                                             *
*  Retour      : Désignation prête à emploi ou NULL si aucune.                *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *get_elf_program_arm_type_desc(uint32_t p_type)
{
    const char *result;                     /* Description à renvoyer      */

#define MAKE_STRING_FROM_PT(pt) case pt: result = #pt; break;

    switch(p_type)
    {
        MAKE_STRING_FROM_PT(PT_ARM_EXIDX);

        default:
            result = NULL;
            break;
    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : virt = adresse virtuelle éventuellement porteuse d'infos.    *
*                                                                             *
*  Description : Fournit une adresse virtuelle prête à emploi.                *
*                                                                             *
*  Retour      : Adresse virtuelle réellement utilisable.                     *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

virt_t fix_elf_arm_virtual_address(virt_t virt)
{
    virt_t result;                          /* Résultat à retourner        */

    result = virt & ~0x1;

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à manipuler.            *
*                range  = emplacement de la procédure de liaison.             *
*                offset = décalage retrouvé par désassemblage... [OUT]        *
*                                                                             *
*  Description : Retrouve le décalage appliqué lors d'une résolution.         *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool retrieve_arm_linkage_offset(GElfFormat *format, const mrange_t *range, uint64_t *offset)
{
    bool result;                            /* Bilan à retourner           */
    GBinContent *content;                   /* Contenu binaire à parcourir */
    vmpa2t pos;                             /* Tete de lecture             */
    uint32_t raw;                           /* Valeur brute lue            */
    uint32_t shift;                         /* Décalage arithmétique       */

    /**
     * Pour faciliter la compréhension, on peut s'appuyer sur la lecture de :
     *
     *    http://blog.qt.io/blog/2010/12/04/moving-code-around/
     *
     */

    content = G_BIN_FORMAT(format)->content;

    copy_vmpa(&pos, get_mrange_addr(range));

    result = g_binary_content_read_u32(content, &pos, format->endian, &raw);
    if (!result) goto ralo_exit;

    /**
     * On ne reconnaît pour l'instant que la seule combinaison suivante.
     *
     * Charge de compléter cette reconnaissance en fonction de nouvelles
     * découvertes !
     */

    /**
     * R_ARM_JUMP_SLOT :
     *
     *    e28fc600     add ip, pc, #0, 12
     *    e28cca08     add ip, ip, #8, 20  ; 0x8000
     *    e5bcf310     ldr pc, [ip, #784]! ; 0x310
     */

    if (raw == 0xe28fc600)
    {
        *offset = get_virt_addr(get_mrange_addr(range)) + 8;

        result = g_binary_content_read_u32(content, &pos, format->endian, &raw);
        if (!result) goto ralo_exit;

        /**
         * La seconde instruction répond à l'encodage spécifié dans :
         *
         *    A8.8.5 - ADD (immediate, ARM)
         *
         *    31 30 29 28 | 27 26 25 24 23 22 21 20 | 19 18 17 16 | 15 14 13 12 | 11 10 9 8 7 6 5 4 3 2 1 0
         *        cond    |  0  0  1  0  1  0  0  S |      Rn     |      Rd     |           imm12
         *
         * On a donc :
         *
         *    ADD{S}{<c>}{<q>} {<Rd>,} <Rn>, #<const>
         *
         * Avec :
         *
         *    - S = 0.
         *    - Rn = ip = r12 = 0xc
         *    - Rd = ip = r12 = 0xc
         *    - const = ARMExpandImm(imm12)
         *
         * Le fonctionnement de la macro ARMExpandImm est détaillé dans :
         *
         *    A5.2.4 - Modified immediate constants in ARM instructions
         *
         */

        if ((raw & 0xfffff000) != 0xe28cc000)
        {
            result = false;
            goto ralo_exit;
        }

        shift = 32 - ((raw & 0xf00) >> 8) * 2;

        *offset += (raw & 0xf) << shift;

        result = g_binary_content_read_u32(content, &pos, format->endian, &raw);
        if (!result) goto ralo_exit;

        /**
         * La dernière instruction répond à l'encodage spéficié dans :
         *
         *    A8.8.63 - LDR (immediate, ARM)
         *
         *    31 30 29 28 | 27 26 25 24 23 22 21 20 | 19 18 17 16 | 15 14 13 12 | 11 10 9 8 7 6 5 4 3 2 1 0
         *        cond    |  0  1  0  P  U  0  W  1 |      Rn     |      Rt     |           imm12
         *
         * On a donc :
         *
         *    LDR{<c>}{<q>} <Rt>, [<Rn>, #+/-<imm>]!
         *
         * Avec :
         *
         *    - P = 1 (index).
         *    - U = 1 (add).
         *    - W = 1 (wback).
         *    - Rn = ip = r12 = 0xc
         *    - Rt = pc = r15 = 0xf
         *
         */

        if ((raw & 0xfffff000) != 0xe5bcf000)
        {
            result = false;
            goto ralo_exit;
        }

        *offset += (raw & 0xfff);

    }

    else
        result = false;

 ralo_exit:

    return result;

}