-
Notifications
You must be signed in to change notification settings - Fork 1
/
static_dyn_load_run.c
84 lines (66 loc) · 1.56 KB
/
static_dyn_load_run.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
81
82
83
84
#include <libstatic/libstatic.h>
#include <elf.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libstatic/crt.h>
#include <ulexec.h>
#define JMP_ADDR(x) asm("\tjmp *%0\n" :: "r" (x))
#define SET_STACK(x) asm("\tmovq %0, %%rsp\n" :: "r"(x))
void c_main(int ac, char **av, char **env)
{
char *file_to_map = av[1];
void *mapped;
void *fptr;
struct stat sb;
Elf64_Ehdr *junk1 = NULL, *junk2 = NULL;
/* print_string(1, "static_dyn_load_run\n"); */
if (0 > linux_stat(file_to_map, &sb))
{
error_msg("stat() failed ");
linux_exit(1);
}
mapped = linux_mmap(NULL, sb.st_size, 0x07, 0x22, -1, 0);
/* print_address("File mapped in at", mapped); */
if (mapped == (void *)-1)
{
error_msg("mmap() failed ");
linux_exit(1);
}
copy_in(file_to_map, mapped);
fptr = load_elf(mapped, 1, &junk1, &junk2);
/* print_address("Entry point at", fptr); */
linux_munmap(mapped, sb.st_size);
/* print_address("Y setting stack to ", av - 1); */
SET_STACK(av - 1);
JMP_ADDR(fptr);
}
void
error_msg(char *msg)
{
char buf[32];
print_string(1, msg);
print_string(1, " ");
to_decimal(errno, buf);
print_string(1, buf);
print_string(1, "\n");
}
void
print_address(char *phrase, void *address)
{
char buf[256];
to_hex((unsigned long)address, buf);
print_string(1, phrase);
print_string(1, " 0x");
print_string(1, buf);
print_string(1, "\n");
}
void *
memcpy(void *dest, const void *src, unsigned long n)
{
unsigned long i;
unsigned char *d = (unsigned char *)dest;
unsigned char *s = (unsigned char *)src;
for (i = 0; i < n; ++i)
d[i] = s[i];
return dest;
}