Skip to content
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

Strip debug symbols from kernel #59

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Changes from 1 commit
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
Next Next commit
Strip debug symbols from kernel
  • Loading branch information
phil-opp committed Jun 24, 2019
commit a4d5f011ba77ecb2850e8a115ed9ca042cbbb6d7
29 changes: 21 additions & 8 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ fn main() {
.expect("KERNEL has no valid file name")
.to_str()
.expect("kernel file name not valid utf8");
let kernel_file_name_replaced = kernel_file_name.replace('-', "_");
let kernel_stripped_file_name = format!("kernel_stripped-{}", kernel_file_name);
let kernel_stripped_file_name_replaced = kernel_stripped_file_name.replace('-', "_");
let kernel_stripped_out_path = out_dir.join(&kernel_stripped_file_name);
let kernel_out_path = out_dir.join(format!("kernel_bin-{}.o", kernel_file_name));
let kernel_archive_path = out_dir.join(format!("libkernel_bin-{}.a", kernel_file_name));

Expand Down Expand Up @@ -76,29 +78,40 @@ fn main() {
Kernel executable at `{}`\n", kernel.display());
}

// wrap the kernel executable as binary in a new ELF file
// strip debug symbols from kernel for faster loading
let objcopy = llvm_tools
.tool(&llvm_tools::exe("llvm-objcopy"))
.expect("llvm-objcopy not found in llvm-tools");
let mut cmd = Command::new(objcopy);
let mut cmd = Command::new(&objcopy);
cmd.arg("--strip-debug");
cmd.arg(&kernel);
cmd.arg(&kernel_stripped_out_path);
let exit_status = cmd.status().expect("failed to run objcopy to strip debug symbols");
if !exit_status.success() {
eprintln!("Error: Stripping debug symbols failed");
process::exit(1);
}

// wrap the kernel executable as binary in a new ELF file
let mut cmd = Command::new(&objcopy);
cmd.arg("-I").arg("binary");
cmd.arg("-O").arg("elf64-x86-64");
cmd.arg("--binary-architecture=i386:x86-64");
cmd.arg("--rename-section").arg(".data=.kernel");
cmd.arg("--redefine-sym").arg(format!(
"_binary_{}_start=_kernel_start_addr",
kernel_file_name_replaced
kernel_stripped_file_name_replaced
));
cmd.arg("--redefine-sym").arg(format!(
"_binary_{}_end=_kernel_end_addr",
kernel_file_name_replaced
kernel_stripped_file_name_replaced
));
cmd.arg("--redefine-sym").arg(format!(
"_binary_{}_size=_kernel_size",
kernel_file_name_replaced
kernel_stripped_file_name_replaced
));
cmd.current_dir(kernel.parent().expect("KERNEL has no valid parent dir"));
cmd.arg(&kernel_file_name);
cmd.current_dir(&out_dir);
cmd.arg(&kernel_stripped_file_name);
cmd.arg(&kernel_out_path);
let exit_status = cmd.status().expect("failed to run objcopy");
if !exit_status.success() {
Expand Down