Skip to content

Commit 2e6833f

Browse files
committed
libbpf-cargo: Introduce dedicated link step to BpfObjBuilder
Building a BPF object file already invokes the libbpf linker in order to strip DWARF debug information from the resulting file. In the future we would like to link multiple files instead of only a single one, so refactor the code in such a way that we have a dedicated link step in BpfObjBuilder::build(). Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent f0fc826 commit 2e6833f

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

libbpf-cargo/src/build.rs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,35 @@ impl BpfObjBuilder {
153153

154154
/// Build a BPF object file.
155155
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+
156160
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)
158169
.with_context(|| format!("failed to compile `{}`", src.display()))?;
159170

171+
linker
172+
.add_file(tmp_dst)
173+
.context("failed to add object file to BPF linker")?;
174+
160175
Ok(output)
161176
})?;
162177

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")?;
169185

170186
Ok(output)
171187
}
@@ -230,25 +246,6 @@ fn extract_libbpf_headers_to_disk(_target_dir: &Path) -> Result<Option<PathBuf>>
230246
Ok(None)
231247
}
232248

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-
252249
/// Concatenate a command and its arguments into a single string.
253250
fn concat_command<C, A, S>(command: C, args: A) -> OsString
254251
where

0 commit comments

Comments
 (0)