ckernelelf

How would I be able to parse ELF format for my kernel in C?


I am trying to create a kernel in C, and I was wondering how I could parse ELF code. Any suggestions on where i can learn how to make an ELF parser?


Solution

  • First of all, you need to learn how an ELF file works.

    Wikipedia has a great article on ELF files. All the ELF format/layout can be found here: https://en.wikipedia.org/wiki/Executable_and_Linkable_Format#File_layout

    You can also use a tool called readelf which is included in GNU Binutils, so it should be preinstalled if you're using linux. Although readelf isn't a parser, it's more of an elf information reader. It could be useful for the purpose of reference to see if you're on the right track.

    Alternatively, you can use objdump which does the same too.

    If you're using C/C++, there's a header file called elf.h that handles elf files for you. I haven't read all the documentation on the linux manual page, but it should be useful in some way for your case.

    ELF files are NOT designed to be ran on obscure/new/hobby kernel architectures.

    Originally, ELF files were designed to be ran on UNIX systems or systems that are similar in nature. In the ELF header, there's a value called the OSABI (at the 7th byte in the beginning) which specifies the ABI of the ELF file. The ELF standard supports a handful of systems in the current version. Here's the full table:

    value ABI
    0x00 System V
    0x01 HP-UX
    0x02 NetBSD
    0x03 Linux
    0x04 GNU Hurd
    0x06 Solaris
    0x07 AIX
    0x08 IRIX
    0x09 FreeBSD
    0x0A Tru64
    0x0B Novell Modesto
    0x0C OpenBSD
    0x0D OpenVMS
    0x0E NonStop Kernel
    0x0F AROS
    0x10 Fenix OS
    0x11 CloudABI
    0x12 Stratus Technologies OpenVOS

    However, this doesn't mean it's impossible to make it run on your own kernel, but it would be notoriously hard to do that on an entirely different system that's not supported by the ELF standard. I haven't designed any kernels yet, let alone have a great enough knowledge in how kernels work, but there are a few value ranges in the ELF binary that supports custom systems such as yours. This includes fields such as the object file type in the header, the segment type at the program header, and finally the header type and section attributes in the segment header.

    You could create a small virtual machine that would simulate an ELF executable and make it communicate with your kernel, but this should be your last option if nothing else works.