toolkit/crashreporter/google-breakpad/src/third_party/libdisasm/swig/libdisasm.i

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/google-breakpad/src/third_party/libdisasm/swig/libdisasm.i	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,508 @@
     1.4 +%module x86disasm
     1.5 +%{
     1.6 +#include "../../libdis.h"
     1.7 +#include "../../../config.h"
     1.8 +%}
     1.9 +
    1.10 +%rename(version_string) x86_version_string;
    1.11 +%include "../../libdis.h"
    1.12 +#include "../../../config.h"
    1.13 +
    1.14 +%inline %{
    1.15 +	const char * x86_version_string( void ) {
    1.16 +		return PACKAGE_VERSION;
    1.17 +	}
    1.18 +%}
    1.19 +
    1.20 +%rename(report_codes) x86_report_codes;
    1.21 +%rename(report_error) x86_report_error;
    1.22 +%rename(options) x86_options;
    1.23 +%rename(init) x86_init;
    1.24 +%rename(set_reporter) x86_set_reporter;
    1.25 +%rename(set_options) x86_set_options;
    1.26 +%rename(options) x86_get_options;
    1.27 +%rename(cleanup) x86_cleanup;
    1.28 +%rename(reg_type) x86_reg_type;
    1.29 +%rename(reg) x86_reg_t;
    1.30 +%rename(eaddr) x86_ea_t;
    1.31 +%rename(op_type) x86_op_type;
    1.32 +%rename(optype_is_address) x86_optype_is_address;
    1.33 +%rename(optype_is_relative) x86_optype_is_relative;
    1.34 +%rename(op_datatype) x86_op_datatype;
    1.35 +%rename(op_access) x86_op_access;
    1.36 +%rename(op_flags) x86_op_flags;
    1.37 +%rename(operand) x86_op_t;
    1.38 +%rename(insn_group) x86_insn_group; 
    1.39 +%rename(insn_type) x86_insn_type;
    1.40 +%rename(insn_note) x86_insn_note ;
    1.41 +%rename(flag_status) x86_flag_status; 
    1.42 +%rename(insn_cpu) x86_insn_cpu ;
    1.43 +%rename(insn_isa) x86_insn_isa ;
    1.44 +%rename(insn_prefix) x86_insn_prefix ;
    1.45 +%rename(insn) x86_insn_t;
    1.46 +%rename(insn_is_valid) x86_insn_is_valid;
    1.47 +%rename(i_disasm) x86_disasm;
    1.48 +%rename(i_disasm_range) x86_disasm_range;
    1.49 +%rename(i_disasm_forward) x86_disasm_forward;
    1.50 +%rename(insn_operand_count) x86_operand_count;
    1.51 +%rename(insn_operand_1st) x86_operand_1st;
    1.52 +%rename(insn_operand_2nd) x86_operand_2nd;
    1.53 +%rename(insn_operand_3rd) x86_operand_3rd;
    1.54 +%rename(insn_dest_operand) x86_get_dest_operand;
    1.55 +%rename(insn_src_operand) x86_get_src_operand;
    1.56 +%rename(insn_imm_operand) x86_get_imm_operand;
    1.57 +%rename(operand_size) x86_operand_size;
    1.58 +%rename(insn_rel_offset) x86_get_rel_offset;
    1.59 +%rename(insn_branch_target) x86_get_branch_target;
    1.60 +%rename(insn_imm) x86_get_imm;
    1.61 +%rename(insn_raw_imm) x86_get_raw_imm;
    1.62 +%rename(insn_set_addr) x86_set_insn_addr;
    1.63 +%rename(insn_set_offset) x86_set_insn_offset;
    1.64 +%rename(insn_set_function) x86_set_insn_function;
    1.65 +%rename(insn_set_block) x86_set_insn_block;
    1.66 +%rename(insn_tag) x86_tag_insn;
    1.67 +%rename(insn_untag) x86_untag_insn;
    1.68 +%rename(insn_is_tagged) x86_insn_is_tagged;
    1.69 +%rename(asm_format) x86_asm_format;
    1.70 +%rename(operand_format) x86_format_operand;
    1.71 +%rename(insn_format_mnemonic) x86_format_mnemonic;
    1.72 +%rename(insn_format) x86_format_insn;
    1.73 +%rename(header_format) x86_format_header;
    1.74 +%rename(endian) x86_endian;
    1.75 +%rename(size_default_address) x86_addr_size;
    1.76 +%rename(size_default_operand) x86_op_size;
    1.77 +%rename(size_machine_word) x86_word_size;
    1.78 +%rename(size_max_insn) x86_max_insn_size;
    1.79 +%rename(reg_sp) x86_sp_reg;
    1.80 +%rename(reg_fp) x86_fp_reg;
    1.81 +%rename(reg_ip) x86_ip_reg;
    1.82 +%rename(reg_from_id) x86_reg_from_id;
    1.83 +%rename(reg_from_alias) x86_get_aliased_reg;
    1.84 +%rename(invariant_op) x86_invariant_op_t;
    1.85 +%rename(invariant) x86_invariant_t;
    1.86 +%rename(disasm_invariant) x86_invariant_disasm;
    1.87 +%rename(disasm_size) x86_size_disasm;
    1.88 +
    1.89 +%include "carrays.i"
    1.90 +
    1.91 +%array_class( unsigned char, byteArray );
    1.92 +
    1.93 +
    1.94 +%apply (unsigned char *STRING, int LENGTH) { 
    1.95 +	(unsigned char *buf, size_t buf_len) 
    1.96 +};
    1.97 +
    1.98 +
    1.99 +%newobject x86_op_copy;
   1.100 +%inline %{
   1.101 +	x86_op_t * x86_op_copy( x86_op_t * src ) {
   1.102 +		x86_op_t *op;
   1.103 +		
   1.104 +		if (! src ) {
   1.105 +			return NULL;
   1.106 +		}
   1.107 +
   1.108 +		op = (x86_op_t *) calloc( sizeof(x86_op_t), 1 );
   1.109 +		if ( op ) {
   1.110 +			memcpy( op, src, sizeof(x86_op_t) );
   1.111 +		}
   1.112 +
   1.113 +		return op;
   1.114 +	}
   1.115 +
   1.116 +	typedef struct x86_op_list_node {
   1.117 +		x86_op_t *op;
   1.118 +		struct x86_op_list_node *next, *prev;
   1.119 +	} x86_op_list_node;
   1.120 +
   1.121 +	typedef struct x86_op_list {
   1.122 +		size_t count;
   1.123 +		x86_op_list_node *head, *tail, *curr;
   1.124 +	} x86_op_list;
   1.125 +
   1.126 +	x86_op_list * x86_op_list_new () {
   1.127 +		x86_op_list *list = (x86_op_list *) 
   1.128 +				calloc( sizeof(x86_op_list), 1 );
   1.129 +		list->count = 0;
   1.130 +		return list;
   1.131 +	}
   1.132 +
   1.133 +	void x86_op_list_free(x86_op_list *list) {
   1.134 +		x86_op_list_node *node, *next;
   1.135 +
   1.136 +		node = list->head;
   1.137 +		while ( node ) {
   1.138 +			next = node->next;
   1.139 +			/* free( node->insn ); */
   1.140 +			free( node );
   1.141 +			node = next;
   1.142 +		}
   1.143 +
   1.144 +		free( list );
   1.145 +	}
   1.146 +
   1.147 +	x86_op_list_node * x86_op_list_first(x86_op_list *list) { 
   1.148 +		return list->head; 
   1.149 +	}
   1.150 +
   1.151 +	x86_op_list_node * x86_op_list_last(x86_op_list *list) { 
   1.152 +		return list->tail; 
   1.153 +	}
   1.154 +
   1.155 +	x86_op_list_node * x86_op_list_next(x86_op_list *list) { 
   1.156 +		if (! list->curr ) {
   1.157 +			list->curr = list->head;
   1.158 +			return list->head;
   1.159 +		}
   1.160 +
   1.161 +		list->curr = list->curr->next;
   1.162 +		return list->curr;
   1.163 +	}
   1.164 +
   1.165 +	x86_op_list_node * x86_op_list_prev(x86_op_list *list) { 
   1.166 +		if (! list->curr ) {
   1.167 +			list->curr = list->tail;
   1.168 +			return list->tail;
   1.169 +		}
   1.170 +
   1.171 +		list->curr = list->curr->prev;
   1.172 +		return list->curr;
   1.173 +	}
   1.174 +
   1.175 +%}
   1.176 +
   1.177 +%newobject x86_op_list_append;
   1.178 +
   1.179 +%inline %{
   1.180 +	void x86_op_list_append( x86_op_list * list, x86_op_t *op ) {
   1.181 +		x86_op_list_node *node = (x86_op_list_node *)
   1.182 +					calloc( sizeof(x86_op_list_node) , 1 );
   1.183 +		if (! node ) {
   1.184 +			return;
   1.185 +		}
   1.186 +
   1.187 +		list->count++;
   1.188 +		if ( ! list->tail ) {
   1.189 +			list->head = list->tail = node;
   1.190 +		} else {
   1.191 +			list->tail->next = node;
   1.192 +			node->prev = list->tail;
   1.193 +			list->tail = node;
   1.194 +		}
   1.195 +
   1.196 +		node->op = x86_op_copy( op );
   1.197 +	}
   1.198 +
   1.199 +	x86_oplist_t * x86_op_list_node_copy( x86_oplist_t * list ) {
   1.200 +		x86_oplist_t *ptr;
   1.201 +		ptr = (x86_oplist_t *) calloc( sizeof(x86_oplist_t), 1 );
   1.202 +		if ( ptr ) {
   1.203 +			memcpy( &ptr->op, &list->op, sizeof(x86_op_t) );
   1.204 +		}
   1.205 +
   1.206 +		return ptr;
   1.207 +	}
   1.208 +
   1.209 +	x86_insn_t * x86_insn_new() {
   1.210 +		x86_insn_t *insn = (x86_insn_t *)
   1.211 +				   calloc( sizeof(x86_insn_t), 1 );
   1.212 +		return insn;
   1.213 +	}
   1.214 +
   1.215 +	void x86_insn_free( x86_insn_t *insn ) {
   1.216 +		x86_oplist_free( insn );
   1.217 +		free( insn );
   1.218 +	}
   1.219 +%}
   1.220 +
   1.221 +%newobject x86_insn_copy;
   1.222 +
   1.223 +%inline %{
   1.224 +	x86_insn_t * x86_insn_copy( x86_insn_t *src) {
   1.225 +		x86_oplist_t *ptr, *list, *last = NULL;
   1.226 +		x86_insn_t *insn = (x86_insn_t *)
   1.227 +				   calloc( sizeof(x86_insn_t), 1 );
   1.228 +
   1.229 +		if ( insn ) {
   1.230 +			memcpy( insn, src, sizeof(x86_insn_t) );
   1.231 +			insn->operands = NULL;
   1.232 +			insn->block = NULL;
   1.233 +			insn->function = NULL;
   1.234 +
   1.235 +			/* copy operand list */
   1.236 +			for ( list = src->operands; list; list = list->next ) {
   1.237 +				ptr = x86_op_list_node_copy( list );
   1.238 +
   1.239 +				if (! ptr ) {
   1.240 +					continue;
   1.241 +				}
   1.242 +
   1.243 +				if ( insn->operands ) {
   1.244 +					last->next = ptr;
   1.245 +				} else {
   1.246 +					insn->operands = ptr;
   1.247 +				}
   1.248 +				last = ptr;
   1.249 +			}
   1.250 +		}
   1.251 +
   1.252 +		return insn;
   1.253 +	}
   1.254 +
   1.255 +	x86_op_list * x86_insn_op_list( x86_insn_t *insn ) {
   1.256 +		x86_oplist_t *list = insn->operands;
   1.257 +		x86_op_list *op_list = x86_op_list_new();
   1.258 +
   1.259 +		for ( list = insn->operands; list; list = list->next ) {
   1.260 +			x86_op_list_append( op_list, &list->op );
   1.261 +		}
   1.262 +
   1.263 +		return op_list;
   1.264 +	}
   1.265 +
   1.266 +	typedef struct x86_insn_list_node {
   1.267 +		x86_insn_t *insn;
   1.268 +		struct x86_insn_list_node *next, *prev;
   1.269 +	} x86_insn_list_node;
   1.270 +
   1.271 +	typedef struct x86_insn_list {
   1.272 +		size_t count;
   1.273 +		x86_insn_list_node *head, *tail, *curr;
   1.274 +	} x86_insn_list;
   1.275 +
   1.276 +%}
   1.277 +
   1.278 +%newobject x86_insn_list_new;
   1.279 +
   1.280 +%inline %{
   1.281 +	x86_insn_list * x86_insn_list_new () {
   1.282 +		x86_insn_list *list = (x86_insn_list *) 
   1.283 +				calloc( sizeof(x86_insn_list), 1 );
   1.284 +		list->count = 0;
   1.285 +		return list;
   1.286 +	}
   1.287 +
   1.288 +	void x86_insn_list_free( x86_insn_list * list ) {
   1.289 +		x86_insn_list_node *node, *next;
   1.290 +
   1.291 +		if (! list ) {
   1.292 +			return;
   1.293 +		}
   1.294 +
   1.295 +		node = list->head;
   1.296 +		while ( node ) {
   1.297 +			next = node->next;
   1.298 +			/* free( node->insn ); */
   1.299 +			free( node );
   1.300 +			node = next;
   1.301 +		}
   1.302 +
   1.303 +		free( list );
   1.304 +	}
   1.305 +
   1.306 +	x86_insn_list_node * x86_insn_list_first( x86_insn_list *list ) { 
   1.307 +		if (! list ) {
   1.308 +			return NULL;
   1.309 +		}
   1.310 +		return list->head; 
   1.311 +	}
   1.312 +
   1.313 +	x86_insn_list_node * x86_insn_list_last( x86_insn_list *list ) { 
   1.314 +		if (! list ) {
   1.315 +			return NULL;
   1.316 +		}
   1.317 +		return list->tail; 
   1.318 +	}
   1.319 +
   1.320 +	x86_insn_list_node * x86_insn_list_next( x86_insn_list *list ) { 
   1.321 +		if (! list ) {
   1.322 +			return NULL;
   1.323 +		}
   1.324 +		if (! list->curr ) {
   1.325 +			list->curr = list->head;
   1.326 +			return list->head;
   1.327 +		}
   1.328 +
   1.329 +		list->curr = list->curr->next;
   1.330 +		return list->curr;
   1.331 +	}
   1.332 +
   1.333 +	x86_insn_list_node * x86_insn_list_prev( x86_insn_list *list ) { 
   1.334 +		if (! list ) {
   1.335 +			return NULL;
   1.336 +		}
   1.337 +		if (! list->curr ) {
   1.338 +			list->curr = list->tail;
   1.339 +			return list->tail;
   1.340 +		}
   1.341 +
   1.342 +		list->curr = list->curr->prev;
   1.343 +		return list->curr;
   1.344 +	}
   1.345 +
   1.346 +%}
   1.347 +
   1.348 +%newobject x86_insn_list_append;
   1.349 +
   1.350 +%inline %{
   1.351 +	void x86_insn_list_append( x86_insn_list *list, x86_insn_t *insn ) {
   1.352 +		x86_insn_list_node *node;
   1.353 +		if (! list ) {
   1.354 +			return;
   1.355 +		}
   1.356 +
   1.357 +		node = (x86_insn_list_node *)
   1.358 +					calloc( sizeof(x86_insn_list_node) , 1 );
   1.359 +
   1.360 +		if (! node ) {
   1.361 +			return;
   1.362 +		}
   1.363 +
   1.364 +		list->count++;
   1.365 +		if ( ! list->tail ) {
   1.366 +			list->head = list->tail = node;
   1.367 +		} else {
   1.368 +			list->tail->next = node;
   1.369 +			node->prev = list->tail;
   1.370 +			list->tail = node;
   1.371 +		}
   1.372 +
   1.373 +		node->insn = x86_insn_copy( insn );
   1.374 +	}
   1.375 +
   1.376 +	typedef struct {
   1.377 +		enum x86_report_codes last_error;
   1.378 +		void * last_error_data;
   1.379 +		void * disasm_callback;
   1.380 +		void * disasm_resolver;
   1.381 +	} x86disasm;
   1.382 +
   1.383 +	void x86_default_reporter( enum x86_report_codes code, 
   1.384 +				   void *data, void *arg ) {
   1.385 +		x86disasm *dis = (x86disasm *) arg;
   1.386 +		if ( dis ) {
   1.387 +			dis->last_error = code;
   1.388 +			dis->last_error_data = data;
   1.389 +		}
   1.390 +	}
   1.391 +
   1.392 +	void x86_default_callback( x86_insn_t *insn, void *arg ) {
   1.393 +		x86_insn_list *list = (x86_insn_list *) arg;
   1.394 +		if ( list ) {
   1.395 +			x86_insn_list_append( list, insn );
   1.396 +		}
   1.397 +	}
   1.398 +
   1.399 +	/* TODO: resolver stack, maybe a callback */
   1.400 +	long x86_default_resolver( x86_op_t *op, x86_insn_t *insn, void *arg ) {
   1.401 +		x86disasm *dis = (x86disasm *) arg;
   1.402 +		if ( dis ) {
   1.403 +			//return dis->resolver( op, insn );
   1.404 +			return 0;
   1.405 +		}
   1.406 +
   1.407 +		return 0;
   1.408 +	}
   1.409 +
   1.410 +	
   1.411 +%}
   1.412 +
   1.413 +%newobject x86disasm_new;
   1.414 +
   1.415 +%inline %{
   1.416 +	x86disasm * x86disasm_new ( enum x86_options options ) {
   1.417 +		x86disasm * dis = (x86disasm *)
   1.418 +				calloc( sizeof( x86disasm ), 1 );
   1.419 +		x86_init( options, x86_default_reporter, dis );
   1.420 +		return dis;
   1.421 +	}
   1.422 +
   1.423 +	void x86disasm_free( x86disasm * dis ) {
   1.424 +		x86_cleanup();
   1.425 +		free( dis );
   1.426 +	}
   1.427 +%}
   1.428 +
   1.429 +%newobject x86_disasm;
   1.430 +
   1.431 +%inline %{
   1.432 +	x86_insn_t * disasm( unsigned char *buf, size_t buf_len, 
   1.433 +		           unsigned long buf_rva, unsigned int offset ) {
   1.434 +		x86_insn_t *insn = calloc( sizeof( x86_insn_t ), 1 );
   1.435 +		x86_disasm( buf, buf_len, buf_rva, offset, insn );
   1.436 +		return insn;
   1.437 +	}
   1.438 +
   1.439 +	int disasm_range( unsigned char *buf, size_t buf_len, 
   1.440 +	              unsigned long buf_rva, unsigned int offset,
   1.441 +		      unsigned int len ) {
   1.442 +
   1.443 +		x86_insn_list *list = x86_insn_list_new();
   1.444 +
   1.445 +		if ( len > buf_len ) {
   1.446 +			len = buf_len;
   1.447 +		}
   1.448 +
   1.449 +		return x86_disasm_range( buf, buf_rva, offset, len, 
   1.450 +				x86_default_callback, list );
   1.451 +	}
   1.452 +
   1.453 +	int disasm_forward( unsigned char *buf, size_t buf_len,
   1.454 +			    unsigned long buf_rva, unsigned int offset ) {
   1.455 +		x86_insn_list *list = x86_insn_list_new();
   1.456 +
   1.457 +		/* use default resolver: damn SWIG callbacks! */
   1.458 +		return x86_disasm_forward( buf, buf_len, buf_rva, offset,
   1.459 +			                   x86_default_callback, list, 
   1.460 +					   x86_default_resolver, NULL );
   1.461 +	}
   1.462 +
   1.463 +	size_t disasm_invariant( unsigned char *buf, size_t buf_len, 
   1.464 +			  x86_invariant_t *inv ) {
   1.465 +		return x86_invariant_disasm( buf, buf_len, inv );
   1.466 +	}
   1.467 +
   1.468 +	size_t disasm_size( unsigned char *buf, size_t buf_len ) {
   1.469 +		return x86_size_disasm( buf, buf_len );
   1.470 +	}
   1.471 +
   1.472 +	int x86_max_operand_string( enum x86_asm_format format ) {
   1.473 +		switch ( format ) {
   1.474 +			case xml_syntax:
   1.475 +				return  MAX_OP_XML_STRING;
   1.476 +				break;
   1.477 +			case raw_syntax:
   1.478 +				return MAX_OP_RAW_STRING;
   1.479 +				break;
   1.480 +			case native_syntax:
   1.481 +			case intel_syntax:
   1.482 +			case att_syntax:
   1.483 +			case unknown_syntax:
   1.484 +			default:
   1.485 +				return MAX_OP_STRING;
   1.486 +				break;
   1.487 +		}
   1.488 +	}
   1.489 +
   1.490 +
   1.491 +	int x86_max_insn_string( enum x86_asm_format format ) {
   1.492 +		switch ( format ) {
   1.493 +			case xml_syntax:
   1.494 +				return  MAX_INSN_XML_STRING;
   1.495 +				break;
   1.496 +			case raw_syntax:
   1.497 +				return MAX_INSN_RAW_STRING;
   1.498 +				break;
   1.499 +			case native_syntax:
   1.500 +			case intel_syntax:
   1.501 +			case att_syntax:
   1.502 +			case unknown_syntax:
   1.503 +			default:
   1.504 +				return MAX_INSN_STRING;
   1.505 +				break;
   1.506 +		}
   1.507 +	}
   1.508 +
   1.509 +	int x86_max_num_operands( ) { return MAX_NUM_OPERANDS; }
   1.510 +%}
   1.511 +

mercurial