Skip to content

Commit 14edd8d

Browse files
committed
A simple elf sample code
1 parent 1fcde51 commit 14edd8d

File tree

8 files changed

+148
-4
lines changed

8 files changed

+148
-4
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
xmas
1+

Makefile

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
xmas: xmas.c xmas.ld Makefile
2-
gcc -T xmas.ld -fno-asynchronous-unwind-tables -fno-unwind-tables -nostdlib -no-pie -o $@ $<
2+
gcc -T xmas.ld -fno-asynchronous-unwind-tables -fno-unwind-tables -nostdlib -no-pie -o build/$@ $<
3+
4+
hello: hello.c Makefile
5+
gcc -o build/$@ $<
6+
7+
hello-nostd: hello-syscall.c Makefile
8+
gcc -Wl,-entry=startup -nostdlib -o build/$@ $<
9+
10+
hello-nostd-nopie: hello-syscall.c Makefile
11+
gcc -no-pie -nostdlib -Wl,-e,startup -Wl,--build-id=none -fcf-protection=none -fno-stack-protector -fno-asynchronous-unwind-tables -fno-unwind-tables -o build/$@ $<
12+
13+
hello-data: hello-data.c Makefile
14+
gcc -T hello.ld -no-pie -nostdlib -Wl,-e,startup -Wl,--build-id=none -fcf-protection=none -fno-stack-protector -fno-asynchronous-unwind-tables -fno-unwind-tables -o build/$@ $<

README.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This repo contains sample code for my two blog posts: [A Simple ELF](https://4zm
44

55
## XMAS Special
66

7-
Let's have some fun with linker scripts, ANSI codes and emojis!
7+
Let's have some fun with linker scripts, ANSI codes, and emojis!
88

99
![objdump](xmas.gif)
1010

@@ -25,6 +25,43 @@ SECTIONS
2525
ENTRY(xmas)
2626
```
2727

28-
If you want to dig in to some more details about how all this works, my other article "[A Simple ELF](https://4zm.org/2024/12/25/a-simple-elf.html)" might be helpful.
28+
If you want to dig in to some more details about how all this works, my blog post "[A Simple ELF](https://4zm.org/2024/12/25/a-simple-elf.html)" might be helpful.
2929

3030
What other fun things can you come up with? Multi-line (ie. multi segment) ASCII art? Or hiding text using other ANSI codes? Let me know!
31+
32+
## A Simple ELF
33+
34+
These are a few examples of peeling away complexity from my blog post [A Simple ELF](https://4zm.org/2024/12/25/a-simple-elf.html):
35+
36+
```
37+
$ make hello
38+
$ make hello-nostd
39+
$ make hello-nostd-nopie
40+
$ make hello-data
41+
```
42+
43+
In the end, we arrive at:
44+
45+
```
46+
$ objdump -t -h ./build/hello-data
47+
48+
./build/hello-data: file format elf64-x86-64
49+
50+
Sections:
51+
Idx Name Size VMA LMA File off Algn
52+
0 📜 .text 0000005c 00000000c0de0000 00000000c0de0000 00001000 2**0
53+
CONTENTS, ALLOC, LOAD, READONLY, CODE
54+
1 📦 .data 00000008 00000000feed0000 00000000feed0000 00003000 2**3
55+
CONTENTS, ALLOC, LOAD, DATA
56+
2 📁 .bss 00000008 00000000feed0008 00000000feed0008 00003008 2**3
57+
ALLOC
58+
3 🧊 .rodata 00000013 00000000dead0000 00000000dead0000 00002000 2**4
59+
CONTENTS, ALLOC, LOAD, READONLY, DATA
60+
SYMBOL TABLE:
61+
0000000000000000 l df *ABS* 0000000000000000 hello-data.c
62+
00000000c0de002f g F 📜 .text 000000000000002d startup
63+
00000000feed0000 g O 📦 .data 0000000000000008 length
64+
00000000dead0000 g O 🧊 .rodata 0000000000000013 message
65+
00000000c0de0000 g F 📜 .text 000000000000002f main
66+
00000000feed0008 g O 📁 .bss 0000000000000008 status
67+
```

build/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

hello-data.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const char message[] = "Hello Simplicity!\n"; // .rodata
2+
unsigned long length = sizeof(message) - 1; // .data
3+
unsigned long status; // .bss
4+
5+
int main() {
6+
// write(1, message, length)
7+
asm volatile("mov $1, %%rax\n" // write syscall number (0x01)
8+
"mov $1, %%rdi\n" // Stdout file descriptor (0x01)
9+
"mov %0, %%rsi\n" // Message buffer
10+
"mov %1, %%rdx\n" // Buffer length
11+
"syscall" // Make the syscall
12+
: // No output operands
13+
: "r"(message), "r"(length) // Input operands
14+
: "%rax", "%rdi", "%rsi", "%rdx" // Clobbered registers
15+
);
16+
17+
return 0;
18+
}
19+
20+
void startup() {
21+
status = main();
22+
23+
// exit(status)
24+
asm volatile("mov $0x3c, %%rax\n" // exit syscall number (0x3c)
25+
"mov %0, %%rdi\n" // exit status
26+
"syscall" // Make the syscall
27+
: // No output operands
28+
: "r"(status) // Input operands
29+
: "%rax", "%rdi" // Clobbered registers
30+
);
31+
}

hello-syscall.c

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
int main() {
2+
volatile const char message[] = "Hello Simplicity!\n";
3+
volatile const unsigned long length = sizeof(message) - 1;
4+
5+
// write(1, message, length)
6+
asm volatile("mov $1, %%rax\n" // write syscall number (0x01)
7+
"mov $1, %%rdi\n" // Stdout file descriptor (0x01)
8+
"mov %0, %%rsi\n" // Message buffer
9+
"mov %1, %%rdx\n" // Buffer length
10+
"syscall" // Make the syscall
11+
: // No output operands
12+
: "r"(message), "r"(length) // Input operands
13+
: "%rax", "%rdi", "%rsi", "%rdx" // Clobbered registers
14+
);
15+
16+
return 0;
17+
}
18+
19+
void startup() {
20+
volatile unsigned long status = main();
21+
22+
// exit(status)
23+
asm volatile("mov $0x3c, %%rax\n" // exit syscall number (0x3c)
24+
"mov %0, %%rdi\n" // exit status
25+
"syscall" // Make the syscall
26+
: // No output operands
27+
: "r"(status) // Input operands
28+
: "%rax", "%rdi" // Clobbered registers
29+
);
30+
}

hello.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdio.h>
2+
3+
int main() {
4+
printf("Hello Simplicity!\n");
5+
}

hello.ld

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
MEMORY {
2+
IRAM (rx) : ORIGIN = 0xC0DE0000, LENGTH = 0x1000
3+
RAM (rw) : ORIGIN = 0xFEED0000, LENGTH = 0x1000
4+
ROM (r) : ORIGIN = 0xDEAD0000, LENGTH = 0x1000
5+
}
6+
7+
SECTIONS
8+
{
9+
"📜 .text" : {
10+
*(.text*)
11+
} > IRAM
12+
13+
"📦 .data" : {
14+
*(.data*)
15+
} > RAM
16+
17+
"📁 .bss" : {
18+
*(.bss*)
19+
} > RAM
20+
21+
"🧊 .rodata" : {
22+
*(.rodata*)
23+
} > ROM
24+
25+
/DISCARD/ : { *(.comment) }
26+
}
27+
28+
ENTRY(startup)

0 commit comments

Comments
 (0)