Nyxstone
Loading...
Searching...
No Matches
nyxstone::ObjectWriterWrapper Class Reference

#include <ObjectWriterWrapper.h>

Inheritance diagram for nyxstone::ObjectWriterWrapper:

Public Member Functions

 ObjectWriterWrapper (std::unique_ptr< llvm::MCObjectWriter > &&object_writer, llvm::raw_pwrite_stream &stream, llvm::MCContext &context, bool write_text_section_only, u64 start_address, std::string &extended_error, std::vector< Nyxstone::Instruction > *instructions)
 Creates an ObjectWriterWrapper.
 
void executePostLayoutBinding (llvm::MCAssembler &Asm, const llvm::MCAsmLayout &Layout) override
 Simple function wrapper calling the wrapped object wrapper function directly.
 
bool resolve_relocation (llvm::MCAssembler &assembler, const llvm::MCAsmLayout &layout, const llvm::MCFragment *fragment, const llvm::MCFixup &fixup, llvm::MCValue target, uint64_t &fixed_value)
 Internal resolve for relocations.
 
void recordRelocation (llvm::MCAssembler &Asm, const llvm::MCAsmLayout &Layout, const llvm::MCFragment *Fragment, const llvm::MCFixup &Fixup, llvm::MCValue Target, uint64_t &FixedValue) override
 Tries to resolve relocations (that are normally resolved at link time) instead of recording them.
 
uint64_t writeObject (llvm::MCAssembler &Asm, const llvm::MCAsmLayout &Layout) override
 Write object to the stream and update the bytes of the instruction details.
 

Static Public Member Functions

static std::unique_ptr< llvm::MCObjectWriter > createObjectWriterWrapper (std::unique_ptr< llvm::MCObjectWriter > &&object_writer, llvm::raw_pwrite_stream &stream, llvm::MCContext &context, bool write_text_section_only, u64 start_address, std::string &extended_error, std::vector< Nyxstone::Instruction > *instructions)
 Creates a UniquePtr holding the the ObjectWriterWrapper.
 

Detailed Description

This class enables us to limit the final output byte stream to the relevant bytes (as opposed to the whole ELF object file) and grab final instruction bytes (after relaxation and fixups) (via 'writeObject()').

This class is also used to insert custom relocations and fixup validations. These are necessary when a relocation is normally performed at link time or when LLVM does not verify a fixup according to the specification, leading to wrong output for specific instruction/label combinations.

Constructor & Destructor Documentation

◆ ObjectWriterWrapper()

nyxstone::ObjectWriterWrapper::ObjectWriterWrapper ( std::unique_ptr< llvm::MCObjectWriter > && object_writer,
llvm::raw_pwrite_stream & stream,
llvm::MCContext & context,
bool write_text_section_only,
u64 start_address,
std::string & extended_error,
std::vector< Nyxstone::Instruction > * instructions )
inline

Creates an ObjectWriterWrapper.

The ObjectWriterWrapper is created using the wrapped MCObjectWriter, some additional llvm classes used for querying additional information from LLVM, and a (nullable) pointer to the instruction information.

Parameters
object_writerThe object writer object to wrap, must implement the function used by this class.
streamStream (used for the object_writer) to write the .text section to if requested via write_text_section_only.
write_text_section_onlyIf only the .text section should be written to stream.
start_addressAbsolute start address of the instructions
extended_errorAccumulation for llvm errors, since Exceptions might not be supported by the linked LLVM.
instructionsInstruction information for which the bytes should be corrected.

Member Function Documentation

◆ createObjectWriterWrapper()

std::unique_ptr< MCObjectWriter > nyxstone::ObjectWriterWrapper::createObjectWriterWrapper ( std::unique_ptr< llvm::MCObjectWriter > && object_writer,
llvm::raw_pwrite_stream & stream,
llvm::MCContext & context,
bool write_text_section_only,
u64 start_address,
std::string & extended_error,
std::vector< Nyxstone::Instruction > * instructions )
static

Creates a UniquePtr holding the the ObjectWriterWrapper.

Parameters
object_writerThe object writer object to wrap, must implement the function used by this class.
streamStream (used for the object_writer) to write the .text section to if requested via write_text_section_only.
write_text_section_onlyIf only the .text section should be written to stream.
start_addressAbsolute start address of the instructions
extended_errorAccumulation for llvm errors, since Exceptions might not be supported by the linked LLVM.
instructionsInstruction information for which the bytes should be corrected.

◆ recordRelocation()

void nyxstone::ObjectWriterWrapper::recordRelocation ( llvm::MCAssembler & Asm,
const llvm::MCAsmLayout & Layout,
const llvm::MCFragment * Fragment,
const llvm::MCFixup & Fixup,
llvm::MCValue Target,
uint64_t & FixedValue )
override

Tries to resolve relocations (that are normally resolved at link time) instead of recording them.

This function serves multiple purposes:

  • Resolve (some) relocations
  • Ensure relocations which can not be resolved are an error instead of invalid machine bytes.
  • Ensure that any missing label is correctly reported.

Normally, this function records relocations, which are resolved by the linker. Since we do not have a linking step, we must assume that any relocation which would be recorded is not yet correctly assembled. Thus, we try to resolve relocations ourself, although this must be implemented on a relocation basis, and we currently implement:

  • adrp

Any missing label in the assembly must be resolved by the linker and leads to this function being called, we use this fact to emit errors for missing labels.

◆ resolve_relocation()

bool nyxstone::ObjectWriterWrapper::resolve_relocation ( llvm::MCAssembler & assembler,
const llvm::MCAsmLayout & layout,
const llvm::MCFragment * fragment,
const llvm::MCFixup & fixup,
llvm::MCValue target,
uint64_t & fixed_value )

Internal resolve for relocations.

Implements relocation for:

  • adrp

The documentation for this class was generated from the following files: