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

Update Rust to 1.75.0 #87

Closed
wants to merge 103 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
3216078
Auto merge of #11538 - Jarcho:proc_mac, r=dswij
bors Dec 11, 2023
6a30b10
Auto merge of #11756 - y21:issue11755, r=Manishearth
bors Nov 3, 2023
05f142f
Auto merge of #11760 - compiler-errors:escaping, r=Jarcho
bors Nov 12, 2023
7a76731
Auto merge of #11953 - Jarcho:issue_11952, r=Alexendoo
bors Dec 13, 2023
56d4e57
[SOL] Use Solana's LLVM fork
jackcmay Feb 13, 2019
1a0feaf
[SOL] Enable LLVM backend
jackcmay Feb 13, 2019
e401ce7
[SOL] Implement C abi modifier
jackcmay Feb 14, 2019
a313218
[SOL] Add custom build configuration
jackcmay Feb 14, 2019
c3b3675
[SOL] Don't build rustdoc to save time
jackcmay Feb 14, 2019
b16951c
[SOL] Custom README.md
jackcmay Feb 14, 2019
fad85b6
[SOL] Add build script
jackcmay Feb 20, 2019
5e45393
[SOL] Preserve lib dir, needed for sysroot building
jackcmay Feb 20, 2019
89c012c
[SOL] Allow older gcc, debian does not support a newer one yet
jackcmay Feb 20, 2019
d1f2075
[SOL] Reduce size of output binaries
jackcmay Feb 23, 2019
fe5552e
[SOL] Add BPF as a built-in target
jackcmay Feb 27, 2019
6581244
[SOL] Add bin dir later
jackcmay Feb 28, 2019
d2c9674
[SOL] Update README.md
jackcmay Mar 2, 2019
e6e81d9
[SOL] Script nit
jackcmay Jun 4, 2019
9e1748e
[SOL] BPF Spec use os=unknown
jackcmay Sep 19, 2019
64421ad
[SOL] Rust libstd requires atomic cas support
jackcmay Sep 25, 2019
2d98f90
[SOL] Updates from latest config.example.toml
jackcmay Dec 10, 2019
3a68e3d
[SOL] Updates from rust 1.39
jackcmay Dec 10, 2019
9a45ecd
[SOL] Allow all nightly options to help interactions with newer Cargo
jackcmay Apr 28, 2020
df47a16
[SOL] Update README.md
veryoddthomas Jul 26, 2020
5e1e60d
[SOL] Renames to match the upstream changes
Lichtso Sep 7, 2020
24ce202
[SOL] Disable eh_frame_hdr linker option
dmakarov Feb 10, 2021
9f94fb5
[SOL] Build llvm tools from llvm-project
jackcmay Feb 16, 2021
888bb9a
[SOL] Link llvm tools statically
jackcmay Feb 17, 2021
fa8962a
[SOL] Do not disable all builtins for BPF target
dmakarov Feb 17, 2021
a746528
[SOL] Adjust ABI implementation to pass 128-bit arguments by value
dmakarov Feb 26, 2021
d663636
[SOL] Adjust BPF target spec to upstream changes
dmakarov Feb 28, 2021
beb6b9b
[SOL] Adjust for building in tree the bpf target with std library
dmakarov Apr 2, 2021
c331c81
[SOL] Update build script to automatically build for correct targets
dmakarov Apr 2, 2021
1c1137d
[SOL] Use +solana feature when compiling for BPF target
dmakarov Apr 12, 2021
8d20de3
[SOL] Document source tree upgrade and bpf-tools release process
dmakarov Apr 12, 2021
7a81ac9
[SOL] Enable test harness for BPF programs
dmakarov May 7, 2021
5f8055b
[SOL] Fix github actions configuration
dmakarov May 11, 2021
3cfbaa8
[SOL] Add linker script and command line options to BPF target spec
dmakarov May 13, 2021
8fe60f5
[SOL] Fix BPF target spec after upgrade to rust 1.52.1
dmakarov May 17, 2021
e1328e6
[SOL] Adjust BPF customization after upgrading to rust 1.52.1
dmakarov May 18, 2021
4227476
[SOL] Strip full paths from source filenames referred in messages
dmakarov Jun 17, 2021
5e4de60
[SOL] Adjust BPF customization after upgrading to rust 1.53.0
dmakarov Jul 4, 2021
d53a8b9
[SOL] Make adjustments to work around CI failures
dmakarov Jul 5, 2021
93885ef
[SOL] Adjust BPF customization after upgrading to rust 1.54.0
dmakarov Aug 5, 2021
ffede79
[SOL] Disable channel check in CI as we use different branch names
dmakarov Aug 7, 2021
aa02f0a
[SOL] Add support for windows in build script
joncinque Sep 27, 2021
4df6425
[SOL] Adjust BPF customization after upgrading to rust 1.56.0
dmakarov Nov 3, 2021
1f448d3
[SOL] Enable build on apple silicon
dmakarov Nov 10, 2021
099824c
[SOL] Adjust test harness entrypoint function generation
dmakarov Nov 18, 2021
a360c60
[SOL] rework test harness codegen a bit
alessandrod Nov 26, 2021
7568b46
[SOL] libtest: disable should_panic tests and benchmarks
alessandrod Nov 26, 2021
ce29278
[SOL] tests: core: depend on dummy getrandom
alessandrod Nov 26, 2021
6dfab12
[SOL] tests: disable failing core tests
alessandrod Nov 26, 2021
c1f8648
[SOL] link using rust-lld
alessandrod Dec 1, 2021
9d4ae58
[SOL] CI: run tests for the bpfel-unknown-unknown target
alessandrod Dec 1, 2021
777d8dd
[SOL] CI: run tests on push and improve run times
alessandrod Dec 6, 2021
8a918d3
[SOL] CI: run push job on solana-* branches instead of master
alessandrod Dec 8, 2021
4e9523f
[SOL] Install rustup and use more recent cargo and rustc
dmakarov Dec 9, 2021
189a73d
[SOL] Rename bpf target to sbf
dmakarov Dec 9, 2021
430579c
[SOL] make all core tests (but atomics) pass
alessandrod Dec 16, 2021
5d567f9
[SOL] Fix missing SBF customizations
dmakarov Dec 22, 2021
d3e1438
[SOL] CI: build and test sbf target
dmakarov Dec 15, 2021
4495cdc
[SOL] CI: speed up PR runs
alessandrod Jan 3, 2022
3c88513
[SOL] remove stubs for atomics
alessandrod Jan 3, 2022
e87cbe1
[SOL] use codegen-units=1 by default for SBF
alessandrod Feb 4, 2022
ad6cca5
[SOL] build: add aarch64-linux target
lithdew Jan 24, 2022
42b1448
[SOL] Update run-bpf-tests revision in CI
dmakarov Feb 22, 2022
d726b06
[SOL] Adjust BPF customization after upgrading to rust 1.59.0
dmakarov Mar 30, 2022
d71b0f9
[SOL] Skip 'ensure the stable version number is correct' step in CI
dmakarov Apr 2, 2022
d5561ad
[SOL] Update run-bpf-tests revision in CI
dmakarov Apr 2, 2022
09ec2c6
[SOL] Temporary disable some failing core tests
dmakarov Apr 2, 2022
d41d4c0
[SOL] Don't create invalid string slices in stdout/stderr on Solana
brson Feb 18, 2022
63579e1
[SOL] tweak linker script
alessandrod Mar 25, 2022
83ad57f
[SOL] Update run-bpf-tests revision in CI
dmakarov Apr 27, 2022
4419577
[SOL] Update run-bpf-tests revision in CI
dmakarov May 2, 2022
00b04aa
[SOL] Prefer command line supplied linker script over embedded one
dmakarov May 2, 2022
09c7774
[SOL] Enable the std backtrace API
Aimeedeer Mar 26, 2022
6f3a31d
[SOL] Add os field to SBF target specification
dmakarov May 10, 2022
0fc64e0
[SOL] sbf: implement static-syscalls target feature
alessandrod May 7, 2022
ec32b55
[SOL] sbfv2: place the rodata segment at MM_PROGRAM_START
alessandrod May 14, 2022
5d88875
[SOL] sbfv2: output _RELATIVE relocations in a separate SHT_RELR section
alessandrod May 14, 2022
cf7807a
[SOL] tests: update docker image name
alessandrod May 17, 2022
b257692
[SOL] Update run-bpf-tests revision in CI
dmakarov Jul 7, 2022
288d618
[SOL] Update run-bpf-tests revision in CI
dmakarov Jul 9, 2022
985799f
[SOL] Adjust BPF customization after upgrading to rust 1.62.0
dmakarov Jul 21, 2022
7ffc576
[SOL] Update cargo-run-bpf-test revision for CI
dmakarov Aug 1, 2022
c4c41bb
[SOL] Update run-sbf-tests revision in CI
dmakarov Oct 14, 2022
6f98186
[SOL] switch to new SBF LLVM backend
dmakarov Oct 24, 2022
2ac4c61
[SOL] Adjust SBF customization after upgrading to rust 1.65.0
dmakarov Dec 5, 2022
19463cc
[SOL] Update run-sbf-tests revision in CI
dmakarov Dec 7, 2022
ca8abbe
[SOL] Temporarily patch BPF ELF file header with SBF machine code
dmakarov Dec 8, 2022
44697ca
[SOL] Update run-sbf-tests revision in CI
dmakarov Dec 13, 2022
09d7bfa
[SOL] Add lldb to enabled projects in [llvm] section of config.toml
dmakarov Jan 31, 2023
1af5a38
[SOL] Fix Ci error caused by insufficient git history fetched
dmakarov Feb 2, 2023
ee8e638
[SOL] Temporarily patch BPF ELF file header only for SBFv2 cpu
dmakarov Feb 20, 2023
77e22f6
[SOL] Update run-sbf-tests revision in CI
dmakarov Mar 5, 2023
866b0a9
[SOL] Adjust SBF customization after upgrading to rust 1.68.0
dmakarov Mar 21, 2023
94411ff
[SOL] Set default visibility of symbols in SBF binaries to hidden
dmakarov Aug 16, 2023
fe9b2b9
[SOL] Update run-sbf-tests revision in CI
dmakarov Aug 19, 2023
4a3a7c4
[SOL] Adjust SBF customization after upgrading to rust 1.72.0
dmakarov Sep 26, 2023
7f1fc21
[SOL] Fix CI failures
dmakarov Oct 19, 2023
87d0b56
[SOL] Adjust compiler after upgrade to 1.75.0
LucasSte Feb 2, 2024
933bd94
[SOL] Adjust flaky CI test
LucasSte Feb 7, 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
272 changes: 207 additions & 65 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/17.0-2023-12-14
url = https://github.com/solana-labs/llvm-project.git
branch = solana-rustc/17.0-2023-12-14
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 @@ -13,10 +13,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
35 changes: 26 additions & 9 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,7 @@ checksum = "55b672471b4e9f9e95499ea597ff64941a309b2cdbffcc46f2cc5e2d971fd335"
[[package]]
name = "compiler_builtins"
version = "0.1.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3b73c3443a5fd2438d7ba4853c64e4c8efc2404a9e28a9234cc2d5eebc6c242"
source = "git+https://github.com/solana-labs/compiler-builtins?tag=solana-tools-v1.40#81ef46f3fe1357095acdd089a700890fe4e13974"
dependencies = [
"cc",
"rustc-std-workspace-core",
Expand Down Expand Up @@ -781,6 +780,7 @@ checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
name = "core"
version = "0.0.0"
dependencies = [
"getrandom 0.1.16",
"rand",
"rand_xorshift",
]
Expand Down Expand Up @@ -1591,6 +1591,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.10"
Expand All @@ -1599,7 +1610,7 @@ checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [
"cfg-if",
"libc",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]

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

Expand All @@ -2544,7 +2555,7 @@ dependencies = [
"colored",
"ctrlc",
"env_logger 0.10.0",
"getrandom",
"getrandom 0.2.10",
"lazy_static",
"libc",
"libffi",
Expand Down Expand Up @@ -3175,7 +3186,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
"getrandom 0.2.10",
]

[[package]]
Expand Down Expand Up @@ -3240,7 +3251,7 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b"
dependencies = [
"getrandom",
"getrandom 0.2.10",
"redox_syscall 0.2.16",
"thiserror",
]
Expand Down Expand Up @@ -5137,7 +5148,7 @@ dependencies = [
"rustc-demangle",
"std_detect",
"unwind",
"wasi",
"wasi 0.11.0+wasi-snapshot-preview1",
]

[[package]]
Expand Down Expand Up @@ -5963,7 +5974,7 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d023da39d1fde5a8a3fe1f3e01ca9632ada0a63e9797de55a879d6e2236277be"
dependencies = [
"getrandom",
"getrandom 0.2.10",
]

[[package]]
Expand Down Expand Up @@ -6003,6 +6014,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 @@ -105,6 +105,7 @@ miniz_oxide.debug = 0
object.debug = 0

[patch.crates-io]
compiler_builtins = { git = "https://github.com/solana-labs/compiler-builtins", tag = "solana-tools-v1.40" }
# 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/solana-labs/llvm-project) of Rust's LLVM
fork.

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

[bpf-tools]: https://github.com/solana-labs/bpf-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/solana-labs/compiler-builtins
[llvm-project]: https://github.com/solana-labs/llvm-project

---

# The Rust Programming Language

[![Rust Community](https://img.shields.io/badge/Rust_Community%20-Join_us-brightgreen?style=plastic&logo=rust)](https://www.rust-lang.org/community)
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},bpfel-unknown-unknown,sbf-solana-solana
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_gcc/src/intrinsic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::{ArgAbiMethods, BuilderMethods, ConstMethods, IntrinsicCallMethods};
#[cfg(feature="master")]
use rustc_codegen_ssa::traits::{BaseTypeMethods, MiscMethods};
use rustc_codegen_ssa::traits::MiscMethods;
use rustc_codegen_ssa::traits::BaseTypeMethods;
use rustc_codegen_ssa::errors::InvalidMonomorphization;
use rustc_middle::bug;
use rustc_middle::ty::{self, Instance, Ty};
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 @@ -126,6 +122,10 @@ impl<'gcc, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
self.double_type
}

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.target.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.target.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 @@ -164,6 +164,10 @@ impl<'ll, 'tcx> BaseTypeMethods<'tcx> for CodegenCx<'ll, 'tcx> {
unsafe { llvm::LLVMDoubleTypeInContext(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 std::cell::OnceCell;
use std::collections::BTreeSet;
use std::ffi::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 @@ -310,6 +310,10 @@ fn link_rlib<'a>(
let (metadata, metadata_position) =
create_wrapper_file(sess, b".rmeta".to_vec(), 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 @@ -1798,14 +1802,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.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 @@ -1936,9 +1945,24 @@ fn add_linked_symbol_object(
if let Err(error) = result {
sess.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(&EM_SBF).unwrap();
}
} else {
sess.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 @@ -354,6 +354,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
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/back/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,10 @@ pub(crate) fn create_object_file(sess: &Session) -> Option<write::Object<'static
"avr" => Architecture::Avr,
"msp430" => Architecture::Msp430,
"hexagon" => Architecture::Hexagon,
"bpf" => Architecture::Bpf,
"loongarch64" => Architecture::LoongArch64,
"csky" => Architecture::Csky,
"bpf" => Architecture::Bpf,
"sbf" => Architecture::Bpf,
// Unsupported architecture.
_ => return None,
};
Expand Down
Loading
Loading