Skip to content

Commit 2f6b907

Browse files
authored
Add comments about the size of the tiny executable. (#44)
1 parent 4fa8ab1 commit 2f6b907

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ include = ["src", "Cargo.toml", "COPYRIGHT", "LICENSE*", "/*.md"]
1515

1616
[dependencies]
1717
linux-raw-sys = { version = "0.4.7", default-features = false, features = ["general", "no_std", "elf"] }
18-
rustix = { version = "0.38.11", default-features = false }
18+
rustix = { version = "0.38.13", default-features = false }
1919
bitflags = "2.4.0"
2020
memoffset = { version = "0.9.0", optional = true }
2121
log = { version = "0.4.14", default-features = false, optional = true }

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ Origin can also be used on its own, in several different configurations:
5757

5858
- The [origin-start-lto example] is like origin-start, but builds with LTO.
5959

60-
- The [tiny example] is like origin-start, but builds with optimization flags
61-
and disables features to build a very small binary.
60+
- The [tiny example] is like origin-start, but builds with optimization flags,
61+
disables features, and adds an objcopy trick to produce a very small
62+
binary—408 bytes on x86-64!
6263

6364
## Fully static linking
6465

example-crates/tiny/README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,27 @@ and `.comment` sections:
99
objcopy -R .eh_frame -R .comment target/release/tiny even-smaller
1010
```
1111

12-
For details on the specific optimizations performed, see the options under
13-
`[profile.release]` and the use of `default-features = false`, in Cargo.toml,
14-
and the additional link flags passed in build.rs.
12+
On x86_64, this produces a executable with size 408 bytes.
13+
14+
How is this achieved?
1515

1616
## The optimizations
1717

18-
First, `origin` makes much of its functionality optional, so we add
19-
`default-features = false` to disable things like `.init_array`/`.fini_array`
20-
support, thread support, and other things. We only enable the features needed
21-
for our minimal test program:
18+
First, to make this example really simple, we change it from printing a message
19+
to just returning the number 42.
20+
21+
Next, `origin` makes much of its functionality optional, so we add
22+
`default-features = false` when declaring the `origin` dependency, to disable
23+
things like `.init_array`/`.fini_array` support, thread support, and other
24+
things that our simple example doesn't need. We only enable the features
25+
needed for our minimal test program:
2226

2327
```toml
2428
origin = { path = "../..", default-features = false, features = ["origin-program", "origin-start"] }
2529
```
2630

27-
Then, we enable several optimizations in the `#[profile.release]` section of
28-
Cargo.toml:
31+
Then, we add a `#[profile.release]` section to our Cargo.toml, to enable
32+
several optimizations:
2933

3034
```toml
3135
# Give the optimizer more lattitude to optimize and delete unneeded code.
@@ -194,6 +198,8 @@ Those first 3 instructions are origin's `_start` function. The next 5
194198
instructions are `origin::program::entry` and everything, including the user
195199
`main` function and the `exit_group` syscall inlined into it.
196200

201+
## Optimizations not done
202+
197203
In theory this code code be made even smaller.
198204

199205
That first `mov $rsp,%rdi` is moving the incoming stack pointer we got from the

0 commit comments

Comments
 (0)