-
Notifications
You must be signed in to change notification settings - Fork 13
/
map_file.c
56 lines (44 loc) · 944 Bytes
/
map_file.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
#include <libstatic/libstatic.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <elf.h>
#include <ulexec.h>
void *
map_file(char *file_to_map, unsigned long *sz)
{
struct stat sb;
void *mapped;
if (0 > linux_stat(file_to_map, &sb))
{
error_msg("map_file stat() failed ");
linux_exit(1);
}
*sz = sb.st_size;
mapped = linux_mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (mapped == (void *)-1)
{
error_msg("map_file mmap() failed ");
linux_exit(1);
}
copy_in(file_to_map, mapped);
return mapped;
}
void
copy_in(char *filename, void *address)
{
int fd, cc;
off_t offset = 0;
char buf[1024];
if (0 > (fd = linux_open(filename, 0, 0)))
{
error_msg("opening dynamically-loaded file failed");
linux_exit(2);
}
while (0 < (cc = linux_read(fd, buf, sizeof(buf))))
{
memcpy((address + offset), buf, cc);
offset += cc;
}
linux_close(fd);
}