1
- # Using Rust to make a 145 -byte static AMD64 Linux binary
1
+ # Using Rust to make a 137 -byte static AMD64 Linux binary
2
2
3
3
Requires nightly Rust because it uses the ` sc ` crate to make direct
4
4
system calls.
5
5
6
6
` elf.s ` contains a custom ELF header, but no instructions.
7
- All of the machine code comes out of ` rustc ` . (Although all except one of the instructions that survive optimization * went in* as inline assembly.)
7
+ All of the machine code comes out of ` rustc ` .
8
+ (While most of the operations originate from inline assembly, LLVM replaces the instructions with more compact ones!)
8
9
9
10
```
10
11
$ ./build.sh
@@ -14,9 +15,9 @@ You have rustc 1.46.0-nightly (346aec9b0 2020-07-11)
14
15
Cloning into 'syscall.rs'...
15
16
Compiling sc v0.2.3 (file:///home/tormol/p/rust/tiny-rust-executable/syscall.rs)
16
17
17
- + rustc tinyrust.rs -O -C relocation-model=static -L syscall.rs/target/release
18
- ++ grep '.o$'
18
+ + rustc tinyrust.rs --crate-type lib -L syscall.rs/target/release -C relocation-model=static -O -C opt-level=z
19
19
++ ar t libtinyrust.rlib
20
+ ++ grep '.o$'
20
21
+ OBJECT=tinyrust.tinyrust.3a1fbbbh-cgu.0.rcgu.o
21
22
+ ar x libtinyrust.rlib tinyrust.tinyrust.3a1fbbbh-cgu.0.rcgu.o
22
23
+ objdump -dr tinyrust.tinyrust.3a1fbbbh-cgu.0.rcgu.o
@@ -27,23 +28,25 @@ tinyrust.tinyrust.3a1fbbbh-cgu.0.rcgu.o: file format elf64-x86-64
27
28
Disassembly of section .text.main:
28
29
29
30
0000000000000000 <main>:
30
- 0: bf 01 00 00 00 mov $0x1,%edi
31
- 5: be 08 00 40 00 mov $0x400008,%esi
32
- a: ba 07 00 00 00 mov $0x7,%edx
33
- f: b8 01 00 00 00 mov $0x1,%eax
34
- 14: 0f 05 syscall
35
- 16: 31 ff xor %edi,%edi
36
- 18: b8 3c 00 00 00 mov $0x3c,%eax
37
- 1d: 0f 05 syscall
38
- 1f: 0f 0b ud2
31
+ 0: 6a 07 pushq $0x7
32
+ 2: 5a pop %rdx
33
+ 3: 6a 01 pushq $0x1
34
+ 5: 58 pop %rax
35
+ 6: be 08 00 40 00 mov $0x400008,%esi
36
+ b: 48 89 c7 mov %rax,%rdi
37
+ e: 0f 05 syscall
38
+ 10: 6a 3c pushq $0x3c
39
+ 12: 58 pop %rax
40
+ 13: 31 ff xor %edi,%edi
41
+ 15: 0f 05 syscall
42
+ 17: 0f 0b ud2
39
43
+ echo
40
44
41
-
42
45
+ ld --gc-sections -e main -T script.ld -o payload tinyrust.tinyrust.3a1fbbbh-cgu.0.rcgu.o
43
46
+ objcopy -j combined -O binary payload payload.bin
44
47
++ nm --format=posix payload
45
- ++ awk '{print $3}'
46
48
++ grep '^main '
49
+ ++ awk '{print $3}'
47
50
+ ENTRY=0000000000400070
48
51
+ nasm -f bin -o tinyrust -D entry=0x0000000000400070 elf.s
49
52
+ chmod +x tinyrust
@@ -53,14 +56,13 @@ Disassembly of section .text.main:
53
56
00000020 38 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |8...............|
54
57
00000030 00 00 00 00 38 00 38 00 01 00 00 00 07 00 00 00 |....8.8.........|
55
58
00000040 00 00 00 00 00 00 00 00 00 00 40 00 00 00 00 00 |..........@.....|
56
- 00000050 00 00 40 00 00 00 00 00 91 00 00 00 00 00 00 00 |..@.............|
57
- 00000060 91 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 |................|
58
- 00000070 bf 01 00 00 00 be 08 00 40 00 ba 07 00 00 00 b8 |........@.......|
59
- 00000080 01 00 00 00 0f 05 31 ff b8 3c 00 00 00 0f 05 0f |......1..<......|
60
- 00000090 0b |.|
61
- 00000091
59
+ 00000050 00 00 40 00 00 00 00 00 89 00 00 00 00 00 00 00 |..@.............|
60
+ 00000060 89 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 |................|
61
+ 00000070 6a 07 5a 6a 01 58 be 08 00 40 00 48 89 c7 0f 05 |j.Zj.X...@.H....|
62
+ 00000080 6a 3c 58 31 ff 0f 05 0f 0b |j<X1.....|
63
+ 00000089
62
64
+ wc -c tinyrust
63
- 145 tinyrust
65
+ 137 tinyrust
64
66
65
67
$ ./tinyrust
66
68
Hello!
0 commit comments