forked from martinribelotta/elfloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_config.h
260 lines (225 loc) · 6.66 KB
/
loader_config.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/****************************************************************************
* ARMv7M ELF loader
* Copyright (c) 2013-2015 Martin Ribelotta
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifndef LOADER_CONFIG_H_
#define LOADER_CONFIG_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifndef DOX
#if 0
#define LOADER_FD_T FILE *
#define LOADER_OPEN_FOR_RD(path) fopen(path, "rb")
#define LOADER_FD_VALID(fd) (fd != NULL)
#define LOADER_READ(fd, buffer, size) fread(buffer, 1, size, fd)
#define LOADER_WRITE(fd, buffer, size) fwrite(buffer, 1, size, fd)
#define LOADER_CLOSE(fd) fclose(fd)
#define LOADER_SEEK_FROM_START(fd, off) fseek(fd, off, SEEK_SET)
#define LOADER_TELL(fd) ftell(fd)
#else
#define LOADER_FD_T int
#define LOADER_OPEN_FOR_RD(path) open(path, O_RDONLY)
#define LOADER_FD_VALID(fd) (fd != -1)
#define LOADER_READ(fd, buffer, size) read(fd, buffer, size)
#define LOADER_WRITE(fd, buffer, size) write(fd, buffer, size)
#define LOADER_CLOSE(fd) close(fd)
#define LOADER_SEEK_FROM_START(fd, off) (lseek(fd, off, SEEK_SET) == -1)
#define LOADER_TELL(fd) lseek(fd, 0, SEEK_CUR)
#endif
#if 0
#define LOADER_ALIGN_ALLOC(size, align, perm) ((void*) memalign(align, size))
#else
extern void *do_alloc(size_t size, size_t align, ELFSecPerm_t perm);
#define LOADER_ALIGN_ALLOC(size, align, perm) do_alloc(size, align, perm)
#endif
extern int is_streq(const char *s1, const char *s2);
#define LOADER_FREE(ptr) free(ptr)
#define LOADER_CLEAR(ptr, size) memset(ptr, 0, size)
#define LOADER_STREQ(s1, s2) (is_streq(s1, s2))
#if 0
#define LOADER_JUMP_TO(entry) entry();
#else
extern void arch_jumpTo(entry_t entry);
#define LOADER_JUMP_TO(entry) arch_jumpTo(entry)
#endif
#define DBG(...) printf("ELF: " __VA_ARGS__)
#define ERR(msg) do { perror("ELF: " msg); __asm__ volatile ("bkpt"); } while(0)
#define MSG(msg) puts("ELF: " msg)
#else
/**
* @defgroup Configuration Configuration macros
*
* This macros defines the access function to various internal function of
* elf loader
*
* You need to define this macros to compile and use the elf loader library
* in your system
*
* @{
*/
/**
* File handler descriptor type macro
*
* This define the file handler declaration type
*/
#define LOADER_FD_T
/**
* Open for read function macro
*
* This macro define the function name and convention call to open file for
* read. This need to return #LOADER_FD_T type
*
* @param path Path to file for open
*/
#define LOADER_OPEN_FOR_RD(path)
/**
* Macro for check file descriptor validity
*
* This macro is used for check the validity of #LOADER_FD_T after open
* operation
*
* @param fd File descriptor object
* @retval Zero if fd is unusable
* @retval Non-zero if fd is usable
*/
#define LOADER_FD_VALID(fd)
/**
* Macro for read buffer from file
*
* This macro is used when need read a block for the #LOADER_FD_T previous
* opened.
* @param fd File descriptor
* @param buffer Writable buffer to store read data
* @param size Number of bytes to read
*/
#define LOADER_READ(fd, buffer, size)
/**
* Close file macro
*
* Close a file descriptor previously opened with LOADER_OPEN_FOR_RD
*
* @param fd File descriptor to close
*/
#define LOADER_CLOSE(fd)
/**
* Seek read cursor of file
*
* Seek read cursor from begin of file
*
* @param fd File descriptor
* @param off Offset from begin of file
*/
#define LOADER_SEEK_FROM_START(fd, off)
/**
* Tell cursor of file
*
* Tell current read cursor of file
*
* @param fd File descriptor
* @return The current read cursor position
*/
#define LOADER_TELL(fd)
/**
* Allocate memory service
*
* Allocate aligned memory with required right access
*
* @param size Size in bytes to allocate
* @param align Aligned size in bytes
* @param perm Access rights of allocated block. Mask of #ELFSecPerm_t values
* @return Void pointer to allocated memory or NULL on fail
*/
#define LOADER_ALIGN_ALLOC(size, align, perm)
/**
* Free memory
*
* Free memory allocated with #LOADER_ALIGN_ALLOC
*
* @param ptr Pointer to allocated memory to free
*/
#define LOADER_FREE(ptr)
/**
* Clear memory block
*
* Set to zero memory block
*
* @param ptr Pointer to memory
* @param size Bytes to clear
*/
#define LOADER_CLEAR(ptr, size)
/**
* Compare string
*
* Compare zero terminated C-strings
*
* @param s1 First string
* @param s2 Second string
* @retval Zero if non equals
* @retval Non-zero if s1==s2
*/
#define LOADER_STREQ(s1, s2)
/**
* Jump to code
*
* Execute loaded code from entry point
*
* @param entry Pointer to function code to execute
*/
#define LOADER_JUMP_TO(entry)
/**
* Debug macro
*
* This macro is used for trace code execution and not side effect is needed
*
* @param ... printf style format and variadic argument list
*/
#define DBG(...)
/**
* Error macro
*
* This macro is used on unrecoverable error message
*
* @param msg C string printable text
*/
#define ERR(msg)
/**
* Information/Warnings message macro
*
* This macro is used on information or recoverable warnings mesages
*
* @param msg C string printable text
*/
#define MSG(msg)
/** @} */
#endif
#endif /* LOADER_CONFIG_H_ */