@@ -153,19 +153,35 @@ impl BpfObjBuilder {
153
153
154
154
/// Build a BPF object file.
155
155
pub fn build ( & mut self , src : & Path , dst : & Path ) -> Result < CompilationOutput > {
156
+ let obj_dir = tempdir ( ) . context ( "failed to create temporary directory" ) ?;
157
+ let mut linker = libbpf_rs:: Linker :: new ( dst)
158
+ . context ( "failed to instantiate libbpf object file linker" ) ?;
159
+
156
160
let output = self . with_compiler_args ( |compiler_args| {
157
- let output = Self :: compile_single ( src, dst, & self . compiler , compiler_args)
161
+ let tmp_dst = obj_dir. path ( ) . join ( src. file_name ( ) . with_context ( || {
162
+ format ! (
163
+ "input path `{}` does not have a proper file name" ,
164
+ src. display( )
165
+ )
166
+ } ) ?) ;
167
+
168
+ let output = Self :: compile_single ( src, & tmp_dst, & self . compiler , compiler_args)
158
169
. with_context ( || format ! ( "failed to compile `{}`" , src. display( ) ) ) ?;
159
170
171
+ linker
172
+ . add_file ( tmp_dst)
173
+ . context ( "failed to add object file to BPF linker" ) ?;
174
+
160
175
Ok ( output)
161
176
} ) ?;
162
177
163
- // Compilation with clang may contain DWARF information that references
164
- // system specific and temporary paths. That can render our generated
165
- // skeletons unstable, potentially rendering them unsuitable for inclusion
166
- // in version control systems. So strip this information.
167
- strip_dwarf_info ( dst)
168
- . with_context ( || format ! ( "Failed to strip object file {}" , dst. display( ) ) ) ?;
178
+ // The resulting object file may contain DWARF information
179
+ // that references system specific and temporary paths. That
180
+ // can render our generated skeletons unstable, potentially
181
+ // making them unsuitable for inclusion in version control
182
+ // systems. Linking has the side effect of stripping this
183
+ // information.
184
+ linker. link ( ) . context ( "failed to link object file" ) ?;
169
185
170
186
Ok ( output)
171
187
}
@@ -230,25 +246,6 @@ fn extract_libbpf_headers_to_disk(_target_dir: &Path) -> Result<Option<PathBuf>>
230
246
Ok ( None )
231
247
}
232
248
233
- /// Strip DWARF information from the provided BPF object file.
234
- ///
235
- /// We rely on the `libbpf` linker here, which removes debug information as a
236
- /// side-effect.
237
- fn strip_dwarf_info ( file : & Path ) -> Result < ( ) > {
238
- let mut temp_file = file. as_os_str ( ) . to_os_string ( ) ;
239
- temp_file. push ( ".tmp" ) ;
240
-
241
- fs:: rename ( file, & temp_file) . context ( "Failed to rename compiled BPF object file" ) ?;
242
-
243
- let mut linker =
244
- libbpf_rs:: Linker :: new ( file) . context ( "Failed to instantiate libbpf object file linker" ) ?;
245
- linker
246
- . add_file ( temp_file)
247
- . context ( "Failed to add object file to BPF linker" ) ?;
248
- linker. link ( ) . context ( "Failed to link object file" ) ?;
249
- Ok ( ( ) )
250
- }
251
-
252
249
/// Concatenate a command and its arguments into a single string.
253
250
fn concat_command < C , A , S > ( command : C , args : A ) -> OsString
254
251
where
0 commit comments