The Pedigree Project 0.1
elf.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef ELF_H
21#define ELF_H
22
23#include <stddef.h>
24#include <stdint.h>
25
26#define SHT_REL 0x9
27#define R_SYM(val) ((val) >> 8)
28#define R_TYPE(val) ((val) & 0xff)
29#define ST_TYPE(i) ((i) & 0xf)
30
31typedef uint32_t Elf_Addr;
32typedef uint32_t Elf_Off;
33typedef uint16_t Elf_Half;
34typedef uint32_t Elf_Word;
35typedef int32_t Elf_Sword;
36
37// We define the Xword and Sxword types for ELF32 even though they don't exist
38// in the spec for forwards compatibility with ELF64.
39typedef uint32_t Elf_Xword;
40typedef int32_t Elf_Sxword;
41
42typedef struct ElfHeader_t {
43 uint8_t ident[16];
44 Elf_Half type;
45 Elf_Half machine;
46 Elf_Word version;
47 Elf_Addr entry;
48 Elf_Off phoff;
49 Elf_Off shoff;
50 Elf_Word flags;
51 Elf_Half ehsize;
52 Elf_Half phentsize;
53 Elf_Half phnum;
54 Elf_Half shentsize;
55 Elf_Half shnum;
56 Elf_Half shstrndx;
57} __attribute__((packed)) ElfHeader_t;
58
59typedef struct ElfSectionHeader_t {
60 Elf_Word name;
61 Elf_Word type;
62 Elf_Xword flags;
63 Elf_Addr addr;
64 Elf_Off offset;
65 Elf_Xword size;
66 Elf_Word link;
67 Elf_Word info;
68 Elf_Xword addralign;
69 Elf_Xword entsize;
71
72typedef struct ElfSymbol_t {
73 Elf_Word name;
74 Elf_Addr value;
75 Elf_Xword size;
76 uint8_t info;
77 uint8_t other;
78 Elf_Half shndx;
79} __attribute__((packed)) ElfSymbol_t;
80
81typedef struct ElfRel_t {
82 Elf_Addr offset;
83 Elf_Xword info;
84} __attribute__((packed)) ElfRel_t;
85
86typedef struct Elf_t {
87 uint8_t* buffer;
88 ElfHeader_t* header;
89 ElfSectionHeader_t* section_headers;
90 ElfSymbol_t* symtab;
91 size_t symtabsz;
92 char* strtab;
93 size_t strtabsz;
94} Elf_t;
95
96#define SYM_PTR(elf, sym) \
97 ((uint32_t*)&(elf->buffer[sym->value + elf->section_headers[sym->shndx].offset]))
98
99Elf_t* elf_create(uint8_t* buffer, size_t len);
100ElfSymbol_t* elf_get_symbol(Elf_t* elf, const char* name);
101uint8_t* elf_relptr(Elf_t* elf, ElfSymbol_t* sym, uint32_t ptr);
102
103#endif
Definition elf.h:81
Definition elf.h:86