summaryrefslogtreecommitdiff
path: root/tools/d2c/hooks/manager.c
blob: 02544d0941010f77979f210b4b01f5fa542727a9 (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

/* Chrysalide - Outil d'analyse de fichiers binaires
 * manager.c - prise en compte d'une syntaxe du langage d'assemblage
 *
 * Copyright (C) 2016-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 "manager.h"


#include <malloc.h>
#include <string.h>


#include "../helpers.h"



/* Paramèter d'une fonction de renvoi */
typedef struct _instr_func
{
    char *type;                             /* Type de fonction définie    */
    char *name;                             /* Désignation humaine         */

} instr_func;

/* Liste des fonctions de renvoi pour une instruction */
struct _instr_hooks
{
    instr_func *funcs;                      /* Liste de fonctions présentes*/
    size_t func_count;                      /* Taille de cette liste       */

};



/******************************************************************************
*                                                                             *
*  Paramètres  : -                                                            *
*                                                                             *
*  Description : Crée une liste de fonctions à lier à une instruction.        *
*                                                                             *
*  Retour      : Nouvelle structure prête à emploi.                           *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

instr_hooks *create_instr_hooks(void)
{
    instr_hooks *result;                    /* Définition vierge à renvoyer*/

    result = (instr_hooks *)calloc(1, sizeof(instr_hooks));

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : hooks = gestionnaire d'un ensemble de fonctions associées.   *
*                                                                             *
*  Description : Supprime de la mémoire une liste de fonctions liées.         *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void delete_instr_hooks(instr_hooks *hooks)
{
    size_t i;                               /* Boucle de parcours          */

    for (i = 0; i < hooks->func_count; i++)
    {
        free(hooks->funcs[i].type);
        free(hooks->funcs[i].name);
    }

    if (hooks->funcs != NULL)
        free(hooks->funcs);

    free(hooks);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : hooks = gestionnaire d'un ensemble de fonctions associées.   *
*                type  = type de fonction à enregistrer pour une instruction. *
*                name  = désignation de la fonction à associer.               *
*                                                                             *
*  Description : Enregistre l'utilité d'une fonction pour une instruction.    *
*                                                                             *
*  Retour      : -                                                            *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

void register_hook_function(instr_hooks *hooks, char *type, char *name)
{
    instr_func *func;                       /* Nouvelle prise en compte    */

    hooks->funcs = (instr_func *)realloc(hooks->funcs, ++hooks->func_count * sizeof(instr_func));

    func = &hooks->funcs[hooks->func_count - 1];

    func->type = make_string_upper(type);
    func->name = strdup(name);

}


/******************************************************************************
*                                                                             *
*  Paramètres  : hooks = gestionnaire d'un ensemble de fonctions associées.   *
*                fd    = descripteur d'un flux ouvert en écriture.            *
*                                                                             *
*  Description : Déclare des opérations complémentaires pour une instruction. *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool declare_hook_functions(const instr_hooks *hooks, int fd)
{
    bool result;                            /* Bilan à retourner           */
    size_t i;                               /* Boucle de parcours #1       */
    const instr_func *func;                 /* Nouvelle prise en compte    */

    static const char *hook_types[] = {
        "FETCH",
        "LINK",
        "POST"
    };

    const instr_func *find_hook_by_name(const instr_hooks *list, const char *type)
    {
        const instr_func *hook;             /* Trouvaille à retourner      */
        size_t k;                           /* Boucle de parcours #2       */

        hook = NULL;

        for (k = 0; k < list->func_count && hook == NULL; k++)
            if (strcmp(list->funcs[k].type, type) == 0)
                hook = &list->funcs[k];

        return hook;

    }

    result = true;

    if (hooks->func_count > 0)
    {
        dprintf(fd, "\tstatic const instr_hook_fc hooks[IPH_COUNT] = {\n\n");

        for (i = 0; i < (sizeof(hook_types) / sizeof(hook_types[0])); i++)
        {
            func = find_hook_by_name(hooks, hook_types[i]);

            dprintf(fd, "\t\t[IPH_%s] = (instr_hook_fc)%s,\n", hook_types[i], func != NULL ? func->name : "NULL");

        }

        dprintf(fd, "\n");

        dprintf(fd, "\t};\n");

        dprintf(fd, "\n");

    }

    return result;

}


/******************************************************************************
*                                                                             *
*  Paramètres  : hooks = gestionnaire d'un ensemble de fonctions associées.   *
*                fd    = descripteur d'un flux ouvert en écriture.            *
*                                                                             *
*  Description : Associe dans le code des fonctions à une instruction.        *
*                                                                             *
*  Retour      : Bilan de l'opération.                                        *
*                                                                             *
*  Remarques   : -                                                            *
*                                                                             *
******************************************************************************/

bool write_hook_functions(const instr_hooks *hooks, int fd)
{
    bool result;                            /* Bilan à retourner           */

    result = true;

    if (hooks->func_count > 0)
    {
        dprintf(fd, "\tg_arch_instruction_set_hooks(result, hooks);\n");

        dprintf(fd, "\n");

    }

    return result;

}