27 #ifndef FCML_ASSEMBLER_HPP_ 28 #define FCML_ASSEMBLER_HPP_ 72 set(buffer, len, errorContainer);
80 set(cpy._code, cpy._codeLength, cpy._warningContainer);
95 set(cpy._code, cpy._codeLength, cpy._warningContainer);
139 return _warningContainer;
152 void set(
const fcml_uint8_t *buffer, fcml_usize len,
154 _warningContainer = warnigns;
156 _code =
new fcml_uint8_t[len];
157 for (fcml_usize i = 0; i < len; i++) {
158 _code[i] = buffer[i];
175 fcml_usize _codeLength;
189 _chosenInstructionIndex(-1) {
201 if (_chosenInstructionIndex == -1) {
204 return &(_assembledInstructions[_chosenInstructionIndex]);
214 return _errorContainer;
224 return static_cast<fcml_usize
>(_assembledInstructions.size());
234 if (_chosenInstructionIndex == -1) {
237 return &(_assembledInstructions[_chosenInstructionIndex]);
248 if (index > _assembledInstructions.size()) {
252 return _assembledInstructions[index];
264 std::basic_ostream<fcml_uint8_t> &out,
284 _errorContainer.clean();
285 _assembledInstructions.clear();
286 _chosenInstructionIndex = -1;
294 _errorContainer = errorContainer;
297 std::vector<AssembledInstruction>& getAssembledInstructions() {
298 return _assembledInstructions;
301 void setChoosenInstructionIndex(fcml_int index) {
302 _chosenInstructionIndex = index;
310 std::vector<AssembledInstruction> _assembledInstructions;
312 fcml_int _chosenInstructionIndex;
328 _throwExceptionOnError(true),
330 _enableErrorMessages(true),
331 _chooseSibEncoding(false),
332 _chooseAbsEncoding(false),
333 _forceRexPrefix(false),
334 _forceThreeByteVEX(false),
335 _noBranchPrediction(false),
347 return _chooseAbsEncoding;
354 _chooseAbsEncoding = chooseAbsEncoding;
375 return _chooseSibEncoding;
382 _chooseSibEncoding = chooseSibEncoding;
389 return _enableErrorMessages;
396 _enableErrorMessages = enableErrorMessages;
403 return _forceRexPrefix;
410 _forceRexPrefix = forceRexPrefix;
417 return _forceThreeByteVEX;
424 _forceThreeByteVEX = forceThreeByteVex;
438 _incrementIp = incrementIp;
452 _optimizer = optimizer;
459 return _optimizerFlags;
466 _optimizerFlags = optimizerFlags;
477 return _throwExceptionOnError;
488 _throwExceptionOnError = throwExceptionOnError;
492 bool _throwExceptionOnError;
494 bool _enableErrorMessages;
495 bool _chooseSibEncoding;
496 bool _chooseAbsEncoding;
497 bool _forceRexPrefix;
498 bool _forceThreeByteVEX;
499 bool _noBranchPrediction;
501 fcml_uint16_t _optimizerFlags;
529 _entryPoint(operatingMode, ip) {
595 _entryPoint = entryPoint;
605 _entryPoint.setIP(ip);
616 _entryPoint.incrementIP(ip);
626 _entryPoint.setOpMode(operatingMode);
636 _entryPoint.setAddressSizeAttribute(addressSizeAttribute);
646 _entryPoint.setOperandSizeAttribute(operandSizeAttribute);
765 AssemblerTypeConverter::convert(ctx, context);
771 TypeConverter::convert(instruction, inst);
786 TypeConverter::free(inst);
790 ErrorTypeConverter::convert(res.
errors, errorContainer);
793 result.setErrorContainer(errorContainer);
798 errorContainer, error);
803 std::vector<AssembledInstruction> &assembledInstructions =
804 result.getAssembledInstructions();
806 assembledInstructions.clear();
813 while (next_instruction) {
816 ErrorTypeConverter::convert(instruction_warnings,
817 instructionWarnings);
819 next_instruction->
code,
821 instructionWarnings);
822 assembledInstructions.push_back(assembledInstruction);
824 result.setChoosenInstructionIndex(i);
826 next_instruction = next_instruction->
next;
840 }
catch (std::exception &exc) {
842 TypeConverter::free(inst);
880 CodeIterator(std::vector<AssembledInstruction> &assembledInstructions) :
881 _buffer(NULL), _len(0), _pos(0), _iterator(
882 assembledInstructions.begin()), _assembledInstructions(
883 assembledInstructions) {
899 if (_buffer && _pos >= _len) {
903 if (_iterator == _assembledInstructions.end()) {
922 if ((!_buffer || _pos >= _len) && !hasNext()) {
924 FCML_TEXT(
"No more elements in the iterator."));
926 return _buffer[_pos++];
932 const fcml_uint8_t *_buffer;
938 std::vector<AssembledInstruction>::iterator _iterator;
940 std::vector<AssembledInstruction> &_assembledInstructions;
946 #endif //FCML_ASSEMBLER_HPP_ bool isChooseAbsEncoding() const
Definition: fcml_assembler.hpp:346
AssemblerConf & getConfig()
Gets assembler configuration associated with the context.
Definition: fcml_assembler.hpp:550
AssembledInstruction(const AssembledInstruction &cpy)
Copy constructor.
Definition: fcml_assembler.hpp:79
fcml_usize getCodeLength() const
Gets number of bytes in the buffer.
Definition: fcml_assembler.hpp:128
fcml_ptr(LIB_CALL * fcml_fnp_asm_instruction_chooser)(fcml_st_chooser_context *chooser_context)
Instruction chooser function pointer declaration.
Definition: fcml_choosers.h:68
C++ wrappers common classes.
void setEnableErrorMessages(bool enableErrorMessages)
Definition: fcml_assembler.hpp:395
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_assembler.hpp:487
fcml_bool choose_abs_encoding
If memory address can be encoded as relative or absolute value choose the absolute addressing...
Definition: fcml_assembler.h:60
Assembler result.
Definition: fcml_assembler.hpp:182
Illegal state exception.
Definition: fcml_common.hpp:253
bool isForceThreeByteVex() const
Definition: fcml_assembler.hpp:416
const AssemblerConf & getConfig() const
Gets constant assembler configuration associated with the context.
Definition: fcml_assembler.hpp:540
Assembling failed.
Definition: fcml_assembler.hpp:45
bool isForceRexPrefix() const
Definition: fcml_assembler.hpp:402
fcml_st_assembler_conf configuration
Assembler behavior can be configured here.
Definition: fcml_assembler.h:116
friend std::basic_ostream< fcml_uint8_t > & operator<<(std::basic_ostream< fcml_uint8_t > &out, const AssemblerResult &result)
Copies machine code of the chosen instruction to the output stream.
Definition: fcml_assembler.hpp:263
std::basic_string< fcml_char > fcml_cstring
By using this type definition here, it will be definitely much easier to support UNICODE in future re...
Definition: fcml_common.hpp:53
virtual ~Assembler()
Definition: fcml_assembler.hpp:740
void setChooseSibEncoding(bool chooseSibEncoding)
Definition: fcml_assembler.hpp:381
AssembledInstruction & operator=(const AssembledInstruction &cpy)
Copies one instruction into another.
Definition: fcml_assembler.hpp:90
void setIP(fcml_ip ip)
Sets instruction pointer directly into the entry point.
Definition: fcml_assembler.hpp:604
void setAddressSizeAttribute(fcml_usize addressSizeAttribute)
Sets a new address size attribute for the entry point.
Definition: fcml_assembler.hpp:635
Converts objects to their structures counterparts.
Definition: fcml_assembler.hpp:662
fcml_uint16_t getOptimizerFlags() const
Definition: fcml_assembler.hpp:458
Assembler result.
Definition: fcml_assembler.h:100
Assembler context.
Definition: fcml_assembler.hpp:510
fcml_st_assembled_instruction * instructions
Chain of assembled instructions.
Definition: fcml_assembler.h:104
EntryPoint & getEntryPoint()
Gets reference to the entry point instance associated with the context.
Definition: fcml_assembler.hpp:583
Assembler runtime configuration.
Definition: fcml_assembler.h:48
bool isIncrementIp() const
Definition: fcml_assembler.hpp:430
Container for all collected errors and warnings.
Definition: fcml_errors.h:180
const AssembledInstruction & operator[](fcml_usize index) const
Gets an assembled instruction reference by its index.
Definition: fcml_assembler.hpp:247
void setOptimizerFlags(fcml_uint16_t optimizerFlags)
Definition: fcml_assembler.hpp:465
AssembledInstruction(const fcml_uint8_t *buffer, fcml_usize len, const ErrorContainer &errorContainer)
Creates an assembled instruction basing on given code buffer and errors.
Definition: fcml_assembler.hpp:70
#define FCML_TEXT(x)
Used to code literal strings.
Definition: fcml_types.h:61
fcml_st_assembler * assembler
Assembler instance that should be used to assemble instructions.
Definition: fcml_assembler.h:114
struct fcml_st_assembled_instruction * next
Next assembled instruction in the chain.
Definition: fcml_assembler.h:87
const fcml_uint8_t * getCode() const
Gets pointer to machine code buffer.
Definition: fcml_assembler.hpp:118
Holds instruction pointer, processor operating mode and memory segment flags.
Definition: fcml_common.hpp:524
Base class for all exceptions that are aware of ErrorContainer.
Definition: fcml_errors.hpp:347
Generic instruction model.
Definition: fcml_common.h:783
const AssembledInstruction * getChosenInstruction() const
Gets instruction chosen by the assembler as the preferred one.
Definition: fcml_assembler.hpp:200
LIB_EXPORT void LIB_CALL fcml_fn_assembler_result_prepare(fcml_st_assembler_result *result)
Prepares reusable result holder for assembler.
fcml_uint16_t optimizer_flags
This field is passed to the chosen optimizer.
Definition: fcml_assembler.h:73
fcml_st_ceh_error_container warnings
Warning messages related to assembled instruction.
Definition: fcml_assembler.h:89
Definition: fcml_assembler.hpp:39
void setChooseAbsEncoding(bool chooseAbsEncoding)
Definition: fcml_assembler.hpp:353
fcml_ceh_error assemble(AssemblerContext &ctx, const Instruction &instruction, AssemblerResult &result)
Assembles given generic instruction model.
Definition: fcml_assembler.hpp:760
fcml_fnp_asm_optimizer getOptimizer() const
Definition: fcml_assembler.hpp:444
fcml_st_entry_point entry_point
Instruction entry point configuration.
Definition: fcml_assembler.h:118
void setForceThreeByteVex(bool forceThreeByteVex)
Definition: fcml_assembler.hpp:423
Inherit from this class in order to get access to the native FCML dialect structure.
Definition: fcml_dialect.hpp:98
fcml_st_assembled_instruction * chosen_instruction
Instruction chosen by used instruction chooser; otherwise NULL.
Definition: fcml_assembler.h:106
Used mainly in case of integers and offsets.
Definition: fcml_errors.h:55
void setChooser(fcml_fnp_asm_instruction_chooser chooser)
Definition: fcml_assembler.hpp:367
const ErrorContainer & getWarningContainer() const
Gets reference to the errors container.
Definition: fcml_assembler.hpp:138
void setIncrementIp(bool incrementIp)
Definition: fcml_assembler.hpp:437
LIB_EXPORT void LIB_CALL fcml_fn_assembler_result_free(fcml_st_assembler_result *result)
Cleans result holder.
void clear()
Clears assembler result by removing all assembled instructions, errors and reseting the chosen instru...
Definition: fcml_assembler.hpp:283
An assembler wrapper.
Definition: fcml_assembler.hpp:717
fcml_bool enable_error_messages
True if optional error and warning messages should be collected during processing.
Definition: fcml_assembler.h:54
bool isChooseSibEncoding() const
Definition: fcml_assembler.hpp:374
AssemblerResult()
Definition: fcml_assembler.hpp:188
const ErrorContainer & getErrorContainer() const
Gets errors container.
Definition: fcml_assembler.hpp:213
fcml_fnp_asm_instruction_chooser getChooser() const
Definition: fcml_assembler.hpp:360
fcml_int64_t fcml_ip
General instruction pointer holder.
Definition: fcml_common.h:96
Describes an assembled instruction.
Definition: fcml_assembler.hpp:57
void setConfig(const AssemblerConf &config)
Copies given configuration to the instance associated with the context.
Definition: fcml_assembler.hpp:562
C++ wrapper for the base dialect.
bool hasNext()
Gets true if there is an another element in the iterator.
Definition: fcml_assembler.hpp:898
void setErrorContainer(const ErrorContainer &errorContainer)
Sets a new error container for the exception.
Definition: fcml_errors.hpp:375
Assembler runtime context.
Definition: fcml_assembler.h:112
OperatingMode
Supported operating modes.
Definition: fcml_common.hpp:531
void setOperatingMode(EntryPoint::OperatingMode operatingMode)
Sets processor operating mode directly into the entry point.
Definition: fcml_assembler.hpp:625
void setForceRexPrefix(bool forceRexPrefix)
Definition: fcml_assembler.hpp:409
fcml_uint8_t next()
Gets the next element from the iterator.
Definition: fcml_assembler.hpp:921
fcml_fnp_asm_instruction_chooser chooser
instruction chooser implementation that should be used by assembler to choose most appropriate instru...
Definition: fcml_assembler.h:77
A base iterator interface.
Definition: fcml_common.hpp:98
const EntryPoint & getEntryPoint() const
Gets reference to the constant entry point instance associated with the context.
Definition: fcml_assembler.hpp:573
fcml_fnp_asm_optimizer optimizer
Optimizer implementation that should be used by assembler.
Definition: fcml_assembler.h:70
void setOptimizer(fcml_fnp_asm_optimizer optimizer)
Definition: fcml_assembler.hpp:451
void incrementIP(fcml_ip ip)
Increments entry point by given number of bytes.
Definition: fcml_assembler.hpp:615
Wraps multiple errors into one component.
Definition: fcml_errors.hpp:148
fcml_bool force_rex_prefix
Sometimes REX prefix is useless so it is just omitted in the final machine code.
Definition: fcml_assembler.h:64
bool isEnableErrorMessages() const
Definition: fcml_assembler.hpp:388
fcml_bool force_three_byte_VEX
Every 2 byte VEX/XOP prefix can be encoded using three byte form.
Definition: fcml_assembler.h:67
bool isThrowExceptionOnError() const
Returns true if exception should be thrown when assembling fails.
Definition: fcml_assembler.hpp:476
fcml_uint8_t * code
Instruction machine code.
Definition: fcml_assembler.h:91
Describes an instruction.
Definition: fcml_common.hpp:7185
virtual ~CodeIterator()
Definition: fcml_assembler.hpp:889
Operation succeed.
Definition: fcml_errors.h:42
AssemblerContext()
Definition: fcml_assembler.hpp:517
Assembler configuration.
Definition: fcml_assembler.hpp:321
void setOperandSizeAttribute(fcml_usize operandSizeAttribute)
Sets a new operand size attribute for the entry point.
Definition: fcml_assembler.hpp:645
Bad arguments.
Definition: fcml_common.hpp:242
LIB_EXPORT void LIB_CALL fcml_fn_assembler_free(fcml_st_assembler *assembler)
Frees assembler instance.
Encoded instruction.
Definition: fcml_assembler.h:85
An abstract dialect.
Definition: fcml_dialect.hpp:41
virtual ~AssembledInstruction()
Definition: fcml_assembler.hpp:103
void setEntryPoint(const EntryPoint &entryPoint)
Copies given entry point to the instance associated with the context.
Definition: fcml_assembler.hpp:594
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_assembler_init(const fcml_st_dialect *dialect, fcml_st_assembler **assembler)
Initializes assembler for given dialect.
fcml_bool choose_sib_encoding
If there are SIB and "ModR/M only" encodings available, choose the SIB based one. ...
Definition: fcml_assembler.h:57
AssemblerContext(EntryPoint::OperatingMode operatingMode, fcml_ip ip=0)
Creates an entry point instance for given operating mode and optional instruction pointer...
Definition: fcml_assembler.hpp:528
Dialect & getDialect() const
Gets dialect associated with the assembler.
Definition: fcml_assembler.hpp:855
fcml_usize number_of_instructions
Number of encoded instruction forms.
Definition: fcml_assembler.h:108
Structures and functions declarations related to one-line FCML assembler.
struct fcml_st_assembler fcml_st_assembler
Abstract assembler representation.
Definition: fcml_assembler.h:43
ErrorContainerAwareException(const fcml_cstring &msg, const ErrorContainer &errorContainer, fcml_ceh_error error=FCML_CEH_GEC_NO_ERROR)
Creates an error container aware exception instance and sets basic information for it...
Definition: fcml_errors.hpp:357
Component can not be initialized correctly.
Definition: fcml_common.hpp:231
fcml_st_ceh_error_container errors
Error and warning messages from assembler.
Definition: fcml_assembler.h:102
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:156
fcml_usize code_length
Instruction code length in bytes.
Definition: fcml_assembler.h:93
LIB_EXPORT fcml_ceh_error LIB_CALL fcml_fn_assemble(fcml_st_assembler_context *context, const fcml_st_instruction *instruction, fcml_st_assembler_result *result)
Assembles one instruction encoded in the generic instruction model.
Iterates over machine code bytes from assembled instructions.
Definition: fcml_assembler.hpp:872
Object which shouldn't be copied can inherit from this class.
Definition: fcml_common.hpp:288
Assembler(Dialect &dialect)
Creates an assembler instance for given dialect.
Definition: fcml_assembler.hpp:727
C++ wrapper for the FCML errors handling.
CodeIterator(std::vector< AssembledInstruction > &assembledInstructions)
Creates a code iterator instance.
Definition: fcml_assembler.hpp:880
fcml_bool increment_ip
Set to true in order to force assembler to increment IP address by length of the assembled instructio...
Definition: fcml_assembler.h:51
fcml_usize getSize() const
Gets number of instructions alternatives available in the result.
Definition: fcml_assembler.hpp:223
fcml_ceh_error(LIB_CALL * fcml_fnp_asm_optimizer)(fcml_st_asm_optimizer_context *context, fcml_st_asm_optimizer_processing_details *ds_flags, fcml_fnp_asm_optimizer_callback callback, fcml_ptr args)
Function pointer declaration for optimizers.
Definition: fcml_optimizers.h:102
AssemblerConf()
Definition: fcml_assembler.hpp:327