From 14662a66dc6369ffc53148a6cd1f6406194b082b Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 23 Sep 2021 21:30:13 +0800 Subject: [PATCH] nac3embedded: run linker (WIP) --- nac3embedded/kernel.ld | 53 +++++++++++++++++++++++++++++++++++++++++ nac3embedded/src/lib.rs | 14 +++++++++-- shell.nix | 9 ++++++- 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 nac3embedded/kernel.ld diff --git a/nac3embedded/kernel.ld b/nac3embedded/kernel.ld new file mode 100644 index 000000000..6523d631a --- /dev/null +++ b/nac3embedded/kernel.ld @@ -0,0 +1,53 @@ +/* Force ld to make the ELF header as loadable. */ +PHDRS +{ + headers PT_LOAD FILEHDR PHDRS ; + text PT_LOAD ; + data PT_LOAD ; + dynamic PT_DYNAMIC ; + eh_frame PT_GNU_EH_FRAME ; +} + +SECTIONS +{ + /* Push back .text section enough so that ld.lld not complain */ + . = SIZEOF_HEADERS; + + .text : + { + *(.text .text.*) + } : text + + .rodata : + { + *(.rodata .rodata.*) + } + + .eh_frame : + { + KEEP(*(.eh_frame)) + } : text + + .eh_frame_hdr : + { + KEEP(*(.eh_frame_hdr)) + } : text : eh_frame + + .data : + { + *(.data) + } : data + + .dynamic : + { + *(.dynamic) + } : data : dynamic + + .bss (NOLOAD) : ALIGN(4) + { + __bss_start = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _end = .; + } +} diff --git a/nac3embedded/src/lib.rs b/nac3embedded/src/lib.rs index 3ee1ef725..fbbb66b42 100644 --- a/nac3embedded/src/lib.rs +++ b/nac3embedded/src/lib.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::sync::Arc; use std::path::Path; +use std::process::Command; use pyo3::prelude::*; use pyo3::exceptions; @@ -163,11 +164,20 @@ impl Nac3 { .write_to_file(module, FileType::Object, Path::new(&format!("{}.o", module.get_name().to_str().unwrap()))) .expect("couldn't write module to file"); }))); - let threads: Vec = (0..4).map(|i| format!("module{}", i)).collect(); - let threads: Vec<_> = threads.iter().map(|s| s.as_str()).collect(); + let thread_names: Vec = (0..4).map(|i| format!("module{}", i)).collect(); + let threads: Vec<_> = thread_names.iter().map(|s| s.as_str()).collect(); let (registry, handles) = WorkerRegistry::create_workers(&threads, top_level.clone(), f); registry.add_task(task); registry.wait_tasks_complete(handles); + + if let Ok(linker_status) = Command::new("ld.lld").args(&["-shared", "--eh-frame-hdr", "-Tkernel.ld"]).status() { + if !linker_status.success() { + return Err(exceptions::PyRuntimeError::new_err("failed to start linker")); + } + } else { + return Err(exceptions::PyRuntimeError::new_err("linker returned non-zero status code")); + } + Ok(()) } } diff --git a/shell.nix b/shell.nix index 35be20556..070196c7e 100644 --- a/shell.nix +++ b/shell.nix @@ -4,6 +4,13 @@ in pkgs.stdenv.mkDerivation { name = "nac3-env"; buildInputs = with pkgs; [ - llvm_11 clang_11 cargo rustc libffi libxml2 clippy + llvm_11 + clang_11 + lld_11 + cargo + rustc + libffi + libxml2 + clippy ]; }