1 /** 2 * \file libyasm/coretype.h 3 * \brief YASM core types and utility functions. 4 * 5 * \license 6 * Copyright (C) 2001-2007 Peter Johnson 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * 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 copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * \endlicense 29 */ 30 #ifndef YASM_CORETYPE_H 31 #define YASM_CORETYPE_H 32 33 #ifndef YASM_LIB_DECL 34 #define YASM_LIB_DECL 35 #endif 36 37 /** Architecture instance (mostly opaque type). \see arch.h for details. */ 38 typedef struct yasm_arch yasm_arch; 39 /** Preprocessor interface. \see preproc.h for details. */ 40 typedef struct yasm_preproc yasm_preproc; 41 /** Parser instance (mostly opaque type). \see parser.h for details. */ 42 typedef struct yasm_parser yasm_parser; 43 /** Object format interface. \see objfmt.h for details. */ 44 typedef struct yasm_objfmt yasm_objfmt; 45 /** Debug format interface. \see dbgfmt.h for details. */ 46 typedef struct yasm_dbgfmt yasm_dbgfmt; 47 /** List format interface. \see listfmt.h for details. */ 48 typedef struct yasm_listfmt yasm_listfmt; 49 50 /** Object format module interface. \see objfmt.h for details. */ 51 typedef struct yasm_objfmt_module yasm_objfmt_module; 52 /** Debug format module interface. \see dbgfmt.h for details. */ 53 typedef struct yasm_dbgfmt_module yasm_dbgfmt_module; 54 55 /** Standard macro structure for modules that allows association of a set of 56 * standard macros with a parser/preprocessor combination. 57 * A NULL-terminated array of these structures is used in a number of module 58 * interfaces. 59 */ 60 typedef struct yasm_stdmac { 61 const char *parser; /**< Parser keyword */ 62 const char *preproc; /**< Preprocessor keyword */ 63 64 /** NULL-terminated array of standard macros. May be NULL if no standard 65 * macros should be added for this preprocessor. 66 */ 67 const char **macros; 68 } yasm_stdmac; 69 70 /** YASM associated data callback structure. Many data structures can have 71 * arbitrary data associated with them. 72 */ 73 typedef struct yasm_assoc_data_callback { 74 /** Free memory allocated for associated data. 75 * \param data associated data 76 */ 77 void (*destroy) (/*@only@*/ void *data); 78 79 /** Print a description of allocated data. For debugging purposes. 80 * \param data associated data 81 * \param f output file 82 * \param indent_level indentation level 83 */ 84 void (*print) (void *data, FILE *f, int indent_level); 85 } yasm_assoc_data_callback; 86 87 /** Set of collected error/warnings (opaque type). 88 * \see errwarn.h for details. 89 */ 90 typedef struct yasm_errwarns yasm_errwarns; 91 92 /** Bytecode. \see bytecode.h for details and related functions. */ 93 typedef struct yasm_bytecode yasm_bytecode; 94 95 /** Object. \see section.h for details and related functions. */ 96 typedef struct yasm_object yasm_object; 97 98 /** Section (opaque type). \see section.h for related functions. */ 99 typedef struct yasm_section yasm_section; 100 101 /** Symbol table (opaque type). \see symrec.h for related functions. */ 102 typedef struct yasm_symtab yasm_symtab; 103 104 /** Symbol record (opaque type). \see symrec.h for related functions. */ 105 typedef struct yasm_symrec yasm_symrec; 106 107 /** Expression. \see expr.h for details and related functions. */ 108 typedef struct yasm_expr yasm_expr; 109 /** Integer value (opaque type). \see intnum.h for related functions. */ 110 typedef struct yasm_intnum yasm_intnum; 111 /** Floating point value (opaque type). 112 * \see floatnum.h for related functions. 113 */ 114 typedef struct yasm_floatnum yasm_floatnum; 115 116 /** A value. May be absolute or relative. Outside the parser, yasm_expr 117 * should only be used for absolute exprs. Anything that could contain 118 * a relocatable value should use this structure instead. 119 * \see value.h for related functions. 120 */ 121 typedef struct yasm_value { 122 /** The absolute portion of the value. May contain *differences* between 123 * symrecs but not standalone symrecs. May be NULL if there is no 124 * absolute portion (e.g. the absolute portion is 0). 125 */ 126 /*@null@*/ /*@only@*/ yasm_expr *abs; 127 128 /** The relative portion of the value. This is the portion that may 129 * need to generate a relocation. May be NULL if no relative portion. 130 */ 131 /*@null@*/ /*@dependent@*/ yasm_symrec *rel; 132 133 /** What the relative portion is in reference to. NULL if the default. */ 134 /*@null@*/ /*@dependent@*/ yasm_symrec *wrt; 135 136 /** If the segment of the relative portion should be used, not the 137 * relative portion itself. Boolean. 138 */ 139 unsigned int seg_of : 1; 140 141 /** If the relative portion of the value should be shifted right 142 * (supported only by a few object formats). If just the absolute portion 143 * should be shifted, that must be in the abs expr, not here! 144 */ 145 unsigned int rshift : 7; 146 147 /** Indicates the relative portion of the value should be relocated 148 * relative to the current assembly position rather than relative to the 149 * section start. "Current assembly position" here refers to the starting 150 * address of the bytecode containing this value. Boolean. 151 */ 152 unsigned int curpos_rel : 1; 153 154 /** Indicates that curpos_rel was set due to IP-relative relocation; 155 * in some objfmt/arch combinations (e.g. win64/x86-amd64) this info 156 * is needed to generate special relocations. 157 */ 158 unsigned int ip_rel : 1; 159 160 /** Indicates the value is a jump target address (rather than a simple 161 * data address). In some objfmt/arch combinations (e.g. macho/amd64) 162 * this info is needed to generate special relocations. 163 */ 164 unsigned int jump_target : 1; 165 166 /** Indicates the relative portion of the value should be relocated 167 * relative to its own section start rather than relative to the 168 * section start of the bytecode containing this value. E.g. the value 169 * resulting from the relative portion should be the offset from its 170 * section start. Boolean. 171 */ 172 unsigned int section_rel : 1; 173 174 /** Indicates overflow warnings have been disabled for this value. */ 175 unsigned int no_warn : 1; 176 177 /** Sign of the value. Nonzero if the final value should be treated as 178 * signed, 0 if it should be treated as signed. 179 */ 180 unsigned int sign : 1; 181 182 /** Size of the value, in bits. */ 183 unsigned int size : 8; 184 } yasm_value; 185 186 /** Maximum value of #yasm_value.rshift */ 187 #define YASM_VALUE_RSHIFT_MAX 127 188 189 /** Line number mapping repository (opaque type). \see linemap.h for related 190 * functions. 191 */ 192 typedef struct yasm_linemap yasm_linemap; 193 194 /** Value/parameter pair (opaque type). 195 * \see valparam.h for related functions. 196 */ 197 typedef struct yasm_valparam yasm_valparam; 198 /** List of value/parameters (opaque type). 199 * \see valparam.h for related functions. 200 */ 201 typedef struct yasm_valparamhead yasm_valparamhead; 202 /** Directive list entry. 203 * \see valparam.h for details and related functions. 204 */ 205 typedef struct yasm_directive yasm_directive; 206 207 /** An effective address. 208 * \see insn.h for related functions. 209 */ 210 typedef struct yasm_effaddr yasm_effaddr; 211 212 /** An instruction. 213 * \see insn.h for related functions. 214 */ 215 typedef struct yasm_insn yasm_insn; 216 217 /** Expression operators usable in #yasm_expr expressions. */ 218 typedef enum yasm_expr_op { 219 YASM_EXPR_IDENT, /**< No operation, just a value. */ 220 YASM_EXPR_ADD, /**< Arithmetic addition (+). */ 221 YASM_EXPR_SUB, /**< Arithmetic subtraction (-). */ 222 YASM_EXPR_MUL, /**< Arithmetic multiplication (*). */ 223 YASM_EXPR_DIV, /**< Arithmetic unsigned division. */ 224 YASM_EXPR_SIGNDIV, /**< Arithmetic signed division. */ 225 YASM_EXPR_MOD, /**< Arithmetic unsigned modulus. */ 226 YASM_EXPR_SIGNMOD, /**< Arithmetic signed modulus. */ 227 YASM_EXPR_NEG, /**< Arithmetic negation (-). */ 228 YASM_EXPR_NOT, /**< Bitwise negation. */ 229 YASM_EXPR_OR, /**< Bitwise OR. */ 230 YASM_EXPR_AND, /**< Bitwise AND. */ 231 YASM_EXPR_XOR, /**< Bitwise XOR. */ 232 YASM_EXPR_XNOR, /**< Bitwise XNOR. */ 233 YASM_EXPR_NOR, /**< Bitwise NOR. */ 234 YASM_EXPR_SHL, /**< Shift left (logical). */ 235 YASM_EXPR_SHR, /**< Shift right (logical). */ 236 YASM_EXPR_LOR, /**< Logical OR. */ 237 YASM_EXPR_LAND, /**< Logical AND. */ 238 YASM_EXPR_LNOT, /**< Logical negation. */ 239 YASM_EXPR_LXOR, /**< Logical XOR. */ 240 YASM_EXPR_LXNOR, /**< Logical XNOR. */ 241 YASM_EXPR_LNOR, /**< Logical NOR. */ 242 YASM_EXPR_LT, /**< Less than comparison. */ 243 YASM_EXPR_GT, /**< Greater than comparison. */ 244 YASM_EXPR_EQ, /**< Equality comparison. */ 245 YASM_EXPR_LE, /**< Less than or equal to comparison. */ 246 YASM_EXPR_GE, /**< Greater than or equal to comparison. */ 247 YASM_EXPR_NE, /**< Not equal comparison. */ 248 YASM_EXPR_NONNUM, /**< Start of non-numeric operations (not an op). */ 249 YASM_EXPR_SEG, /**< SEG operator (gets segment portion of address). */ 250 YASM_EXPR_WRT, /**< WRT operator (gets offset of address relative to 251 * some other segment). */ 252 YASM_EXPR_SEGOFF /**< The ':' in segment:offset. */ 253 } yasm_expr_op; 254 255 /** Convert yasm_value to its byte representation. Usually implemented by 256 * object formats to keep track of relocations and verify legal expressions. 257 * Must put the value into the least significant bits of the destination, 258 * unless shifted into more significant bits by the shift parameter. The 259 * destination bits must be cleared before being set. 260 * \param value value 261 * \param buf buffer for byte representation 262 * \param destsize destination size (in bytes) 263 * \param offset offset (in bytes) of the expr contents from the start 264 * of the bytecode (needed for relative) 265 * \param bc current bytecode (usually passed into higher-level 266 * calling function) 267 * \param warn enables standard warnings: zero for none; 268 * nonzero for overflow/underflow floating point warnings 269 * \param d objfmt-specific data (passed into higher-level calling 270 * function) 271 * \return Nonzero if an error occurred, 0 otherwise. 272 */ 273 typedef int (*yasm_output_value_func) 274 (yasm_value *value, /*@out@*/ unsigned char *buf, unsigned int destsize, 275 unsigned long offset, yasm_bytecode *bc, int warn, /*@null@*/ void *d); 276 277 /** Convert a symbol reference to its byte representation. Usually implemented 278 * by object formats and debug formats to keep track of relocations generated 279 * by themselves. 280 * \param sym symbol 281 * \param bc current bytecode (usually passed into higher-level 282 * calling function) 283 * \param buf buffer for byte representation 284 * \param destsize destination size (in bytes) 285 * \param valsize size (in bits) 286 * \param warn enables standard warnings: zero for none; 287 * nonzero for overflow/underflow floating point warnings; 288 * negative for signed integer warnings, 289 * positive for unsigned integer warnings 290 * \param d objfmt-specific data (passed into higher-level calling 291 * function) 292 * \return Nonzero if an error occurred, 0 otherwise. 293 */ 294 typedef int (*yasm_output_reloc_func) 295 (yasm_symrec *sym, yasm_bytecode *bc, unsigned char *buf, 296 unsigned int destsize, unsigned int valsize, int warn, void *d); 297 298 /** Sort an array using merge sort algorithm. 299 * \internal 300 * \param base base of array 301 * \param nmemb number of elements in array 302 * \param size size of each array element 303 * \param compar element comparison function 304 */ 305 YASM_LIB_DECL 306 int yasm__mergesort(void *base, size_t nmemb, size_t size, 307 int (*compar)(const void *, const void *)); 308 309 /** Separate string by delimiters. 310 * \internal 311 * \param stringp string 312 * \param delim set of 1 or more delimiters 313 * \return First/next substring. 314 */ 315 YASM_LIB_DECL 316 /*@null@*/ char *yasm__strsep(char **stringp, const char *delim); 317 318 /** Compare two strings, ignoring case differences. 319 * \internal 320 * \param s1 string 1 321 * \param s2 string 2 322 * \return 0 if strings are equal, -1 if s1<s2, 1 if s1>s2. 323 */ 324 YASM_LIB_DECL 325 int yasm__strcasecmp(const char *s1, const char *s2); 326 327 /** Compare portion of two strings, ignoring case differences. 328 * \internal 329 * \param s1 string 1 330 * \param s2 string 2 331 * \param n maximum number of characters to compare 332 * \return 0 if strings are equal, -1 if s1<s2, 1 if s1>s2. 333 */ 334 YASM_LIB_DECL 335 int yasm__strncasecmp(const char *s1, const char *s2, size_t n); 336 337 /** strdup() implementation using yasm_xmalloc(). 338 * \internal 339 * \param str string 340 * \return Newly allocated duplicate string. 341 */ 342 YASM_LIB_DECL 343 /*@only@*/ char *yasm__xstrdup(const char *str); 344 345 /** strndup() implementation using yasm_xmalloc(). 346 * \internal 347 * \param str string 348 * \param max maximum number of characters to copy 349 * \return Newly allocated duplicate string. 350 */ 351 YASM_LIB_DECL 352 /*@only@*/ char *yasm__xstrndup(const char *str, size_t max); 353 354 /** Error-checking memory allocation. A default implementation is provided 355 * that calls yasm_fatal() on allocation errors. 356 * A replacement should \em never return NULL. 357 * \param size number of bytes to allocate 358 * \return Allocated memory block. 359 */ 360 YASM_LIB_DECL 361 extern /*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size); 362 363 /** Error-checking memory allocation (with clear-to-0). A default 364 * implementation is provided that calls yasm_fatal() on allocation errors. 365 * A replacement should \em never return NULL. 366 * \param size number of elements to allocate 367 * \param elsize size (in bytes) of each element 368 * \return Allocated and cleared memory block. 369 */ 370 YASM_LIB_DECL 371 extern /*@only@*/ void * (*yasm_xcalloc) (size_t nelem, size_t elsize); 372 373 /** Error-checking memory reallocation. A default implementation is provided 374 * that calls yasm_fatal() on allocation errors. A replacement should 375 * \em never return NULL. 376 * \param oldmem memory block to resize 377 * \param elsize new size, in bytes 378 * \return Re-allocated memory block. 379 */ 380 YASM_LIB_DECL 381 extern /*@only@*/ void * (*yasm_xrealloc) 382 (/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size) 383 /*@modifies oldmem@*/; 384 385 /** Error-checking memory deallocation. A default implementation is provided 386 * that calls yasm_fatal() on allocation errors. 387 * \param p memory block to free 388 */ 389 YASM_LIB_DECL 390 extern void (*yasm_xfree) (/*@only@*/ /*@out@*/ /*@null@*/ void *p) 391 /*@modifies p@*/; 392 393 #endif 394