-
Notifications
You must be signed in to change notification settings - Fork 19
Generating executable files from scratch
The first step in getting the operating system to execute arbitrary assembly is to figure out what types of executable files your operating system supports. Since I would be developing my compiler on Linux, the suitable Executable and Linkable Format (ELF) was chosen. There is a veritable wealth of information on the composition of ELF files, however since the ELF standard is very large and overarching it is not a simple matter to be able to pick and choose what is needed to get a bare minimum example working. As such, I am writing this as a compendium of all the research and piecing together that I did to be able to write YABFC.
Looking through the documentation for the system standard header elf.h, there is a few given structures that we can use to set up the executable file. For certain reasons I will be using a 64 bit version of an ELF executable rather than a 32 bit version. The first few lines for setup are pretty straight forward and rigorously defined:
Elf64_Ehdr ELFHeader; // Initialize the ELF header
ELFHeader.e_ident[EI_MAG0] = 0x7f; // Magic numbers
ELFHeader.e_ident[EI_MAG1] = 'E';
ELFHeader.e_ident[EI_MAG2] = 'L';
ELFHeader.e_ident[EI_MAG3] = 'F';
ELFHeader.e_ident[EI_CLASS] = ELFCLASS64; // 64 bit ELF
ELFHeader.e_ident[EI_DATA] = ELFDATA2LSB; // little-endian
ELFHeader.e_ident[EI_VERSION] = EV_CURRENT; // Current version
ELFHeader.e_ident[EI_OSABI] = ELFOSABI_SYSV; // UNIX System V ABI
ELFHeader.e_ident[EI_ABIVERSION] = 0x0; // ABI version needs to be 0
for (int i = EI_PAD; i < EI_NIDENT; i++) ELFHeader.e_ident[i] = 0x0; // Zero padding
ELFHeader.e_type = ET_EXEC; // Executable file
ELFHeader.e_machine = EM_X86_64; // AMD x86-64
ELFHeader.e_version = EV_CURRENT; // Current version
After this, things start to get a little more complicated. We need to configure the entry point of the program, program & section header table offsets as well as header sizes.
- http://www.skyfree.org/linux/references/ELF_Format.pdf - ELF file format specification
- https://github.com/corkami/pics/blob/master/binary/elf101/elf101-64.pdf - Visualization of how an ELF file goes together
- https://linux.die.net/man/5/elf - elf.h header file reference
- https://en.wikipedia.org/wiki/Executable_and_Linkable_Format - Wikipedia on ELF files
-
http://nullprogram.com/blog/2016/11/17/ - "A Magnetized Needle and a Steady Hand", making an ELF file with
echo
commands - https://defuse.ca/online-x86-assembler.htm - x86 intel-style instruction assembler