2
* \file libyasm/coretype.h
3
* \brief YASM core types and utility functions.
6
* Copyright (C) 2001-2007 Peter Johnson
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
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.
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.
30
#ifndef YASM_CORETYPE_H
31
#define YASM_CORETYPE_H
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;
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;
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
60
typedef struct yasm_stdmac {
61
const char *parser; /**< Parser keyword */
62
const char *preproc; /**< Preprocessor keyword */
64
/** NULL-terminated array of standard macros. May be NULL if no standard
65
* macros should be added for this preprocessor.
70
/** YASM associated data callback structure. Many data structures can have
71
* arbitrary data associated with them.
73
typedef struct yasm_assoc_data_callback {
74
/** Free memory allocated for associated data.
75
* \param data associated data
77
void (*destroy) (/*@only@*/ void *data);
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
84
void (*print) (void *data, FILE *f, int indent_level);
85
} yasm_assoc_data_callback;
87
/** Set of collected error/warnings (opaque type).
88
* \see errwarn.h for details.
90
typedef struct yasm_errwarns yasm_errwarns;
92
/** Bytecode. \see bytecode.h for details and related functions. */
93
typedef struct yasm_bytecode yasm_bytecode;
95
/** Object. \see section.h for details and related functions. */
96
typedef struct yasm_object yasm_object;
98
/** Section (opaque type). \see section.h for related functions. */
99
typedef struct yasm_section yasm_section;
101
/** Symbol table (opaque type). \see symrec.h for related functions. */
102
typedef struct yasm_symtab yasm_symtab;
104
/** Symbol record (opaque type). \see symrec.h for related functions. */
105
typedef struct yasm_symrec yasm_symrec;
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.
114
typedef struct yasm_floatnum yasm_floatnum;
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.
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).
126
/*@null@*/ /*@only@*/ yasm_expr *abs;
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.
131
/*@null@*/ /*@dependent@*/ yasm_symrec *rel;
133
/** What the relative portion is in reference to. NULL if the default. */
134
/*@null@*/ /*@dependent@*/ yasm_symrec *wrt;
136
/** If the segment of the relative portion should be used, not the
137
* relative portion itself. Boolean.
139
unsigned int seg_of : 1;
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!
145
unsigned int rshift : 7;
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.
152
unsigned int curpos_rel : 1;
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.
158
unsigned int ip_rel : 1;
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.
164
unsigned int jump_target : 1;
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.
172
unsigned int section_rel : 1;
174
/** Indicates overflow warnings have been disabled for this value. */
175
unsigned int no_warn : 1;
177
/** Sign of the value. Nonzero if the final value should be treated as
178
* signed, 0 if it should be treated as signed.
180
unsigned int sign : 1;
182
/** Size of the value, in bits. */
183
unsigned int size : 8;
186
/** Maximum value of #yasm_value.rshift */
187
#define YASM_VALUE_RSHIFT_MAX 127
189
/** Line number mapping repository (opaque type). \see linemap.h for related
192
typedef struct yasm_linemap yasm_linemap;
194
/** Value/parameter pair (opaque type).
195
* \see valparam.h for related functions.
197
typedef struct yasm_valparam yasm_valparam;
198
/** List of value/parameters (opaque type).
199
* \see valparam.h for related functions.
201
typedef struct yasm_valparamhead yasm_valparamhead;
202
/** Directive list entry.
203
* \see valparam.h for details and related functions.
205
typedef struct yasm_directive yasm_directive;
207
/** An effective address.
208
* \see insn.h for related functions.
210
typedef struct yasm_effaddr yasm_effaddr;
213
* \see insn.h for related functions.
215
typedef struct yasm_insn yasm_insn;
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. */
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.
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
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
271
* \return Nonzero if an error occurred, 0 otherwise.
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);
277
/** Convert a symbol reference to its byte representation. Usually implemented
278
* by object formats and debug formats to keep track of relocations generated
281
* \param bc current bytecode (usually passed into higher-level
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
292
* \return Nonzero if an error occurred, 0 otherwise.
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);
298
/** Sort an array using merge sort algorithm.
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
306
int yasm__mergesort(void *base, size_t nmemb, size_t size,
307
int (*compar)(const void *, const void *));
309
/** Separate string by delimiters.
311
* \param stringp string
312
* \param delim set of 1 or more delimiters
313
* \return First/next substring.
316
/*@null@*/ char *yasm__strsep(char **stringp, const char *delim);
318
/** Compare two strings, ignoring case differences.
322
* \return 0 if strings are equal, -1 if s1<s2, 1 if s1>s2.
325
int yasm__strcasecmp(const char *s1, const char *s2);
327
/** Compare portion of two strings, ignoring case differences.
331
* \param n maximum number of characters to compare
332
* \return 0 if strings are equal, -1 if s1<s2, 1 if s1>s2.
335
int yasm__strncasecmp(const char *s1, const char *s2, size_t n);
337
/** strdup() implementation using yasm_xmalloc().
340
* \return Newly allocated duplicate string.
343
/*@only@*/ char *yasm__xstrdup(const char *str);
345
/** strndup() implementation using yasm_xmalloc().
348
* \param max maximum number of characters to copy
349
* \return Newly allocated duplicate string.
352
/*@only@*/ char *yasm__xstrndup(const char *str, size_t max);
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.
361
extern /*@only@*/ /*@out@*/ void * (*yasm_xmalloc) (size_t size);
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.
371
extern /*@only@*/ void * (*yasm_xcalloc) (size_t nelem, size_t elsize);
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.
381
extern /*@only@*/ void * (*yasm_xrealloc)
382
(/*@only@*/ /*@out@*/ /*@returned@*/ /*@null@*/ void *oldmem, size_t size)
383
/*@modifies oldmem@*/;
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
390
extern void (*yasm_xfree) (/*@only@*/ /*@out@*/ /*@null@*/ void *p)