aboutsummaryrefslogtreecommitdiffstats
path: root/elf/elftest.c
blob: 0a38bbe6c60e26806daf4f9d3332273057f18441 (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
#include <stdio.h>
#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>

#include "elf_module.h"

void print_usage() {
	fprintf(stderr, "Usage:\n");
	fprintf(stderr, "\telftest objfile ...\n");
}

void test_hello() {
	int i;
	
	struct elf_module *module;
	Elf32_Sym *symbol;
	
	symbol = global_find_symbol("undef_func", &module);
	
	void (*undef_func)(int) = module_get_absolute(symbol->st_value, module);
	
	symbol = global_find_symbol("test_func", &module);
	
	int (*test_func)(void) = module_get_absolute(symbol->st_value, module); 
	
	undef_func(0);
	
	for (i=0; i < 10; i++) {
		printf("%d\n", test_func());
	}
}

int main(int argc, char **argv) {
	int res;
	int i;
	struct elf_module *module;
	const char *module_name = NULL;
	
	// Skip program name
	argc--;
	argv++;
	
	if (argc < 1) {
		print_usage();
		return 1;
	}
	
	res = modules_init();
		
	if (res < 0) {
		fprintf(stderr, "Could not initialize module subsystem\n");
		exit(1);
	}
	
	for (i=0; i < argc; i++){
		module_name = argv[i];
		
		module = module_alloc(module_name);
			
		if (module == NULL) {
			fprintf(stderr, "Could not allocate the module\n");
			goto error;
		}
		
		res = module_load(module);
			
		if (res < 0) {
			fprintf(stderr, "Could not load the module\n");
			goto error;
		}
		
	}
	
	test_hello();
	
	for (i=argc-1; i >= 0; i--) {
		module_name = argv[i];
		module = module_find(module_name);
		
		res = module_unload(module);
		
		if (res < 0) {
			fprintf(stderr, "Could not unload the module\n");
			goto error;
		}
	}
	
	modules_term();
	
	return 0;
	
error:
	modules_term();
	
	return 1;
}