Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/ianos/elfload/elfload.h
1476 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
bool (*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
/* size in memory of binary */
65
Elf_Addr memsz;
66
67
/* required alignment */
68
Elf_Addr align;
69
70
/* ELF header */
71
Elf_Ehdr ehdr;
72
73
// Section Header Str Table
74
Elf_Shdr shstr;
75
Elf_Shdr symtab;
76
77
/* Offset of dynamic table (0 if not ET_DYN) */
78
Elf_Off dynoff;
79
/* Size of dynamic table (0 if not ET_DYN) */
80
Elf_Addr dynsize;
81
} el_ctx;
82
83
el_status el_pread(el_ctx *ctx, void *def, size_t nb, size_t offset);
84
85
el_status el_init(el_ctx *ctx);
86
typedef void *(*el_alloc_cb)(
87
el_ctx *ctx,
88
Elf_Addr phys,
89
Elf_Addr virt,
90
Elf_Addr size);
91
92
el_status el_load(el_ctx *ctx, el_alloc_cb alloccb);
93
94
/* find the next phdr of type \p type, starting at \p *i.
95
* On success, returns EL_OK with *i set to the phdr number, and the phdr loaded
96
* in *phdr.
97
*
98
* If the end of the phdrs table was reached, *i is set to -1 and the contents
99
* of *phdr are undefined
100
*/
101
el_status el_findphdr(el_ctx *ctx, Elf_Phdr *phdr, u32 type, unsigned *i);
102
103
/* Relocate the loaded executable */
104
el_status el_relocate(el_ctx *ctx);
105
106
/* find a dynamic table entry
107
* returns the entry on success, dyn->d_tag = DT_NULL on failure
108
*/
109
el_status el_finddyn(el_ctx *ctx, Elf_Dyn *dyn, u32 type);
110
111
typedef struct
112
{
113
Elf_Off tableoff;
114
Elf_Addr tablesize;
115
Elf_Addr entrysize;
116
} el_relocinfo;
117
118
/* find all information regarding relocations of a specific type.
119
*
120
* pass DT_REL or DT_RELA for type
121
* sets ri->entrysize = 0 if not found
122
*/
123
el_status el_findrelocs(el_ctx *ctx, el_relocinfo *ri, u32 type);
124
125
#endif
126
127