1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 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
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 * ----------------------------------------------------------------------- */
35 * nasmlib.h header file for nasmlib.c
38 #ifndef NASM_NASMLIB_H
39 #define NASM_NASMLIB_H
51 * Wrappers around malloc, realloc, free and a few more. nasm_malloc
52 * will fatal-error and die rather than return NULL; nasm_realloc will
53 * do likewise, and will also guarantee to work right on being passed
54 * a NULL pointer; nasm_free will do nothing if it is passed a NULL
57 void * safe_malloc(1) nasm_malloc(size_t);
58 void * safe_malloc(1) nasm_zalloc(size_t);
59 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
60 void * safe_realloc(2) nasm_realloc(void *, size_t);
61 void nasm_free(void *);
62 char * safe_alloc
nasm_strdup(const char *);
63 char * safe_alloc
nasm_strndup(const char *, size_t);
64 char * safe_alloc
nasm_strcat(const char *one
, const char *two
);
65 char * safe_alloc end_with_null
nasm_strcatn(const char *one
, ...);
68 * nasm_[v]asprintf() are variants of the semi-standard [v]asprintf()
69 * functions, except that we return the pointer instead of a count.
70 * The size of the string (including the final NUL!) is available
71 * by calling nasm_aprintf_size() afterwards.
73 * nasm_[v]axprintf() are similar, but allocates a user-defined amount
74 * of storage before the string, and returns a pointer to the
77 char * safe_alloc
printf_func(1, 2) nasm_asprintf(const char *fmt
, ...);
78 char * safe_alloc
nasm_vasprintf(const char *fmt
, va_list ap
);
79 void * safe_alloc
printf_func(2, 3) nasm_axprintf(size_t extra
, const char *fmt
, ...);
80 void * safe_alloc
nasm_vaxprintf(size_t extra
, const char *fmt
, va_list ap
);
82 extern size_t _nasm_aprintf_size
;
83 static inline size_t nasm_aprintf_size(void)
85 return _nasm_aprintf_size
;
88 /* Assert the argument is a pointer without evaluating it */
89 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
91 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
92 #define nasm_newn(p,n) ((p) = nasm_calloc(sizeof(*(p)),(n)))
94 * This is broken on platforms where there are pointers which don't
95 * match void * in their internal layout. It unfortunately also
96 * loses any "const" part of the argument, although hopefully the
97 * compiler will warn in that case.
99 #define nasm_delete(p) \
101 void **_pp = (void **)&(p); \
102 nasm_assert_pointer(p); \
106 #define nasm_zero(x) (memset(&(x), 0, sizeof(x)))
107 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
110 * Wrappers around fread()/fwrite() which fatal-errors on failure.
111 * For fread(), only use this if EOF is supposed to be a fatal error!
113 void nasm_read(void *, size_t, FILE *);
114 void nasm_write(const void *, size_t, FILE *);
117 * NASM failure at build time if the argument is false
120 # define nasm_static_assert(x) static_assert((x), #x)
121 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
122 # define nasm_static_assert(x) \
125 extern void __attribute__((error("assertion " #x " failed"))) \
126 _nasm_static_fail(void); \
127 _nasm_static_fail(); \
131 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
132 # define nasm_static_assert(x) \
133 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
137 * conditional static assert, if we know it is possible to determine
138 * the assert value at compile time. Since if_constant triggers
139 * pedantic warnings on gcc, turn them off explicitly around this code.
142 # define nasm_try_static_assert(x) \
145 static_assert(if_constant(x, true), #x); \
148 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
149 # define nasm_try_static_assert(x) \
151 if (!if_constant(x, true)) { \
152 extern void __attribute__((error("assertion " #x " failed"))) \
153 _nasm_static_fail(void); \
154 _nasm_static_fail(); \
158 # define nasm_try_static_assert(x) ((void)0)
162 * NASM assert failure
164 fatal_func
nasm_assert_failed(const char *, int, const char *);
165 #define nasm_assert(x) \
167 nasm_try_static_assert(x); \
168 if (unlikely(!(x))) \
169 nasm_assert_failed(__FILE__,__LINE__,#x); \
172 /* Utility function to generate a string for an invalid enum */
173 const char *invalid_enum_str(int);
176 * ANSI doesn't guarantee the presence of `stricmp' or
179 #if defined(HAVE_STRCASECMP)
180 #define nasm_stricmp strcasecmp
181 #elif defined(HAVE_STRICMP)
182 #define nasm_stricmp stricmp
184 int pure_func
nasm_stricmp(const char *, const char *);
187 #if defined(HAVE_STRNCASECMP)
188 #define nasm_strnicmp strncasecmp
189 #elif defined(HAVE_STRNICMP)
190 #define nasm_strnicmp strnicmp
192 int pure_func
nasm_strnicmp(const char *, const char *, size_t);
195 int pure_func
nasm_memicmp(const char *, const char *, size_t);
197 #if defined(HAVE_STRSEP)
198 #define nasm_strsep strsep
200 char *nasm_strsep(char **stringp
, const char *delim
);
203 #ifndef HAVE_DECL_STRNLEN
204 size_t pure_func
strnlen(const char *, size_t);
207 /* This returns the numeric value of a given 'digit'. */
208 #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
211 * Convert a string into a number, using NASM number rules. Sets
212 * `*error' to true if an error occurs, and false otherwise.
214 int64_t readnum(const char *str
, bool *error
);
217 * Convert a character constant into a number. Sets
218 * `*warn' to true if an overflow occurs, and false otherwise.
219 * str points to and length covers the middle of the string,
220 * without the quotes.
222 int64_t readstrnum(char *str
, int length
, bool *warn
);
225 * seg_alloc: allocate a hitherto unused segment number.
227 int32_t seg_alloc(void);
230 * Add/replace or remove an extension to the end of a filename
232 const char *filename_set_extension(const char *inname
, const char *extension
);
237 * This is a useful #define which I keep meaning to use more often:
238 * the number of elements of a statically defined array.
240 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
245 * list_for_each - regular iterator over list
246 * list_for_each_safe - the same but safe against list items removal
247 * list_last - find the last element in a list
249 #define list_for_each(pos, head) \
250 for (pos = head; pos; pos = pos->next)
251 #define list_for_each_safe(pos, n, head) \
252 for (pos = head, n = (pos ? pos->next : NULL); pos; \
253 pos = n, n = (n ? n->next : NULL))
254 #define list_last(pos, head) \
255 for (pos = head; pos && pos->next; pos = pos->next) \
257 #define list_reverse(head, prev, next) \
259 if (!head || !head->next) \
272 * Power of 2 align helpers
274 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
276 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
277 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
278 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
281 * Routines to write littleendian data to a file
283 #define fwriteint8_t(d,f) putc(d,f)
284 void fwriteint16_t(uint16_t data
, FILE * fp
);
285 void fwriteint32_t(uint32_t data
, FILE * fp
);
286 void fwriteint64_t(uint64_t data
, FILE * fp
);
287 void fwriteaddr(uint64_t data
, int size
, FILE * fp
);
290 * Binary search routine. Returns index into `array' of an entry
291 * matching `string', or <0 if no match. `array' is taken to
292 * contain `size' elements.
294 * bsi() is case sensitive, bsii() is case insensitive.
296 int bsi(const char *string
, const char **array
, int size
);
297 int bsii(const char *string
, const char **array
, int size
);
300 * Convenient string processing helper routines
302 char *nasm_skip_spaces(const char *p
);
303 char *nasm_skip_word(const char *p
);
304 char *nasm_zap_spaces_fwd(char *p
);
305 char *nasm_zap_spaces_rev(char *p
);
306 char *nasm_trim_spaces(char *p
);
307 char *nasm_get_word(char *p
, char **tail
);
308 char *nasm_opt_val(char *p
, char **opt
, char **val
);
311 * Converts a relative pathname rel_path into an absolute path name.
313 * The buffer returned must be freed by the caller
315 char * safe_alloc
nasm_realpath(const char *rel_path
);
318 * Path-splitting and merging functions
320 char * safe_alloc
nasm_dirname(const char *path
);
321 char * safe_alloc
nasm_basename(const char *path
);
322 char * safe_alloc
nasm_catfile(const char *dir
, const char *path
);
324 const char * pure_func
prefix_name(int);
327 * Wrappers around fopen()... for future change to a dedicated structure
330 NF_BINARY
= 0x00000000, /* Binary file (default) */
331 NF_TEXT
= 0x00000001, /* Text file */
332 NF_NONFATAL
= 0x00000000, /* Don't die on open failure (default) */
333 NF_FATAL
= 0x00000002, /* Die on open failure */
334 NF_FORMAP
= 0x00000004 /* Intended to use nasm_map_file() */
337 FILE *nasm_open_read(const char *filename
, enum file_flags flags
);
338 FILE *nasm_open_write(const char *filename
, enum file_flags flags
);
340 /* Probe for existence of a file */
341 bool nasm_file_exists(const char *filename
);
343 #define ZERO_BUF_SIZE 65536 /* Default value */
344 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
345 # undef ZERO_BUF_SIZE
346 # define ZERO_BUF_SIZE BUFSIZ
348 extern const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
350 /* Missing fseeko/ftello */
352 # undef off_t /* Just in case it is a macro */
353 # ifdef HAVE__FSEEKI64
354 # define fseeko _fseeki64
355 # define ftello _ftelli64
356 # define off_t int64_t
358 # define fseeko fseek
359 # define ftello ftell
364 const void *nasm_map_file(FILE *fp
, off_t start
, off_t len
);
365 void nasm_unmap_file(const void *p
, size_t len
);
366 off_t
nasm_file_size(FILE *f
);
367 off_t
nasm_file_size_by_path(const char *pathname
);
368 bool nasm_file_time(time_t *t
, const char *pathname
);
369 void fwritezero(off_t bytes
, FILE *fp
);
371 static inline bool const_func
overflow_general(int64_t value
, int bytes
)
379 sbit
= (bytes
<< 3) - 1;
380 vmax
= ((int64_t)2 << sbit
) - 1;
381 vmin
= -((int64_t)2 << sbit
);
383 return value
< vmin
|| value
> vmax
;
386 static inline bool const_func
overflow_signed(int64_t value
, int bytes
)
394 sbit
= (bytes
<< 3) - 1;
395 vmax
= ((int64_t)1 << sbit
) - 1;
396 vmin
= -((int64_t)1 << sbit
);
398 return value
< vmin
|| value
> vmax
;
401 static inline bool const_func
overflow_unsigned(int64_t value
, int bytes
)
409 sbit
= (bytes
<< 3) - 1;
410 vmax
= ((int64_t)2 << sbit
) - 1;
413 return value
< vmin
|| value
> vmax
;
416 static inline int64_t const_func
signed_bits(int64_t value
, int bits
)
419 value
&= ((int64_t)1 << bits
) - 1;
420 if (value
& (int64_t)1 << (bits
- 1))
421 value
|= (int64_t)((uint64_t)-1 << bits
);
426 /* check if value is power of 2 */
427 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)