summaryrefslogtreecommitdiff
path: root/include/libelfu/modelops.h
blob: 0602d4e8e999a81af5380032e8bfbcedf3f20e0a (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* This file is part of centaur.
 *
 * centaur is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License 2 as
 * published by the Free Software Foundation.

 * centaur is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with centaur.  If not, see <http://www.gnu.org/licenses/>.
 */

/*!
 * @file modelops.h
 * @brief Operations offered on libelfu's Elfu* models.
 *
 * This includes:
 *  - Allocation/initialization, teardown of objects
 *  - Iteration (*Forall)
 *  - Stats (counts, lowest/highest element, ...)
 *  - Lookups (addresses, ...)
 *  - Scripted high-level operations (reladd, detour, ...)
 */

#ifndef __LIBELFU_MODELOPS_H__
#define __LIBELFU_MODELOPS_H__

#include <elf.h>
#include <gelf.h>

#include <libelfu/types.h>


/*!
 * @brief Lookup name of a string.
 * @param symtabscn ElfuScn of symbol table.
 * @param off       Offset in string table at which name starts.
 * @result Pointer to name.
 * @result INTERNAL USE ONLY.
 *         See elfu_mSymtabSymToName() for an alternative.
 *
 */
#define ELFU_SYMSTR(symtabscn, off) ((symtabscn)->linkptr->databuf + (off))


/*!
 * @brief Lookup the address a symbol points to.
 * @param me     Entire ELF model.
 * @param msst   ElfuScn containing the symbol table this symbol is in.
 * @param sym    The symbol itself.
 * @param result Will be set to the calculated address.
 * @result 0 if *result is valid. Otherwise, *result is undefined and the
 *         address could not be resolved.
 * @note This is currently for INTERNAL USE in the relocator ONLY.
 */
int elfu_mSymtabLookupSymToAddr(ElfuElf *me, ElfuScn *msst, ElfuSym *sym, GElf_Addr *result);

/*!
 * @brief Lookup name of a symbol.
 * @param msst ElfuScn containing the symbol table this symbol is in.
 * @param sym  The symbol itself.
 * @result Pointer to name.
 */
char* elfu_mSymtabSymToName(ElfuScn *msst, ElfuSym *sym);

/*!
 * @brief Lookup a symbol by its index in a symbol table.
 * @param msst  ElfuScn containing the symbol table this symbol is in.
 * @param entry The symbol's index in the table.
 * @result Pointer to the symbol.
 */
ElfuSym* elfu_mSymtabIndexToSym(ElfuScn *msst, GElf_Word entry);

/*!
 * @brief Lookup the address a symbol points to, by the symbol name.
 * @param me   Entire ELF model.
 * @param msst ElfuScn containing the symbol table this symbol is in.
 * @param name The symbol's name.
 * @result The address the symbol points to.
 */
GElf_Addr elfu_mSymtabLookupAddrByName(ElfuElf *me, ElfuScn *msst, char *name);

/*!
 * @brief Serialize an ELF's global symbol table.
 * @param me Entire ELF model.
 */
void elfu_mSymtabFlatten(ElfuElf *me);

/*!
 * @brief Check if a global symbol table exists, and add it otherwise.
 * @param me Entire ELF model.
 */
void elfu_mSymtabAddGlobalDymtabIfNotPresent(ElfuElf *me);




/*!
 * @brief Callback for elfu_mPhdrForall().
 * @param me   Entire ELF model.
 * @param mp   Current PHDR to process.
 * @param aux1 User defined.
 * @param aux2 User defined.
 * @result NULL if iteration is to continue.
 *         Otherwise it is aborted and the handler's return value is
 *         returned by elfu_mPhdrForall() itself.
 */
typedef void* (PhdrHandlerFunc)(ElfuElf *me, ElfuPhdr *mp, void *aux1, void *aux2);

/*!
 * @brief Iterate over all PHDRs.
 * @param me   Entire ELF model.
 * @param f    Callback function.
 * @param aux1 User defined.
 * @param aux2 User defined.
 * @result NULL if all items have been processed.
 *         Otherwise the return value of the last handler function
 *         call before aborting.
 */
void* elfu_mPhdrForall(ElfuElf *me, PhdrHandlerFunc f, void *aux1, void *aux2);

/*!
 * @brief Calculate number of PHDRs in an ElfuElf.
 * @param me Entire ELF model.
 * @result Total number of PHDRs.
 */
size_t elfu_mPhdrCount(ElfuElf *me);

/*!
 * @brief Find a PHDR that covers a memory address.
 * @param me   Entire ELF model.
 * @param addr A memory address.
 * @result Pointer to a PHDR containing the given memory address.
 *         NULL if none found.
 */
ElfuPhdr* elfu_mPhdrByAddr(ElfuElf *me, GElf_Addr addr);

/*!
 * @brief Find a PHDR that covers a file offset.
 * @param me     Entire ELF model.
 * @param offset A file offset.
 * @result Pointer to a PHDR containing the given file offset.
 *         NULL if none found.
 */
ElfuPhdr* elfu_mPhdrByOffset(ElfuElf *me, GElf_Off offset);

/*!
 * @brief Find the ElfuElf's memory address and file offset
 *        extrema in terms of PHDRs.
 * @param me Entire ELF model.
 * @param lowestAddr     Will be set to PHDR containing the lowest address referenced.
 * @param highestAddr    Will be set to PHDR containing the highest address referenced.
 * @param lowestOffs     Will be set to PHDR containing the lowest offset referenced.
 * @param highestOffsEnd Will be set to PHDR containing the highest offset referenced.
 */
void elfu_mPhdrLoadLowestHighest(ElfuElf *me,
                                 ElfuPhdr **lowestAddr,
                                 ElfuPhdr **highestAddr,
                                 ElfuPhdr **lowestOffs,
                                 ElfuPhdr **highestOffsEnd);

/*!
 * @brief Update child sections' offsets in the file according to parent
 *        PHDR's offset and address.
 * @param mp Parent PHDR whose children to update.
 */
void elfu_mPhdrUpdateChildOffsets(ElfuPhdr *mp);

/*!
 * @brief Allocate and initialize a PHDR model.
 * @result Pointer to a fresh ElfuPhdr.
 *         NULL if allocation failed.
 */
ElfuPhdr* elfu_mPhdrAlloc();

/*!
 * @brief Tear down a PHDR and its children.
 * @param mp PHDR to delete.
 */
void elfu_mPhdrDestroy(ElfuPhdr* mp);




/*!
 * @brief Callback for elfu_mScnForall().
 * @param me   Entire ELF model.
 * @param ms   Current section to process.
 * @param aux1 User defined.
 * @param aux2 User defined.
 * @result NULL if iteration is to continue.
 *         Otherwise it is aborted and the handler's return value is
 *         returned by elfu_mScnForall() itself.
 */
typedef void* (SectionHandlerFunc)(ElfuElf *me, ElfuScn *ms, void *aux1, void *aux2);

/*!
 * @brief Iterate over all sections.
 * @param me   Entire ELF model.
 * @param f    Callback function.
 * @param aux1 User defined.
 * @param aux2 User defined.
 * @result NULL if all items have been processed.
 *         Otherwise the return value of the last handler function
 *         call before aborting.
 */
void* elfu_mScnForall(ElfuElf *me, SectionHandlerFunc f, void *aux1, void *aux2);

/*!
 * @brief Calculate number of sections in an ElfuElf.
 * @param me Entire ELF model.
 * @result Total number of sections.
 */
size_t elfu_mScnCount(ElfuElf *me);

/*!
 * @brief Calculate index a section would currently have in its ElfuElf.
 * @param me Entire ELF model.
 * @param ms A section in *me.
 * @result Estimated index.
 */
size_t elfu_mScnIndex(ElfuElf *me, ElfuScn *ms);

/*!
 * @brief Find a cloned section by its oldscn value.
 * @param me     Entire ELF model.
 * @param oldscn Original section to find the clone of.
 * @result A section that is a clone of *oldscn.
 *         NULL if none found.
 */
ElfuScn* elfu_mScnByOldscn(ElfuElf *me, ElfuScn *oldscn);

/*!
 * @brief Get a section's name.
 * @param me Entire ELF model.
 * @param ms A section in *me.
 * @result Pointer to the section's name in .shstrtab.
 */
char* elfu_mScnName(ElfuElf *me, ElfuScn *ms);

/*!
 * @brief Allocate an array of pointers to all sections,
 *        and sort them by offset.
 * @param me    Entire ELF model.
 * @param count Where to write total count of sections to.
 * @result Pointer to the section's name in .shstrtab.
 *         NULL if an error occurred.
 */
ElfuScn** elfu_mScnSortedByOffset(ElfuElf *me, size_t *count);

/*!
 * @brief Enlarge a section's buffer and append data to it.
 * @param ms Section to append to.
 * @param buf Source buffer with data to append.
 * @param len Length of source buffer.
 * @result 0 if successful.
 *         Anything else indicates an error.
 */
int elfu_mScnAppendData(ElfuScn *ms, void *buf, size_t len);

/*!
 * @brief Allocate and initialize a section model.
 * @result Pointer to a fresh ElfuScn.
 *         NULL if allocation failed.
 */
ElfuScn* elfu_mScnAlloc();

/*!
 * @brief Tear down a section and associated buffers.
 * @param ms Section to delete.
 */
void elfu_mScnDestroy(ElfuScn* ms);




/*!
 * @brief Allocate and initialize an ELF file model.
 * @result Pointer to a fresh ElfuElf.
 *         NULL if allocation failed.
 */
ElfuElf* elfu_mElfAlloc();

/*!
 * @brief Tear down an ELF file model and associated structures.
 * @param me ElfuElf to destroy.
 */
void elfu_mElfDestroy(ElfuElf* me);




/*!
 * @brief Find a LOAD segment to inject into, and expand/create it.
 * @param me      Entire ELF model.
 * @param size    Size of data to inject.
 * @param align   Alignment requirement of new data.
 * @param w       Whether the new data should be writable when loaded.
 * @param x       Whether the new data should be executable when loaded.
 * @param injPhdr Will be set to the identified target PHDR.
 * @result The address where the data will be located.
 */
GElf_Addr elfu_mLayoutGetSpaceInPhdr(ElfuElf *me, GElf_Word size,
                                     GElf_Word align, int w, int x,
                                     ElfuPhdr **injPhdr);
/*!
 * @brief Re-layout an ELF file so nothing overlaps that should not.
 *        Also, recalculate various offsets and sizes where necessary.
 * @param me Entire ELF model.
 */
int elfu_mLayoutAuto(ElfuElf *me);




/*!
 * @brief Lookup the address of a function in the PLT by its name.
 * @param me   Entire ELF model.
 * @param name The function's name.
 * @param result Will be set to the calculated address.
 * @result 0 if *result is valid. Otherwise, *result is undefined and the
 *         address could not be resolved.
 */
int elfu_mDynLookupPltAddrByName(ElfuElf *me, char *name, GElf_Addr *result);

/*!
 * @brief Lookup the address of a dynamically loaded variable by its name.
 * @param me   Entire ELF model.
 * @param name The variable's name.
 * @param result Will be set to the calculated address.
 * @result 0 if *result is valid. Otherwise, *result is undefined and the
 *         address could not be resolved.
 */
int elfu_mDynLookupReldynAddrByName(ElfuElf *me, char *name, GElf_Addr *result);




/*!
 * @brief Relocate a section.
 * @param metarget ELF model containing cloned section.
 * @param mstarget Cloned section to be relocated.
 * @param msrt     Section in original ELF model,
 *                 containing relocation table.
 * @result 0 if successful.
 *         Anything else indicates an error.
 * @note This is currently for INTERNAL USE in Reladd ONLY.
 */
int elfu_mRelocate(ElfuElf *metarget, ElfuScn *mstarget, ElfuScn *msrt);




/*!
 * @brief Perform a few sanity checks.
 * @param me Entire ELF model.
 * @result 0 if successful.
 *         Anything else indicates an error.
 */
int elfu_mCheck(ElfuElf *me);




/*!
 * @brief Dump contents of a PHDR to stdout.
 * @param me Entire ELF model.
 * @param mp PHDR to dump.
 */
void elfu_mDumpPhdr(ElfuElf *me, ElfuPhdr *mp);

/*!
 * @brief Dump details of a section to stdout.
 * @param me Entire ELF model.
 * @param ms Section to dump.
 */
void elfu_mDumpScn(ElfuElf *me, ElfuScn *ms);

/*!
 * @brief Dump contents of an entire ELF file model to stdout.
 * @param me Entire ELF model to dump.
 */
void elfu_mDumpElf(ElfuElf *me);




/*!
 * @brief Parse an ELF file to a libelfu model via libelf.
 * @param e libelf handle to source file.
 * @result NULL if an error occurred, a fresh ElfuElf otherwise.
 */
ElfuElf* elfu_mFromElf(Elf *e);

/*!
 * @brief Serialize a libelfu model to an ELF file via libelf.
 * @param me Entire ELF model.
 * @param e  libelf handle to destination file.
 */
void elfu_mToElf(ElfuElf *me, Elf *e);




/*!
 * @brief Inject contents of an object file into an executable.
 * @param me   Destination ELF model.
 * @param mrel Source ELF model.
 * @result 0 if successful.
 *         Anything else indicates an error.
 */
int elfu_mReladd(ElfuElf *me, const ElfuElf *mrel);




/*!
 * @brief Overwrite a location with an unconditional jump.
 * @param me   Entire ELF model.
 * @param from Memory address to overwrite at.
 * @param to   Memory address to jump to.
 */
void elfu_mDetour(ElfuElf *me, GElf_Addr from, GElf_Addr to);


#endif