Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Rust 1.79.0 #95

Merged
merged 102 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
1ab791f
[SOL] Use Solana's LLVM fork
jackcmay Feb 13, 2019
ea500b9
[SOL] Enable LLVM backend
jackcmay Feb 13, 2019
eea8e13
[SOL] Implement C abi modifier
jackcmay Feb 14, 2019
68bcc1f
[SOL] Add custom build configuration
jackcmay Feb 14, 2019
13c035e
[SOL] Don't build rustdoc to save time
jackcmay Feb 14, 2019
51722bf
[SOL] Custom README.md
jackcmay Feb 14, 2019
899a9f9
[SOL] Add build script
jackcmay Feb 20, 2019
a0bfd54
[SOL] Preserve lib dir, needed for sysroot building
jackcmay Feb 20, 2019
59f5187
[SOL] Allow older gcc, debian does not support a newer one yet
jackcmay Feb 20, 2019
82c2463
[SOL] Reduce size of output binaries
jackcmay Feb 23, 2019
a4aa836
[SOL] Add BPF as a built-in target
jackcmay Feb 27, 2019
f638a7c
[SOL] Add bin dir later
jackcmay Feb 28, 2019
badecba
[SOL] Update README.md
jackcmay Mar 2, 2019
aa35f39
[SOL] Script nit
jackcmay Jun 4, 2019
a460379
[SOL] BPF Spec use os=unknown
jackcmay Sep 19, 2019
1b0f0e1
[SOL] Rust libstd requires atomic cas support
jackcmay Sep 25, 2019
cee0c4f
[SOL] Updates from latest config.example.toml
jackcmay Dec 10, 2019
77b8b70
[SOL] Updates from rust 1.39
jackcmay Dec 10, 2019
0e8b4d6
[SOL] Allow all nightly options to help interactions with newer Cargo
jackcmay Apr 28, 2020
06ca7aa
[SOL] Renames to match the upstream changes
Lichtso Sep 7, 2020
f060fec
[SOL] Disable eh_frame_hdr linker option
dmakarov Feb 10, 2021
5bff3ad
[SOL] Build llvm tools from llvm-project
jackcmay Feb 16, 2021
1684ebb
[SOL] Link llvm tools statically
jackcmay Feb 17, 2021
8aa7851
[SOL] Do not disable all builtins for BPF target
dmakarov Feb 17, 2021
4c05ddd
[SOL] Adjust ABI implementation to pass 128-bit arguments by value
dmakarov Feb 26, 2021
5a245d7
[SOL] Adjust BPF target spec to upstream changes
dmakarov Feb 28, 2021
f143372
[SOL] Adjust for building in tree the bpf target with std library
dmakarov Apr 2, 2021
021d27e
[SOL] Update build script to automatically build for correct targets
dmakarov Apr 2, 2021
7336f84
[SOL] Use +solana feature when compiling for BPF target
dmakarov Apr 12, 2021
9e6047a
[SOL] Document source tree upgrade and bpf-tools release process
dmakarov Apr 12, 2021
d5dd7a1
[SOL] Enable test harness for BPF programs
dmakarov May 7, 2021
c62359b
[SOL] Fix github actions configuration
dmakarov May 11, 2021
3b23ecb
[SOL] Add linker script and command line options to BPF target spec
dmakarov May 13, 2021
6dec20d
[SOL] Fix BPF target spec after upgrade to rust 1.52.1
dmakarov May 17, 2021
5e44201
[SOL] Adjust BPF customization after upgrading to rust 1.52.1
dmakarov May 18, 2021
fcbc266
[SOL] Strip full paths from source filenames referred in messages
dmakarov Jun 17, 2021
1ccc7b1
[SOL] Adjust BPF customization after upgrading to rust 1.53.0
dmakarov Jul 4, 2021
dd3db40
[SOL] Make adjustments to work around CI failures
dmakarov Jul 5, 2021
71d4f7a
[SOL] Adjust BPF customization after upgrading to rust 1.54.0
dmakarov Aug 5, 2021
78d103c
[SOL] Add support for windows in build script
joncinque Sep 27, 2021
eb278f2
[SOL] Adjust BPF customization after upgrading to rust 1.56.0
dmakarov Nov 3, 2021
7e9e760
[SOL] Enable build on apple silicon
dmakarov Nov 10, 2021
08a8b9d
[SOL] Adjust test harness entrypoint function generation
dmakarov Nov 18, 2021
bab7a77
[SOL] rework test harness codegen a bit
alessandrod Nov 26, 2021
01ed539
[SOL] libtest: disable should_panic tests and benchmarks
alessandrod Nov 26, 2021
d8920ae
[SOL] tests: core: depend on dummy getrandom
alessandrod Nov 26, 2021
286b53a
[SOL] tests: disable failing core tests
alessandrod Nov 26, 2021
981c2f2
[SOL] link using rust-lld
alessandrod Dec 1, 2021
1a6c45d
[SOL] CI: run tests for the bpfel-unknown-unknown target
alessandrod Dec 1, 2021
aac20d4
[SOL] CI: run tests on push and improve run times
alessandrod Dec 6, 2021
f4251df
[SOL] CI: run push job on solana-* branches instead of master
alessandrod Dec 8, 2021
9035bff
[SOL] Install rustup and use more recent cargo and rustc
dmakarov Dec 9, 2021
b10e11b
[SOL] Rename bpf target to sbf
dmakarov Dec 9, 2021
b353d19
[SOL] make all core tests (but atomics) pass
alessandrod Dec 16, 2021
e118ce5
[SOL] Fix missing SBF customizations
dmakarov Dec 22, 2021
9d737db
[SOL] CI: build and test sbf target
dmakarov Dec 15, 2021
7380bb3
[SOL] CI: speed up PR runs
alessandrod Jan 3, 2022
ff81d87
[SOL] remove stubs for atomics
alessandrod Jan 3, 2022
7d3ec29
[SOL] use codegen-units=1 by default for SBF
alessandrod Feb 4, 2022
6b4d73c
[SOL] build: add aarch64-linux target
lithdew Jan 24, 2022
e24c984
[SOL] Update run-bpf-tests revision in CI
dmakarov Feb 22, 2022
1c32993
[SOL] Adjust BPF customization after upgrading to rust 1.59.0
dmakarov Mar 30, 2022
ab57a1e
[SOL] Skip 'ensure the stable version number is correct' step in CI
dmakarov Apr 2, 2022
f258aa3
[SOL] Update run-bpf-tests revision in CI
dmakarov Apr 2, 2022
41b5f0d
[SOL] Temporary disable some failing core tests
dmakarov Apr 2, 2022
4c3cb35
[SOL] Don't create invalid string slices in stdout/stderr on Solana
brson Feb 18, 2022
24ef424
[SOL] tweak linker script
alessandrod Mar 25, 2022
765e817
[SOL] Update run-bpf-tests revision in CI
dmakarov Apr 27, 2022
e360415
[SOL] Update run-bpf-tests revision in CI
dmakarov May 2, 2022
31f7bb5
[SOL] Prefer command line supplied linker script over embedded one
dmakarov May 2, 2022
59abb26
[SOL] Enable the std backtrace API
Aimeedeer Mar 26, 2022
268f0a4
[SOL] Add os field to SBF target specification
dmakarov May 10, 2022
c5d4c59
[SOL] sbf: implement static-syscalls target feature
alessandrod May 7, 2022
581078e
[SOL] sbfv2: place the rodata segment at MM_PROGRAM_START
alessandrod May 14, 2022
e5bc74e
[SOL] sbfv2: output _RELATIVE relocations in a separate SHT_RELR section
alessandrod May 14, 2022
76d97a6
[SOL] tests: update docker image name
alessandrod May 17, 2022
bb115e3
[SOL] Update run-bpf-tests revision in CI
dmakarov Jul 7, 2022
0c3afbd
[SOL] Update run-bpf-tests revision in CI
dmakarov Jul 9, 2022
c849e64
[SOL] Adjust BPF customization after upgrading to rust 1.62.0
dmakarov Jul 21, 2022
8850f97
[SOL] Update cargo-run-bpf-test revision for CI
dmakarov Aug 1, 2022
bd86ce7
[SOL] Update run-sbf-tests revision in CI
dmakarov Oct 14, 2022
1faafe9
[SOL] switch to new SBF LLVM backend
dmakarov Oct 24, 2022
b653b6e
[SOL] Adjust SBF customization after upgrading to rust 1.65.0
dmakarov Dec 5, 2022
c352aef
[SOL] Update run-sbf-tests revision in CI
dmakarov Dec 7, 2022
fd6b43f
[SOL] Temporarily patch BPF ELF file header with SBF machine code
dmakarov Dec 8, 2022
ea22938
[SOL] Update run-sbf-tests revision in CI
dmakarov Dec 13, 2022
d304f36
[SOL] Add lldb to enabled projects in [llvm] section of config.toml
dmakarov Jan 31, 2023
243d475
[SOL] Fix Ci error caused by insufficient git history fetched
dmakarov Feb 2, 2023
1957926
[SOL] Temporarily patch BPF ELF file header only for SBFv2 cpu
dmakarov Feb 20, 2023
f6029a3
[SOL] Update run-sbf-tests revision in CI
dmakarov Mar 5, 2023
be84ef8
[SOL] Adjust SBF customization after upgrading to rust 1.68.0
dmakarov Mar 21, 2023
dc81fc5
[SOL] Set default visibility of symbols in SBF binaries to hidden
dmakarov Aug 16, 2023
f43c32a
[SOL] Update run-sbf-tests revision in CI
dmakarov Aug 19, 2023
586803a
[SOL] Adjust SBF customization after upgrading to rust 1.72.0
dmakarov Sep 26, 2023
8da5689
[SOL] Fix CI failures
dmakarov Oct 19, 2023
beecea3
[SOL] Adjust compiler after upgrade to 1.75.0
LucasSte Feb 2, 2024
6fd02e1
[SOL] Free up space in github runner
LucasSte Feb 20, 2024
d4c550d
[SOL] Update references to solana-labs redirecting them to anza-xyz
dmakarov Feb 23, 2024
b11627e
[SOL] Set 32 bits for SBF minimum enum size (#90)
LucasSte Feb 26, 2024
bcc71b1
[SOL] Update SBF call ABI (#91)
LucasSte Mar 13, 2024
e666af6
[SOL] Adjust compiler after 1.79 upgrade
LucasSte Jul 18, 2024
f2f6ad2
[SOL] Update CI scripts for 1.79
LucasSte Aug 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
643 changes: 631 additions & 12 deletions .github/workflows/ci.yml

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ Session.vim
!/tests/run-make/thumb-none-qemu/example/.cargo

## Configuration
/config.toml
/Makefile
config.mk
config.stamp
Expand Down
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
shallow = true
[submodule "src/llvm-project"]
path = src/llvm-project
url = https://github.com/rust-lang/llvm-project.git
branch = rustc/18.1-2024-05-19
url = https://github.com/anza-xyz/llvm-project.git
branch = solana-rustc/18.1-2024-05-19
shallow = true
[submodule "src/doc/embedded-book"]
path = src/doc/embedded-book
Expand Down
2 changes: 2 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ Files: compiler/*
tests/*
src/*
.github/*
build.sh
Cargo.lock
Cargo.toml
CODE_OF_CONDUCT.md
config.example.toml
config.toml
configure
CONTRIBUTING.md
COPYRIGHT
Expand Down
42 changes: 29 additions & 13 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,8 @@ checksum = "55b672471b4e9f9e95499ea597ff64941a309b2cdbffcc46f2cc5e2d971fd335"

[[package]]
name = "compiler_builtins"
version = "0.1.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f11973008a8cf741fe6d22f339eba21fd0ca81e2760a769ba8243ed6c21edd7e"
version = "0.1.112"
source = "git+https://github.com/anza-xyz/compiler-builtins?tag=solana-tools-v1.43#8e7bd0b5b32d58c96aca7c6d45d3989211e1a378"
dependencies = [
"cc",
"rustc-std-workspace-core",
Expand Down Expand Up @@ -791,6 +790,7 @@ dependencies = [
name = "core"
version = "0.0.0"
dependencies = [
"getrandom 0.1.16",
"rand",
"rand_xorshift",
]
Expand Down Expand Up @@ -1551,6 +1551,17 @@ dependencies = [
"unicode-width",
]

[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]

[[package]]
name = "getrandom"
version = "0.2.13"
Expand All @@ -1559,7 +1570,7 @@ checksum = "a06fddc2749e0528d2813f95e050e87e52c8cbbae56223b9babf73b3e53b0cc6"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]

[[package]]
Expand Down Expand Up @@ -2467,7 +2478,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys 0.48.0",
]

Expand All @@ -2489,7 +2500,7 @@ dependencies = [
"colored",
"ctrlc",
"directories",
"getrandom",
"getrandom 0.2.13",
"jemalloc-sys",
"lazy_static",
"libc",
Expand Down Expand Up @@ -3148,7 +3159,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
"getrandom 0.2.13",
]

[[package]]
Expand Down Expand Up @@ -3204,7 +3215,7 @@ version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891"
dependencies = [
"getrandom",
"getrandom 0.2.13",
"libredox",
"thiserror",
]
Expand Down Expand Up @@ -3360,9 +3371,8 @@ dependencies = [

[[package]]
name = "rustc-build-sysroot"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9bf37423495cd3a6a9ef8c75fc4566de0d26de0ab75f36f67a426e58df5768c"
version = "0.5.3"
source = "git+https://github.com/anza-xyz/rustc-build-sysroot?tag=solana-tools-v1.43#54b64222f01199bfc1fc7ac36f244cef4f573c7e"
dependencies = [
"anyhow",
"rustc_version",
Expand Down Expand Up @@ -5274,7 +5284,7 @@ dependencies = [
"rustc-demangle",
"std_detect",
"unwind",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]

[[package]]
Expand Down Expand Up @@ -6145,7 +6155,7 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a183cf7feeba97b4dd1c0d46788634f6221d87fa961b305bed08c851829efcc0"
dependencies = [
"getrandom",
"getrandom 0.2.13",
]

[[package]]
Expand Down Expand Up @@ -6185,6 +6195,12 @@ dependencies = [
"try-lock",
]

[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"

[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ debug = 0
strip = true

[patch.crates-io]
compiler_builtins = { git = "https://github.com/anza-xyz/compiler-builtins", tag = "solana-tools-v1.43" }
# See comments in `library/rustc-std-workspace-core/README.md` for what's going on
# here
rustc-std-workspace-core = { path = 'library/rustc-std-workspace-core' }
Expand Down
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,50 @@
# Fork of the Rust Programming Language that supports Berkley Packet Filter (BPF) targets

This fork of Rust contains changes that enable rustc to build BPF
modules. It depends on a customized
[fork](https://github.com/anza-xyz/llvm-project) of Rust's LLVM
fork.

Solana SDK does not depend directly on this repo. Instead [platform-tools]
builds and releases binary packages that the Solana SDK pulls in.

[platform-tools]: https://github.com/anza-xyz/platform-tools

BPF modules are built using target triple `bpfel-unknown-unknown`
which represents the little endian version of BPF. There is no
support for big endian at this time.

Upgrading the compiler and standard library source tree
-------------------------------------------------------

The source tree has two external dependencies
1. [compiler-builtins]
2. [llvm-project]

If any of the depencies is changed or this repository is updated to
make a new release of the bpf-tools, tag the dependencies, and this
repository with a new bpf-tools-v1.x tag, so that all components of
the released bpf-tools have the same tag, e.g. bpf-tools-v1.6. Thus,
release of every version of the bpf-tools is fully specified by the
release version.

The [llvm-project] is a submodule of this repository, therefore its
version is explicitly committed in this repository. However,
[compiler-builtins] is pulled in as a cargo package. Therefore, it is
necessary to update the `[patch.crates-io]` subsection of the
top-level `Cargo.toml` file, and specify which tag must be used to
pull the correct version of [compiler-builtins].

After this repository is tagged for a new release, update the
`bpf-tools/build.sh` in [bpf-tools] repository to pull the correct
version of the rust repository and make a new release tag in
[bpf-tools] repository.

[compiler-builtins]: https://github.com/anza-xyz/compiler-builtins
[llvm-project]: https://github.com/anza-xyz/llvm-project

---

<div align="center">
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/rust-lang/www.rust-lang.org/master/static/images/rust-social-wide-dark.svg">
Expand Down
23 changes: 23 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -ex

if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
echo "--llvm to rebuild llvm";
exit;
fi

unameOut="$(uname -s)-$(uname -m)"
case "${unameOut}" in
Linux-x86_64*) HOST_TRIPLE=x86_64-unknown-linux-gnu;;
Linux-aarch64*) HOST_TRIPLE=aarch64-unknown-linux-gnu;;
Darwin-x86_64*) HOST_TRIPLE=x86_64-apple-darwin;;
Darwin-arm64*) HOST_TRIPLE=aarch64-apple-darwin;;
MINGW*) HOST_TRIPLE=x86_64-pc-windows-msvc;;
*) HOST_TRIPLE=x86_64-unknown-linux-gnu
esac

if [ "$1" == "--llvm" ]; then
rm -f build/${HOST_TRIPLE}/llvm/llvm-finished-building;
fi
./x.py build --stage 1 --target ${HOST_TRIPLE},sbf-solana-solana
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_gcc/build_sysroot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolver = "2"

[dependencies]
core = { path = "./sysroot_src/library/core" }
compiler_builtins = "0.1"
compiler_builtins = { git = "https://github.com/anza-xyz/compiler-builtins", tag = "solana-tools-v1.43" }
alloc = { path = "./sysroot_src/library/alloc" }
std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] }
test = { path = "./sysroot_src/library/test" }
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_codegen_gcc/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
}
}

pub fn type_void(&self) -> Type<'gcc> {
self.context.new_type::<()>()
}

pub fn type_size_t(&self) -> Type<'gcc> {
self.context.new_type::<usize>()
}
Expand Down Expand Up @@ -136,6 +132,10 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
unimplemented!("f16_f128")
}

fn type_void(&self) -> Type<'gcc> {
self.context.new_type::<()>()
}

fn type_func(&self, params: &[Type<'gcc>], return_type: Type<'gcc>) -> Type<'gcc> {
self.context.new_function_pointer_type(None, return_type, params, false)
}
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_codegen_llvm/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,15 @@ pub(crate) unsafe fn codegen(
let llval = llvm::LLVMConstInt(i8, val as u64, False);
llvm::LLVMSetInitializer(ll_g, llval);

let name = NO_ALLOC_SHIM_IS_UNSTABLE;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
if tcx.sess.default_hidden_visibility() {
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
if tcx.sess.target.arch != "sbf" {
let name = NO_ALLOC_SHIM_IS_UNSTABLE;
let ll_g = llvm::LLVMRustGetOrInsertGlobal(llmod, name.as_ptr().cast(), name.len(), i8);
if tcx.sess.default_hidden_visibility() {
llvm::LLVMRustSetVisibility(ll_g, llvm::Visibility::Hidden);
}
let llval = llvm::LLVMConstInt(i8, 0, False);
llvm::LLVMSetInitializer(ll_g, llval);
}
let llval = llvm::LLVMConstInt(i8, 0, False);
llvm::LLVMSetInitializer(ll_g, llval);

if tcx.sess.opts.debuginfo != DebugInfo::None {
let dbg_cx = debuginfo::CodegenUnitDebugContext::new(llmod);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_codegen_llvm/src/type_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl<'ll, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
unsafe { llvm::LLVMFP128TypeInContext(self.llcx) }
}

fn type_void(&self) -> &'ll Type {
unsafe { llvm::LLVMVoidTypeInContext(self.llcx) }
}

fn type_func(&self, args: &[&'ll Type], ret: &'ll Type) -> &'ll Type {
unsafe { llvm::LLVMFunctionType(ret, args.as_ptr(), args.len() as c_uint, False) }
}
Expand Down
28 changes: 26 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use itertools::Itertools;
use std::collections::BTreeSet;
use std::ffi::{OsStr, OsString};
use std::fs::{read, File, OpenOptions};
use std::io::{BufWriter, Write};
use std::io::{prelude::*, BufWriter, SeekFrom, Write};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Output, Stdio};
Expand Down Expand Up @@ -311,6 +311,10 @@ fn link_rlib<'a>(
codegen_results.metadata.raw_data(),
);
let metadata = emit_wrapper_file(sess, &metadata, tmpdir, METADATA_FILENAME);
if sess.opts.cg.target_cpu.as_ref().unwrap_or(
&sess.target.cpu.as_ref().to_string()) == "sbfv2" {
patch_synthetic_object_file(sess, &metadata);
}
match metadata_position {
MetadataPosition::First => {
// Most of the time metadata in rlib files is wrapped in a "dummy" object
Expand Down Expand Up @@ -1870,14 +1874,19 @@ fn add_pre_link_args(cmd: &mut dyn Linker, sess: &Session, flavor: LinkerFlavor)
cmd.args(&sess.opts.unstable_opts.pre_link_args);
}

/// Add a link script embedded in the target, if applicable.
/// Add a link script embedded in the target, if applicable and not found in the command line.
fn add_link_script(cmd: &mut dyn Linker, sess: &Session, tmpdir: &Path, crate_type: CrateType) {
match (crate_type, &sess.target.link_script) {
(CrateType::Cdylib | CrateType::Executable, Some(script)) => {
if !sess.target.linker_flavor.is_gnu() {
sess.dcx().emit_fatal(errors::LinkScriptUnavailable);
}

if sess.opts.cg.link_args.contains(&String::from("--script"))
|| sess.opts.cg.link_args.contains(&String::from("-T")) {
return;
}

let file_name = ["rustc", &sess.target.llvm_target, "linkfile.ld"].join("-");

let path = tmpdir.join(file_name);
Expand Down Expand Up @@ -2008,9 +2017,24 @@ fn add_linked_symbol_object(
if let Err(error) = result {
sess.dcx().emit_fatal(errors::FailedToWrite { path, error });
}
if sess.opts.cg.target_cpu.as_ref().unwrap_or(
&sess.target.cpu.as_ref().to_string()) == "sbfv2" {
patch_synthetic_object_file(sess, &path);
}
cmd.add_object(&path);
}

fn patch_synthetic_object_file(sess: &Session, path: &PathBuf) {
const EM_SBF: [u8; 2] = [0x07, 0x01];
if let Ok(mut sf) = fs::OpenOptions::new().write(true).open(path) {
if let Ok(_) = sf.seek(SeekFrom::Start(0x12)) {
sf.write_all(&EM_SBF).unwrap();
}
} else {
sess.psess.dcx.fatal(format!("failed to patch {}", path.display()));
}
}

/// Add object files containing code from the current crate.
fn add_local_crate_regular_objects(cmd: &mut dyn Linker, codegen_results: &CodegenResults) {
for obj in codegen_results.modules.iter().filter_map(|m| m.object.as_ref()) {
Expand Down
11 changes: 11 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,17 @@ impl<'a> GccLinker<'a> {
self.linker_arg(&format!("--out-implib={}", (*implib).to_str().unwrap()));
}
}
} else if self.sess.target.arch == "bpf" || self.sess.target.arch == "sbf" {
if self.sess.opts.test {
self.linker_arg("--entry=main");
} else {
self.linker_arg("--entry=entrypoint");
}
if self.sess.opts.cg.target_cpu.as_ref().unwrap_or(&self.sess.target.cpu.as_ref().to_string()) == "sbfv2"
{
self.linker_arg("--section-start=.text=0x100000000");
self.linker_arg("--pack-dyn-relocs=relr");
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_ssa/src/back/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
"loongarch64" => (Architecture::LoongArch64, None),
"csky" => (Architecture::Csky, None),
"arm64ec" => (Architecture::Aarch64, Some(SubArchitecture::Arm64EC)),
"sbf" => (Architecture::Bpf, None),
// Unsupported architecture.
_ => return None,
};
Expand Down
Loading
Loading