aboutsummaryrefslogtreecommitdiffstats
path: root/com32/elflink/exec.c
blob: 58f04b4552a2fa0eb663315ed20ac7b150bcdb06 (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
/*
 * exec.c
 *
 *  Created on: Aug 14, 2008
 *      Author: Stefan Bucur <stefanb@zytor.com>
 */

#include <sys/module.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "exec.h"

#define DBG_PRINT(fmt, args...)	fprintf(stderr, "[EXEC] " fmt, ##args)


static struct elf_module    *mod_root = NULL;

static char *module_get_fullname(const char *name) {
	static char name_buff[MODULE_NAME_SIZE];

	strcpy(name_buff, EXEC_DIRECTORY);
	strcat(name_buff, name);

	return name_buff;
}

int exec_init() {
	int res;

	res = modules_init();

	if (res != 0)
		return res;

	// Load the root module
	mod_root = module_alloc(module_get_fullname(EXEC_ROOT_NAME));

	if (mod_root == NULL)
		return -1;

	res = module_load_shallow(mod_root);

	if (res != 0) {
		mod_root = NULL;
		return res;
	}

	return 0;
}

int load_library(const char *name) {
	int res;
	struct elf_module *module = module_alloc(module_get_fullname(name));

	if (module == NULL)
		return -1;

	res = module_load(module);


	if (res != 0) {
		return res;
	}

	if (*(module->init_func) != NULL) {
		res = (*(module->init_func))();
		DBG_PRINT("Initialization function returned: %d\n", res);
	} else {
		DBG_PRINT("No initialization function present.\n");
	}

	if (res != 0) {
		module_unload(module);
		return res;
	}

	return 0;
}

int unload_library(const char *name) {
	int res;
	struct elf_module *module = module_find(module_get_fullname(name));

	if (module == NULL)
		return -1;

	if (*(module->exit_func) != NULL) {
		(*(module->exit_func))();
	}

	res = module_unload(module);

	return res;
}

int spawnv(const char *name, const char **argv) {
	int res, ret_val;

	struct elf_module *module = module_alloc(module_get_fullname(name));

	if (module == NULL)
		return -1;

	res = module_load(module);

	if (res != 0) {
		return res;
	}

	if (*(module->main_func) != NULL) {
		const char **last_arg = argv;
		while (*last_arg != NULL)
			last_arg++;

		ret_val = (*(module->main_func))(last_arg - argv, argv);
	} else {
		// We can't execute without a main function
		module_unload(module);
		return -1;
	}

	res = module_unload(module);

	if (res != 0) {
		return res;
	}

	return ((unsigned int)ret_val & 0xFF);
}

int spawnl(const char *name, const char *arg, ...) {
	/*
	 * NOTE: We assume the standard ABI specification for the i386
	 * architecture. This code may not work if used in other
	 * circumstances, including non-variadic functions, different
	 * architectures and calling conventions.
	 */

	return spawnv(name, &arg);
}


void exec_term() {
	modules_term();
}