@@ -26,22 +26,18 @@ fn main() {
26
26
process:: exit ( 1 ) ;
27
27
}
28
28
} ) ;
29
+ let kernel_file_name = kernel
30
+ . file_name ( )
31
+ . expect ( "KERNEL has no valid file name" )
32
+ . to_str ( )
33
+ . expect ( "kernel file name not valid utf8" ) ;
29
34
30
35
// check that the kernel file exists
31
36
assert ! (
32
37
kernel. exists( ) ,
33
38
format!( "KERNEL does not exist: {}" , kernel. display( ) )
34
39
) ;
35
40
36
- let kernel_file_name = kernel
37
- . file_name ( )
38
- . expect ( "KERNEL has no valid file name" )
39
- . to_str ( )
40
- . expect ( "kernel file name not valid utf8" ) ;
41
- let kernel_file_name_replaced = kernel_file_name. replace ( '-' , "_" ) ;
42
- let kernel_out_path = out_dir. join ( format ! ( "kernel_bin-{}.o" , kernel_file_name) ) ;
43
- let kernel_archive_path = out_dir. join ( format ! ( "libkernel_bin-{}.a" , kernel_file_name) ) ;
44
-
45
41
// get access to llvm tools shipped in the llvm-tools-preview rustup component
46
42
let llvm_tools = match llvm_tools:: LlvmTools :: new ( ) {
47
43
Ok ( tools) => tools,
@@ -74,30 +70,48 @@ fn main() {
74
70
Kernel executable at `{}`\n ", kernel. display( ) ) ;
75
71
}
76
72
77
- // wrap the kernel executable as binary in a new ELF file
73
+ // strip debug symbols from kernel for faster loading
74
+ let stripped_kernel_file_name = format ! ( "kernel_stripped-{}" , kernel_file_name) ;
75
+ let stripped_kernel = out_dir. join ( & stripped_kernel_file_name) ;
78
76
let objcopy = llvm_tools
79
77
. tool ( & llvm_tools:: exe ( "llvm-objcopy" ) )
80
78
. expect ( "llvm-objcopy not found in llvm-tools" ) ;
81
- let mut cmd = Command :: new ( objcopy) ;
79
+ let mut cmd = Command :: new ( & objcopy) ;
80
+ cmd. arg ( "--strip-debug" ) ;
81
+ cmd. arg ( & kernel) ;
82
+ cmd. arg ( & stripped_kernel) ;
83
+ let exit_status = cmd
84
+ . status ( )
85
+ . expect ( "failed to run objcopy to strip debug symbols" ) ;
86
+ if !exit_status. success ( ) {
87
+ eprintln ! ( "Error: Stripping debug symbols failed" ) ;
88
+ process:: exit ( 1 ) ;
89
+ }
90
+
91
+ // wrap the kernel executable as binary in a new ELF file
92
+ let stripped_kernel_file_name_replaced = stripped_kernel_file_name. replace ( '-' , "_" ) ;
93
+ let kernel_bin = out_dir. join ( format ! ( "kernel_bin-{}.o" , kernel_file_name) ) ;
94
+ let kernel_archive = out_dir. join ( format ! ( "libkernel_bin-{}.a" , kernel_file_name) ) ;
95
+ let mut cmd = Command :: new ( & objcopy) ;
82
96
cmd. arg ( "-I" ) . arg ( "binary" ) ;
83
97
cmd. arg ( "-O" ) . arg ( "elf64-x86-64" ) ;
84
98
cmd. arg ( "--binary-architecture=i386:x86-64" ) ;
85
99
cmd. arg ( "--rename-section" ) . arg ( ".data=.kernel" ) ;
86
100
cmd. arg ( "--redefine-sym" ) . arg ( format ! (
87
101
"_binary_{}_start=_kernel_start_addr" ,
88
- kernel_file_name_replaced
102
+ stripped_kernel_file_name_replaced
89
103
) ) ;
90
104
cmd. arg ( "--redefine-sym" ) . arg ( format ! (
91
105
"_binary_{}_end=_kernel_end_addr" ,
92
- kernel_file_name_replaced
106
+ stripped_kernel_file_name_replaced
93
107
) ) ;
94
108
cmd. arg ( "--redefine-sym" ) . arg ( format ! (
95
109
"_binary_{}_size=_kernel_size" ,
96
- kernel_file_name_replaced
110
+ stripped_kernel_file_name_replaced
97
111
) ) ;
98
- cmd. current_dir ( kernel . parent ( ) . expect ( "KERNEL has no valid parent dir" ) ) ;
99
- cmd. arg ( & kernel_file_name ) ;
100
- cmd. arg ( & kernel_out_path ) ;
112
+ cmd. current_dir ( & out_dir ) ;
113
+ cmd. arg ( & stripped_kernel_file_name ) ;
114
+ cmd. arg ( & kernel_bin ) ;
101
115
let exit_status = cmd. status ( ) . expect ( "failed to run objcopy" ) ;
102
116
if !exit_status. success ( ) {
103
117
eprintln ! ( "Error: Running objcopy failed" ) ;
@@ -115,8 +129,8 @@ fn main() {
115
129
} ) ;
116
130
let mut cmd = Command :: new ( ar) ;
117
131
cmd. arg ( "crs" ) ;
118
- cmd. arg ( & kernel_archive_path ) ;
119
- cmd. arg ( & kernel_out_path ) ;
132
+ cmd. arg ( & kernel_archive ) ;
133
+ cmd. arg ( & kernel_bin ) ;
120
134
let exit_status = cmd. status ( ) . expect ( "failed to run ar" ) ;
121
135
if !exit_status. success ( ) {
122
136
eprintln ! ( "Error: Running ar failed" ) ;
0 commit comments