aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjohannst <johannes.stoelp@gmail.com>2021-04-17 23:43:01 +0200
committerjohannst <johannes.stoelp@gmail.com>2021-04-17 23:43:01 +0200
commit0358652ff40546877c63fee8ca272db76b94a37d (patch)
tree7f5c5958bd2241bce276ef02d07e24c9133a8320
parentb3f93e87f773c0e9099f256c78c1485fd7ce1dce (diff)
downloaddynld-0358652ff40546877c63fee8ca272db76b94a37d.tar.gz
dynld-0358652ff40546877c63fee8ca272db76b94a37d.zip
elf: add ehdr, sym, reloc
-rw-r--r--lib/include/elf.h114
1 files changed, 104 insertions, 10 deletions
diff --git a/lib/include/elf.h b/lib/include/elf.h
index a0fe6f7..4856c79 100644
--- a/lib/include/elf.h
+++ b/lib/include/elf.h
@@ -4,6 +4,51 @@
#include <stdint.h>
+/// ----------
+/// ELF Header
+/// ----------
+
+// Index into `ident`.
+#define EI_MAG0 0
+#define EI_MAG1 1
+#define EI_MAG2 2
+#define EI_MAG3 3
+#define EI_CLASS 4
+#define EI_DATA 5
+#define EI_OSABI 7
+
+// indent[EI_CLASS]
+#define ELFCLASS32 1
+#define ELFCLASS64 2
+
+// indent[EI_CLASS]
+#define ELFDATA2LSB 1
+#define ELFDATA2MSB 2
+
+// indent[EI_OSABI]
+#define ELFOSABI_SYSV 0
+
+// Objec file `type`.
+#define ET_NONE 0
+#define ET_DYN 3
+
+typedef struct {
+ uint8_t ident[16]; // ELF identification.
+ uint16_t type; // Object file type.
+ uint16_t machine; // Machine type.
+ uint32_t version; // Object file version.
+ uint64_t entry; // Entrypoint address.
+ uint64_t phoff; // Program header file offset.
+ uint64_t shoff; // Section header file offset.
+ uint32_t flags; // Processor specific flags.
+ uint16_t ehsize; // ELF header size.
+ uint16_t phentsize; // Program header entry size.
+ uint16_t phnum; // Number of program header entries.
+ uint16_t shentsize; // Section header entry size.
+ uint16_t shnum; // Number of section header entries.
+ uint16_t shstrndx; // Section name string table index.
+} Elf64Ehdr;
+
/// --------------
/// Program Header
/// --------------
@@ -39,16 +84,16 @@ typedef struct {
/// Dynamic Section
/// ---------------
-#define DT_NULL 0 /* [ignored] Marks end of dynamic section */
-#define DT_NEEDED 1 /* [val] Name of needed library */
-#define DT_PLTRELSZ 2 /* [val] Size in bytes of PLT relocs */
-#define DT_PLTGOT 3 /* [ptr] Processor defined value */
-#define DT_HASH 4 /* [ptr] Address of symbol hash table */
-#define DT_STRTAB 5 /* [ptr] Address of string table */
-#define DT_SYMTAB 6 /* [ptr] Address of symbol table */
-#define DT_RELA 7 /* [ptr] Address of Rela relocs */
-#define DT_RELASZ 8 /* [val] Total size of Rela relocs */
-#define DT_RELAENT 9 /* [val] Size of one Rela reloc */
+#define DT_NULL 0 /* [ignored] Marks end of dynamic section */
+#define DT_NEEDED 1 /* [val] Name of needed library */
+#define DT_PLTRELSZ 2 /* [val] Size in bytes of PLT relocs */
+#define DT_PLTGOT 3 /* [ptr] Processor defined value */
+#define DT_HASH 4 /* [ptr] Address of symbol hash table */
+#define DT_STRTAB 5 /* [ptr] Address of string table */
+#define DT_SYMTAB 6 /* [ptr] Address of symbol table */
+#define DT_RELA 7 /* [ptr] Address of Rela relocs */
+#define DT_RELASZ 8 /* [val] Total size of Rela relocs */
+#define DT_RELAENT 9 /* [val] Size of one Rela reloc */
#define DT_STRSZ 10 /* [val] Size of string table */
#define DT_SYMENT 11 /* [val] Size of one symbol table entry */
#define DT_INIT 12 /* [ptr] Address of init function */
@@ -73,3 +118,52 @@ typedef struct {
void* ptr;
};
} Elf64Dyn;
+
+/// ------------
+/// Symbol Entry
+/// ------------
+
+typedef struct {
+ uint32_t name; // Symbol name (index into string table).
+ uint8_t info; // Symbol Binding bits[7..4] + Symbol Type bits[3..0].
+ uint8_t other; // Reserved.
+ uint16_t shndx; // Section table index.
+ uint64_t value; //
+ uint64_t size; //
+} Elf64Sym;
+
+#define ELF64_ST_BIND(i) ((i) >> 4)
+#define ELF64_ST_TYPE(i) ((i)&0xf)
+
+// Symbold Bindings.
+#define STB_GLOBAL 1 /* Global symbol, visible to all object files. */
+#define STB_WEAK 2 /* Global scope, but with lower precedence than global symbols. */
+
+// Symbol Types.
+#define STT_NOTYPE 0 /* No type. */
+#define STT_FUNC 2 /* Function entry point. */
+
+// Special Section Indicies.
+#define SHN_UNDEF 0 /* Undefined section. */
+#define SHN_ABS 0xff1 /* Indicates an absolute value. */
+
+/// -----------------
+/// Relocations Entry
+/// -----------------
+
+typedef struct {
+ uint64_t offset; // Virtual address of the storage unit affected by the relocation.
+ uint64_t info; // Symbol table index + relocation type.
+} Elf64Rel;
+
+typedef struct {
+ uint64_t offset; // Virtual address of the storage unit affected by the relocation.
+ uint64_t info; // Symbol table index + relocation type.
+ int64_t addend; // Constant value used to compute the relocation value.
+} Elf64Rela;
+
+#define ELF64_R_SYM(i) ((i) >> 32)
+#define ELF64_R_TYPE(i) ((i)&0xffffffffL)
+
+// x86_64 relocation types.
+#define R_X86_64_JUMP_SLOT 7 /* Address affected by relocation: `offset` (+ base) */