nasmlib.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* ----------------------------------------------------------------------- *
  2. *
  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.
  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. * nasmlib.h header file for nasmlib.c
  35. */
  36. #ifndef NASM_NASMLIB_H
  37. #define NASM_NASMLIB_H
  38. #include "compiler.h"
  39. #include "bytesex.h"
  40. #include <ctype.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #ifdef HAVE_STRINGS_H
  44. # include <strings.h>
  45. #endif
  46. /*
  47. * tolower table -- avoids a function call on some platforms.
  48. * NOTE: unlike the tolower() function in ctype, EOF is *NOT*
  49. * a permitted value, for obvious reasons.
  50. */
  51. void tolower_init(void);
  52. extern unsigned char nasm_tolower_tab[256];
  53. #define nasm_tolower(x) nasm_tolower_tab[(unsigned char)(x)]
  54. /* Wrappers around <ctype.h> functions */
  55. /* These are only valid for values that cannot include EOF */
  56. #define nasm_isspace(x) isspace((unsigned char)(x))
  57. #define nasm_isalpha(x) isalpha((unsigned char)(x))
  58. #define nasm_isdigit(x) isdigit((unsigned char)(x))
  59. #define nasm_isalnum(x) isalnum((unsigned char)(x))
  60. #define nasm_isxdigit(x) isxdigit((unsigned char)(x))
  61. /*
  62. * Wrappers around malloc, realloc and free. nasm_malloc will
  63. * fatal-error and die rather than return NULL; nasm_realloc will
  64. * do likewise, and will also guarantee to work right on being
  65. * passed a NULL pointer; nasm_free will do nothing if it is passed
  66. * a NULL pointer.
  67. */
  68. void * safe_malloc(1) nasm_malloc(size_t);
  69. void * safe_malloc(1) nasm_zalloc(size_t);
  70. void * safe_malloc2(1,2) nasm_calloc(size_t, size_t);
  71. void * safe_realloc(2) nasm_realloc(void *, size_t);
  72. void nasm_free(void *);
  73. char * safe_alloc nasm_strdup(const char *);
  74. char * safe_alloc nasm_strndup(const char *, size_t);
  75. char * safe_alloc nasm_strcat(const char *one, const char *two);
  76. char * safe_alloc end_with_null nasm_strcatn(const char *one, ...);
  77. /* Assert the argument is a pointer without evaluating it */
  78. #define nasm_assert_pointer(p) ((void)sizeof(*(p)))
  79. #define nasm_new(p) ((p) = nasm_zalloc(sizeof(*(p))))
  80. #define nasm_newn(p,n) ((p) = nasm_calloc(sizeof(*(p)),(n)))
  81. /*
  82. * This is broken on platforms where there are pointers which don't
  83. * match void * in their internal layout. It unfortunately also
  84. * loses any "const" part of the argument, although hopefully the
  85. * compiler will warn in that case.
  86. */
  87. #define nasm_delete(p) \
  88. do { \
  89. void **_pp = (void **)&(p); \
  90. nasm_assert_pointer(p); \
  91. nasm_free(*_pp); \
  92. *_pp = NULL; \
  93. } while (0)
  94. #define nasm_zero(x) (memset(&(x), 0, sizeof(x)))
  95. #define nasm_zeron(p,n) (memset((p), 0, (n)*sizeof(*(p))))
  96. /*
  97. * Wrappers around fread()/fwrite() which fatal-errors on failure.
  98. * For fread(), only use this if EOF is supposed to be a fatal error!
  99. */
  100. void nasm_read(void *, size_t, FILE *);
  101. void nasm_write(const void *, size_t, FILE *);
  102. /*
  103. * NASM assert failure
  104. */
  105. fatal_func nasm_assert_failed(const char *, int, const char *);
  106. #define nasm_assert(x) \
  107. do { \
  108. if (unlikely(!(x))) \
  109. nasm_assert_failed(__FILE__,__LINE__,#x); \
  110. } while (0)
  111. /*
  112. * NASM failure at build time if the argument is false
  113. */
  114. #ifdef static_assert
  115. # define nasm_static_assert(x) static_assert(x, #x)
  116. #elif defined(HAVE_FUNC_ATTRIBUTE_ERROR) && defined(__OPTIMIZE__)
  117. # define nasm_static_assert(x) \
  118. if (!(x)) { \
  119. extern void __attribute__((error("assertion " #x " failed"))) \
  120. _nasm_static_fail(void); \
  121. _nasm_static_fail(); \
  122. }
  123. #else
  124. /* See http://www.drdobbs.com/compile-time-assertions/184401873 */
  125. # define nasm_static_assert(x) \
  126. do { enum { _static_assert_failed = 1/(!!(x)) }; } while (0)
  127. #endif
  128. /* Utility function to generate a string for an invalid enum */
  129. const char *invalid_enum_str(int);
  130. /*
  131. * ANSI doesn't guarantee the presence of `stricmp' or
  132. * `strcasecmp'.
  133. */
  134. #if defined(HAVE_STRCASECMP)
  135. #define nasm_stricmp strcasecmp
  136. #elif defined(HAVE_STRICMP)
  137. #define nasm_stricmp stricmp
  138. #else
  139. int pure_func nasm_stricmp(const char *, const char *);
  140. #endif
  141. #if defined(HAVE_STRNCASECMP)
  142. #define nasm_strnicmp strncasecmp
  143. #elif defined(HAVE_STRNICMP)
  144. #define nasm_strnicmp strnicmp
  145. #else
  146. int pure_func nasm_strnicmp(const char *, const char *, size_t);
  147. #endif
  148. int pure_func nasm_memicmp(const char *, const char *, size_t);
  149. #if defined(HAVE_STRSEP)
  150. #define nasm_strsep strsep
  151. #else
  152. char *nasm_strsep(char **stringp, const char *delim);
  153. #endif
  154. #ifndef HAVE_DECL_STRNLEN
  155. size_t pure_func strnlen(const char *, size_t);
  156. #endif
  157. /* This returns the numeric value of a given 'digit'. */
  158. #define numvalue(c) ((c) >= 'a' ? (c) - 'a' + 10 : (c) >= 'A' ? (c) - 'A' + 10 : (c) - '0')
  159. /*
  160. * Convert a string into a number, using NASM number rules. Sets
  161. * `*error' to true if an error occurs, and false otherwise.
  162. */
  163. int64_t readnum(const char *str, bool *error);
  164. /*
  165. * Convert a character constant into a number. Sets
  166. * `*warn' to true if an overflow occurs, and false otherwise.
  167. * str points to and length covers the middle of the string,
  168. * without the quotes.
  169. */
  170. int64_t readstrnum(char *str, int length, bool *warn);
  171. /*
  172. * seg_alloc: allocate a hitherto unused segment number.
  173. */
  174. int32_t seg_alloc(void);
  175. /*
  176. * Add/replace or remove an extension to the end of a filename
  177. */
  178. const char *filename_set_extension(const char *inname, const char *extension);
  179. /*
  180. * Utility macros...
  181. *
  182. * This is a useful #define which I keep meaning to use more often:
  183. * the number of elements of a statically defined array.
  184. */
  185. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  186. /*
  187. * List handling
  188. *
  189. * list_for_each - regular iterator over list
  190. * list_for_each_safe - the same but safe against list items removal
  191. * list_last - find the last element in a list
  192. */
  193. #define list_for_each(pos, head) \
  194. for (pos = head; pos; pos = pos->next)
  195. #define list_for_each_safe(pos, n, head) \
  196. for (pos = head, n = (pos ? pos->next : NULL); pos; \
  197. pos = n, n = (n ? n->next : NULL))
  198. #define list_last(pos, head) \
  199. for (pos = head; pos && pos->next; pos = pos->next) \
  200. ;
  201. #define list_reverse(head, prev, next) \
  202. do { \
  203. if (!head || !head->next) \
  204. break; \
  205. prev = NULL; \
  206. while (head) { \
  207. next = head->next; \
  208. head->next = prev; \
  209. prev = head; \
  210. head = next; \
  211. } \
  212. head = prev; \
  213. } while (0)
  214. /*
  215. * Power of 2 align helpers
  216. */
  217. #undef ALIGN_MASK /* Some BSD flavors define these in system headers */
  218. #undef ALIGN
  219. #define ALIGN_MASK(v, mask) (((v) + (mask)) & ~(mask))
  220. #define ALIGN(v, a) ALIGN_MASK(v, (a) - 1)
  221. #define IS_ALIGNED(v, a) (((v) & ((a) - 1)) == 0)
  222. /*
  223. * Routines to write littleendian data to a file
  224. */
  225. #define fwriteint8_t(d,f) putc(d,f)
  226. void fwriteint16_t(uint16_t data, FILE * fp);
  227. void fwriteint32_t(uint32_t data, FILE * fp);
  228. void fwriteint64_t(uint64_t data, FILE * fp);
  229. void fwriteaddr(uint64_t data, int size, FILE * fp);
  230. /*
  231. * Binary search routine. Returns index into `array' of an entry
  232. * matching `string', or <0 if no match. `array' is taken to
  233. * contain `size' elements.
  234. *
  235. * bsi() is case sensitive, bsii() is case insensitive.
  236. */
  237. int bsi(const char *string, const char **array, int size);
  238. int bsii(const char *string, const char **array, int size);
  239. /*
  240. * These functions are used to keep track of the source code file and name.
  241. */
  242. void src_init(void);
  243. void src_free(void);
  244. const char *src_set_fname(const char *newname);
  245. const char *src_get_fname(void);
  246. int32_t src_set_linnum(int32_t newline);
  247. int32_t src_get_linnum(void);
  248. /* Can be used when there is no need for the old information */
  249. void src_set(int32_t line, const char *filename);
  250. /*
  251. * src_get gets both the source file name and line.
  252. * It is also used if you maintain private status about the source location
  253. * It return 0 if the information was the same as the last time you
  254. * checked, -2 if the name changed and (new-old) if just the line changed.
  255. */
  256. int32_t src_get(int32_t *xline, const char **xname);
  257. char *nasm_skip_spaces(const char *p);
  258. char *nasm_skip_word(const char *p);
  259. char *nasm_zap_spaces_fwd(char *p);
  260. char *nasm_zap_spaces_rev(char *p);
  261. char *nasm_trim_spaces(char *p);
  262. char *nasm_get_word(char *p, char **tail);
  263. char *nasm_opt_val(char *p, char **opt, char **val);
  264. /*
  265. * Converts a relative pathname rel_path into an absolute path name.
  266. *
  267. * The buffer returned must be freed by the caller
  268. */
  269. char * safe_alloc nasm_realpath(const char *rel_path);
  270. /*
  271. * Path-splitting and merging functions
  272. */
  273. char * safe_alloc nasm_dirname(const char *path);
  274. char * safe_alloc nasm_basename(const char *path);
  275. char * safe_alloc nasm_catfile(const char *dir, const char *path);
  276. const char * pure_func prefix_name(int);
  277. /*
  278. * Wrappers around fopen()... for future change to a dedicated structure
  279. */
  280. enum file_flags {
  281. NF_BINARY = 0x00000000, /* Binary file (default) */
  282. NF_TEXT = 0x00000001, /* Text file */
  283. NF_NONFATAL = 0x00000000, /* Don't die on open failure (default) */
  284. NF_FATAL = 0x00000002, /* Die on open failure */
  285. NF_FORMAP = 0x00000004 /* Intended to use nasm_map_file() */
  286. };
  287. FILE *nasm_open_read(const char *filename, enum file_flags flags);
  288. FILE *nasm_open_write(const char *filename, enum file_flags flags);
  289. /* Probe for existence of a file */
  290. bool nasm_file_exists(const char *filename);
  291. #define ZERO_BUF_SIZE 65536 /* Default value */
  292. #if defined(BUFSIZ) && (BUFSIZ > ZERO_BUF_SIZE)
  293. # undef ZERO_BUF_SIZE
  294. # define ZERO_BUF_SIZE BUFSIZ
  295. #endif
  296. extern const uint8_t zero_buffer[ZERO_BUF_SIZE];
  297. /* Missing fseeko/ftello */
  298. #ifndef HAVE_FSEEKO
  299. # undef off_t /* Just in case it is a macro */
  300. # ifdef HAVE__FSEEKI64
  301. # define fseeko _fseeki64
  302. # define ftello _ftelli64
  303. # define off_t int64_t
  304. # else
  305. # define fseeko fseek
  306. # define ftello ftell
  307. # define off_t long
  308. # endif
  309. #endif
  310. const void *nasm_map_file(FILE *fp, off_t start, off_t len);
  311. void nasm_unmap_file(const void *p, size_t len);
  312. off_t nasm_file_size(FILE *f);
  313. off_t nasm_file_size_by_path(const char *pathname);
  314. bool nasm_file_time(time_t *t, const char *pathname);
  315. void fwritezero(off_t bytes, FILE *fp);
  316. static inline bool const_func overflow_general(int64_t value, int bytes)
  317. {
  318. int sbit;
  319. int64_t vmax, vmin;
  320. if (bytes >= 8)
  321. return false;
  322. sbit = (bytes << 3) - 1;
  323. vmax = ((int64_t)2 << sbit) - 1;
  324. vmin = -((int64_t)2 << sbit);
  325. return value < vmin || value > vmax;
  326. }
  327. static inline bool const_func overflow_signed(int64_t value, int bytes)
  328. {
  329. int sbit;
  330. int64_t vmax, vmin;
  331. if (bytes >= 8)
  332. return false;
  333. sbit = (bytes << 3) - 1;
  334. vmax = ((int64_t)1 << sbit) - 1;
  335. vmin = -((int64_t)1 << sbit);
  336. return value < vmin || value > vmax;
  337. }
  338. static inline bool const_func overflow_unsigned(int64_t value, int bytes)
  339. {
  340. int sbit;
  341. int64_t vmax, vmin;
  342. if (bytes >= 8)
  343. return false;
  344. sbit = (bytes << 3) - 1;
  345. vmax = ((int64_t)2 << sbit) - 1;
  346. vmin = 0;
  347. return value < vmin || value > vmax;
  348. }
  349. static inline int64_t const_func signed_bits(int64_t value, int bits)
  350. {
  351. if (bits < 64) {
  352. value &= ((int64_t)1 << bits) - 1;
  353. if (value & (int64_t)1 << (bits - 1))
  354. value |= (int64_t)((uint64_t)-1 << bits);
  355. }
  356. return value;
  357. }
  358. /* check if value is power of 2 */
  359. #define is_power2(v) ((v) && ((v) & ((v) - 1)) == 0)
  360. #endif