global_asm_data!() { /* proc-macro */ }
Expand description
Embed the compiled assembly as an array.
This macro will generate a module with the given $name
and provides a data
function in the
module to allow accessing the compiled machine code as an array.
Note that this macro uses std::arch::global_asm
, so we can only use this macro in a global
scope, outside a function.
§Example
Given the following x86 assembly:
0: 01 d8 add eax,ebx
2: f4 hlt
global_asm_data!(
my_code,
".code64",
"add eax, ebx",
"hlt",
);
assert_eq!([0x01, 0xd8, 0xf4], my_code::data());
It is supported to pass arbitrary supported std::arch::global_asm
operands and options.
fn f() {}
global_asm_data!(
my_code1,
".global {0}",
".code64",
"add eax, ebx",
"hlt",
sym f,
);
global_asm_data!(
my_code2,
".code64",
"add eax, ebx",
"hlt",
options(raw),
);
assert_eq!([0x01, 0xd8, 0xf4], my_code1::data());
assert_eq!([0x01, 0xd8, 0xf4], my_code2::data());
It is also supported to specify the visibility of the generated module. Note that the below
example won’t work if the pub
in the macro is missing.
mod my_mod {
// This use is needed to import the global_asm_data macro to this module.
use super::*;
global_asm_data!(
// pub is needed so that my_mod::my_code is visible to the outer scope.
pub my_code,
".code64",
"add eax, ebx",
"hlt",
);
}
assert_eq!([0x01, 0xd8, 0xf4], my_mod::my_code::data());