Skip to content

Added the option to enable printing the operand as an absolute address #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bindings/rust/examples/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn main() -> Result<()> {
cpu: &cli.cpu.unwrap_or_default(),
features: &cli.features.unwrap_or_default(),
immediate_style: IntegerBase::HexPrefix,
print_branch_imm_as_address: false,
},
)?;

Expand Down
11 changes: 10 additions & 1 deletion bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl Nyxstone {
config.cpu,
config.features,
config.immediate_style.into(),
config.print_branch_imm_as_address,
);

if !result.error.is_empty() {
Expand Down Expand Up @@ -272,6 +273,8 @@ pub struct NyxstoneConfig<'a, 'b> {
pub features: &'b str,
/// The printing style of immediates.
pub immediate_style: IntegerBase,
/// Option If true, a branch immediate (e.g. bl 4) will be printed as a hexadecimal address (e.g. bl 0x20004)
pub print_branch_imm_as_address: bool,
}

#[cxx::bridge]
Expand Down Expand Up @@ -341,7 +344,13 @@ mod ffi {
/// - features: llvm features string (features delimited by `,` with `+` for enable and `-` for disable), can be empty
/// # Returns
/// Ok() and UniquePtr holding a NyxstoneFFI on success, Err() otherwise.
fn create_nyxstone_ffi(triple_name: &str, cpu: &str, features: &str, style: IntegerBase) -> NyxstoneResult;
fn create_nyxstone_ffi(
triple_name: &str,
cpu: &str,
features: &str,
style: IntegerBase,
print_branch_imm_as_address: bool,
) -> NyxstoneResult;

// Translates assembly instructions at a given start address to bytes.
// Additional label definitions by absolute address may be supplied.
Expand Down
3 changes: 2 additions & 1 deletion bindings/rust/src/nyxstone_ffi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,15 @@ InstructionResult NyxstoneFFI::disassemble_to_instructions(
}

NyxstoneResult create_nyxstone_ffi( // cppcheck-suppress unusedFunction
const rust::str triple_name, const rust::str cpu, const rust::str features, const IntegerBase imm_style)
const rust::str triple_name, const rust::str cpu, const rust::str features, const IntegerBase imm_style, const bool print_branch_immediate_as_address)
{
NyxstoneBuilder::IntegerBase style = static_cast<NyxstoneBuilder::IntegerBase>(static_cast<uint8_t>(imm_style));

auto result = NyxstoneBuilder(std::string { triple_name })
.with_cpu(std::string { cpu })
.with_features(std::string { features })
.with_immediate_style(style)
.with_print_immediate_as_address(print_branch_immediate_as_address)
.build();

// Note: This is disgusting, but this is necesarry for two reasons:
Expand Down
2 changes: 1 addition & 1 deletion bindings/rust/src/nyxstone_ffi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ class NyxstoneFFI {
/// @param cpu The cpu to be used.
/// @param features Llvm features string.
/// @param imm_style The integer representation for immediates.
NyxstoneResult create_nyxstone_ffi(rust::str triple_name, rust::str cpu, rust::str features, IntegerBase imm_style);
NyxstoneResult create_nyxstone_ffi(rust::str triple_name, rust::str cpu, rust::str features, IntegerBase imm_style, bool print_branch_immediate_as_address);
27 changes: 27 additions & 0 deletions bindings/rust/tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ mod tests {
Ok(())
}

#[test]
fn nyxstone_disassemble_with_branch_as_address() -> Result<()> {
let config = NyxstoneConfig {
print_branch_imm_as_address: true,
..Default::default()
};

let labels = HashMap::from([(".label", 0x1010)]);

let nyxstone_x86_64 = Nyxstone::new("x86_64-linux-gnu", config)?;

let result = nyxstone_x86_64.assemble_with("jmp .label", 0x1000, &labels)?;
assert_eq!(result, vec![235, 14]);

let result = nyxstone_x86_64.disassemble_to_instructions(&result, 0x1000, 0)?;
assert_eq!(
result,
vec![Instruction {
address: 0x1000,
assembly: "jmp 0x100e".into(),
bytes: vec![235, 14],
}],
);

Ok(())
}

#[test]
fn nyxstone_test() -> Result<()> {
let nyxstone_x86_64 = Nyxstone::new("x86_64-linux-gnu", NyxstoneConfig::default())?;
Expand Down
7 changes: 7 additions & 0 deletions include/nyxstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ class NyxstoneBuilder {
/// @return Reference to the updated NyxstoneBuilder object.
NyxstoneBuilder& with_immediate_style(IntegerBase style) noexcept;

/// @brief Specify if some operands should be printed as an absolute address.
///
/// @return Reference to the updated NyxstoneBuilder object.
NyxstoneBuilder& with_print_immediate_as_address(bool option) noexcept;

/// @brief Builds a nyxstone instance from the builder.
///
/// @return A unique_ptr holding the created nyxstone instance on success, an error string otherwise.
Expand All @@ -206,6 +211,8 @@ class NyxstoneBuilder {
std::string m_features;
/// @brief In which style immediates should be represented in disassembly.
IntegerBase m_imm_style = IntegerBase::Dec;
/// @brief Option if true a branch immediate will be printed as an absolute address
bool m_print_branch_immediate_as_address = false;
};

/// Detects all ARM Thumb architectures. LLVM doesn't seem to have a short way to check this.
Expand Down
8 changes: 8 additions & 0 deletions src/nyxstone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ NyxstoneBuilder& NyxstoneBuilder::with_immediate_style(NyxstoneBuilder::IntegerB
return *this;
}

NyxstoneBuilder& NyxstoneBuilder::with_print_immediate_as_address(bool option) noexcept
{
m_print_branch_immediate_as_address = option;
return *this;
}

tl::expected<std::unique_ptr<Nyxstone>, std::string> NyxstoneBuilder::build()
{
// # Note
Expand Down Expand Up @@ -118,6 +124,8 @@ tl::expected<std::unique_ptr<Nyxstone>, std::string> NyxstoneBuilder::build()
break;
}

instruction_printer->setPrintBranchImmAsAddress(m_print_branch_immediate_as_address);

return std::make_unique<Nyxstone>(std::move(triple),
// target is a static object, thus it is safe to take its reference here:
*target, std::move(target_options), std::move(register_info), std::move(assembler_info),
Expand Down
Loading