Loading ELF PIE Executables | 32 Bit OS Dev (in C)

Channel:
Subscribers:
5,840
Published on ● Video Link: https://www.youtube.com/watch?v=AEST2jH36vQ



Duration: 2:58:03
614 views
31


Making an elf loader to replace running flat binary files. Currently it only loads the program headers for an ET_DYN type ELF PIE executable, as I didn't need the section headers, symbol table, or other relocation setups for the calculator (although they may be needed elsewhere).
This is also why you should use the correct compiler & linker flags first, or search it up online, to save hours of futile debugging. PE loading should go smoother as e.g. the sections include the RVA value already. Re-learning things on video is hard.

Links:
https://en.wikipedia.org/wiki/Executable_and_Linkable_Format
https://linux.die.net/man/5/elf

Much better elf loader, handles relocations as well:
https://www.mgaillard.fr/2021/04/15/load-elf-user-mode.html

This is what I looked at for phdr loading logic, particularly the elf64_image_size() and
load_kernel() functions in loader.c:
https://krinkinmu.github.io/2020/11/15/loading-elf-image.html
https://github.com/krinkinmu/efi/blob/master/loader.c

OS Dev Playlist:
https://www.youtube.com/playlist?list=PLT7NbkyNWaqZYHNLtOZ1MNxOt8myP5K0p

Git Repos:
https://git.sr.ht/~queso_fuego/quesos
https://github.com/queso-fuego/amateuros

Repo state at the start of this video:
git clone https://github.com/queso-fuego/amateuros
cd amateuros
git checkout b7260779e08ca0ec010eccd81b5ecda80b05117a

Repo state at the end of this video:
git checkout 0dafbb75b4425f44d317db541938906f61d6084b

Next video:
- Adding make and change directory commands, taking only slightly less time than this one.

Join the Community Discord: https://discord.gg/yKm4T89QFn

Contact:
https://queso_fuego.srht.site/contact.html

- Let me know if there's anything specific you'd like to see!

Questions about setup/software/etc.?
Check the FAQ: https://queso_fuego.srht.site/about.html

Outline:
0:00:00 Intro; fix rebooting after runtests command
0:13:16 Move malloc/free tests into runtests command
0:22:59 Change editor/calculator to ELF32 executables
0:35:26 Create elf.h header
0:40:00 Looking at elf header & program header fields
0:45:34 Start load_elf function in kernel
1:10:13 Print elf header info
1:18:50 Print program headers info
1:33:34 Get memory size of all phdrs
1:42:36 Load phdrs to buffer, fail at debugging
2:12:42 Fixes for elf loading, failing at debugging again
2:38:55 Find the answer on stackoverflow
2:41:38 Fix get_key() in calculator
2:54:08 Outro?

Music credits:
Winter Night by Sakura Girl | https://soundcloud.com/sakuragirl_official
Music promoted by https://www.chosic.com/free-music/all/
Creative Commons CC BY 3.0
https://creativecommons.org/licenses/by/3.0/

#osdev #elfloader #pie







Tags:
queso fuego
os dev
elf loading
elf loader
exe files
elf header
program header
buffers
operating systems
c programming
phdr
edhr
elf32