/*  Disassembly Database for BASTARD */
/*  
 *  SECTION     -- Distinct regions of code or data within the program
 *  ADDRESS     -- Distinct code or data units within the program
 *  CODE        -- An address object interpreted as a machine instruction
 *  NAME        -- Unique text strings associated with address objects
 *  COMMENT     -- Text strings associated with address objects
 *  IMPORT      -- External code references in the program
 *  EXPORT      -- Program Entry Points
 *  STRING      -- Data regions considered strings
 *  FUNCTION    -- Code regions defined as a function
 *  F_INLINE    -- Code regions associated with C macro/inline function
 *  STRUCTURE   -- Data regions associated with a defined structure
 *  CONSTANT    -- User-defined enumerations [from .h files]
 *  SEER_MACRO  -- User-defined SeerC macros [char strings]
 */


database target {

	data file "address.db"   contains address;
	key  file "address.ix1"  contains address.rva;
	key  file "address.ix2"  contains address.pa;
	data file "section.db"   contains section;
	key  file "section.ix1"  contains section.name;
	key  file "section.ix2"  contains section.rva;
	data file "xref.db"      contains xref;
	key  file "xref.ix1"     contains xref.id;
	key  file "xref.ix2"     contains xref.from_rva;
	key  file "xref.ix2"     contains xref.to_rva;
	data file "code.db"      contains code;
	key  file "code.ix1"     contains code.rva;
	key  file "code.ix2"     contains code.dest;
	key  file "code.ix3"     contains code.src;
	key  file "code.ix4"     contains code.mnemonic;
	data file "int_code.db"  contains int_code;
	key  file "int_code.ix1" contains int_code.id;
	key  file "int_code.ix1" contains int_code.rva;
	data file "fin_code.db"  contains fin_code;
	key  file "fin_code.ix1" contains fin_code.id;
	key  file "fin_code.ix1" contains fin_code.rva;
	data file "export.db"    contains export;
	key  file "export.ix1"   contains export.rva;
	data file "import.db"    contains import;
	key  file "import.ix1"   contains import.rva;
	key  file "import.ix2"   contains import.library;
	data file "lib.db"    	contains lib;
	key  file "lib.ix1"   	contains lib.id;
	key  file "lib.ix2"   	contains lib.name;
	data file "string.db"    contains string;
	key  file "string.ix1"   contains string.rva;
	data file "name.db"      contains name;
	key  file "name.ix1"     contains name.rva;
	key  file "name.ix2"     contains name.name;
	data file "comment.db"   contains comment;
	key  file "comment.ix1"  contains comment.id;
	data file "function.db"   contains function;
	key  file "function.ix1"  contains function.id;
	key  file "function.ix2"  contains function.rva;
	data file "func_param.db"   contains func_param;
	key  file "func_param.ix1"  contains func_param.id;
	key  file "func_param.ix2"  contains func_param.func;
	data file "func_local.db"   contains func_local;
	key  file "func_local.ix1"  contains func_local.id;
	key  file "func_local.ix2"  contains func_local.func;
	data file "f_inline.db"    contains f_inline;
	key  file "f_inline.ix1"   contains f_inline.id;
	data file "inline_type.db"    contains inline_type;
	key  file "inline_type.ix1"   contains inline_type.id;
	key  file "inline_type.ix2"   contains inline_type.rva;
	data file "structure.db"   contains structure;
	key  file "structure.ix1"  contains structure.id;
	data file "struct_member.db"   contains struct_member;
	key  file "struct_member.ix1"  contains struct_member.id;
	key  file "struct_member.ix2"  contains struct_member.structure;
	data file "data_type.db"    contains data_type;
	key  file "data_type.ix1"   contains data_type.id;
	data file "constant.db"    contains constant;
	key  file "constant.ix1"   contains constant.id;
	key  file "constant.ix2"   contains constant.name;
	key  file "constant.ix3"   contains constant.value;
	data file "seer_macro.db"       contains seer_macro;
	key  file "seer_macro.ix1"      contains seer_macro.id;
	key  file "seer_macro.ix2"      contains seer_macro.name;

	/* ADDRESS : A block of code or data in the file. */
	record address {
		ulong rva;			/* unique relative virtual address */
		ulong pa;			/* offset from file start [disasm_env.image] */
		ushort size;		/* number of bytes in this address object */
		int flags;			/* Flags such as code/data, etc */
		ulong dataConst;	/* ID of CONSTANT record for data addr [0=none] */
		ulong structure;	/* ID of STRUCTURE record for this addr [0=none] */
		ulong comment;		/* ID of COMMENT record for this addr [0=none] */

		primary key rva;
		alternate key pa;
	}

	/* SECTION : logical code, data, or header [info] sections of the file.
	             Each section begins with an ADDRESS record */
	record section {
		char name[32];		/* Name of Section */
		ulong rva;			/* Starting RVA */
		ulong size;			/* Size of Section */
		int flags;			/* Section Flags, e.g. rwx */

		primary key name;
		alternate key rva;
	}

	/* XREF : Cross references between ADDRESS records in the file. */
	record xref {
		ulong id;			/* Unique ID, from seq.xref */
		ulong from_rva;	/* "From" ADDRESS record */
		ulong to_rva;		/* "To" ADDRESS record */
		int type;			/* Type of reference, i.e. rwx */

		primary key id;
		alternate key from_rva;
		alternate key to_rva;
	}

	/* CODE : A line of disassembled code tied to an ADDRESS record */
	record code {
		ulong rva;				/* RVA of ADDRESS record */
		ushort flags;			/* Instruction flags, e.g. INS_BRANCH */
		char mnemonic[16];	/* Assembly language mnemonic */
		char dest[32];			/* Destination operand */
		char src[32];			/* Source operand */
		char aux[32];			/* Additional [auxiliary] operand */
		int mnemType;			/* Flags for mnemonic */
		int destType;			/* Flags for Destination */
		int srcType;			/* Flags for Source */
		int auxType;			/* Flags for Aux */
		ulong destConst;		/* ID of CONSTANT for Destination */
		ulong srcConst;		/* ID of CONSTANT for Source */
		ulong auxConst;		/* ID of CONSTANT for Aux */

		primary key rva;
		alternate key dest;	/* huge, slow, but useful */
		alternate key src;	/* ditto */	
		alternate key mnemonic;
	}

	/* INT_CODE : Intermediate representation of a CODE record -- generated
	              as part of a decmopilation pass */
	record int_code {
		ulong id;				/* unique ID from seq.int_code */
		ulong rva;				/* RVA where the code starts */
		int size;				/* Size [in bytes] of code 'replaced' by this */
		char lvalue[32];		/* left side of expression */
		char rvalue[64];		/* right side of expression */

		primary key id;
		alternate key rva;
	}

	/* FIN_CODE : Final representation of a decompiled INT_CODE record */
	record fin_code {
		ulong id;				/* unique ID from seq.fin_code */
		ulong rva;				/* RVA where code starts */
		int size;				/* Size of code replaced by record */
		char line[256];		/* line of source code */

		primary key id;
		alternate key rva;
	}

	/* EXPORT : Exported or 'public' symbols in the program */
	record export {
		ulong rva;				/* RVA of exported code/data */

		primary key rva;
	}

	/* IMPORT : Imported or 'library' functions in the program */
	record import {
		int library;			/* ID of library [if known] */
		int type;				/* Import type [func, data] */
		ulong rva;				/* Address of relocation */

		primary key rva;
		alternate key library;	/* non-unique but useful */
	}

	/* LIBRARY : Shared libraries required by the program */
	record lib {
		int id;					/* unique ID for each library */
		char name[64];			/* name of library file [soname] */
		int ver_hi;				/* version [major] */
		int ver_lo;				/* version [minor] */

		primary key id;
		alternate key name;
	}
		

	/* STRING : Arrays of characters [data ADDRESS records] */
	record string {
		ulong rva;				/* RVA of the ADDRESS record */
		int length;				/* length of string */
		char text[256];		/* ASCII representation of string */

		primary key rva;
	}

	/* NAME : Global names for ADDRESS records */
	/*        NOTE: a name is for anything with an rva -- not libs, etc */
	record name {
		ulong rva;				/* RVA of ADDRESS record */
		int type;				/* Type of name [auto, user, etc] */
		char name[64];		

		primary key rva;
		alternate key name;
	}

	/* COMMENT : Comment strings linked to ADDRESS or FUNCTION records */
	record comment {
		ulong id;				/* Unique ID from seq.comment */
		int type;				/* Type of comment [repeatable, auto, user] */
		char text[256];

		primary key id;
	}

	/* FUNCTION : A complete subroutine within the program */
	record function {
		ulong id;				/* Unique ID from seq.func */
		ulong rva;				/* RVA of ADDRESS object */
		ushort size;			/* Size of function */
		ulong comment;			/* ID of COMMENT record for this function */

		primary key id;
		alternate key rva;
	}

	/* FUNC_PARAM : Represents parameters associated with a FUNCTION record */
	record func_param {
		ulong id;				/* Unique ID from seq.func_param */
		ulong func;				/* FUNCTION record that 'owns' the param */
		ulong type;				/* DATA_TYPE record [size, type of param] */
		int offset;				/* Negative offset from EBP || register code */
		char name[32];			

		primary key id;
		alternate key func;
	}

	/* FUNC_LOCAL : Local variables associated with a FUNCTION record */
	record func_local {
		ulong id;				/* Unique ID from seq.func_local */
		ulong func;				/* FUNCTION record that 'owns' the var */
		ulong type;				/* DATA_TYPE record [size, type of var] */
		int offset;				/* Positive offset from EBP || register code */
		char name[32];			

		primary key id;
		alternate key func;
	}

	/* F_INLINE : A block of library code linked to an ADDRESS record; used to
	              mark blocks of repeated inline library code [e.g. memcpy]
					  so they can be hidden/folded like regular functions. An
					  ADDRESS record is associated with an INLINE_TYPE, which 
					  contains details regarding size and a code template */
	record f_inline {
		ulong id;				/* Unique ID from seq.f_inline */
		ulong type;				/* Link to INLINE_TYPE record describing block */

		primary key id;
	}
		
	/* INLINE_TYPE : Templates for F_INLINE records  */
	record inline_type {
		ulong id;				/* Unique ID from seq.f_inline */
		char name[64];
		ulong rva;				/* Rva of 'template' code */
		ushort size;			/* Size of inline function */

		primary key id;
		alternate key rva;
	}

	/* STRUCTURE : A user-defined data structure which can be applied to an
	               ADDRESS record */
	record structure {
		ulong id;				/* Unique ID from seq.structure */
		char name[32];
		ushort size;			/* Total size of structure */

		primary key id;
	}

	/* STRUCT_MEMBER : Member data types for a given STRUCTURE record */
	record struct_member {
		ulong id;				/* Unique ID from seq.struct_member */
		ulong type;				/* ID of DATA_TYPE for this member */
		ulong structure;		/* ID of STRUCTURE owning this member */
		int order;				/* Order of member in the structure [1,2,3...] */
		char name[32];

		primary key id;
		alternate key structure;
	}
		
	/* DATA_TYPE : Name and size for predefined data or variable types --
	               like typedef */
	record data_type {
		ulong id;				/* Unique ID from seq.data_type */
		int size;				/* Size of data type */
		char name[32];			/* High-level name of data type, e.g. 'long' */

		primary key id;
	}
		
	/* CONSTANT : Name and value for numeric constants, like #define */
	record constant {
		ulong id;				/* Unique ID from seq.constant */
		char name[32];			
		long value;				/* Value of constant */

		primary key id;
		alternate key name;
		alternate key value;
	}

	/* SEER_MACRO : A user-recorded or -scripted seer macro */
	record seer_macro {
		ulong id;				/* Unique ID from seq.seer_macro */
		int type;				/* 1: macro= script, 2: macro = name of .sc file */
		char name[32];
		char macro[2048];		/* SeerC Source Code */

		primary key id;
		alternate key name;
	}
}

/* end-of-file */

/*
target {
	char *name/location[ ];
	void *header
};

Address type will be important. The address can refer to 
	Code -- refers to ADDR; ADDR has flag set
	Import -- refers to ADDR; ADDR has flag set
	String -- refers to ADDR; ADDR has flag set
	Function -- refers to ADDR; ADDR has flag set
	InlineFunction -- refers to ADDR; ADDR has flag set
	Structure -- referred to by ADDR [these are shared]
	Named Constant -- referred to by ADDR [these are shared]
*/
