summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2015-01-10 16:37:34 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2015-01-10 16:37:34 (GMT)
commit8ef66a1e0225c9e00175fbaf3f3038f537de511f (patch)
treedd7112dd50c02e0ad1565ce7a890991b5e1270c6 /tools
parentcc3e31eecd90766ae4f0bb391428c5c59567ef4c (diff)
Extended the grammar to allow hooks inclusion.
git-svn-id: svn://svn.gna.org/svn/chrysalide/trunk@453 abbe820e-26c8-41b2-8c08-b7b2b41f8b0a
Diffstat (limited to 'tools')
-rw-r--r--tools/d2c/Makefile.am1
-rw-r--r--tools/d2c/d2c_gram.y27
-rw-r--r--tools/d2c/d2c_tok.l13
-rw-r--r--tools/d2c/hooks.c169
-rw-r--r--tools/d2c/hooks.h51
-rw-r--r--tools/d2c/spec.c47
-rw-r--r--tools/d2c/spec.h4
7 files changed, 284 insertions, 28 deletions
diff --git a/tools/d2c/Makefile.am b/tools/d2c/Makefile.am
index d0fdbe8..e997beb 100644
--- a/tools/d2c/Makefile.am
+++ b/tools/d2c/Makefile.am
@@ -13,6 +13,7 @@ d2c_SOURCES = \
d2c_tok.l \
d2c_gram.y \
helpers.h helpers.c \
+ hooks.h hooks.c \
pproc.h pproc.c \
rules.h rules.c \
spec.h spec.c \
diff --git a/tools/d2c/d2c_gram.y b/tools/d2c/d2c_gram.y
index b1519a1..12eff11 100644
--- a/tools/d2c/d2c_gram.y
+++ b/tools/d2c/d2c_gram.y
@@ -41,8 +41,6 @@ struct action_tmp
};
-
-
#define register_named_field_in_coder(c, n, l) \
({ \
encoding_spec *__spec; \
@@ -88,6 +86,15 @@ struct action_tmp
register_conversion(__list, f); \
})
+#define register_hook_in_coder(c, t, f) \
+ ({ \
+ encoding_spec *__spec; \
+ instr_hooks *__hooks;; \
+ __spec = get_current_encoding_spec(c); \
+ __hooks = get_hooks_in_encoding_spec(__spec); \
+ register_hook_function(__hooks, t, f); \
+ })
+
#define add_conditional_rule_to_coder(c, e, a, d) \
({ \
encoding_spec *__spec; \
@@ -97,11 +104,6 @@ struct action_tmp
register_conditional_rule(__rules, e, a, d); \
})
-
-
-
-
-
}
%union {
@@ -142,6 +144,8 @@ struct action_tmp
%token CONV EQ OP COMMA CP NOT EOR COLON
+%token HOOKS
+
%token RULES IF EXPR_START EQUAL BINVAL IMMVAL EXPR_END AND THEN SEE UNPREDICTABLE
@@ -190,6 +194,7 @@ content : /* empty */
| bitfield content
| syntax content
| conversions content
+ | hooks content
| rules content
@@ -250,6 +255,14 @@ conv_arg_field : NAME { $$ = $1; printf(" composed::name '%s'\n", $1); }
| BINVAL { $$ = $1; printf(" composed::bin '%s'\n", $1); }
+hooks : HOOKS hookings
+
+hookings : /* empty */
+ | hookings hooking
+
+hooking : NAME EQ NAME { register_hook_in_coder(coder, $1, $3); }
+
+
rules : RULES rules_list
rules_list : /* empty */
diff --git a/tools/d2c/d2c_tok.l b/tools/d2c/d2c_tok.l
index 71f1a15..8f54c8d 100644
--- a/tools/d2c/d2c_tok.l
+++ b/tools/d2c/d2c_tok.l
@@ -33,6 +33,8 @@ void free_flex_memory(void) ;
%x conv_begin conv_content conv_arg conv_arg_binval
+%x hooks_begin hooks_content
+
%x rules_begin rules_content rules_cond rules_cond_binval rules_action rules_action_see
@@ -145,6 +147,17 @@ void free_flex_memory(void) ;
+<encoding_content>"@hooks" { BEGIN(hooks_begin); return HOOKS; }
+<hooks_begin>[ ]+ { }
+<hooks_begin>"{" { BEGIN(hooks_content); }
+<hooks_content>"}" { BEGIN(encoding_content); }
+
+<hooks_content>[ \t\n]+ { }
+<hooks_content>[a-z_][a-z0-9_]* { d2c_lval.string = strdup(yytext); return NAME; }
+<hooks_content>"=" { return EQ; }
+
+
+
<encoding_content>"@rules" { BEGIN(rules_begin); return RULES; }
<rules_content>\/\/[^\n]+ { printf("SKIP '%s'\n", yytext); }
<rules_begin>[ ]+ { }
diff --git a/tools/d2c/hooks.c b/tools/d2c/hooks.c
new file mode 100644
index 0000000..a110664
--- /dev/null
+++ b/tools/d2c/hooks.c
@@ -0,0 +1,169 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * syntax.c - prise en compte d'une syntaxe du langage d'assemblage
+ *
+ * Copyright (C) 2014 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA 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.
+ *
+ * OpenIDA 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 "hooks.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 : 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 */
+ size_t i; /* Boucle de parcours */
+ instr_func *func; /* Nouvelle prise en compte */
+
+ result = true;
+
+ for (i = 0; i < hooks->func_count && result; i++)
+ {
+ func = &hooks->funcs[i];
+
+ dprintf(fd, "\t\tg_arch_instruction_set_hook(instr, IPH_%s, (instr_hook_fc)%s);\n",
+ func->type, func->name);
+
+ }
+
+ if (hooks->func_count > 0 && result)
+ dprintf(fd, "\n");
+
+ return result;
+
+}
diff --git a/tools/d2c/hooks.h b/tools/d2c/hooks.h
new file mode 100644
index 0000000..5dbf223
--- /dev/null
+++ b/tools/d2c/hooks.h
@@ -0,0 +1,51 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * hooks.h - prototypes pour la prise en compte d'une hookse du langage d'assemblage
+ *
+ * Copyright (C) 2014 Cyrille Bagard
+ *
+ * This file is part of Chrysalide.
+ *
+ * OpenIDA 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.
+ *
+ * OpenIDA 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/>.
+ */
+
+
+#ifndef _TOOLS_HOOKS_H
+#define _TOOLS_HOOKS_H
+
+
+#include <stdbool.h>
+
+
+
+/* Liste des fonctions de renvoi pour une instruction */
+typedef struct _instr_hooks instr_hooks;
+
+
+
+/* Crée une liste de fonctions à lier à une instruction. */
+instr_hooks *create_instr_hooks(void);
+
+/* Supprime de la mémoire une liste de fonctions liées. */
+void delete_instr_hooks(instr_hooks *);
+
+/* Enregistre l'utilité d'une fonction pour une instruction. */
+void register_hook_function(instr_hooks *, char *, char *);
+
+/* Associe dans le code des fonctions à une instruction. */
+bool write_hook_functions(const instr_hooks *, int);
+
+
+
+#endif /* _TOOLS_HOOKS_H */
diff --git a/tools/d2c/spec.c b/tools/d2c/spec.c
index 9990881..b56c6aa 100644
--- a/tools/d2c/spec.c
+++ b/tools/d2c/spec.c
@@ -33,22 +33,6 @@
-
-
-#define get_raw_bitfield_name(bf) ""
-
-
-
-
-
-#define get_syntax_item_name(si) ""
-
-
-
-
-
-
-
/* Mémorisation d'un encodage complet */
struct _encoding_spec
{
@@ -59,17 +43,13 @@ struct _encoding_spec
coding_bits *bits; /* Encodage des bits associés */
asm_syntax *syntax; /* Calligraphe d'assemblage */
conv_list *conversions; /* Conversions des données */
+ instr_hooks *hooks; /* Fonctions complémentaires */
decoding_rules *rules; /* Règles supplémentaires */
};
-
-
-
-
-
/******************************************************************************
* *
* Paramètres : - *
@@ -91,6 +71,7 @@ encoding_spec *create_encoding_spec(void)
result->bits = create_coding_bits();
result->syntax = create_asm_syntax();
result->conversions = create_conv_list();
+ result->hooks = create_instr_hooks();
result->rules = create_decoding_rules();
return result;
@@ -115,6 +96,7 @@ void delete_encoding_spec(encoding_spec *spec)
delete_coding_bits(spec->bits);
delete_asm_syntax(spec->syntax);
delete_conv_list(spec->conversions);
+ delete_instr_hooks(spec->hooks);
delete_decoding_rules(spec->rules);
free(spec);
@@ -226,6 +208,25 @@ conv_list *get_conversions_in_encoding_spec(const encoding_spec *spec)
* *
* Paramètres : spec = spécification d'encodage à consulter. *
* *
+* Description : Fournit la liste des fonctions à lier à une instruction. *
+* *
+* Retour : Structure assurant la gestion des fonctions de conversion. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+instr_hooks *get_hooks_in_encoding_spec(const encoding_spec *spec)
+{
+ return spec->hooks;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : spec = spécification d'encodage à consulter. *
+* *
* Description : Fournit un ensemble de règles supplémentaires éventuel. *
* *
* Retour : Structure assurant la gestion de ces règles. *
@@ -313,6 +314,10 @@ bool write_encoding_spec_disass(const encoding_spec *spec, int fd, const char *a
dprintf(fd, "\n");
+ /* Inscriptions des éventuelles fonctions à lier */
+
+ result &= write_hook_functions(spec->hooks, fd);
+
/* Création des opérandes */
result &= define_syntax_items(spec->syntax, fd, arch, spec->bits, spec->conversions, pp);
diff --git a/tools/d2c/spec.h b/tools/d2c/spec.h
index 561fc32..39bfdb7 100644
--- a/tools/d2c/spec.h
+++ b/tools/d2c/spec.h
@@ -30,6 +30,7 @@
#include "bits.h"
#include "conv.h"
+#include "hooks.h"
#include "pproc.h"
#include "rules.h"
#include "syntax.h"
@@ -61,6 +62,9 @@ asm_syntax *get_syntax_in_encoding_spec(const encoding_spec *);
/* Fournit la liste des fonctions de conversion. */
conv_list *get_conversions_in_encoding_spec(const encoding_spec *);
+/* Fournit la liste des fonctions à lier à une instruction. */
+instr_hooks *get_hooks_in_encoding_spec(const encoding_spec *);
+
/* Fournit un ensemble de règles supplémentaires éventuel. */
decoding_rules *get_rules_in_encoding_spec(const encoding_spec *);