From 68166adcbdf53ba04b7f727b1d85e3b516e15263 Mon Sep 17 00:00:00 2001 From: Andrei KISARI Date: Wed, 28 Aug 2024 10:18:10 +0300 Subject: [PATCH] Add support for builds without mnemonics. --- CMakeLists.txt | 6 ++++++ bddisasm/bdx86_decoder.c | 2 ++ bddisasm/bdx86_formatter.c | 5 ++--- bddisasm/include/bdx86_mnemonics.h | 4 ++++ inc/bdx86_core.h | 2 ++ isagenerator/generate_tables.py | 10 +++++++++- 6 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f97bb1..9cc5dcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ option(BDD_INCLUDE_ISAGENERATOR_X86 "Include the x86 isagenerator target (if a p option(BDD_INCLUDE_FUZZERS "Include the bdshemu fuzzer" OFF) option(BDD_USE_EXTERNAL_VSNPRINTF "Expect nd_vsnprintf_s implementation from the integrator" OFF) option(BDD_USE_EXTERNAL_MEMSET "Expect nd_memset implementation from the integrator" OFF) +option(BDD_NO_MNEMONIC "Exclude mnemonics - this option involves setting the BDDISASM_NO_FORMAT flag" OFF) option(BDD_ASAN "Build with ASAN" OFF) option(BDD_UBSAN "Build with UBSAN" OFF) @@ -143,6 +144,11 @@ if (NOT BDD_USE_EXTERNAL_MEMSET) endif () endif () +if (BDD_NO_MNEMONIC) + target_compile_definitions(bddisasm PUBLIC -DBDDISASM_NO_MNEMONIC) + target_compile_definitions(bddisasm PUBLIC -DBDDISASM_NO_FORMAT) +endif() + set_target_properties( bddisasm PROPERTIES POSITION_INDEPENDENT_CODE ON diff --git a/bddisasm/bdx86_decoder.c b/bddisasm/bdx86_decoder.c index 3e04516..3fd7e17 100644 --- a/bddisasm/bdx86_decoder.c +++ b/bddisasm/bdx86_decoder.c @@ -4308,7 +4308,9 @@ NdCopyInstructionInfo( ND_IDBE *Idbe ) { +#ifndef BDDISASM_NO_MNEMONIC Instrux->Mnemonic = gMnemonics[Idbe->Mnemonic]; +#endif // !BDDISASM_NO_MNEMONIC Instrux->Attributes = Idbe->Attributes; Instrux->Instruction = (ND_INS_CLASS)Idbe->Instruction; Instrux->Category = (ND_INS_CATEGORY)Idbe->Category; diff --git a/bddisasm/bdx86_formatter.c b/bddisasm/bdx86_formatter.c index a81a32c..c358260 100644 --- a/bddisasm/bdx86_formatter.c +++ b/bddisasm/bdx86_formatter.c @@ -6,8 +6,7 @@ #include "../inc/bddisasm.h" - -#ifndef BDDISASM_NO_FORMAT +#if !defined(BDDISASM_NO_MNEMONIC) && !defined(BDDISASM_NO_FORMAT) static const char *const gReg8Bit[] = { @@ -1033,4 +1032,4 @@ NdToText( return ND_STATUS_SUCCESS; } -#endif // !BDDISASM_NO_FORMAT +#endif // !defined(BDDISASM_NO_MNEMONIC) && !defined(BDDISASM_NO_FORMAT) diff --git a/bddisasm/include/bdx86_mnemonics.h b/bddisasm/include/bdx86_mnemonics.h index 64e1fd9..01eea3a 100644 --- a/bddisasm/include/bdx86_mnemonics.h +++ b/bddisasm/include/bdx86_mnemonics.h @@ -10,6 +10,8 @@ #ifndef BDX86_MNEMONICS_H #define BDX86_MNEMONICS_H +#ifndef BDDISASM_NO_MNEMONIC + const char *gMnemonics[1786] = { "AAA", "AAD", "AADD", "AAM", "AAND", "AAS", "ADC", "ADCX", "ADD", @@ -320,6 +322,8 @@ const char *gMnemonics[1786] = "XSAVES", "XSAVES64", "XSETBV", "XSUSLDTRK", "XTEST", }; +#endif // !BDDISASM_NO_MNEMONIC + #endif diff --git a/inc/bdx86_core.h b/inc/bdx86_core.h index 75271e4..5c6173d 100644 --- a/inc/bdx86_core.h +++ b/inc/bdx86_core.h @@ -1439,7 +1439,9 @@ typedef struct _INSTRUX // Aliased over low 4 bits inside the main opcode. }; +#ifndef BDDISASM_NO_MNEMONIC const char *Mnemonic; // Instruction mnemonic. +#endif // !BDDISASM_NO_MNEMONIC ND_UINT8 InstructionBytes[16]; // The entire instruction. ND_UINT8 OpCodeBytes[3]; // Opcode bytes - escape codes and main opcode. diff --git a/isagenerator/generate_tables.py b/isagenerator/generate_tables.py index 3cb5366..2d27ec6 100644 --- a/isagenerator/generate_tables.py +++ b/isagenerator/generate_tables.py @@ -801,6 +801,8 @@ def dump_mnemonics(mnemonics, prefixes, fname): f.write('#ifndef BDX86_MNEMONICS_H\n') f.write('#define BDX86_MNEMONICS_H\n') f.write('\n') + f.write('#ifndef BDDISASM_NO_MNEMONIC\n') + f.write('\n') f.write('const char *gMnemonics[%d] = \n' % len(mnemonics)) f.write('{\n') f.write(' ') @@ -815,7 +817,13 @@ def dump_mnemonics(mnemonics, prefixes, fname): ln = 0 f.write('\n ') - f.write('\n};\n\n\n') + + f.write('\n};\n') + + f.write('\n') + f.write('#endif // !BDDISASM_NO_MNEMONIC\n') + + f.write('\n\n') f.write('#endif\n\n')