Skip to content

Commit 42bf2fa

Browse files
dmakarovLucasSte
authored andcommitted
[SOL] Temporarily patch BPF ELF file header with SBF machine code
1 parent f36ae41 commit 42bf2fa

File tree

1 file changed

+17
-0
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+17
-0
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ fn link_rlib<'a>(
309309
codegen_results.metadata.raw_data(),
310310
);
311311
let metadata = emit_wrapper_file(sess, &metadata, tmpdir, METADATA_FILENAME);
312+
if sess.target.arch == "sbf" {
313+
patch_synthetic_object_file(sess, &metadata);
314+
}
312315
match metadata_position {
313316
MetadataPosition::First => {
314317
// Most of the time metadata in rlib files is wrapped in a "dummy" object
@@ -2110,9 +2113,23 @@ fn add_linked_symbol_object(
21102113
if let Err(error) = result {
21112114
sess.dcx().emit_fatal(errors::FailedToWrite { path, error });
21122115
}
2116+
if sess.target.arch == "sbf" {
2117+
patch_synthetic_object_file(sess, &path);
2118+
}
21132119
cmd.add_object(&path);
21142120
}
21152121

2122+
fn patch_synthetic_object_file(sess: &Session, path: &PathBuf) {
2123+
const EM_SBF: [u8; 2] = [0x07, 0x01];
2124+
if let Ok(mut sf) = fs::OpenOptions::new().write(true).open(path) {
2125+
if let Ok(_) = sf.seek(SeekFrom::Start(0x12)) {
2126+
sf.write(&EM_SBF).unwrap();
2127+
}
2128+
} else {
2129+
sess.fatal(&format!("failed to patch {}", path.display()));
2130+
}
2131+
}
2132+
21162133
/// Add object files containing code from the current crate.
21172134
fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
21182135
for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {

0 commit comments

Comments
 (0)