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
|
/*
* libHX/ux-mmap.c
* Copyright Jan Engelhardt, 2005-2006
*
* This file is part of libHX. libHX is free software; you can
* redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation;
* either version 2.1 or (at your option) any later version.
*/
#include <sys/types.h>
#include <io.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <windows.h>
#include "internal.h"
#include <libHX/misc.h>
static __inline__ DWORD dw_desired_access(int, int);
static __inline__ DWORD fl_protect(int, int);
EXPORT_SYMBOL void *mmap(void *start, size_t length, int prot, int flags,
int fd, off_t offset)
{
HANDLE filp, fmap;
void *p;
filp = reinterpret_cast(HANDLE, _get_osfhandle(fd));
fmap = CreateFileMapping(filp, NULL, fl_protect(prot, flags),
0, 0, NULL);
if (fmap == NULL)
return MAP_FAILED;
p = MapViewOfFile(fmap, dw_desired_access(prot, flags),
((int64_t)offset >> 32) & 0xFFFFFFFFUL,
offset & 0xFFFFFFFFUL, length);
CloseHandle(fmap);
if (p == NULL)
return MAP_FAILED;
return p;
}
EXPORT_SYMBOL int munmap(void *start, size_t length)
{
if (!UnmapViewOfFile(start))
return -1;
return 0;
}
static __inline__ DWORD dw_desired_access(int prot, int flags)
{
if (flags & MAP_PRIVATE) return FILE_MAP_COPY;
if (prot & PROT_WRITE) return FILE_MAP_WRITE;
if (prot & PROT_READ) return FILE_MAP_READ;
#ifdef FILE_MAP_EXECUTE /* WinXP SP2 or WinServer2003 SP1 */
if (prot & PROT_EXEC) return FILE_MAP_EXECUTE;
#endif
return 0;
}
static __inline__ DWORD fl_protect(int prot, int flags)
{
if (flags & MAP_PRIVATE)
return PAGE_WRITECOPY;
#ifdef PAGE_EXECUTE_READ
if (flags & (PROT_EXEC | PROT_READ))
return PAGE_EXECUTE_READ;
#endif
#ifdef PAGE_EXECUTE_READWRITE
if (flags & (PROT_EXEC | PROT_READ | PROT_WRITE))
return PAGE_EXECUTE_READWRITE;
#endif
if (flags & PROT_WRITE)
return PAGE_READWRITE;
if (flags & PROT_READ)
return PAGE_READONLY;
return 0;
}
|