1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2009 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 /*
35 * nasmlib.h header file for nasmlib.c
36 */
37
38 #ifndef NASM_NASMLIB_H
39 #define NASM_NASMLIB_H
40
41 #include "compiler.h"
42 #include "inttypes.h"
43 #include <stdio.h>
44 #include <string.h>
45 #ifdef HAVE_STRINGS_H
46 #include <strings.h>
47 #endif
48
49 /*
50 * tolower table -- avoids a function call on some platforms.
51 * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
52 * a permitted value, for obvious reasons.
53 */
54 void tolower_init(void);
55 extern unsigned char nasm_tolower_tab[256];
56 #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
57
58 /* Wrappers around <ctype.h> functions */
59 /* These are only valid for values that cannot include EOF */
60 #define nasm_isspace(x) isspace((unsigned char)(x))
61 #define nasm_isalpha(x) isalpha((unsigned char)(x))
62 #define nasm_isdigit(x) isdigit((unsigned char)(x))
63 #define nasm_isalnum(x) isalnum((unsigned char)(x))
64 #define nasm_isxdigit(x) isxdigit((unsigned char)(x))
65
66 /*
67 * If this is defined, the wrappers around malloc et al will
68 * transform into logging variants, which will cause NASM to create
69 * a file called `malloc.log' when run, and spew details of all its
70 * memory management into that. That can then be analysed to detect
71 * memory leaks and potentially other problems too.
72 */
73 /* #define LOGALLOC */
74
75 /*
76 * -------------------------
77 * Error reporting functions
78 * -------------------------
79 */
80
81 /*
82 * An error reporting function should look like this.
83 */
84 typedef void (*efunc) (int severity, const char *fmt, ...);
85 typedef void (*vefunc) (int severity, const char *fmt, va_list ap);
86 #ifdef __GNUC__
87 void nasm_error(int severity, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
88 #else
89 void nasm_error(int severity, const char *fmt, ...);
90 #endif
91 void nasm_set_verror(vefunc);
92
93 /*
94 * These are the error severity codes which get passed as the first
95 * argument to an efunc.
96 */
97
98 #define ERR_DEBUG 0x00000008 /* put out debugging message */
99 #define ERR_WARNING 0x00000000 /* warn only: no further action */
100 #define ERR_NONFATAL 0x00000001 /* terminate assembly after phase */
101 #define ERR_FATAL 0x00000002 /* instantly fatal: exit with error */
102 #define ERR_PANIC 0x00000003 /* internal error: panic instantly
103 * and dump core for reference */
104 #define ERR_MASK 0x0000000F /* mask off the above codes */
105 #define ERR_NOFILE 0x00000010 /* don't give source file name/line */
106 #define ERR_USAGE 0x00000020 /* print a usage message */
107 #define ERR_PASS1 0x00000040 /* only print this error on pass one */
108 #define ERR_PASS2 0x00000080
109 #define ERR_NO_SEVERITY 0x00000100 /* suppress printing severity */
110
111 /*
112 * These codes define specific types of suppressible warning.
113 */
114
115 #define ERR_WARN_MASK 0xFFFFF000 /* the mask for this feature */
116 #define ERR_WARN_SHR 12 /* how far to shift right */
117
118 #define WARN(x) ((x) << ERR_WARN_SHR)
119
120 #define ERR_WARN_MNP WARN( 1) /* macro-num-parameters warning */
121 #define ERR_WARN_MSR WARN( 2) /* macro self-reference */
122 #define ERR_WARN_MDP WARN( 3) /* macro default parameters check */
123 #define ERR_WARN_OL WARN( 4) /* orphan label (no colon, and
124 * alone on line) */
125 #define ERR_WARN_NOV WARN( 5) /* numeric overflow */
126 #define ERR_WARN_GNUELF WARN( 6) /* using GNU ELF extensions */
127 #define ERR_WARN_FL_OVERFLOW WARN( 7) /* FP overflow */
128 #define ERR_WARN_FL_DENORM WARN( 8) /* FP denormal */
129 #define ERR_WARN_FL_UNDERFLOW WARN( 9) /* FP underflow */
130 #define ERR_WARN_FL_TOOLONG WARN(10) /* FP too many digits */
131 #define ERR_WARN_USER WARN(11) /* %warning directives */
132 #define ERR_WARN_MAX 11 /* the highest numbered one */
133
134 /*
135 * Wrappers around malloc, realloc and free. nasm_malloc will
136 * fatal-error and die rather than return NULL; nasm_realloc will
137 * do likewise, and will also guarantee to work right on being
138 * passed a NULL pointer; nasm_free will do nothing if it is passed
139 * a NULL pointer.
140 */
141 void nasm_init_malloc_error(void);
142 #ifndef LOGALLOC
143 void *nasm_malloc(size_t);
144 void *nasm_zalloc(size_t);
145 void *nasm_realloc(void *, size_t);
146 void nasm_free(void *);
147 char *nasm_strdup(const char *);
148 char *nasm_strndup(const char *, size_t);
149 #else
150 void *nasm_malloc_log(const char *, int, size_t);
151 void *nasm_zalloc_log(const char *, int, size_t);
152 void *nasm_realloc_log(const char *, int, void *, size_t);
153 void nasm_free_log(const char *, int, void *);
154 char *nasm_strdup_log(const char *, int, const char *);
155 char *nasm_strndup_log(const char *, int, const char *, size_t);
156 #define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x)
157 #define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x)
158 #define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y)
159 #define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x)
160 #define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x)
161 #define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y)
162 #endif
163
164 /*
165 * NASM assert failure
166 */
167 no_return nasm_assert_failed(const char *, int, const char *);
168 #define nasm_assert(x) \
169 do { \
170 if (unlikely(!(x))) \
171 nasm_assert_failed(__FILE__,__LINE__,#x); \
172 } while (0)
173
174 /*
175 * NASM failure at build time if x != 0
176 */
177 #define nasm_build_assert(x) (void)(sizeof(char[1-2*!!(x)]))
178
179 /*
180 * ANSI doesn't guarantee the presence of `stricmp' or
181 * `strcasecmp'.
182 */
183 #if defined(HAVE_STRCASECMP)
184 #define nasm_stricmp strcasecmp
185 #elif defined(HAVE_STRICMP)
186 #define nasm_stricmp stricmp
187 #else
188 int nasm_stricmp(const char *, const char *);
189 #endif
190
191 #if defined(HAVE_STRNCASECMP)
192 #define nasm_strnicmp strncasecmp
193 #elif defined(HAVE_STRNICMP)
194 #define nasm_strnicmp strnicmp
195 #else
196 int nasm_strnicmp(const char *, const char *, size_t);
197 #endif
198
199 int nasm_memicmp(const char *, const char *, size_t);
200
201 #if defined(HAVE_STRSEP)
202 #define nasm_strsep strsep
203 #else
204 char *nasm_strsep(char **stringp, const char *delim);
205 #endif
206
207
208 /*
209 * Convert a string into a number, using NASM number rules. Sets
210 * `*error' to true if an error occurs, and false otherwise.
211 */
212 int64_t readnum(char *str, bool *error);
213
214 /*
215 * Convert a character constant into a number. Sets
216 * `*warn' to true if an overflow occurs, and false otherwise.
217 * str points to and length covers the middle of the string,
218 * without the quotes.
219 */
220 int64_t readstrnum(char *str, int length, bool *warn);
221
222 /*
223 * seg_init: Initialise the segment-number allocator.
224 * seg_alloc: allocate a hitherto unused segment number.
225 */
226 void seg_init(void);
227 int32_t seg_alloc(void);
228
229 /*
230 * many output formats will be able to make use of this: a standard
231 * function to add an extension to the name of the input file
232 */
233 void standard_extension(char *inname, char *outname, char *extension);
234
235 /*
236 * Utility macros...
237 *
238 * This is a useful #define which I keep meaning to use more often:
239 * the number of elements of a statically defined array.
240 */
241
242 #define elements(x) ( sizeof(x) / sizeof(*(x)) )
243
244 /*
245 * List handling
246 *
247 * list_for_each - regular iterator over list
248 * list_for_each_safe - the same but safe against list items removal
249 */
250 #define list_for_each(pos, head) \
251 for (pos = head; pos; pos = pos->next)
252 #define list_for_each_safe(pos, n, head) \
253 for (pos = head, n = (pos ? pos->next : NULL); pos; \
254 pos = n, n = (n ? n->next : NULL))
255
256 /*
257 * Power of 2 align helpers
258 */
259 #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
260 #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
261 #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
262
263 /*
264 * some handy macros that will probably be of use in more than one
265 * output format: convert integers into little-endian byte packed
266 * format in memory
267 */
268
269 #if X86_MEMORY
270
271 #define WRITECHAR(p,v) \
272 do { \
273 *(uint8_t *)(p) = (v); \
274 (p) += 1; \
275 } while (0)
276
277 #define WRITESHORT(p,v) \
278 do { \
279 *(uint16_t *)(p) = (v); \
280 (p) += 2; \
281 } while (0)
282
283 #define WRITELONG(p,v) \
284 do { \
285 *(uint32_t *)(p) = (v); \
286 (p) += 4; \
287 } while (0)
288
289 #define WRITEDLONG(p,v) \
290 do { \
291 *(uint64_t *)(p) = (v); \
292 (p) += 8; \
293 } while (0)
294
295 #define WRITEADDR(p,v,s) \
296 do { \
297 uint64_t _wa_v = (v); \
298 memcpy((p), &_wa_v, (s)); \
299 (p) += (s); \
300 } while (0)
301
302 #else /* !X86_MEMORY */
303
304 #define WRITECHAR(p,v) \
305 do { \
306 uint8_t *_wc_p = (uint8_t *)(p); \
307 uint8_t _wc_v = (v); \
308 _wc_p[0] = _wc_v; \
309 (p) = (void *)(_wc_p + 1); \
310 } while (0)
311
312 #define WRITESHORT(p,v) \
313 do { \
314 uint8_t *_ws_p = (uint8_t *)(p); \
315 uint16_t _ws_v = (v); \
316 _ws_p[0] = _ws_v; \
317 _ws_p[1] = _ws_v >> 8; \
318 (p) = (void *)(_ws_p + 2); \
319 } while (0)
320
321 #define WRITELONG(p,v) \
322 do { \
323 uint8_t *_wl_p = (uint8_t *)(p); \
324 uint32_t _wl_v = (v); \
325 _wl_p[0] = _wl_v; \
326 _wl_p[1] = _wl_v >> 8; \
327 _wl_p[2] = _wl_v >> 16; \
328 _wl_p[3] = _wl_v >> 24; \
329 (p) = (void *)(_wl_p + 4); \
330 } while (0)
331
332 #define WRITEDLONG(p,v) \
333 do { \
334 uint8_t *_wq_p = (uint8_t *)(p); \
335 uint64_t _wq_v = (v); \
336 _wq_p[0] = _wq_v; \
337 _wq_p[1] = _wq_v >> 8; \
338 _wq_p[2] = _wq_v >> 16; \
339 _wq_p[3] = _wq_v >> 24; \
340 _wq_p[4] = _wq_v >> 32; \
341 _wq_p[5] = _wq_v >> 40; \
342 _wq_p[6] = _wq_v >> 48; \
343 _wq_p[7] = _wq_v >> 56; \
344 (p) = (void *)(_wq_p + 8); \
345 } while (0)
346
347 #define WRITEADDR(p,v,s) \
348 do { \
349 int _wa_s = (s); \
350 uint64_t _wa_v = (v); \
351 while (_wa_s--) { \
352 WRITECHAR(p,_wa_v); \
353 _wa_v >>= 8; \
354 } \
355 } while(0)
356
357 #endif
358
359 /*
360 * and routines to do the same thing to a file
361 */
362 #define fwriteint8_t(d,f) putc(d,f)
363 void fwriteint16_t(uint16_t data, FILE * fp);
364 void fwriteint32_t(uint32_t data, FILE * fp);
365 void fwriteint64_t(uint64_t data, FILE * fp);
366 void fwriteaddr(uint64_t data, int size, FILE * fp);
367
368 /*
369 * Binary search routine. Returns index into `array' of an entry
370 * matching `string', or <0 if no match. `array' is taken to
371 * contain `size' elements.
372 *
373 * bsi() is case sensitive, bsii() is case insensitive.
374 */
375 int bsi(const char *string, const char **array, int size);
376 int bsii(const char *string, const char **array, int size);
377
378 char *src_set_fname(char *newname);
379 int32_t src_set_linnum(int32_t newline);
380 int32_t src_get_linnum(void);
381 /*
382 * src_get may be used if you simply want to know the source file and line.
383 * It is also used if you maintain private status about the source location
384 * It return 0 if the information was the same as the last time you
385 * checked, -1 if the name changed and (new-old) if just the line changed.
386 */
387 int src_get(int32_t *xline, char **xname);
388
389 char *nasm_strcat(const char *one, const char *two);
390
391 char *nasm_skip_spaces(const char *p);
392 char *nasm_skip_word(const char *p);
393 char *nasm_zap_spaces_fwd(char *p);
394 char *nasm_zap_spaces_rev(char *p);
395
396 const char *prefix_name(int);
397
398 #define ZERO_BUF_SIZE 4096
399 extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
400 size_t fwritezero(size_t bytes, FILE *fp);
401
overflow_general(int64_t value,int bytes)402 static inline bool overflow_general(int64_t value, int bytes)
403 {
404 int sbit;
405 int64_t vmax, vmin;
406
407 if (bytes >= 8)
408 return false;
409
410 sbit = (bytes << 3) - 1;
411 vmax = ((int64_t)2 << sbit) - 1;
412 vmin = -((int64_t)1 << sbit);
413
414 return value < vmin || value > vmax;
415 }
416
overflow_signed(int64_t value,int bytes)417 static inline bool overflow_signed(int64_t value, int bytes)
418 {
419 int sbit;
420 int64_t vmax, vmin;
421
422 if (bytes >= 8)
423 return false;
424
425 sbit = (bytes << 3) - 1;
426 vmax = ((int64_t)1 << sbit) - 1;
427 vmin = -((int64_t)1 << sbit);
428
429 return value < vmin || value > vmax;
430 }
431
overflow_unsigned(int64_t value,int bytes)432 static inline bool overflow_unsigned(int64_t value, int bytes)
433 {
434 int sbit;
435 int64_t vmax, vmin;
436
437 if (bytes >= 8)
438 return false;
439
440 sbit = (bytes << 3) - 1;
441 vmax = ((int64_t)2 << sbit) - 1;
442 vmin = 0;
443
444 return value < vmin || value > vmax;
445 }
446
447 int idata_bytes(int opcode);
448
449 /* check if value is power of 2 */
450 #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
451
452 #endif
453