-
Notifications
You must be signed in to change notification settings - Fork 0
/
eld.c
80 lines (64 loc) · 1.87 KB
/
eld.c
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
#include "eld.h"
#include "support.h"
elf_object_list_head_t elves;
/**
* Initialize ELF object list
*
* @return zero, if success, non-zero otherwise.
*/
int eld_init() {
int result = SUCCESS;
DBG_MSG("Initializing ELD dynamic loader");
SLIST_INIT(&elves);
elf_object_t *main_elf = eld_elf_object_new(STR_PAR("loader"));
RETURN_ON_NULL(main_elf);
main_elf->dynamic_info_section = &_DYNAMIC;
RETURN_ON_ERROR(eld_elf_object_handle_dyn(main_elf));
SLIST_INSERT_HEAD(&elves, main_elf, next);
return SUCCESS;
}
/**
* Cleanup ELF object list
*
* @return zero, if success, non-zero otherwise.
*/
int eld_finish() {
elf_object_t *elf = NULL;
while (!SLIST_EMPTY(&elves)) {
elf = SLIST_FIRST(&elves);
SLIST_REMOVE_HEAD(&elves, next);
eld_elf_object_destroy(elf);
}
return SUCCESS;
}
/**
* Load a library already in memory.
*
* @param library pointer to the current position of the ELF file.
* @param library_descriptor pointer where a pointer the newly allocated library
* descriptor will be stored.
*
* @return zero, if success, non-zero otherwise.
*/
int eld_open(mem_t *library, elf_object_t **library_descriptor) {
CHECK_ARGS(library && library_descriptor);
int result = SUCCESS;
elf_object_t *library_elf = eld_elf_object_new(NULL, 0);
library_elf->file_address = library;
FAIL_ON_ERROR(eld_elf_object_check(library_elf));
FAIL_ON_ERROR(eld_elf_object_load(library_elf));
if (library_elf->dynamic_info_section) {
FAIL_ON_ERROR(eld_elf_object_handle_dyn(library_elf));
} else {
DBG_MSG("Not a dynamic library.");
result = ERROR_UNEXPECTED_FORMAT;
goto fail;
}
*library_descriptor = library_elf;
SLIST_INSERT_HEAD(&elves, library_elf, next);
DBG_MSG("%p correctly loaded: %s", library, library_elf->soname);
return SUCCESS;
fail:
if (library_elf) eld_elf_object_destroy(library_elf);
return result;
}