Skip to content

Commit fd6b43f

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

File tree

1 file changed

+18
-1
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+18
-1
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use itertools::Itertools;
4646
use std::collections::BTreeSet;
4747
use std::ffi::{OsStr, OsString};
4848
use std::fs::{read, File, OpenOptions};
49-
use std::io::{BufWriter, Write};
49+
use std::io::{prelude::*, BufWriter, SeekFrom, Write};
5050
use std::ops::Deref;
5151
use std::path::{Path, PathBuf};
5252
use std::process::{ExitStatus, Output, Stdio};
@@ -311,6 +311,9 @@ fn link_rlib<'a>(
311311
codegen_results.metadata.raw_data(),
312312
);
313313
let metadata = emit_wrapper_file(sess, &metadata, tmpdir, METADATA_FILENAME);
314+
if sess.target.arch == "sbf" {
315+
patch_synthetic_object_file(sess, &metadata);
316+
}
314317
match metadata_position {
315318
MetadataPosition::First => {
316319
// Most of the time metadata in rlib files is wrapped in a "dummy" object
@@ -2013,9 +2016,23 @@ fn add_linked_symbol_object(
20132016
if let Err(error) = result {
20142017
sess.dcx().emit_fatal(errors::FailedToWrite { path, error });
20152018
}
2019+
if sess.target.arch == "sbf" {
2020+
patch_synthetic_object_file(sess, &path);
2021+
}
20162022
cmd.add_object(&path);
20172023
}
20182024

2025+
fn patch_synthetic_object_file(sess: &Session, path: &PathBuf) {
2026+
const EM_SBF: [u8; 2] = [0x07, 0x01];
2027+
if let Ok(mut sf) = fs::OpenOptions::new().write(true).open(path) {
2028+
if let Ok(_) = sf.seek(SeekFrom::Start(0x12)) {
2029+
sf.write(&EM_SBF).unwrap();
2030+
}
2031+
} else {
2032+
sess.fatal(&format!("failed to patch {}", path.display()));
2033+
}
2034+
}
2035+
20192036
/// Add object files containing code from the current crate.
20202037
fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
20212038
for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {

0 commit comments

Comments
 (0)