public/sourcepawn/sp_file_headers.h
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2471 194be299b700
child 3114 a30528e5cb8a
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourcePawn
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SPFILE_HEADERS_H
    33 #define _INCLUDE_SPFILE_HEADERS_H
    34 
    35 /** 
    36  * @file sp_file_headers.h
    37  * @brief Defines the structure present in a SourcePawn compiled binary.
    38  *
    39  * Note: These structures should be 1-byte packed to match the file format.
    40  */
    41 
    42 #include <stddef.h>
    43 #if defined __GNUC__ || defined HAVE_STDINT_
    44 #include <stdint.h>
    45 #else
    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 */
    55  #define HAVE_STDINT_H
    56  #endif
    57 #endif
    58 
    59 #define SPFILE_MAGIC	0x53504646		/**< Source Pawn File Format (SPFF) */
    60 #define SPFILE_VERSION	0x0102			/**< File format version */
    61 
    62 //:TODO: better compiler/nix support
    63 #if defined __GNUC__
    64 	#pragma pack(1)         /* structures must be packed (byte-aligned) */
    65 #else
    66 	#pragma pack(push)
    67 	#pragma pack(1)         /* structures must be packed (byte-aligned) */
    68 #endif
    69 
    70 #define SPFILE_COMPRESSION_NONE		0		/**< No compression in file */
    71 #define SPFILE_COMPRESSION_GZ		1		/**< GZ compression */
    72 
    73 /**
    74  * @brief File section header format.
    75  */
    76 typedef struct sp_file_section_s
    77 {
    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 */
    81 } sp_file_section_t;
    82 
    83 /**
    84  * @brief File header format.  If compression is 0, then disksize may be 0 
    85  * to mean that only the imagesize is needed.
    86  */
    87 typedef struct sp_file_hdr_s
    88 {
    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) */
    97 } sp_file_hdr_t;
    98 
    99 #define SP_FLAG_DEBUG	(1<<0)		/**< Debug information is present in the file */
   100 
   101 #define SP_CODEVERS_JIT1       9    /**< Code version for JIT1 */
   102 #define SP_CODEVERS_JIT2      10    /**< Code version for JIT2 */    
   103 
   104 /**
   105  * @brief File-encoded format of the ".code" section.
   106  */
   107 typedef struct sp_file_code_s
   108 {
   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 */
   115 } sp_file_code_t;
   116 
   117 /** 
   118  * @brief File-encoded format of the ".data" section.
   119  */
   120 typedef struct sp_file_data_s
   121 {
   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) */
   125 } sp_file_data_t;
   126 
   127 /**
   128  * @brief File-encoded format of the ".publics" section.
   129  */
   130 typedef struct sp_file_publics_s
   131 {
   132 	uint32_t	address;		/**< Address relative to code section */
   133 	uint32_t	name;			/**< Index into nametable */
   134 } sp_file_publics_t;
   135 
   136 /**
   137  * @brief File-encoded format of the ".natives" section.
   138  */
   139 typedef struct sp_file_natives_s
   140 {
   141 	uint32_t	name;			/**< Index into nametable */
   142 } sp_file_natives_t;
   143 
   144 /**
   145  * @brief File-encoded format of the ".pubvars" section.
   146  */
   147 typedef struct sp_file_pubvars_s
   148 {
   149 	uint32_t	address;		/**< Address relative to the DAT section */
   150 	uint32_t	name;			/**< Index into nametable */
   151 } sp_file_pubvars_t;
   152 
   153 /**
   154  * @brief File-encoded tag info.
   155  */
   156 typedef struct sp_file_tag_s
   157 {
   158 	uint32_t	tag_id;			/**< Tag ID from compiler */
   159 	uint32_t	name;			/**< Index into nametable */
   160 } sp_file_tag_t;
   161 
   162 /**
   163  * @brief File-encoded debug information table.
   164  */
   165 typedef struct sp_fdbg_info_s
   166 {
   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 */
   171 } sp_fdbg_info_t;
   172 
   173 /**
   174  * @brief File-encoded debug file table.
   175  */
   176 typedef struct sp_fdbg_file_s
   177 {
   178 	uint32_t	addr;		/**< Address into code */
   179 	uint32_t	name;		/**< Offset into debug nametable */
   180 } sp_fdbg_file_t;
   181 
   182 /**
   183  * @brief File-encoded debug line table.
   184  */
   185 typedef struct sp_fdbg_line_s
   186 {
   187 	uint32_t	addr;		/**< Address into code */
   188 	uint32_t	line;		/**< Line number */
   189 } sp_fdbg_line_t;
   190 
   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. */
   197 
   198 /**
   199  * @brief File-encoded debug symbol information.
   200  */
   201 typedef struct sp_fdbg_symbol_s
   202 {
   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 */
   211 } sp_fdbg_symbol_t;
   212 
   213 /**
   214  * @brief File-encoded debug symbol array dimension info.
   215  */
   216 typedef struct sp_fdbg_arraydim_s
   217 {
   218 	int16_t		tagid;		/**< Tag id */
   219 	uint32_t	size;		/**< Size of dimension */
   220 } sp_fdbg_arraydim_t;
   221 
   222 /** Typedef for .names table */
   223 typedef char * sp_file_nametab_t;
   224 
   225 /**
   226  * @brief File encoding for the dbg.natives table.
   227  *
   228  * This header is followed by variable length entries of sp_fdbg_native.
   229  */
   230 typedef struct sp_fdbg_ntvtab_s
   231 {
   232 	uint32_t num_entries;	/**< Number of entries. */
   233 } sp_fdbg_ntvtab_t;
   234 
   235 #define SP_NTVDBG_VARARGS	(1<<0)		/**< Formal args are followed by '...' */
   236 
   237 /**
   238  * @brief File encoding of native debug info.
   239  *
   240  * Each entry is followed by an sp_fdbg_ntvarg_t for each narg.
   241  */
   242 typedef struct sp_fdbg_native_s
   243 {
   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. */
   248 } sp_fdbg_native_t;
   249 
   250 /**
   251  * @brief File encoding of native arguments.
   252  *
   253  * Each entry is followed by an sp_fdbg_arraydim_t for each dimcount.
   254  */
   255 typedef struct fp_fdbg_ntvarg_s
   256 {
   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 */
   261 } sp_fdbg_ntvarg_t;
   262 
   263 #if defined __linux__
   264 #pragma pack()    /* reset default packing */
   265 #else
   266 #pragma pack(pop) /* reset previous packing */
   267 #endif
   268 
   269 /**
   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.
   277  */
   278 
   279 /**
   280  * @brief Unpacked file-encoded debug symbol array dimension info.
   281  */
   282 typedef struct sp_u_fdbg_arraydim_s
   283 {
   284 	int16_t		tagid;		/**< Tag id */
   285 	uint32_t	size;		/**< Size of dimension */
   286 } sp_u_fdbg_arraydim_t;
   287 
   288 /**
   289  * @brief Unpacked file-encoded debug symbol information.
   290  */
   291 typedef struct sp_u_fdbg_symbol_s
   292 {
   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;
   302 
   303 
   304 #endif //_INCLUDE_SPFILE_HEADERS_H
   305