Skip to content

Commit 02dbd81

Browse files
authored
Add a complete example of how to create an executable for our kernel (#104)
* Add a complete example of how to create an executable for our kernel * Update 02_Loading_And_Running.md
1 parent edb122a commit 02dbd81

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

09_Loading_Elf/02_Loading_And_Running.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Then from the other fields that need validation (that area not in the `e_ident`
4747

4848
Be aware that most of the variables and their values have a specific naming convention, for more information refer to the ELF specs.
4949

50-
Beware that some compilers when generating a simple executable are not using the `ET_EXEC` value, but it could be of the type `ET_REL` (value 1), to obtain an executable we need to link it using a linker. For example if we generated the executable: `example.elf` with `ET_REL` type, we can use `ld` (or another equivalent linker):
50+
Another thing to be aware is that some compilers when generating a simple executable are not using the `ET_EXEC` value, but it could be of the type `ET_REL` (value 1), to obtain an executable we need to link it using a linker. For example if we generated the executable: `example.elf` with `ET_REL` type, we can use `ld` (or another equivalent linker):
5151

5252
```sh
5353
ld -o example.o example.elf
@@ -62,6 +62,41 @@ readelf -e example.elf
6262

6363
Will print out all the executable information, including the type.
6464

65+
### Example: Creating a simple program for our kernel
66+
67+
For this example, let's create a very basic assembly program, that is a simple infinite loop:
68+
69+
```
70+
extern loop
71+
[bits 64]
72+
loop:
73+
jmp loop
74+
75+
```
76+
77+
The code, as we can expect is pretty simple, and self-explanatory, it declares a `loop` function, and mark it as global using the `extern` keyword..
78+
79+
The above code now can be compiled with nasm:
80+
81+
```sh
82+
nasm -g -f elf64 -F dwarf example_file.s -o example_file.o
83+
```
84+
85+
Where:
86+
* `-f elf64` is the output format (in our case we use elf64, but this depend on the target architecture).
87+
* `-g` enable debug symbols
88+
* `-F dwarf` is the debug symbols format (for elf64 we use dwarf, but again it can depends on the target architecture).
89+
90+
91+
The last step is to use a linker to link the file, in this example we are going to use `ld`:
92+
93+
```sh
94+
ld -g example_file.o -o example_file.elf -e loop
95+
```
96+
97+
Where `-g` is a parameter that instructs the linker to include the debugging symbols, and `-e loop` instructs the linker to look for the symbol called `loop` as entry point of the program.
98+
99+
Now the program is ready to be loaded by our kernel, either as a bootloader module or a file on a filesystem (or any other way that allow the kernel to reach this executable).
65100

66101

67102
## Caveats

0 commit comments

Comments
 (0)