1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2020 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
45 * Useful construct for private values
55 typedef union intorptr intorptr
;
58 * Wrappers around malloc, realloc, free and a few more. nasm_malloc
59 * will fatal-error and die rather than return NULL; nasm_realloc will
60 * do likewise, and will also guarantee to work right on being passed
61 * a NULL pointer; nasm_free will do nothing if it is passed a NULL
64 void * safe_malloc(1) nasm_malloc(size_t);
65 void * safe_malloc(1) nasm_zalloc(size_t);
66 void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
67 void * safe_realloc(2) nasm_realloc(void *, size_t);
68 void nasm_free(void *);
69 char * safe_alloc
nasm_strdup(const char *);
70 char * safe_alloc
nasm_strndup(const char *, size_t);
71 char * safe_alloc
nasm_strcat(const char *one
, const char *two
);
72 char * safe_alloc end_with_null
nasm_strcatn(const char *one
, ...);
75 * nasm_[v]asprintf() are variants of the semi-standard [v]asprintf()
76 * functions, except that we return the pointer instead of a count.
77 * The size of the string (including the final NUL!) is available
78 * by calling nasm_aprintf_size() afterwards.
80 * nasm_[v]axprintf() are similar, but allocates a user-defined amount
81 * of storage before the string, and returns a pointer to the
82 * allocated buffer. The value of nasm_aprintf_size() does *not* include
83 * this additional storage.
85 char * safe_alloc
printf_func(1, 2) nasm_asprintf(const char *fmt
, ...);
86 char * safe_alloc
nasm_vasprintf(const char *fmt
, va_list ap
);
87 void * safe_alloc
printf_func(2, 3) nasm_axprintf(size_t extra
, const char *fmt
, ...);
88 void * safe_alloc
nasm_vaxprintf(size_t extra
, const char *fmt
, va_list ap
);
91 * nasm_last_string_len() returns the length of the last string allocated
92 * by [v]asprintf, nasm_strdup, nasm_strcat, or nasm_strcatn.
94 * nasm_last_string_size() returns the equivalent size including the
97 static inline size_t nasm_last_string_len(void)
99 extern size_t _nasm_last_string_size
;
100 return _nasm_last_string_size
- 1;
102 static inline size_t nasm_last_string_size(void)
104 extern size_t _nasm_last_string_size
;
105 return _nasm_last_string_size
;
108 /* Assert the argument is a pointer without evaluating it */
109 #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
111 #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
112 #define nasm_newn(p,n) ((p) = nasm_calloc((n), sizeof(*(p))))
114 * This is broken on platforms where there are pointers which don't
115 * match void * in their internal layout. It unfortunately also
116 * loses any "const" part of the argument, although hopefully the
117 * compiler will warn in that case.
119 #define nasm_delete(p) \
121 void **_pp = (void **)&(p); \
122 nasm_assert_pointer(p); \
126 #define nasm_zero(x) (memset(&(x), 0, sizeof(x)))
127 #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
130 * Wrappers around fread()/fwrite() which fatal-errors on failure.
131 * For fread(), only use this if EOF is supposed to be a fatal error!
133 void nasm_read(void *, size_t, FILE *);
134 void nasm_write(const void *, size_t, FILE *);
137 * NASM failure at build time if the argument is false
140 # define nasm_static_assert(x) static_assert((x), #x)
141 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
142 # define nasm_static_assert(x) \
145 extern void __attribute__((error("assertion " #x " failed"))) \
146 _nasm_static_fail(void); \
147 _nasm_static_fail(); \
151 /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
152 # define nasm_static_assert(x) \
153 do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
157 * conditional static assert, if we know it is possible to determine
158 * the assert value at compile time. Since if_constant triggers
159 * pedantic warnings on gcc, turn them off explicitly around this code.
162 # define nasm_try_static_assert(x) \
165 static_assert(if_constant(x, true), #x); \
168 #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
169 # define nasm_try_static_assert(x) \
171 if (!if_constant(x, true)) { \
172 extern void __attribute__((error("assertion " #x " failed"))) \
173 _nasm_static_fail(void); \
174 _nasm_static_fail(); \
178 # define nasm_try_static_assert(x) ((void)0)
182 * NASM assert failure
184 fatal_func
nasm_assert_failed(const char *, int, const char *);
185 #define nasm_assert(x) \
187 nasm_try_static_assert(x); \
188 if (unlikely(!(x))) \
189 nasm_assert_failed(__FILE__,__LINE__,#x); \
192 /* Utility function to generate a string for an invalid enum */
193 const char *invalid_enum_str(int);
196 * ANSI doesn't guarantee the presence of `stricmp' or
199 #if defined(HAVE_STRCASECMP)
200 #define nasm_stricmp strcasecmp
201 #elif defined(HAVE_STRICMP)
202 #define nasm_stricmp stricmp
204 int pure_func
nasm_stricmp(const char *, const char *);
207 #if defined(HAVE_STRNCASECMP)
208 #define nasm_strnicmp strncasecmp
209 #elif defined(HAVE_STRNICMP)
210 #define nasm_strnicmp strnicmp
212 int pure_func
nasm_strnicmp(const char *, const char *, size_t);
215 int pure_func
nasm_memicmp(const char *, const char *, size_t);
217 #if defined(HAVE_STRSEP)
218 #define nasm_strsep strsep
220 char *nasm_strsep(char **stringp
, const char *delim
);
223 #ifndef HAVE_DECL_STRNLEN
224 size_t pure_func
strnlen(const char *, size_t);
227 /* This returns the numeric value of a given 'digit'; no check for validity */
228 static inline unsigned int numvalue(unsigned char c
)
231 return c
>= 'a' ? c
- 'a' + 10 : c
- '0';
235 * Convert a string into a number, using NASM number rules. Sets
236 * `*error' to true if an error occurs, and false otherwise.
238 int64_t readnum(const char *str
, bool *error
);
241 * Convert a character constant into a number. Sets
242 * `*warn' to true if an overflow occurs, and false otherwise.
243 * str points to and length covers the middle of the string,
244 * without the quotes.
246 int64_t readstrnum(char *str
, int length
, bool *warn
);
249 * seg_alloc: allocate a hitherto unused segment number.
251 int32_t seg_alloc(void);
254 * Add/replace or remove an extension to the end of a filename
256 const char *filename_set_extension(const char *inname
, const char *extension
);
261 * This is a useful #define which I keep meaning to use more often:
262 * the number of elements of a statically defined array.
264 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
269 * list_for_each - regular iterator over list
270 * list_for_each_safe - the same but safe against list items removal
271 * list_last - find the last element in a list
273 #define list_for_each(pos, head) \
274 for (pos = head; pos; pos = pos->next)
275 #define list_for_each_safe(pos, n, head) \
276 for (pos = head, n = (pos ? pos->next : NULL); pos; \
277 pos = n, n = (n ? n->next : NULL))
278 #define list_last(pos, head) \
279 for (pos = head; pos && pos->next; pos = pos->next) \
281 #define list_reverse(head, prev, next) \
283 if (!head || !head->next) \
296 * Power of 2 align helpers
298 #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
300 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
301 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
302 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
305 * Routines to write littleendian data to a file
307 #define fwriteint8_t(d,f) putc(d,f)
308 void fwriteint16_t(uint16_t data
, FILE * fp
);
309 void fwriteint32_t(uint32_t data
, FILE * fp
);
310 void fwriteint64_t(uint64_t data
, FILE * fp
);
311 void fwriteaddr(uint64_t data
, int size
, FILE * fp
);
314 * Binary search routine. Returns index into `array' of an entry
315 * matching `string', or <0 if no match. `array' is taken to
316 * contain `size' elements.
318 * bsi() is case sensitive, bsii() is case insensitive.
320 int bsi(const char *string
, const char **array
, int size
);
321 int bsii(const char *string
, const char **array
, int size
);
324 * Convenient string processing helper routines
326 char *nasm_skip_spaces(const char *p
);
327 char *nasm_skip_word(const char *p
);
328 char *nasm_zap_spaces_fwd(char *p
);
329 char *nasm_zap_spaces_rev(char *p
);
330 char *nasm_trim_spaces(char *p
);
331 char *nasm_get_word(char *p
, char **tail
);
332 char *nasm_opt_val(char *p
, char **opt
, char **val
);
335 * Converts a relative pathname rel_path into an absolute path name.
337 * The buffer returned must be freed by the caller
339 char * safe_alloc
nasm_realpath(const char *rel_path
);
342 * Path-splitting and merging functions
344 char * safe_alloc
nasm_dirname(const char *path
);
345 char * safe_alloc
nasm_basename(const char *path
);
346 char * safe_alloc
nasm_catfile(const char *dir
, const char *path
);
348 const char * pure_func
prefix_name(int);
351 * Wrappers around fopen()... for future change to a dedicated structure
354 NF_BINARY
= 0x00000000, /* Binary file (default) */
355 NF_TEXT
= 0x00000001, /* Text file */
356 NF_NONFATAL
= 0x00000000, /* Don't die on open failure (default) */
357 NF_FATAL
= 0x00000002, /* Die on open failure */
358 NF_FORMAP
= 0x00000004, /* Intended to use nasm_map_file() */
359 NF_IONBF
= 0x00000010, /* Force unbuffered stdio */
360 NF_IOLBF
= 0x00000020, /* Force line buffered stdio */
361 NF_IOFBF
= 0000000030 /* Force fully buffered stdio */
363 #define NF_BUF_MASK 0x30
365 FILE *nasm_open_read(const char *filename
, enum file_flags flags
);
366 FILE *nasm_open_write(const char *filename
, enum file_flags flags
);
368 void nasm_set_binary_mode(FILE *f
);
370 /* Probe for existence of a file */
371 bool nasm_file_exists(const char *filename
);
373 #define ZERO_BUF_SIZE 65536 /* Default value */
374 #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
375 # undef ZERO_BUF_SIZE
376 # define ZERO_BUF_SIZE BUFSIZ
378 extern const uint8_t zero_buffer
[ZERO_BUF_SIZE
];
380 /* Missing fseeko/ftello */
382 # undef off_t /* Just in case it is a macro */
383 # ifdef HAVE__FSEEKI64
384 # define fseeko _fseeki64
385 # define ftello _ftelli64
386 # define off_t int64_t
388 # define fseeko fseek
389 # define ftello ftell
394 const void *nasm_map_file(FILE *fp
, off_t start
, off_t len
);
395 void nasm_unmap_file(const void *p
, size_t len
);
396 off_t
nasm_file_size(FILE *f
);
397 off_t
nasm_file_size_by_path(const char *pathname
);
398 bool nasm_file_time(time_t *t
, const char *pathname
);
399 void fwritezero(off_t bytes
, FILE *fp
);
401 static inline bool const_func
overflow_general(int64_t value
, int bytes
)
409 sbit
= (bytes
<< 3) - 1;
410 vmax
= ((int64_t)2 << sbit
) - 1;
411 vmin
= -((int64_t)2 << sbit
);
413 return value
< vmin
|| value
> vmax
;
416 static inline bool const_func
overflow_signed(int64_t value
, int bytes
)
424 sbit
= (bytes
<< 3) - 1;
425 vmax
= ((int64_t)1 << sbit
) - 1;
426 vmin
= -((int64_t)1 << sbit
);
428 return value
< vmin
|| value
> vmax
;
431 static inline bool const_func
overflow_unsigned(int64_t value
, int bytes
)
439 sbit
= (bytes
<< 3) - 1;
440 vmax
= ((int64_t)2 << sbit
) - 1;
443 return value
< vmin
|| value
> vmax
;
446 static inline int64_t const_func
signed_bits(int64_t value
, int bits
)
449 value
&= ((int64_t)1 << bits
) - 1;
450 if (value
& (int64_t)1 << (bits
- 1))
451 value
|= (int64_t)((uint64_t)-1 << bits
);
456 /* check if value is power of 2 */
457 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
459 /* try to get the system stack size */
460 extern size_t nasm_get_stack_size_limit(void);