output: macho -- Add support for N_PEXT in macho output
[nasm.git] / nasmlib / mmap.c
blob5fc5646fb1be8e45517970d5c46143ac647ffa35
1 /* ----------------------------------------------------------------------- *
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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
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.
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.
32 * ----------------------------------------------------------------------- */
34 #include "file.h"
36 /* ----------------------------------------------------------------------- *
37 * Unix-style memory mapping, using mmap().
38 * ----------------------------------------------------------------------- */
39 #if defined(HAVE_FILENO) && defined(HAVE_MMAP)
42 * System page size
45 /* File scope since not all compilers like static data in inline functions */
46 static size_t nasm_pagemask;
48 static size_t get_pagemask(void)
50 size_t ps = 0;
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
60 nasm_pagemask = ps = is_power2(ps) ? (ps - 1) : 0;
61 return ps;
64 static inline size_t pagemask(void)
66 size_t pm = nasm_pagemask;
68 if (unlikely(!pm))
69 return get_pagemask();
71 return pm;
75 * Try to map an input file into memory
77 const void *nasm_map_file(FILE *fp, off_t start, off_t len)
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();
85 if (unlikely(!page_mask))
86 return NULL; /* Page size undefined? */
88 if (unlikely(!len))
89 return NULL; /* Mapping nothing... */
91 if (unlikely(len != (off_t)(size_t)len))
92 return NULL; /* Address space insufficient */
94 astart = start & ~(off_t)page_mask;
95 salign = start - astart;
96 alen = (len + salign + page_mask) & ~page_mask;
98 p = mmap(NULL, alen, PROT_READ, MAP_SHARED, fileno(fp), astart);
99 return unlikely(p == MAP_FAILED) ? NULL : p + salign;
103 * Unmap an input file
105 void nasm_unmap_file(const void *p, size_t len)
107 const size_t page_mask = pagemask();
108 uintptr_t astart;
109 size_t salign;
110 size_t alen;
112 if (unlikely(!page_mask))
113 return;
115 astart = (uintptr_t)p & ~(uintptr_t)page_mask;
116 salign = (uintptr_t)p - astart;
117 alen = (len + salign + page_mask) & ~page_mask;
119 munmap((void *)astart, alen);
122 /* ----------------------------------------------------------------------- *
123 * No memory map support at all
124 * XXX: Add a section with Windows support
125 * ----------------------------------------------------------------------- */
126 #else
128 const void *nasm_map_file(FILE *fp, off_t start, off_t len)
130 (void)fp; (void)start; (void)len;
131 return NULL;
134 void nasm_unmap_file(const void *p, size_t len)
136 (void)p; (void)len;
139 #endif