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 
31 typedef uint32_t Elf_Addr;
32 typedef uint32_t Elf_Off;
33 typedef uint16_t Elf_Half;
34 typedef uint32_t Elf_Word;
35 typedef 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.
39 typedef uint32_t Elf_Xword;
40 typedef int32_t Elf_Sxword;
41 
42 typedef struct ElfHeader_t
43 {
44  uint8_t ident[16];
45  Elf_Half type;
46  Elf_Half machine;
47  Elf_Word version;
48  Elf_Addr entry;
49  Elf_Off phoff;
50  Elf_Off shoff;
51  Elf_Word flags;
52  Elf_Half ehsize;
53  Elf_Half phentsize;
54  Elf_Half phnum;
55  Elf_Half shentsize;
56  Elf_Half shnum;
57  Elf_Half shstrndx;
58 } __attribute__((packed)) ElfHeader_t;
59 
60 typedef struct ElfSectionHeader_t
61 {
62  Elf_Word name;
63  Elf_Word type;
64  Elf_Xword flags;
65  Elf_Addr addr;
66  Elf_Off offset;
67  Elf_Xword size;
68  Elf_Word link;
69  Elf_Word info;
70  Elf_Xword addralign;
71  Elf_Xword entsize;
73 
74 typedef struct ElfSymbol_t
75 {
76  Elf_Word name;
77  Elf_Addr value;
78  Elf_Xword size;
79  uint8_t info;
80  uint8_t other;
81  Elf_Half shndx;
82 } __attribute__((packed)) ElfSymbol_t;
83 
84 typedef struct ElfRel_t
85 {
86  Elf_Addr offset;
87  Elf_Xword info;
88 } __attribute__((packed)) ElfRel_t;
89 
90 typedef struct Elf_t
91 {
92  uint8_t *buffer;
93  ElfHeader_t *header;
94  ElfSectionHeader_t *section_headers;
95  ElfSymbol_t *symtab;
96  size_t symtabsz;
97  char *strtab;
98  size_t strtabsz;
99 } Elf_t;
100 
101 #define SYM_PTR(elf, sym) \
102  ((uint32_t *) &( \
103  elf->buffer[sym->value + elf->section_headers[sym->shndx].offset]))
104 
105 Elf_t *elf_create(uint8_t *buffer, size_t len);
106 ElfSymbol_t *elf_get_symbol(Elf_t *elf, const char *name);
107 uint8_t *elf_relptr(Elf_t *elf, ElfSymbol_t *sym, uint32_t ptr);
108 
109 #endif
Definition: elf.h:84
Definition: elf.h:90