Skip to content

Commit

Permalink
Merge branch 'main' into cl/debuginfo
Browse files Browse the repository at this point in the history
  • Loading branch information
xermicus committed Nov 21, 2024
2 parents 152b742 + 87f2bce commit 3f4df5d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
3 changes: 3 additions & 0 deletions crates/common/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ pub static EXTENSION_POLKAVM_ASSEMBLY: &str = "pvmasm";

/// The PolkaVM bytecode file extension.
pub static EXTENSION_POLKAVM_BINARY: &str = "pvm";

/// The ELF shared object file extension.
pub static EXTENSION_SHARED_OBJECT: &str = "so";
13 changes: 4 additions & 9 deletions crates/linker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ fn invoke_lld(cmd_args: &[&str]) -> bool {
unsafe { LLDELFLink(args.as_ptr(), args.len()) == 0 }
}

fn polkavm_linker<T: AsRef<[u8]>>(code: T, strip_binary: Option<bool>) -> anyhow::Result<Vec<u8>> {
pub fn polkavm_linker<T: AsRef<[u8]>>(code: T, strip_binary: bool) -> anyhow::Result<Vec<u8>> {
let mut config = polkavm_linker::Config::default();
config.set_strip(strip_binary.unwrap_or(true));
config.set_strip(strip_binary);
config.set_optimize(true);

polkavm_linker::program_from_elf(config, code.as_ref())
.map_err(|reason| anyhow::anyhow!("polkavm linker failed: {}", reason))
}

pub fn link<T: AsRef<[u8]>>(input: T, strip_binary: Option<bool>) -> anyhow::Result<Vec<u8>> {
pub fn link<T: AsRef<[u8]>>(input: T) -> anyhow::Result<Vec<u8>> {
let dir = tempfile::tempdir().expect("failed to create temp directory for linking");
let output_path = dir.path().join("out.so");
let object_path = dir.path().join("out.o");
Expand Down Expand Up @@ -79,10 +79,5 @@ pub fn link<T: AsRef<[u8]>>(input: T, strip_binary: Option<bool>) -> anyhow::Res
return Err(anyhow::anyhow!("ld.lld failed"));
}

if env::var("PVM_LINKER_DUMP_SO").is_ok() {
fs::copy(&output_path, "/tmp/out.so")?;
};

let blob = fs::read(&output_path)?;
polkavm_linker(blob, strip_binary)
Ok(fs::read(&output_path)?)
}
3 changes: 3 additions & 0 deletions crates/llvm-context/src/debug_config/ir_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum IRType {
LLVM,
/// Whether to dump the assembly code.
Assembly,
/// Whether to dump the ELF shared object
SO,
/// Whether to jump JSON
#[cfg(debug_assertions)]
JSON,
Expand All @@ -31,6 +33,7 @@ impl IRType {
Self::Assembly => revive_common::EXTENSION_POLKAVM_ASSEMBLY,
#[cfg(debug_assertions)]
Self::JSON => revive_common::EXTENSION_JSON,
Self::SO => revive_common::EXTENSION_SHARED_OBJECT,
}
}
}
12 changes: 12 additions & 0 deletions crates/llvm-context/src/debug_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ impl DebugConfig {
Ok(())
}

/// Dumps the code object.
pub fn dump_object(&self, contract_path: &str, code: &[u8]) -> anyhow::Result<()> {
if let Some(output_directory) = self.output_directory.as_ref() {
let mut file_path = output_directory.to_owned();
let full_file_name = Self::full_file_name(contract_path, None, IRType::SO);
file_path.push(full_file_name);
std::fs::write(file_path, code)?;
}

Ok(())
}

/// Dumps the stage output as a json file suitable for use with --recursive-process
#[cfg(debug_assertions)]
pub fn dump_stage_output(
Expand Down
10 changes: 8 additions & 2 deletions crates/llvm-context/src/polkavm/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,17 @@ where
)
})?;

let bytecode = revive_linker::link(buffer.as_slice(), Some(self.debug_info().is_none()))?;
let shared_object = revive_linker::link(buffer.as_slice())?;

self.debug_config
.dump_object(contract_path, &shared_object)?;

let polkavm_bytecode =
revive_linker::polkavm_linker(shared_object, !self.debug_config().emit_debug_info)?;

let build = match crate::polkavm::build_assembly_text(
contract_path,
&bytecode,
&polkavm_bytecode,
metadata_hash,
self.debug_config(),
) {
Expand Down

0 comments on commit 3f4df5d

Please sign in to comment.