Skip to content

Commit 90a993f

Browse files
authored
Add a version of the tiny example which prints hello world. (#137)
1 parent 8d39b0b commit 90a993f

5 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build]
2+
# Disable traps on unreachable code.
3+
rustflags = ["-Z", "trap-unreachable=no"]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[package]
2+
name = "tiny-hello"
3+
version = "0.0.0"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
# Origin can be depended on just like any other crate. For no_std, disable
9+
# the default features, and add the desired features.
10+
rustix = { version = "0.38.35", default-features = false, features = ["stdio"] }
11+
origin = { path = "../..", default-features = false, features = ["origin-start", "eh-personality-continue", "panic-handler-abort", "nightly", "optimize_for_size"] }
12+
13+
# This is just an example crate, and not part of the origin workspace.
14+
[workspace]
15+
16+
# Let's optimize for small size!
17+
[profile.release]
18+
# Give the optimizer more lattitude to optimize and delete unneeded code.
19+
lto = true
20+
# "abort" is smaller than "unwind".
21+
panic = "abort"
22+
# Tell the optimizer to optimize for size.
23+
opt-level = "z"
24+
# Delete the symbol table from the executable.
25+
strip = true
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This crate is similar to the [tiny example], except that it prints
2+
"Hello, world!".
3+
4+
[tiny example]: https://github.com/sunfishcode/origin/blob/main/example-crates/origin-start/README.md

example-crates/tiny-hello/build.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn main() {
2+
// Pass -nostartfiles to the linker. In the future this could be obviated
3+
// by a `no_entry` feature: <https://github.com/rust-lang/rfcs/pull/2735>
4+
println!("cargo:rustc-link-arg=-nostartfiles");
5+
6+
// The following options optimize for code size!
7+
8+
// Tell the linker to exclude the .eh_frame_hdr section.
9+
println!("cargo:rustc-link-arg=-Wl,--no-eh-frame-hdr");
10+
// Tell the linker to make the text and data readable and writable. This
11+
// allows them to occupy the same page.
12+
println!("cargo:rustc-link-arg=-Wl,-N");
13+
// Tell the linker to exclude the `.note.gnu.build-id` section.
14+
println!("cargo:rustc-link-arg=-Wl,--build-id=none");
15+
// Disable PIE, which adds some code size.
16+
println!("cargo:rustc-link-arg=-Wl,--no-pie");
17+
// Disable the `GNU-stack` segment, if we're using lld.
18+
println!("cargo:rustc-link-arg=-Wl,-z,nognustack");
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Going for minimal size!
2+
3+
#![no_std]
4+
#![no_main]
5+
6+
extern crate origin;
7+
8+
#[no_mangle]
9+
unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) -> i32 {
10+
let message = b"Hello, world!\n";
11+
let mut remaining = &message[..];
12+
while !remaining.is_empty() {
13+
match rustix::io::write(rustix::stdio::stdout(), message) {
14+
Ok(n) => remaining = &remaining[n as usize..],
15+
Err(rustix::io::Errno::INTR) => continue,
16+
Err(_) => origin::program::abort(),
17+
}
18+
}
19+
origin::program::exit_immediately(0);
20+
}

0 commit comments

Comments
 (0)