summaryrefslogtreecommitdiff
path: root/tools/d2c/pattern
diff options
context:
space:
mode:
authorCyrille Bagard <nocbos@gmail.com>2018-04-02 11:58:42 (GMT)
committerCyrille Bagard <nocbos@gmail.com>2018-04-02 12:39:30 (GMT)
commit1db4ef323b7a76093356ae76268132f3760e1631 (patch)
treefec36ee0ec1b6b2010b62ca4177edca0e31e2114 /tools/d2c/pattern
parent1bc80837dde03a32b5ab185067f7bd4c499a9850 (diff)
Rewritten the whole instruction definition format.
Diffstat (limited to 'tools/d2c/pattern')
-rw-r--r--tools/d2c/pattern/Makefile.am37
-rw-r--r--tools/d2c/pattern/decl.h37
-rw-r--r--tools/d2c/pattern/grammar.y107
-rw-r--r--tools/d2c/pattern/manager.c401
-rw-r--r--tools/d2c/pattern/manager.h63
-rw-r--r--tools/d2c/pattern/tokens.l34
6 files changed, 679 insertions, 0 deletions
diff --git a/tools/d2c/pattern/Makefile.am b/tools/d2c/pattern/Makefile.am
new file mode 100644
index 0000000..458eed0
--- /dev/null
+++ b/tools/d2c/pattern/Makefile.am
@@ -0,0 +1,37 @@
+
+BUILT_SOURCES = grammar.h
+
+
+# On évite d'utiliser les variables personnalisées de type *_la_[YL]FLAGS
+# afin de conserver des noms de fichiers simples, ie sans le nom de la
+# bibliothèque de sortie en préfixe.
+
+AM_YFLAGS = -v -d -p pattern_
+
+AM_LFLAGS = -P pattern_ -o lex.yy.c --header-file=tokens.h \
+ -Dyyget_lineno=pattern_get_lineno \
+ -Dyy_scan_string=pattern__scan_string \
+ -Dyy_delete_buffer=pattern__delete_buffer
+
+AM_CFLAGS = $(DEBUG_CFLAGS) $(WARNING_FLAGS)
+
+
+noinst_LTLIBRARIES = libd2cpattern.la
+
+.NOTPARALLEL: $(noinst_LTLIBRARIES)
+
+libd2cpattern_la_SOURCES = \
+ decl.h \
+ manager.h manager.c \
+ tokens.l \
+ grammar.y
+
+# _GNU_SOURCE : asprintf
+libd2cpattern_la_CFLAGS = -D_GNU_SOURCE
+
+
+# Automake fait les choses à moitié
+CLEANFILES = grammar.h grammar.c grammar.output tokens.c tokens.h
+
+# Pareil : de tous les fichiers générés, seule la sortie de Flex saute pour les distributions !
+EXTRA_DIST = tokens.h
diff --git a/tools/d2c/pattern/decl.h b/tools/d2c/pattern/decl.h
new file mode 100644
index 0000000..c834ced
--- /dev/null
+++ b/tools/d2c/pattern/decl.h
@@ -0,0 +1,37 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * decl.h - déclarations de prototypes utiles
+ *
+ * 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/>.
+ */
+
+
+#ifndef _TOOLS_D2C_PATTERN_DECL_H
+#define _TOOLS_D2C_PATTERN_DECL_H
+
+
+#include "manager.h"
+
+
+
+/* Interprête des données liées à une définition de syntaxe. */
+bool load_asm_pattern_from_raw_line(asm_pattern *, const char *);
+
+
+
+#endif /* _TOOLS_D2C_PATTERN_DECL_H */
diff --git a/tools/d2c/pattern/grammar.y b/tools/d2c/pattern/grammar.y
new file mode 100644
index 0000000..c3399cc
--- /dev/null
+++ b/tools/d2c/pattern/grammar.y
@@ -0,0 +1,107 @@
+
+%{
+
+#include "tokens.h"
+
+
+/* Affiche un message d'erreur suite à l'analyse en échec. */
+static int yyerror(asm_pattern *, char *);
+
+%}
+
+
+%code requires {
+
+#include "decl.h"
+
+}
+
+
+%union {
+
+ char *string; /* Chaîne de caractères */
+
+}
+
+
+%define api.pure full
+
+%parse-param { asm_pattern *pattern }
+
+%code provides {
+
+#define YY_DECL \
+ int pattern_lex(YYSTYPE *yylvalp)
+
+YY_DECL;
+
+}
+
+
+%token OPERAND
+
+%type <string> OPERAND
+
+
+%%
+
+
+operands : /* empty */
+ | operands OPERAND { register_asm_pattern_item(pattern, $2); }
+
+
+%%
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = structure impliquée dans le processus. *
+* msg = message d'erreur. *
+* *
+* Description : Affiche un message d'erreur suite à l'analyse en échec. *
+* *
+* Retour : 0 *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+static int yyerror(asm_pattern *pattern, char *msg)
+{
+ printf("syntax yyerror line %d: %s\n", yyget_lineno(), msg);
+
+ return 0;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = structure à constituer à partir de données lues. *
+* raw = données brutes à analyser. *
+* *
+* Description : Interprête des données liées à une définition de syntaxe. *
+* *
+* Retour : true si l'opération s'est bien déroulée, false sinon. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool load_asm_pattern_from_raw_line(asm_pattern *pattern, const char *raw)
+{
+ bool result; /* Bilan à faire remonter */
+ YY_BUFFER_STATE state; /* Support d'analyse */
+ int status; /* Bilan de l'analyse */
+
+ state = yy_scan_string(raw);
+
+ status = yyparse(pattern);
+
+ result = (status == 0);
+
+ yy_delete_buffer(state);
+
+ return result;
+
+}
diff --git a/tools/d2c/pattern/manager.c b/tools/d2c/pattern/manager.c
new file mode 100644
index 0000000..a756745
--- /dev/null
+++ b/tools/d2c/pattern/manager.c
@@ -0,0 +1,401 @@
+
+/* 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 <assert.h>
+#include <malloc.h>
+#include <string.h>
+
+
+#include "../helpers.h"
+
+
+
+/* Propriétés particulières pour les opérandes */
+typedef enum _SyntaxItemFlags
+{
+ SIF_NONE = (0 << 0), /* Aucune propriété */
+ SIF_DECIMAL = (1 << 0), /* Affichage en décimal */
+ SIF_OPTIONAL = (1 << 1) /* Absence tolérée */
+
+} SyntaxItemFlags;
+
+/* Elément défini dans une syntaxe */
+typedef struct _syntax_item
+{
+ char *name; /* Désignation humaine */
+ SyntaxItemFlags flags; /* Propriétés supplémentaires */
+
+} syntax_item;
+
+/* Syntaxe d'une ligne d'assembleur */
+struct _asm_pattern
+{
+ syntax_item *items; /* Eléments de la syntaxe */
+ size_t items_count; /* Nombre de ces éléments */
+
+};
+
+
+
+/******************************************************************************
+* *
+* Paramètres : - *
+* *
+* Description : Crée un nouvel indicateur pour l'écriture d'une instruction. *
+* *
+* Retour : Nouvelle structure prête à emploi. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+asm_pattern *create_asm_pattern(void)
+{
+ asm_pattern *result; /* Définition vierge à renvoyer*/
+
+ result = (asm_pattern *)calloc(1, sizeof(asm_pattern));
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = gestionnaire d'un ensemble d'éléments de syntaxe. *
+* *
+* Description : Supprime de la mémoire un indicateur d'écriture ASM. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void delete_asm_pattern(asm_pattern *pattern)
+{
+ size_t i; /* Boucle de parcours */
+
+ for (i = 0; i < pattern->items_count; i++)
+ free(pattern->items[i].name);
+
+ if (pattern->items != NULL)
+ free(pattern->items);
+
+ free(pattern);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = gestionnaire d'un ensemble d'éléments de syntaxe. *
+* name = désignation de l'opérande dans la spécification. *
+* *
+* Description : Enregistre la présence d'un nouvel opérande dans la syntaxe. *
+* *
+* Retour : - *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+void register_asm_pattern_item(asm_pattern *pattern, char *name)
+{
+ syntax_item *item; /* Nouvelle prise en compte */
+ size_t len; /* Taille du nom fourni */
+
+ pattern->items = (syntax_item *)realloc(pattern->items, ++pattern->items_count * sizeof(syntax_item));
+
+ item = &pattern->items[pattern->items_count - 1];
+
+ /* Récupération des drapeaux */
+
+ item->flags = SIF_NONE;
+
+ for (len = strlen(name); len > 0; len--)
+ switch (name[0])
+ {
+ case '#':
+ item->flags |= SIF_DECIMAL;
+ memmove(name, name + 1, len);
+ break;
+
+ case '?':
+ item->flags |= SIF_OPTIONAL;
+ memmove(name, name + 1, len);
+ break;
+
+ default:
+ len = 1;
+ break;
+
+ }
+
+ item->name = make_string_lower(name);
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = gestionnaire d'un ensemble d'éléments de syntaxe. *
+* bits = gestionnaire des bits d'encodage. *
+* list = liste de l'ensemble des fonctions de conversion. *
+* *
+* Description : Marque les champs de bits effectivement utilisés. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool mark_asm_pattern_items(const asm_pattern *pattern, const coding_bits *bits, const conv_list *list)
+{
+ bool result; /* Bilan à retourner */
+ size_t i; /* Boucle de parcours */
+ syntax_item *item; /* Lien vers un opérande */
+ conv_func *func; /* Fonction de conversion */
+
+ result = true;
+
+ for (i = 1; i < pattern->items_count && result; i++)
+ {
+ item = &pattern->items[i];
+
+ func = find_named_conv_in_list(list, item->name);
+ if (func == NULL)
+ {
+ fprintf(stderr, "Error: expected conversion for '%s'.\n", item->name);
+ result = false;
+ }
+
+ result = mark_conv_func(func, false, bits, list);
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = gestionnaire d'un ensemble d'éléments de syntaxe. *
+* fd = descripteur d'un flux ouvert en écriture. *
+* bits = gestionnaire des bits d'encodage. *
+* list = liste de l'ensemble des fonctions de conversion. *
+* tab = décalage éventuel selon l'inclusion. *
+* imm_decl = une déclaration d'immédiat est déjà faite ? [OUT] *
+* *
+* Description : Déclare les variables C associées aux opérandes de syntaxe. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool declare_asm_pattern(const asm_pattern *pattern, int fd, const coding_bits *bits, const conv_list *list, const char *tab, bool *imm_decl)
+{
+ bool result; /* Bilan à retourner */
+ size_t i; /* Boucle de parcours */
+ syntax_item *item; /* Lien vers un opérande */
+ conv_func *func; /* Fonction de conversion */
+
+ result = true;
+
+ for (i = 1; i < pattern->items_count && result; i++)
+ {
+ item = &pattern->items[i];
+
+ func = find_named_conv_in_list(list, item->name);
+ assert(func != NULL);
+
+ result = declare_conv_func(func, fd, bits, list, tab);
+
+ if (result && item->flags & SIF_DECIMAL && !*imm_decl)
+ {
+ dprintf(fd, "\t%sGImmOperand *imm;\n", tab);
+ *imm_decl = true;
+ }
+
+ }
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = gestionnaire d'un ensemble d'éléments de syntaxe. *
+* *
+* Description : Fournit si elle existe un nom nouveau pour une instruction. *
+* *
+* Retour : Eventuelle chaîne de caractères trouvée ou NULL. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+const char *get_keyword_from_asm_pattern(const asm_pattern *pattern)
+{
+ const char *result; /* Nom éventuel à renvoyer */
+
+ assert(pattern->items_count > 0);
+
+ result = pattern->items[0].name;
+
+ return result;
+
+}
+
+
+/******************************************************************************
+* *
+* Paramètres : pattern = gestionnaire d'un ensemble d'éléments de syntaxe. *
+* fd = descripteur d'un flux ouvert en écriture. *
+* arch = architecture visée par l'opération globale. *
+* bits = gestionnaire des bits d'encodage. *
+* list = liste de l'ensemble des fonctions de conversion. *
+* tab = décalage éventuel selon l'inclusion. *
+* exit = exprime le besoin d'une voie de sortie. [OUT] *
+* *
+* Description : Définit les variables C associées aux opérandes de syntaxe. *
+* *
+* Retour : Bilan de l'opération. *
+* *
+* Remarques : - *
+* *
+******************************************************************************/
+
+bool define_asm_pattern(const asm_pattern *pattern, int fd, const char *arch, const coding_bits *bits, const conv_list *list, const char *tab, bool *exit)
+{
+ bool result; /* Bilan à retourner */
+ size_t i; /* Boucle de parcours */
+ syntax_item *item; /* Lien vers un opérande */
+ conv_func *func; /* Fonction de conversion */
+ bool optional; /* Opérande optionnelle ? */
+
+ result = true;
+
+ for (i = 1; i < pattern->items_count; i++)
+ {
+ item = &pattern->items[i];
+
+ func = find_named_conv_in_list(list, item->name);
+ assert(func != NULL);
+
+ optional = item->flags & SIF_OPTIONAL;
+
+ result = define_conv_func(func, fd, bits, list, tab, optional, exit);
+ if (!result) break;
+
+ if (optional)
+ {
+ dprintf(fd, "\t%sif (", tab);
+
+ write_conv_func(func, fd, false);
+
+ dprintf(fd, " != NULL)");
+
+ dprintf(fd, "\n");
+
+ if (item->flags & SIF_DECIMAL)
+ {
+ dprintf(fd, "\t%s{\n", tab);
+
+ dprintf(fd, "\t%s\timm = G_IMM_OPERAND(", tab);
+
+ write_conv_func(func, fd, false);
+
+ dprintf(fd, ");\n");
+
+ dprintf(fd, "\t%s\tg_imm_operand_set_default_display(imm, IOD_DEC);\n", tab);
+
+ dprintf(fd, "\n");
+
+ dprintf(fd, "\t%s\tg_arch_instruction_attach_extra_operand(result, ", tab);
+
+ write_conv_func(func, fd, false);
+
+ dprintf(fd, ");\n");
+
+ dprintf(fd, "\n");
+
+ dprintf(fd, "\t%s}\n", tab);
+
+ dprintf(fd, "\n");
+
+ }
+
+ else
+ {
+ dprintf(fd, "\t%s\tg_arch_instruction_attach_extra_operand(result, ", tab);
+
+ write_conv_func(func, fd, false);
+
+ dprintf(fd, ");\n");
+
+ dprintf(fd, "\n");
+
+ }
+
+ }
+
+ else
+ {
+ if (item->flags & SIF_DECIMAL)
+ {
+ dprintf(fd, "\t%simm = G_IMM_OPERAND(", tab);
+
+ write_conv_func(func, fd, false);
+
+ dprintf(fd, ");\n");
+
+ dprintf(fd, "\t%sg_imm_operand_set_default_display(imm, IOD_DEC);\n", tab);
+
+ dprintf(fd, "\n");
+
+ }
+
+ dprintf(fd, "\t%sg_arch_instruction_attach_extra_operand(result, ", tab);
+
+ write_conv_func(func, fd, false);
+
+ dprintf(fd, ");\n");
+
+ dprintf(fd, "\n");
+
+ }
+
+ }
+
+ return result;
+
+}
diff --git a/tools/d2c/pattern/manager.h b/tools/d2c/pattern/manager.h
new file mode 100644
index 0000000..592b169
--- /dev/null
+++ b/tools/d2c/pattern/manager.h
@@ -0,0 +1,63 @@
+
+/* Chrysalide - Outil d'analyse de fichiers binaires
+ * manager.h - prototypes pour la 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/>.
+ */
+
+
+#ifndef _TOOLS_D2C_SYNTAX_MANAGER_H
+#define _TOOLS_D2C_SYNTAX_MANAGER_H
+
+
+#include <stdbool.h>
+
+
+#include "../bits/manager.h"
+#include "../conv/manager.h"
+
+
+
+/* Syntaxe d'une ligne d'assembleur */
+typedef struct _asm_pattern asm_pattern;
+
+
+/* Crée un nouvel indicateur pour l'écriture d'une instruction. */
+asm_pattern *create_asm_pattern(void);
+
+/* Supprime de la mémoire un indicateur d'écriture ASM. */
+void delete_asm_pattern(asm_pattern *);
+
+/* Enregistre la présence d'un nouvel opérande dans la syntaxe. */
+void register_asm_pattern_item(asm_pattern *, char *);
+
+/* Marque les champs de bits effectivement utilisés. */
+bool mark_asm_pattern_items(const asm_pattern *, const coding_bits *, const conv_list *);
+
+/* Déclare les variables C associées aux opérandes de syntaxe. */
+bool declare_asm_pattern(const asm_pattern *, int, const coding_bits *, const conv_list *, const char *, bool *);
+
+/* Fournit si elle existe un nom nouveau pour une instruction. */
+const char *get_keyword_from_asm_pattern(const asm_pattern *);
+
+/* Définit les variables C associées aux opérandes de syntaxe. */
+bool define_asm_pattern(const asm_pattern *, int, const char *, const coding_bits *, const conv_list *, const char *, bool *);
+
+
+
+#endif /* _TOOLS_D2C_SYNTAX_MANAGER_H */
diff --git a/tools/d2c/pattern/tokens.l b/tools/d2c/pattern/tokens.l
new file mode 100644
index 0000000..bef1e44
--- /dev/null
+++ b/tools/d2c/pattern/tokens.l
@@ -0,0 +1,34 @@
+
+%top {
+
+#include "grammar.h"
+
+}
+
+
+%option noyywrap
+%option nounput
+%option noinput
+%option yylineno
+%option stack
+%option noyy_top_state
+%option noyy_push_state
+%option noyy_pop_state
+
+
+%%
+
+
+[ \t] { }
+
+[^ ]+ { yylvalp->string = strdup(yytext); return OPERAND; }
+
+. {
+ char *msg;
+ asprintf(&msg, "Unhandled token in d2c pattern block: '%s'", yytext);
+ YY_FATAL_ERROR(msg);
+ free(msg);
+ }
+
+
+%%