Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/ianos/elfload/elfload.h
3694 views
1
/*
2
* Copyright © 2018, M4xw
3
* Copyright © 2014, Owen Shepherd
4
*
5
* Permission to use, copy, modify, and/or distribute this software for any
6
* purpose with or without fee is hereby granted, provided that the above
7
* copyright notice appear in all copies.
8
*
9
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
* PERFORMANCE OF THIS SOFTWARE.
16
*/
17
18
#ifndef ELFLOAD_H
19
#define ELFLOAD_H
20
#include <stddef.h>
21
22
#include "elfarch.h"
23
#include "elf.h"
24
25
#ifdef DEBUG
26
#include <gfx_utils.h>
27
#define EL_DEBUG(format, ...) \
28
gfx_printf(format __VA_OPT__(, ) __VA_ARGS__)
29
#else
30
#define EL_DEBUG(...) \
31
do \
32
{ \
33
} while (0)
34
#endif
35
36
typedef enum
37
{
38
EL_OK = 0,
39
40
EL_EIO,
41
EL_ENOMEM,
42
43
EL_NOTELF,
44
EL_WRONGBITS,
45
EL_WRONGENDIAN,
46
EL_WRONGARCH,
47
EL_WRONGOS,
48
EL_NOTEXEC,
49
EL_NODYN,
50
EL_BADREL,
51
52
} el_status;
53
54
typedef struct el_ctx
55
{
56
el_status (*pread)(struct el_ctx *ctx, void *dest, size_t nb, size_t offset);
57
58
/* base_load_* -> address we are actually going to load at
59
*/
60
Elf_Addr
61
base_load_paddr,
62
base_load_vaddr;
63
64
/* original memory of binary */
65
Elf_Addr eaddr;
66
67
/* size in memory of binary */
68
Elf_Addr memsz;
69
70
/* required alignment */
71
Elf_Addr align;
72
73
/* ELF header */
74
Elf_Ehdr ehdr;
75
76
// Section Header Str Table
77
Elf_Shdr shstr;
78
Elf_Shdr symtab;
79
80
/* Offset of dynamic table (0 if not ET_DYN) */
81
Elf_Off dynoff;
82
/* Size of dynamic table (0 if not ET_DYN) */
83
Elf_Addr dynsize;
84
} el_ctx;
85
86
el_status el_pread(el_ctx *ctx, void *def, size_t nb, size_t offset);
87
88
el_status el_init(el_ctx *ctx);
89
typedef void *(*el_alloc_cb)(
90
el_ctx *ctx,
91
Elf_Addr phys,
92
Elf_Addr virt,
93
Elf_Addr size);
94
95
el_status el_load(el_ctx *ctx, el_alloc_cb alloccb);
96
97
/* find the next phdr of type \p type, starting at \p *i.
98
* On success, returns EL_OK with *i set to the phdr number, and the phdr loaded
99
* in *phdr.
100
*
101
* If the end of the phdrs table was reached, *i is set to -1 and the contents
102
* of *phdr are undefined
103
*/
104
el_status el_findphdr(el_ctx *ctx, Elf_Phdr *phdr, u32 type, unsigned *i);
105
106
/* Relocate the loaded executable */
107
el_status el_relocate(el_ctx *ctx);
108
109
/* find a dynamic table entry
110
* returns the entry on success, dyn->d_tag = DT_NULL on failure
111
*/
112
el_status el_finddyn(el_ctx *ctx, Elf_Dyn *dyn, u32 type);
113
114
typedef struct
115
{
116
Elf_Off tableoff;
117
Elf_Addr tablesize;
118
Elf_Addr entrysize;
119
} el_relocinfo;
120
121
/* find all information regarding relocations of a specific type.
122
*
123
* pass DT_REL or DT_RELA for type
124
* sets ri->entrysize = 0 if not found
125
*/
126
el_status el_findrelocs(el_ctx *ctx, el_relocinfo *ri, u32 type);
127
128
#endif
129
130