1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
// Copyright (c) 1997,1998 Albrecht Kleine All rights reserved
// Copyright (c) 1998 Artur Biesiadowski, Rene Schmit, Olivier Refalo
//
// tya file version #300
#include "tyaconfig.h"
#include <stdio.h>
#ifdef USE_SYSLOG
#include <syslog.h>
#endif
#include <native.h>
#include <monitor.h>
#include <time.h>
#include "tya.h"
#ifdef VERBOSE_ASM86
const char* rname[] =
{
"eax", "ecx", "edx", "ebx", "esp", "ebp","esi", "edi"
};
const char* rname8[] =
{
"al","cl","dl","bl","ah","ch","dh","bh"
};
int decode_sib_scale[] =
{
1, 2 , 4 ,8
};
// utility function do decode sib encodings
const char * sibdecode( int sibbyte )
{
static char answer[20];
int regscaled;
int regoffset;
int scale;
regoffset = sibbyte & 0x07;
regscaled = (sibbyte & 0x38) >> 3;
scale = decode_sib_scale[ (sibbyte & 0xc0)>>6];
sprintf( answer, "%s+%d*%s", rname[regoffset], scale, rname[regscaled]);
return answer;
}
//----------------utility functions to decode bytecode--------------------
int readU2(unsigned char *p)
{
int x;
x =*(p)<<8;
p++;
x+=*p;
return x;
}
int readU4(unsigned char *p)
{
int x;
x =*(p)<<24;
p++;
x+=*(p)<<16;
p++;
x+=*(p)<<8;
p++;
x+=*p;
return x;
}
int oneParamByte(unsigned char *code)
{
char b=*code;
iprintf("%d",b);
return 1;
}
int twoParamByte(unsigned char *code)
{
char b,c;
b=*code;
c=*(code+1);
iprintf("%d %d",b,c);
return 2;
}
int newArrayParam(unsigned char *code)
{
char c;
iprintf("%d",readU2(code));
c=*(code+2);
iprintf(" %d",c);
return 3;
}
int oneParamLong(unsigned char *code)
{
iprintf("%d",readU4(code));
return 4;
}
int oneParamShort(unsigned char *code)
{
iprintf("%d",readU2(code) );
return 2;
}
int interfaceParam(unsigned char *code)
{
char c;
iprintf("%d",readU2(code));
c=*code;
iprintf("%d",c);
return 4;
}
int nopParam(unsigned char *code)
{
return 1;
}
struct java_opcodes
{
char *name; /* opcode name */
int (*print_param)(unsigned char *code); /* function to print its parameters */
};
struct java_opcodes opcodes[256] = {
{ "nop", nopParam },
{ "aconst_null", NULL },
{ "iconst_m1", NULL },
{ "iconst_0", NULL },
{ "iconst_1", NULL },
{ "iconst_2", NULL },
{ "iconst_3", NULL },
{ "iconst_4", NULL },
{ "iconst_5", NULL },
{ "lconst_0", NULL },
{ "lconst_1", NULL },
{ "fconst_0", NULL },
{ "fconst_1", NULL },
{ "fconst_2", NULL },
{ "dconst_0", NULL },
{ "dconst_1",NULL },
{ "bipush", oneParamByte },
{ "sipush", oneParamShort },
{ "ldc" , oneParamByte },
{ "ldc_w", oneParamShort },
{ "ldc2_w", oneParamShort },
{ "iload", oneParamByte },
{ "lload", oneParamByte },
{ "fload", oneParamByte },
{ "dload", oneParamByte },
{ "aload", oneParamByte },
{ "iload_0",NULL },
{ "iload_1",NULL },
{ "iload_2",NULL },
{ "iload_3",NULL },
{ "lload_0",NULL },
{ "lload_1",NULL },
{ "lload_2",NULL },
{ "lload_3",NULL },
{ "fload_0",NULL },
{ "fload_1",NULL },
{ "fload_2",NULL },
{ "fload_3",NULL },
{ "dload_0",NULL },
{ "dload_1",NULL },
{ "dload_2",NULL },
{ "dload_3",NULL },
{ "aload_0",NULL },
{ "aload_1",NULL },
{ "aload_2",NULL },
{ "aload_3",NULL },
{ "iaload", NULL },
{ "laload", NULL },
{ "faload", NULL },
{ "daload", NULL },
{ "aaload", NULL },
{ "baload", NULL },
{ "caload", NULL },
{ "saload", NULL },
{ "istore", oneParamByte },
{ "lstore", oneParamByte },
{ "fstore", oneParamByte },
{ "dstore", oneParamByte },
{ "astore", oneParamByte },
{ "istore_0", NULL },
{ "istore_1", NULL },
{ "istore_2", NULL },
{ "istore_3", NULL },
{ "lstore_0", NULL },
{ "lstore_1", NULL },
{ "lstore_2", NULL },
{ "lstore_3", NULL },
{ "fstore_0", NULL },
{ "fstore_1", NULL },
{ "fstore_2", NULL },
{ "fstore_3", NULL },
{ "dstore_0", NULL },
{ "dstore_1", NULL },
{ "dstore_2", NULL },
{ "dstore_3", NULL },
{ "astore_0", NULL },
{ "astore_1", NULL },
{ "astore_2", NULL },
{ "astore_3", NULL },
{ "iastore", NULL },
{ "lastore", NULL },
{ "fastore", NULL },
{ "dastore", NULL },
{ "aastore", NULL },
{ "bastore", NULL },
{ "castore", NULL },
{ "sastore", NULL },
{ "pop", NULL },
{ "pop2", NULL },
{ "dup", NULL },
{ "dup_x1", NULL },
{ "dup_x2", NULL },
{ "dup2", NULL },
{ "dup2_x1", NULL },
{ "dup2_x2", NULL },
{ "swap", NULL},
{ "iadd", NULL },
{ "ladd", NULL },
{ "fadd", NULL },
{ "dadd", NULL},
{ "isub", NULL },
{ "lsub", NULL },
{ "fsub", NULL },
{ "dsub", NULL },
{ "imul", NULL },
{ "lmul", NULL },
{ "fmul", NULL },
{ "dmul", NULL },
{ "idiv", NULL },
{ "ldiv", NULL },
{ "fdiv", NULL },
{ "ddiv", NULL },
{ "irem", NULL },
{ "lrem", NULL },
{ "frem", NULL },
{ "drem", NULL },
{ "ineg", NULL },
{ "lneg", NULL },
{ "fneg", NULL },
{ "dneg", NULL },
{ "ishl", NULL },
{ "lshl", NULL },
{ "ishr", NULL },
{ "lshr", NULL },
{ "iushr", NULL },
{ "lushr", NULL },
{ "iand", NULL },
{ "land", NULL },
{ "ior", NULL },
{ "lor", NULL },
{ "ixor", NULL },
{ "lxor", NULL },
{ "iinc", twoParamByte },
{ "i2l", NULL },
{ "i2f", NULL },
{ "i2d", NULL },
{ "l2i", NULL },
{ "l2f", NULL },
{ "l2d", NULL },
{ "f2i", NULL },
{ "f2l", NULL },
{ "f2d", NULL },
{ "d2i", NULL },
{ "d2l", NULL },
{ "d2f", NULL },
{ "i2b", NULL },
{ "i2c", NULL },
{ "i2s", NULL },
{ "lcmp", oneParamShort },
{ "fcmpl", oneParamShort },
{ "fcmpg", oneParamShort },
{ "dcmpl", oneParamShort },
{ "dcmpg", oneParamShort },
{ "ifeq", oneParamShort },
{ "ifne", oneParamShort },
{ "iflt", oneParamShort },
{ "ifge", oneParamShort },
{ "ifgt", oneParamShort },
{ "ifle", oneParamShort },
{ "if_icmpeq",oneParamShort },
{ "if_icmpne",oneParamShort },
{ "if_icmplt",oneParamShort },
{ "if_icmpge",oneParamShort },
{ "if_icmpgt",oneParamShort },
{ "if_icmple",oneParamShort },
{ "if_acmpeq",oneParamShort },
{ "if_acmpne",oneParamShort },
{ "goto",oneParamShort },
{ "jsr",oneParamShort },
{ "ret", oneParamByte },
{ "tableswitch", NULL },
{ "lookupswitch", NULL },
{ "ireturn",NULL },
{ "lreturn",NULL },
{ "freturn",NULL },
{ "dreturn",NULL },
{ "areturn",NULL },
{ "return" ,NULL },
{ "getstatic",oneParamShort },
{ "putstatic",oneParamShort },
{ "getfield",oneParamShort },
{ "putfield",oneParamShort },
{ "invokevirtual", oneParamShort },
{ "invokespecial", oneParamShort },
{ "invokestatic", oneParamShort },
{ "invokeinterface", interfaceParam},
{ "unknown",NULL },
{ "new", oneParamShort },
{ "newarray", oneParamByte },
{ "anewarray", oneParamShort },
{ "arraylength",NULL },
{ "athrow", NULL },
{ "checkcast", oneParamShort },
{ "instanceof", oneParamShort },
{ "monitorenter",NULL },
{ "monitorexit",NULL },
{ "wide",NULL },
{ "multianewarray", newArrayParam },
{ "ifnull", oneParamShort },
{ "ifnonnull", oneParamShort },
{ "goto_w", oneParamLong },
{ "jsr_w" , oneParamLong },
{ "breakpoint", NULL },
{ "ldc_quick", oneParamByte }, /*203*/
{ "unknown" ,NULL },
{ "ldc2w_quick" , oneParamShort },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "ret_w" ,NULL },
{ "getstatic_quick" ,oneParamShort },
{ "putstatic_quick" ,oneParamShort }, /*211*/
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "invokenonvirtual_quick" ,oneParamShort },
{ "unknown" ,NULL },
{ "invokestatic_quick" ,oneParamShort }, /*217*/
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "undocumented_invoke" ,oneParamShort }, /*220*/
{ "new_quick" ,oneParamShort },
{ "newarray_quick" ,oneParamShort },
{ "unknown" ,NULL },
{ "checkcast_quick" ,oneParamShort }, /*224*/
{ "instanceof_quick" ,oneParamShort },
{ "invokevirtual_quick" ,oneParamShort },
{ "getfield_quick_w" ,oneParamShort },
{ "putfield_quick_w" ,oneParamShort },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "unknown" ,NULL },
{ "impdep1" ,NULL },
{ "impdep2" ,NULL },
};
/*
print on stderr the bytecode pointed by p
returns the number of bytes read.
*/
int decode_bytecode(unsigned char *code)
{
int (*param_ptr)(unsigned char *c);
int current_bytecode=0;
current_bytecode=(int)*code;
lprintf("%s ",opcodes[current_bytecode].name);
param_ptr=(opcodes[current_bytecode].print_param);
if(param_ptr!=NULL)
return 1+(*param_ptr)(code+1);
else
return 1;
}
#endif
//-----------------------------------------------------------------------------
// useful for special debugging
//
void printHexDump (unsigned char * p_Code,int p_Size, char * p_Label)
{
int l_Count = -1;
unsigned char * l_End = p_Code + p_Size;
iprintf("\n");
while (p_Code < l_End)
{
if (++l_Count % 20)
iprintf("%02x ",*p_Code++);
else
{
iprintf("\n%s %5d (%p) : %02x ", p_Label, l_Count, p_Code, *p_Code);
p_Code++;
}
}
iprintf ("\n");
fflush (stderr);
}
//-----------------------------------------------------------------
#ifndef USE_SYSLOG
static FILE *logfile = NULL;
#endif
void
lprintf(char *fmt, ...)
{
va_list ap;
#ifdef USE_SYSLOG
char buf [1024];
va_start (ap, fmt);
vsnprintf (buf, sizeof (buf), fmt, ap);
va_end (ap);
syslog (LOG_USER | LOG_NOTICE, "%s", buf);
#else
if(logfile == NULL)
return;
va_start(ap, fmt);
vfprintf(logfile, fmt, ap);
va_end(ap);
fflush(logfile);
#endif
}
#ifdef USE_SYSLOG
void iprintf (char *fmt, ...)
{
va_list ap;
char buf [1024];
va_start (ap, fmt);
vsnprintf (buf, sizeof (buf), fmt, ap);
va_end (ap);
syslog (LOG_USER | LOG_INFO, "%s", buf);
}
#ifdef DEBUG
void
dprintf (FILE *ignore, char *fmt, ...)
{
va_list ap;
char buf [1024];
va_start (ap, fmt);
vsnprintf (buf, sizeof (buf), fmt, ap);
va_end (ap);
syslog (LOG_USER | LOG_DEBUG, "%s", buf);
}
#endif
#endif
void
lopen(void)
{
time_t now;
char s[1024];
#ifdef USE_SYSLOG
openlog ("jit4java", LOG_PID, LOG_USER);
#else
char *logname;
if((logname = getenv("TYA_LOGFILE")) != NULL) {
if(*logname == '\0')
{
logfile = stderr;
return;
}
logfile = fopen(logname, "a");
}
if(logfile == NULL)
logfile = stderr;
else
#endif
{
now = time(NULL);
strftime(s, sizeof(s), "%Y.%m.%d-%H:%M:%S", localtime(&now));
lprintf("%s\n", s);
}
}
void
lclose(void)
{
#ifndef USE_SYSLOG
if(logfile != NULL && logfile != stderr)
fclose(logfile);
logfile = NULL;
#endif
}
#ifdef GATHER_STATS
void tyaprintstats()
{
printf("-----------------------------------------\n");
printf("invokevirtual: %-10d\n", stats_opcodes[0xB6]);
printf("invokeinterface: %-10d\n", stats_opcodes[0xB9]);
printf("invokespecial: %-10d\n", stats_opcodes[0xB7]);
printf("invokestatic: %-10d\n", stats_opcodes[0xB8]);
// interfaces/all
printf("checkcast(interf): %-10d(%d)\n",
stats_opcodes[0xC0], stats_checkcast_interface);
printf("newobj: %-10d\n", stats_opcodes[0xBB]);
printf("newarr(both): %-10d\n",
stats_opcodes[0xBC]+stats_opcodes[0xBD]);
printf("-----------------------------------------\n");
}
#endif
#if 0
// unused
unsigned int get87precision(void)
{
unsigned int cword87;
__asm__ ("fstcw %0" : "=m" (cword87) );
return (cword87 & 0x300) >>8;
}
// NOTE: for more similar stuff look in /usr/include/fpu_control.h
// example:
// __setfpucw (_FPU_DEFAULT & ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM));
// ...plus some nice macros..
#endif
|