summaryrefslogtreecommitdiff
path: root/plugins/elf/program.c
blob: 83fdc6fa17eb10a2519835412528f8ce84e9161b (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * program.c - gestion des en-têtes de programme d'un ELF
 *
 * Copyright (C) 2010-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 Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "program.h"


#include "elf-int.h"



/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                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.                                  *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char *get_elf_program_type_desc(const GElfFormat *format, 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_NULL);
        MAKE_STRING_FROM_PT(PT_LOAD);
        MAKE_STRING_FROM_PT(PT_DYNAMIC);
        MAKE_STRING_FROM_PT(PT_INTERP);
        MAKE_STRING_FROM_PT(PT_NOTE);
        MAKE_STRING_FROM_PT(PT_SHLIB);
        MAKE_STRING_FROM_PT(PT_PHDR);
        MAKE_STRING_FROM_PT(PT_TLS);
        MAKE_STRING_FROM_PT(PT_NUM);
        MAKE_STRING_FROM_PT(PT_GNU_EH_FRAME);
        MAKE_STRING_FROM_PT(PT_GNU_STACK);
        MAKE_STRING_FROM_PT(PT_GNU_RELRO);
        MAKE_STRING_FROM_PT(PT_LOSUNW);
        MAKE_STRING_FROM_PT(PT_SUNWSTACK);

        default:
            result = NULL;
            break;

    }

    if (result == NULL)
        result = format->ops.get_type_desc(p_type);

    if (result == NULL)
        switch(p_type)
        {
            MAKE_STRING_FROM_PT(PT_LOOS);
            MAKE_STRING_FROM_PT(PT_HIOS);
            MAKE_STRING_FROM_PT(PT_LOPROC);
            MAKE_STRING_FROM_PT(PT_HIPROC);

            default:
                result = "PT_???";
                break;
        }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format  = description de l'exécutable à consulter.           *
*                index   = indice de la partie recherchée.                    *
*                program = ensemble d'informations à faire remonter. [OUT]    *
*                                                                             *
*  Description : Recherche un programme donné au sein de binaire par indice.  *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool find_elf_program_by_index(const GElfFormat *format, uint16_t index, elf_phdr *program)
{
    off_t offset;                           /* Emplacement à venir lire    */

    if (index >= ELF_HDR(format, format->header, e_phnum)) return false;

    offset = ELF_HDR(format, format->header, e_phoff)
        + ELF_HDR(format, format->header, e_phentsize) * index;

    return read_elf_program_header(format, offset, program);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format  = description de l'exécutable à consulter.           *
*                type    = type de la partie recherchée.                      *
*                program = ensemble d'informations à faire remonter. [OUT]    *
*                                                                             *
*  Description : Recherche un programme donné au sein de binaire par type.    *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool find_elf_program_by_type(const GElfFormat *format, uint32_t type, elf_phdr *program)
{
    bool result;                            /* Bilan à retourner           */
    uint16_t i;                             /* Boucle de parcours          */

    result = false;

    for (i = 0; i < ELF_HDR(format, format->header, e_phnum) && !result; i++)
    {
        find_elf_program_by_index(format, i, program);

        if (ELF_PHDR(format, *program, p_type) == type)
            result = true;

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                off    = position physique à retrouver.                      *
*                pos    = position correspondante. [OUT]                      *
*                                                                             *
*  Description : Fournit l'emplacement correspondant à une position physique. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool translate_offset_into_vmpa_using_elf_programs(const GElfFormat *format, phys_t off, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    uint16_t i;                             /* Boucle de parcours          */
    elf_phdr program;                       /* Programme à analyser        */
    virt_t addr;                            /* Adresse virtuelle calculée  */

    result = false;

    for (i = 0; i < ELF_HDR(format, format->header, e_phnum) && !result; i++)
    {
        find_elf_program_by_index(format, i, &program);

        if (ELF_PHDR(format, program, p_offset) <= off
            && off < (ELF_PHDR(format, program, p_offset) + ELF_PHDR(format, program, p_filesz)))
        {
            addr = ELF_PHDR(format, program, p_vaddr) + ELF_PHDR(format, program, p_offset) - off;
            init_vmpa(pos, off, addr);
            result = true;
        }

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                addr   = adresse virtuelle à retrouver.                      *
*                pos    = position correspondante. [OUT]                      *
*                                                                             *
*  Description : Fournit l'emplacement correspondant à une adresse virtuelle. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool translate_address_into_vmpa_using_elf_programs(const GElfFormat *format, virt_t addr, vmpa2t *pos)
{
    bool result;                            /* Bilan à retourner           */
    uint16_t i;                             /* Boucle de parcours          */
    elf_phdr program;                       /* Programme à analyser        */
    phys_t off;                             /* Position physique calculée  */

    result = false;

    for (i = 0; i < ELF_HDR(format, format->header, e_phnum) && !result; i++)
    {
        find_elf_program_by_index(format, i, &program);

        if (ELF_PHDR(format, program, p_vaddr) <= addr
            && addr < (ELF_PHDR(format, program, p_vaddr) + ELF_PHDR(format, program, p_filesz)))
        {
            off = ELF_PHDR(format, program, p_offset) + addr - ELF_PHDR(format, program, p_vaddr);
            init_vmpa(pos, off, addr);
            result = true;
        }

    }

    return result;

}