Day 42: How to run a simple ELF executable, from scratch (I don't know)
source link: https://jvns.ca/blog/2013/12/13/day-42-how-to-run-an-elf-executable-i-dont-know/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Day 42: How to run a simple ELF executable, from scratch (I don't know)
I want to compile a 32-bit “Hello, world!” statically-linked ELF binary for Linux, and try to run it in my operating system. I’m trying to understand what I’ll have to do. The goal is to get everything just barely working, so that it will print the string to the screen and not crash the whole system.
I asked a question about this a little while ago, and got lots of helpful responses. Now I need to make it a bit more concrete, though.
I’ve discovered that this “set stuff up so that a program can run” business is called loading, and what I’m doing is writing a loader. Sweet.
Right now I’m doing this before implementing paging and virtual memory and not after, because this seems more fun than virtual memory for now. If this is a very bad idea, I would like to know.
Things I’ll have to do
- Compile “hello-c” for a 32-bit OS, with
gcc -m32 -static
- Parse the ELF headers (using the wikipedia article, and this great picture as a reference)
- Add an interrupt handler for
int 80
(orsysenter
, we’ll see!), so that I can handle system calls. - Write the actual system call implementations, as few as possible.
- Find
e_entry
, the entry point of the binary. - Initialize registers? How?
- Change something, so that the memory addresses in the binary aren’t broken. Maybe? I still don’t 100% understand this.
- Finally: Jump to
_start
, the memory address ine_entry
. I want to just dojmp address
here. Then my program will run?
Things I won’t have to do (yet)
- Read the file into memory – I’m planning to just keep the file as a bunch of bytes in RAM to start, or possibly have a simple RAM filesystem later.
- Security, and making sure process can’t trample on each others’ address spaces.
- Scheduling.
- Set up a special heap for the process. I’m just going to allocate everyone’s memory in the same part of physical memory for now. And never free. Yeah.
Questions I have
- Do I need to make sure my binary is position independent?
- Do I need to implement virtual memory & paging? (I think not)
- Do I need to have a separate “user space” for the code to run in, or will it run in kernel space? (I think it will run in kernel space)
- Do I need to change something in the GOT and/or PLT to make the addresses work right? (I think yes? maybe?) Is there even a PLT in a static executable, or is that just for dynamic linking? Eep. Hmm.
Recommend
-
130
Current release Note that it still requires Microsoft Visual Studio 2015 or newer to be installed. Moving to clang/llvm soon :) Porting Xbox360 executables to Windows
-
55
statik statik allows you to embed a directory of static files into your Go binary to be later served from an http.FileSystem. Is this a crazy idea? No, not necessarily. If you're building a tool that has a Web component, you typica...
-
70
README.md AMaCC = Another Mini ARM C Compiler Introduction AMaCC is built from scratch, supporting ARM architecture. There are 3 execution modes AMaCC implements: Just-in-Tim...
-
52
README.md oooo oooo `888 `888 oooo ooo 888 .oo. .ooooo. .ooooo. 888 oooo `88...
-
30
Latest version of file 'ext/misc/appendvfs.c': Fileext/misc/appendvfs.c — part of check-in[bfbeffab] at2018-01-17 13:15:23 on branchtrunk — Fix harmless compiler warnings, mostly unused parameters for UDF...
-
23
Using a Genetic Algorithm for Optimizing A Staff Planning
-
12
Developing a simple e-commerce application from scratch to production using SpringBootSivaLabs - Developing a simple e-commerce application from scratch to production using SpringBoot
-
12
August 26, 2021 Writing a simple JSON library from scratch: a tour through modern C++ Modern C++ has a lot of cool features. Move semant...
-
8
-
4
This article was published as a part of the Data Science Blogathon. Introduction
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK