Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Linux, what are these elf.h objects?

From elf.h in c (Linux):

#include <elf.h>

What do the following pointers stand for/do? I tried looking up the documentation but there is nothing written about it. I just saw some source code from other people using it to implement an ELF loader:

Elf32_Ehdr
Elf32_Phdr
Elf32_Shdr
Elf32_Sym

Thanks!

like image 450
Sefu Avatar asked Oct 22 '25 17:10

Sefu


2 Answers

They're just structs with a bunch of information. For example, Elf32_Ehdr is defined as such:

typedef struct {
    unsigned char   e_ident[EI_NIDENT]; /* ident bytes */
    Elf32_Half  e_type;         /* file type */
    Elf32_Half  e_machine;      /* target machine */
    Elf32_Word  e_version;      /* file version */
    Elf32_Addr  e_entry;        /* start address */
    Elf32_Off   e_phoff;        /* phdr file offset */
    Elf32_Off   e_shoff;        /* shdr file offset */
    Elf32_Word  e_flags;        /* file flags */
    Elf32_Half  e_ehsize;       /* sizeof ehdr */
    Elf32_Half  e_phentsize;        /* sizeof phdr */
    Elf32_Half  e_phnum;        /* number phdrs */
    Elf32_Half  e_shentsize;        /* sizeof shdr */
    Elf32_Half  e_shnum;        /* number shdrs */
    Elf32_Half  e_shstrndx;     /* shdr string index */
} Elf32_Ehdr;

You can find all of them defined here:

http://www.opensource.apple.com/source/dtrace/dtrace-78/sys/elf.h

like image 105
yamafontes Avatar answered Oct 25 '25 10:10

yamafontes


ELF is a very general format for binary files (e.g. executables, core dumps). You can read its specification as linked from the Wikipedia article.

After you understand what the format looks like, you'll know about the ELF header, program headers, section headers, and much more. The elf.h header file defines C structures which match those parts of the ELF structure. You can use those to write your own ELF files or load existing ones.

tl;dr: If you don't want to write your own binary executable files or parse core dumps manually, you don't need to know this.

like image 34
Kerrek SB Avatar answered Oct 25 '25 09:10

Kerrek SB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!