3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #ifndef _INCLUDE_SPFILE_HEADERS_H
33 #define _INCLUDE_SPFILE_HEADERS_H
36 * @file sp_file_headers.h
37 * @brief Defines the structure present in a SourcePawn compiled binary.
39 * Note: These structures should be 1-byte packed to match the file format.
43 #if defined __GNUC__ || defined HAVE_STDINT_
46 #if !defined HAVE_STDINT_H
47 typedef unsigned __int64 uint64_t; /**< 64bit unsigned integer */
48 typedef __int64 int64_t; /**< 64bit signed integer */
49 typedef unsigned __int32 uint32_t; /**< 32bit unsigned integer */
50 typedef __int32 int32_t; /**< 32bit signed integer */
51 typedef unsigned __int16 uint16_t; /**< 16bit unsigned integer */
52 typedef __int16 int16_t; /**< 16bit signed integer */
53 typedef unsigned __int8 uint8_t; /**< 8bit unsigned integer */
54 typedef __int8 int8_t; /**< 8bit signed integer */
59 #define SPFILE_MAGIC 0x53504646 /**< Source Pawn File Format (SPFF) */
60 #define SPFILE_VERSION 0x0102 /**< File format version */
62 //:TODO: better compiler/nix support
64 #pragma pack(1) /* structures must be packed (byte-aligned) */
67 #pragma pack(1) /* structures must be packed (byte-aligned) */
70 #define SPFILE_COMPRESSION_NONE 0 /**< No compression in file */
71 #define SPFILE_COMPRESSION_GZ 1 /**< GZ compression */
74 * @brief File section header format.
76 typedef struct sp_file_section_s
78 uint32_t nameoffs; /**< Relative offset into global string table */
79 uint32_t dataoffs; /**< Offset into the data section of the file */
80 uint32_t size; /**< Size of the section's entry in the data section */
84 * @brief File header format. If compression is 0, then disksize may be 0
85 * to mean that only the imagesize is needed.
87 typedef struct sp_file_hdr_s
89 uint32_t magic; /**< Magic number */
90 uint16_t version; /**< Version code */
91 uint8_t compression;/**< Compression algorithm */
92 uint32_t disksize; /**< Size on disk */
93 uint32_t imagesize; /**< Size in memory */
94 uint8_t sections; /**< Number of sections */
95 uint32_t stringtab; /**< Offset to string table */
96 uint32_t dataoffs; /**< Offset to file proper (any compression starts here) */
99 #define SP_FLAG_DEBUG (1<<0) /**< Debug information is present in the file */
101 #define SP_CODEVERS_JIT1 9 /**< Code version for JIT1 */
102 #define SP_CODEVERS_JIT2 10 /**< Code version for JIT2 */
105 * @brief File-encoded format of the ".code" section.
107 typedef struct sp_file_code_s
109 uint32_t codesize; /**< Codesize in bytes */
110 uint8_t cellsize; /**< Cellsize in bytes */
111 uint8_t codeversion; /**< Version of opcodes supported */
112 uint16_t flags; /**< Flags */
113 uint32_t main; /**< Address to "main," if any */
114 uint32_t code; /**< Relative offset to code */
118 * @brief File-encoded format of the ".data" section.
120 typedef struct sp_file_data_s
122 uint32_t datasize; /**< Size of data section in memory */
123 uint32_t memsize; /**< Total mem required (includes data) */
124 uint32_t data; /**< File offset to data (helper) */
128 * @brief File-encoded format of the ".publics" section.
130 typedef struct sp_file_publics_s
132 uint32_t address; /**< Address relative to code section */
133 uint32_t name; /**< Index into nametable */
137 * @brief File-encoded format of the ".natives" section.
139 typedef struct sp_file_natives_s
141 uint32_t name; /**< Index into nametable */
145 * @brief File-encoded format of the ".pubvars" section.
147 typedef struct sp_file_pubvars_s
149 uint32_t address; /**< Address relative to the DAT section */
150 uint32_t name; /**< Index into nametable */
154 * @brief File-encoded tag info.
156 typedef struct sp_file_tag_s
158 uint32_t tag_id; /**< Tag ID from compiler */
159 uint32_t name; /**< Index into nametable */
163 * @brief File-encoded debug information table.
165 typedef struct sp_fdbg_info_s
167 uint32_t num_files; /**< number of files */
168 uint32_t num_lines; /**< number of lines */
169 uint32_t num_syms; /**< number of symbols */
170 uint32_t num_arrays; /**< number of symbols which are arrays */
174 * @brief File-encoded debug file table.
176 typedef struct sp_fdbg_file_s
178 uint32_t addr; /**< Address into code */
179 uint32_t name; /**< Offset into debug nametable */
183 * @brief File-encoded debug line table.
185 typedef struct sp_fdbg_line_s
187 uint32_t addr; /**< Address into code */
188 uint32_t line; /**< Line number */
191 #define SP_SYM_VARIABLE 1 /**< Cell that has an address and that can be fetched directly (lvalue) */
192 #define SP_SYM_REFERENCE 2 /**< VARIABLE, but must be dereferenced */
193 #define SP_SYM_ARRAY 3 /**< Symbol is an array */
194 #define SP_SYM_REFARRAY 4 /**< An array passed by reference (i.e. a pointer) */
195 #define SP_SYM_FUNCTION 9 /**< Symbol is a function */
196 #define SP_SYM_VARARGS 11 /**< Variadic argument start. */
199 * @brief File-encoded debug symbol information.
201 typedef struct sp_fdbg_symbol_s
203 int32_t addr; /**< Address rel to DAT or stack frame */
204 int16_t tagid; /**< Tag id */
205 uint32_t codestart; /**< Start scope validity in code */
206 uint32_t codeend; /**< End scope validity in code */
207 uint8_t ident; /**< Variable type */
208 uint8_t vclass; /**< Scope class (local vs global) */
209 uint16_t dimcount; /**< Dimension count (for arrays) */
210 uint32_t name; /**< Offset into debug nametable */
214 * @brief File-encoded debug symbol array dimension info.
216 typedef struct sp_fdbg_arraydim_s
218 int16_t tagid; /**< Tag id */
219 uint32_t size; /**< Size of dimension */
220 } sp_fdbg_arraydim_t;
222 /** Typedef for .names table */
223 typedef char * sp_file_nametab_t;
226 * @brief File encoding for the dbg.natives table.
228 * This header is followed by variable length entries of sp_fdbg_native.
230 typedef struct sp_fdbg_ntvtab_s
232 uint32_t num_entries; /**< Number of entries. */
235 #define SP_NTVDBG_VARARGS (1<<0) /**< Formal args are followed by '...' */
238 * @brief File encoding of native debug info.
240 * Each entry is followed by an sp_fdbg_ntvarg_t for each narg.
242 typedef struct sp_fdbg_native_s
244 uint32_t index; /**< Native index in the plugin. */
245 uint32_t name; /**< Offset into debug nametable. */
246 int16_t tagid; /**< Return tag. */
247 uint16_t nargs; /**< Number of formal arguments. */
251 * @brief File encoding of native arguments.
253 * Each entry is followed by an sp_fdbg_arraydim_t for each dimcount.
255 typedef struct fp_fdbg_ntvarg_s
257 uint8_t ident; /**< Variable type */
258 int16_t tagid; /**< Tag id */
259 uint16_t dimcount; /**< Dimension count (for arrays) */
260 uint32_t name; /**< Offset into debug nametable */
263 #if defined __linux__
264 #pragma pack() /* reset default packing */
266 #pragma pack(pop) /* reset previous packing */
270 * Okay, my mistake here. I apologize. I changed the packing by accident and there is no
271 * version bump aside from the presence of the native debug table. Cat's out of the bag
272 * for SourceMod and we have no choice but to shim compat for the old version. For people
273 * parsing plugins on their own, use the presence of the native debug table to decide this.
274 * If there are no natives (really, there is a very very low chance of this), heuristics
275 * might be necessary. I've bumped the version to 0x0102 but there may have been plugins
276 * in the 0x0101 window with no natives.
280 * @brief Unpacked file-encoded debug symbol array dimension info.
282 typedef struct sp_u_fdbg_arraydim_s
284 int16_t tagid; /**< Tag id */
285 uint32_t size; /**< Size of dimension */
286 } sp_u_fdbg_arraydim_t;
289 * @brief Unpacked file-encoded debug symbol information.
291 typedef struct sp_u_fdbg_symbol_s
293 int32_t addr; /**< Address rel to DAT or stack frame */
294 int16_t tagid; /**< Tag id */
295 uint32_t codestart; /**< Start scope validity in code */
296 uint32_t codeend; /**< End scope validity in code */
297 uint8_t ident; /**< Variable type */
298 uint8_t vclass; /**< Scope class (local vs global) */
299 uint16_t dimcount; /**< Dimension count (for arrays) */
300 uint32_t name; /**< Offset into debug nametable */
301 } sp_u_fdbg_symbol_t;
304 #endif //_INCLUDE_SPFILE_HEADERS_H