summaryrefslogtreecommitdiff
path: root/plugins/elf/dynamic.c
blob: ed681d1b6cb51e7b81f3d01369751939ead71010 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * program.c - gestion des en-têtes de programme d'un ELF
 *
 * Copyright (C) 2015-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 "dynamic.h"


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



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

bool find_elf_dynamic_program_header(const GElfFormat *format, elf_phdr *dynamic)
{
    bool result;                            /* Bilan d'opération à renvoyer*/
    uint16_t max;                           /* Nombre d'en-têtes présents  */
    uint16_t i;                             /* Boucle de parcours          */

    result = false;

    max = ELF_HDR(format, format->header, e_phnum);

    for (i = 0; i < max && !result; i++)
    {
        if (!find_elf_program_by_index(format, i, dynamic))
            break;

        result = (ELF_PHDR(format, *dynamic, p_type) == PT_DYNAMIC);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format  = informations chargées à consulter.                 *
*                dynamic = programme de type PT_DYNAMIC.                      *
*                type    = sorte d'élément recherché.                         *
*                item    = élément retrouvé dans la section. [OUT]            *
*                                                                             *
*  Description : Retrouve un élément donné dans la section dynamique.         *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool find_elf_dynamic_item_from_pheader(const GElfFormat *format, const elf_phdr *dynamic, int64_t type, elf_dyn *item)
{
    bool result;                            /* Bilan à retourner           */
    off_t max;                              /* Nombre d'entités présentes  */
    off_t i;                                /* Boucle de parcours          */
    phys_t pos;                             /* Position de lecture         */

    result = false;

    max = ELF_PHDR(format, *dynamic, p_filesz) / ELF_SIZEOF_DYN(format);

    for (i = 0; i < max && !result; i++)
    {
        pos = ELF_PHDR(format, *dynamic, p_offset) + i * ELF_SIZEOF_DYN(format);

        if (!read_elf_dynamic_entry(format, pos, item))
            break;

        result = (ELF_DYN(format, *item, d_tag) == type);

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : format = description de l'exécutable à consulter.            *
*                count  = nombre d'éléments dans la liste constituée.         *
*                                                                             *
*  Description : Fournit la liste des objets partagés requis.                 *
*                                                                             *
*  Retour      : Liste de noms d'objets ou NULL en cas d'échec.               *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

const char **list_elf_needed_objects(const GElfFormat *format, size_t *count)
{
    const char **result;                    /* Liste à retourner           */
    elf_phdr dynamic;                       /* Programme à analyser        */
    virt_t strtab_virt;                     /* Adresse mémoire des chaînes */
    uint64_t max;                           /* Nombre d'entités présentes  */
    uint64_t i;                             /* Boucle de parcours          */
    phys_t pos;                             /* Position de lecture         */
    elf_dyn item;                           /* Informations extraites      */
    vmpa2t strtab_pos;                      /* Emplacement des chaînes     */
    GBinContent *content;                   /* Contenu global analysé      */
    vmpa2t end;                             /* Limite finale de contenu    */
    vmpa2t str_pos;                         /* Emplacement d'une chaîne    */
    phys_t diff;                            /* Données encore disponibles  */
    const bin_t *string;                    /* Nouvelle chaîne trouvée     */

    result = NULL;
    *count = 0;

    if (!find_elf_program_by_type(format, PT_DYNAMIC, &dynamic))
        goto leno_exit;

    max = ELF_PHDR(format, dynamic, p_filesz) / ELF_SIZEOF_DYN(format);

    /* Première passe : recherche des chaînes */

    strtab_virt = VMPA_NO_VIRTUAL;

    for (i = 0; i < max; i++)
    {
        pos = ELF_PHDR(format, dynamic, p_offset) + i * ELF_SIZEOF_DYN(format);

        if (!read_elf_dynamic_entry(format, pos, &item))
            break;

        if (ELF_DYN(format, item, d_tag) == DT_STRTAB)
            strtab_virt = ELF_DYN(format, item, d_un.d_val);

    }

    if (strtab_virt == VMPA_NO_VIRTUAL)
        goto leno_exit;

    if (!g_exe_format_translate_address_into_vmpa(G_EXE_FORMAT(format), strtab_virt, &strtab_pos))
        goto leno_exit;

    /* Seconde passe : recherche des objets requis */

    content = G_BIN_FORMAT(format)->content;

    g_binary_content_compute_end_pos(content, &end);

    for (i = 0; i < max; i++)
    {
        pos = ELF_PHDR(format, dynamic, p_offset) + i * ELF_SIZEOF_DYN(format);

        if (!read_elf_dynamic_entry(format, pos, &item))
            break;

        if (ELF_DYN(format, item, d_tag) == DT_NEEDED)
        {
            copy_vmpa(&str_pos, &strtab_pos);
            advance_vmpa(&str_pos, ELF_DYN(format, item, d_un.d_val));

            diff = compute_vmpa_diff(&str_pos, &end);

            string = g_binary_content_get_raw_access(content, &str_pos, diff);

            result = (const char **)realloc(result, ++(*count) * sizeof(const char *));
            result[*count - 1] = (const char *)string;

        }

    }

 leno_exit:

    return result;

}