1/* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34#include "file.h"
35
36/* ----------------------------------------------------------------------- *
37 * Unix-style memory mapping, using mmap().
38 * ----------------------------------------------------------------------- */
39#if defined(HAVE_FILENO) && defined(HAVE_MMAP)
40
41/*
42 * System page size
43 */
44
45/* File scope since not all compilers like static data in inline functions */
46static size_t nasm_pagemask;
47
48static size_t get_pagemask(void)
49{
50 size_t ps = 0;
51
52# if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
53 ps = sysconf(_SC_PAGESIZE);
54# elif defined(HAVE_GETPAGESIZE)
55 ps = getpagesize();
56# elif defined(PAGE_SIZE)
57 ps = PAGE_SIZE;
58# endif
59
60 nasm_pagemask = ps = is_power2(ps) ? (ps - 1) : 0;
61 return ps;
62}
63
64static inline size_t pagemask(void)
65{
66 size_t pm = nasm_pagemask;
67
68 if (unlikely(!pm))
69 return get_pagemask();
70
71 return pm;
72}
73
74/*
75 * Try to map an input file into memory
76 */
77const void *nasm_map_file(FILE *fp, off_t start, off_t len)
78{
79 const char *p;
80 off_t astart; /* Aligned start */
81 size_t salign; /* Amount of start adjustment */
82 size_t alen; /* Aligned length */
83 const size_t page_mask = pagemask();
84
85 if (unlikely(!page_mask))
86 return NULL; /* Page size undefined? */
87
88 if (unlikely(!len))
89 return NULL; /* Mapping nothing... */
90
91 if (unlikely(len != (off_t)(size_t)len))
92 return NULL; /* Address space insufficient */
93
94 astart = start & ~(off_t)page_mask;
95 salign = start - astart;
96 alen = (len + salign + page_mask) & ~page_mask;
97
98 p = mmap(NULL, alen, PROT_READ, MAP_SHARED, fileno(fp), astart);
99 return unlikely(p == MAP_FAILED) ? NULL : p + salign;
100}
101
102/*
103 * Unmap an input file
104 */
105void nasm_unmap_file(const void *p, size_t len)
106{
107 const size_t page_mask = pagemask();
108 uintptr_t astart;
109 size_t salign;
110 size_t alen;
111
112 if (unlikely(!page_mask))
113 return;
114
115 astart = (uintptr_t)p & ~(uintptr_t)page_mask;
116 salign = (uintptr_t)p - astart;
117 alen = (len + salign + page_mask) & ~page_mask;
118
119 munmap((void *)astart, alen);
120}
121
122/* ----------------------------------------------------------------------- *
123 * No memory map support at all
124 * XXX: Add a section with Windows support
125 * ----------------------------------------------------------------------- */
126#else
127
128const void *nasm_map_file(FILE *fp, off_t start, off_t len)
129{
130 (void)fp; (void)start; (void)len;
131 return NULL;
132}
133
134void nasm_unmap_file(const void *p, size_t len)
135{
136 (void)p; (void)len;
137}
138
139#endif
140