27 #ifndef FCML_ASSEMBLER_HPP_ 28 #define FCML_ASSEMBLER_HPP_ 68 set( buffer, len, errorContainer );
76 set( cpy._code, cpy._codeLength, cpy._warningContainer );
89 delete [] this->_code;
91 set( cpy._code, cpy._codeLength, cpy._warningContainer );
135 return _warningContainer;
148 void set(
const fcml_uint8_t *buffer, fcml_usize len,
const ErrorContainer warnigns ) {
149 _warningContainer = warnigns;
151 _code =
new fcml_uint8_t[len];
152 for( fcml_usize i = 0; i < len; i++ ) {
153 _code[i] = buffer[i];
170 fcml_usize _codeLength;
184 _chosenInstructionIndex(-1) {
196 if( _chosenInstructionIndex == -1 ) {
199 return &(_assembledInstructions[ _chosenInstructionIndex ]);
209 return _errorContainer;
219 return static_cast<fcml_usize
>(_assembledInstructions.size());
229 if( _chosenInstructionIndex == -1 ) {
232 return &(_assembledInstructions[_chosenInstructionIndex]);
243 if( index > _assembledInstructions.size() ) {
246 return _assembledInstructions[index];
257 friend std::basic_ostream<fcml_uint8_t>& operator<< ( std::basic_ostream<fcml_uint8_t> &out,
const AssemblerResult &result ) {
275 _errorContainer.clean();
276 _assembledInstructions.clear();
277 _chosenInstructionIndex = -1;
285 _errorContainer = errorContainer;
288 std::vector<AssembledInstruction>& getAssembledInstructions() {
289 return _assembledInstructions;
292 void setChoosenInstructionIndex( fcml_int index ) {
293 _chosenInstructionIndex = index;
301 std::vector<AssembledInstruction> _assembledInstructions;
303 fcml_int _chosenInstructionIndex;
318 _throwExceptionOnError(true),
320 _enableErrorMessages(true),
321 _chooseSibEncoding(false),
322 _chooseAbsEncoding(false),
323 _forceRexPrefix(false),
324 _forceThreeByteVEX(false),
325 _noBranchPrediction(false),
337 return _chooseAbsEncoding;
344 _chooseAbsEncoding = chooseAbsEncoding;
365 return _chooseSibEncoding;
372 _chooseSibEncoding = chooseSibEncoding;
379 return _enableErrorMessages;
386 _enableErrorMessages = enableErrorMessages;
393 return _forceRexPrefix;
400 _forceRexPrefix = forceRexPrefix;
407 return _forceThreeByteVEX;
414 _forceThreeByteVEX = forceThreeByteVex;
428 _incrementIp = incrementIp;
442 _optimizer = optimizer;
449 return _optimizerFlags;
456 _optimizerFlags = optimizerFlags;
466 return _throwExceptionOnError;
476 _throwExceptionOnError = throwExceptionOnError;
480 bool _throwExceptionOnError;
482 bool _enableErrorMessages;
483 bool _chooseSibEncoding;
484 bool _chooseAbsEncoding;
485 bool _forceRexPrefix;
486 bool _forceThreeByteVEX;
487 bool _noBranchPrediction;
489 fcml_uint16_t _optimizerFlags;
516 _entryPoint( operatingMode, ip ) {
581 _entryPoint = entryPoint;
591 _entryPoint.setIP(ip);
601 _entryPoint.incrementIP( ip );
611 _entryPoint.setOpMode( operatingMode );
621 _entryPoint.setAddressSizeAttribute( addressSizeAttribute );
631 _entryPoint.setOperandSizeAttribute( operandSizeAttribute );
740 AssemblerTypeConverter::convert( ctx, context );
746 TypeConverter::convert( instruction, inst );
761 TypeConverter::free( inst );
765 ErrorTypeConverter::convert( res.
errors, errorContainer );
768 result.setErrorContainer( errorContainer );
777 std::vector<AssembledInstruction> &assembledInstructions = result.getAssembledInstructions();
779 assembledInstructions.clear();
785 while( next_instruction ) {
787 ErrorTypeConverter::convert( instruction_warnings, instructionWarnings );
789 assembledInstructions.push_back( assembledInstruction );
791 result.setChoosenInstructionIndex(i);
793 next_instruction = next_instruction->
next;
806 }
catch( std::exception &exc ) {
808 TypeConverter::free( inst );
846 CodeIterator( std::vector<AssembledInstruction> &assembledInstructions ) :
850 _iterator(assembledInstructions.begin()),
851 _assembledInstructions(assembledInstructions) {
867 if( _buffer && _pos >= _len ) {
871 if( _iterator == _assembledInstructions.end() ) {
890 if( ( !_buffer || _pos >= _len ) && !hasNext() ) {
893 return _buffer[_pos++];
899 const fcml_uint8_t *_buffer;
905 std::vector<AssembledInstruction>::iterator _iterator;
907 std::vector<AssembledInstruction> &_assembledInstructions;
913 #endif //FCML_ASSEMBLER_HPP_ bool isChooseAbsEncoding() const
Definition: fcml_assembler.hpp:336
AssemblerConf & getConfig()
Gets assembler configuration associated with the context.
Definition: fcml_assembler.hpp:537
AssembledInstruction(const AssembledInstruction &cpy)
Copy constructor.
Definition: fcml_assembler.hpp:75
fcml_usize getCodeLength() const
Gets number of bytes in the buffer.
Definition: fcml_assembler.hpp:124
fcml_ptr(LIB_CALL * fcml_fnp_asm_instruction_chooser)(fcml_st_chooser_context *chooser_context)
Instruction chooser function pointer declaration.
Definition: fcml_choosers.h:67
C++ wrappers common classes.
void setEnableErrorMessages(bool enableErrorMessages)
Definition: fcml_assembler.hpp:385
void setThrowExceptionOnError(bool throwExceptionOnError)
Sets the way how the error handling is done.
Definition: fcml_assembler.hpp:475
fcml_bool choose_abs_encoding
If memory address can be encoded as relative or absolute value choose the absolute addressing...
Definition: fcml_assembler.h:56
Assembler result.
Definition: fcml_assembler.hpp:177
Illegal state exception.
Definition: fcml_common.hpp:228
bool isForceThreeByteVex() const
Definition: fcml_assembler.hpp:406
const AssemblerConf & getConfig() const
Gets constant assembler configuration associated with the context.
Definition: fcml_assembler.hpp:527
Assembling failed.
Definition: fcml_assembler.hpp:45
bool isForceRexPrefix() const
Definition: fcml_assembler.hpp:392
fcml_st_assembler_conf configuration
Assembler behaviour can be configured here.
Definition: fcml_assembler.h:106
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:717
void setChooseSibEncoding(bool chooseSibEncoding)
Definition: fcml_assembler.hpp:371
AssembledInstruction & operator=(const AssembledInstruction &cpy)
Copies one instruction into another.
Definition: fcml_assembler.hpp:86
void setIP(fcml_ip ip)
Sets instruction pointer directly into the entry point.
Definition: fcml_assembler.hpp:590
void setAddressSizeAttribute(fcml_usize addressSizeAttribute)
Sets a new address size attribute for the entry point.
Definition: fcml_assembler.hpp:620
Converts objects to their structures counterparts.
Definition: fcml_assembler.hpp:647
fcml_uint16_t getOptimizerFlags() const
Definition: fcml_assembler.hpp:448
Assembler result.
Definition: fcml_assembler.h:90
Assembler context.
Definition: fcml_assembler.hpp:498
fcml_st_assembled_instruction * instructions
Chain of assembled instructions.
Definition: fcml_assembler.h:94
EntryPoint & getEntryPoint()
Gets reference to the entry point instance associated with the context.
Definition: fcml_assembler.hpp:569
Assembler runtime configuration.
Definition: fcml_assembler.h:48
bool isIncrementIp() const
Definition: fcml_assembler.hpp:420
Container for all collected errors and warnings.
Definition: fcml_errors.h:162
void setOptimizerFlags(fcml_uint16_t optimizerFlags)
Definition: fcml_assembler.hpp:455
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:67
#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:104
struct fcml_st_assembled_instruction * next
Next assembled instruction in the chain.
Definition: fcml_assembler.h:77
const fcml_uint8_t * getCode() const
Gets pointer to machine code buffer.
Definition: fcml_assembler.hpp:114
Holds instruction pointer, processor operating mode and memory segment flags.
Definition: fcml_common.hpp:499
Base class for all exceptions that are aware of ErrorContainer.
Definition: fcml_errors.hpp:347
Generic instruction model.
Definition: fcml_common.h:611
const AssembledInstruction * getChosenInstruction() const
Gets instruction chosen by the assembler as the preferred one.
Definition: fcml_assembler.hpp:195
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:64
fcml_st_ceh_error_container warnings
Warning messages related to assembled instruction.
Definition: fcml_assembler.h:79
Definition: fcml_assembler.hpp:39
void setChooseAbsEncoding(bool chooseAbsEncoding)
Definition: fcml_assembler.hpp:343
fcml_ceh_error assemble(AssemblerContext &ctx, const Instruction &instruction, AssemblerResult &result)
Assembles given generic instruction model.
Definition: fcml_assembler.hpp:736
fcml_fnp_asm_optimizer getOptimizer() const
Definition: fcml_assembler.hpp:434
fcml_st_entry_point entry_point
Instruction entry point configuration.
Definition: fcml_assembler.h:108
void setForceThreeByteVex(bool forceThreeByteVex)
Definition: fcml_assembler.hpp:413
Inherit from this class in order to get access to the native FCML dialect structure.
Definition: fcml_dialect.hpp:97
fcml_st_assembled_instruction * chosen_instruction
Instruction chosen by used instruction chooser; otherwise NULL.
Definition: fcml_assembler.h:96
Used mainly in case of integers and offsets.
Definition: fcml_errors.h:54
void setChooser(fcml_fnp_asm_instruction_chooser chooser)
Definition: fcml_assembler.hpp:357
const ErrorContainer & getWarningContainer() const
Gets reference to the errors container.
Definition: fcml_assembler.hpp:134
void setIncrementIp(bool incrementIp)
Definition: fcml_assembler.hpp:427
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:274
An assembler wrapper.
Definition: fcml_assembler.hpp:696
fcml_bool enable_error_messages
True if optional error and warning messages should be collected during processing.
Definition: fcml_assembler.h:52
bool isChooseSibEncoding() const
Definition: fcml_assembler.hpp:364
AssemblerResult()
Definition: fcml_assembler.hpp:183
const ErrorContainer & getErrorContainer() const
Gets errors container.
Definition: fcml_assembler.hpp:208
fcml_fnp_asm_instruction_chooser getChooser() const
Definition: fcml_assembler.hpp:350
fcml_int64_t fcml_ip
General instruction pointer holder.
Definition: fcml_common.h:95
Describes an assembled instruction.
Definition: fcml_assembler.hpp:55
void setConfig(const AssemblerConf &config)
Copies given configuration to the instance associated with the context.
Definition: fcml_assembler.hpp:549
C++ wrapper for the base dialect.
bool hasNext()
Gets true if there is an another element in the iterator.
Definition: fcml_assembler.hpp:866
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:102
OperatingMode
Supported operating modes.
Definition: fcml_common.hpp:506
void setOperatingMode(EntryPoint::OperatingMode operatingMode)
Sets processor operating mode directly into the entry point.
Definition: fcml_assembler.hpp:610
void setForceRexPrefix(bool forceRexPrefix)
Definition: fcml_assembler.hpp:399
fcml_uint8_t next()
Gets the next element from the iterator.
Definition: fcml_assembler.hpp:889
fcml_fnp_asm_instruction_chooser chooser
instruction chooser implementation that should be used by assembler to choose most appropriate instru...
Definition: fcml_assembler.h:67
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:559
fcml_fnp_asm_optimizer optimizer
Optimizer implementation that should be used by assembler.
Definition: fcml_assembler.h:62
void setOptimizer(fcml_fnp_asm_optimizer optimizer)
Definition: fcml_assembler.hpp:441
void incrementIP(fcml_ip ip)
Increments entry point by given number of bytes.
Definition: fcml_assembler.hpp:600
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:58
bool isEnableErrorMessages() const
Definition: fcml_assembler.hpp:378
fcml_bool force_three_byte_VEX
Every 2 byte VEX/XOP prefix can be encoded using three byte form.
Definition: fcml_assembler.h:60
bool isThrowExceptionOnError() const
Returns true if exception should be thrown when assembling fails.
Definition: fcml_assembler.hpp:465
fcml_uint8_t * code
Instruction machine code.
Definition: fcml_assembler.h:81
Describes an instruction.
Definition: fcml_common.hpp:6207
virtual ~CodeIterator()
Definition: fcml_assembler.hpp:857
Operation succeed.
Definition: fcml_errors.h:42
AssemblerContext()
Definition: fcml_assembler.hpp:505
Assembler configuration.
Definition: fcml_assembler.hpp:311
void setOperandSizeAttribute(fcml_usize operandSizeAttribute)
Sets a new operand size attribute for the entry point.
Definition: fcml_assembler.hpp:630
Bad arguments.
Definition: fcml_common.hpp:217
LIB_EXPORT void LIB_CALL fcml_fn_assembler_free(fcml_st_assembler *assembler)
Frees assembler instance.
Encoded instruction.
Definition: fcml_assembler.h:75
An abstract dialect.
Definition: fcml_dialect.hpp:41
virtual ~AssembledInstruction()
Definition: fcml_assembler.hpp:99
void setEntryPoint(const EntryPoint &entryPoint)
Copies given entry point to the instance associated with the context.
Definition: fcml_assembler.hpp:580
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:54
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:515
Dialect & getDialect() const
Gets dialect associated with the assembler.
Definition: fcml_assembler.hpp:821
fcml_usize number_of_instructions
Number of encoded instruction forms.
Definition: fcml_assembler.h:98
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:206
fcml_st_ceh_error_container errors
Error and warning messages from assembler.
Definition: fcml_assembler.h:92
fcml_uint16_t fcml_ceh_error
All error codes should be held in variables of this type.
Definition: fcml_errors.h:139
fcml_usize code_length
Instruction code length in bytes.
Definition: fcml_assembler.h:83
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:838
Object which shouldn't be copied can inherit from this class.
Definition: fcml_common.hpp:263
Assembler(Dialect &dialect)
Creates an assembler instance for given dialect.
Definition: fcml_assembler.hpp:706
C++ wrapper for the FCML errors handling.
CodeIterator(std::vector< AssembledInstruction > &assembledInstructions)
Creates a code iterator instance.
Definition: fcml_assembler.hpp:846
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:50
fcml_usize getSize() const
Gets number of instructions alternatives available in the result.
Definition: fcml_assembler.hpp:218
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:94
AssemblerConf()
Definition: fcml_assembler.hpp:317