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 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
|
/***************************************************************************************************
Zyan Disassembler Library (Zydis)
Original Author : Florian Bernd, Joel Hoener
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
***************************************************************************************************/
#include "config.h"
#if ENABLE(ZYDIS)
#include "ZydisInternalFormatterBase.h"
#include "ZydisUtils.h"
#include <wtf/Compiler.h>
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
/* ============================================================================================== */
/* Constants */
/* ============================================================================================== */
#include "ZydisGeneratedFormatterStrings.inc"
static const ZydisShortString* const STR_PREF_REX[16] =
{
&STR_PREF_REX_40,
&STR_PREF_REX_41,
&STR_PREF_REX_42,
&STR_PREF_REX_43,
&STR_PREF_REX_44,
&STR_PREF_REX_45,
&STR_PREF_REX_46,
&STR_PREF_REX_47,
&STR_PREF_REX_48,
&STR_PREF_REX_49,
&STR_PREF_REX_4A,
&STR_PREF_REX_4B,
&STR_PREF_REX_4C,
&STR_PREF_REX_4D,
&STR_PREF_REX_4E,
&STR_PREF_REX_4F
};
static const ZydisPredefinedToken* const TOK_PREF_REX[16] =
{
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_40,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_41,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_42,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_43,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_44,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_45,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_46,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_47,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_48,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_49,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_4A,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_4B,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_4C,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_4D,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_4E,
(const ZydisPredefinedToken* const)&TOK_DATA_PREF_REX_4F
};
/* ============================================================================================== */
/* Helper functions */
/* ============================================================================================== */
ZyanU32 ZydisFormatterHelperGetExplicitSize(const ZydisFormatter* formatter,
ZydisFormatterContext* context, ZyanU8 memop_id)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(context);
ZYAN_ASSERT(memop_id < context->instruction->operand_count);
const ZydisDecodedOperand* const operand = &context->instruction->operands[memop_id];
ZYAN_ASSERT(operand->type == ZYDIS_OPERAND_TYPE_MEMORY);
ZYAN_ASSERT(operand->mem.type == ZYDIS_MEMOP_TYPE_MEM);
if (formatter->force_memory_size)
{
return operand->size;
}
switch (operand->id)
{
case 0:
if ((context->instruction->operands[1].type == ZYDIS_OPERAND_TYPE_UNUSED) ||
(context->instruction->operands[1].type == ZYDIS_OPERAND_TYPE_IMMEDIATE))
{
return context->instruction->operands[0].size;
}
if (context->instruction->operands[0].size != context->instruction->operands[1].size)
{
return context->instruction->operands[0].size;
}
if ((context->instruction->operands[1].type == ZYDIS_OPERAND_TYPE_REGISTER) &&
(context->instruction->operands[1].visibility == ZYDIS_OPERAND_VISIBILITY_IMPLICIT) &&
(context->instruction->operands[1].reg.value == ZYDIS_REGISTER_CL))
{
return context->instruction->operands[0].size;
}
break;
case 1:
case 2:
if (context->instruction->operands[operand->id - 1].size !=
context->instruction->operands[operand->id].size)
{
return context->instruction->operands[operand->id].size;
}
break;
default:
break;
}
return 0;
}
/* ============================================================================================== */
/* Formatter functions */
/* ============================================================================================== */
/* ---------------------------------------------------------------------------------------------- */
/* Operands */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisFormatterBaseFormatOperandREG(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
return formatter->func_print_register(formatter, buffer, context, context->operand->reg.value);
}
ZyanStatus ZydisFormatterBaseFormatOperandPTR(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_IMMEDIATE);
ZYDIS_STRING_APPEND_NUM_U(formatter, formatter->addr_base, &buffer->string,
context->operand->ptr.segment, 4);
ZYDIS_BUFFER_APPEND(buffer, DELIM_SEGMENT);
ZyanU8 padding;
switch (context->instruction->operand_width)
{
case 16:
padding = 4;
break;
case 32:
padding = 8;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_IMMEDIATE);
ZYDIS_STRING_APPEND_NUM_U(formatter, formatter->addr_base, &buffer->string,
context->operand->ptr.offset , padding);
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisFormatterBaseFormatOperandIMM(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
// The immediate operand contains an address
if (context->operand->imm.is_relative)
{
const ZyanBool absolute = !formatter->force_relative_branches &&
(context->runtime_address != ZYDIS_RUNTIME_ADDRESS_NONE);
if (absolute)
{
return formatter->func_print_address_abs(formatter, buffer, context);
}
return formatter->func_print_address_rel(formatter, buffer, context);
}
// The immediate operand contains an actual ordinal value
return formatter->func_print_imm(formatter, buffer, context);
}
/* ---------------------------------------------------------------------------------------------- */
/* Elemental tokens */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisFormatterBasePrintAddressABS(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZyanU64 address;
ZYAN_CHECK(ZydisCalcAbsoluteAddress(context->instruction, context->operand,
context->runtime_address, &address));
ZyanU8 padding = (formatter->addr_padding_absolute ==
ZYDIS_PADDING_AUTO) ? 0 : (ZyanU8)formatter->addr_padding_absolute;
if ((formatter->addr_padding_absolute == ZYDIS_PADDING_AUTO) &&
(formatter->addr_base == ZYDIS_NUMERIC_BASE_HEX))
{
switch (context->instruction->stack_width)
{
case 16:
padding = 4;
address = (ZyanU16)address;
break;
case 32:
padding = 8;
address = (ZyanU32)address;
break;
case 64:
padding = 16;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_ADDRESS_ABS);
ZYDIS_STRING_APPEND_NUM_U(formatter, formatter->addr_base, &buffer->string, address, padding);
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisFormatterBasePrintAddressREL(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZyanU64 address;
ZYAN_CHECK(ZydisCalcAbsoluteAddress(context->instruction, context->operand, 0, &address));
ZyanU8 padding = (formatter->addr_padding_relative ==
ZYDIS_PADDING_AUTO) ? 0 : (ZyanU8)formatter->addr_padding_relative;
if ((formatter->addr_padding_relative == ZYDIS_PADDING_AUTO) &&
(formatter->addr_base == ZYDIS_NUMERIC_BASE_HEX))
{
switch (context->instruction->stack_width)
{
case 16:
padding = 4;
address = (ZyanU16)address;
break;
case 32:
padding = 8;
address = (ZyanU32)address;
break;
case 64:
padding = 16;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_ADDRESS_REL);
switch (formatter->addr_signedness)
{
case ZYDIS_SIGNEDNESS_AUTO:
case ZYDIS_SIGNEDNESS_SIGNED:
ZYDIS_STRING_APPEND_NUM_S(formatter, formatter->addr_base, &buffer->string, address,
padding, ZYAN_TRUE);
break;
case ZYDIS_SIGNEDNESS_UNSIGNED:
ZYAN_CHECK(ZydisStringAppendShort(&buffer->string, &STR_ADD));
ZYDIS_STRING_APPEND_NUM_U(formatter, formatter->addr_base, &buffer->string, address,
padding);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisFormatterBasePrintIMM(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_IMMEDIATE);
const ZyanBool is_signed =
(formatter->imm_signedness == ZYDIS_SIGNEDNESS_SIGNED) ||
(formatter->imm_signedness == ZYDIS_SIGNEDNESS_AUTO && (context->operand->imm.is_signed));
if (is_signed && (context->operand->imm.value.s < 0))
{
ZYDIS_STRING_APPEND_NUM_S(formatter, formatter->imm_base, &buffer->string,
context->operand->imm.value.s, formatter->imm_padding, ZYAN_FALSE);
return ZYAN_STATUS_SUCCESS;
}
ZyanU64 value;
ZyanU8 padding = (formatter->imm_padding ==
ZYDIS_PADDING_AUTO) ? 0 : (ZyanU8)formatter->imm_padding;
switch (context->instruction->operand_width)
{
case 8:
if (formatter->imm_padding == ZYDIS_PADDING_AUTO)
{
padding = 2;
}
value = (ZyanU8 )context->operand->imm.value.u;
break;
case 16:
if (formatter->imm_padding == ZYDIS_PADDING_AUTO)
{
padding = 4;
}
value = (ZyanU16)context->operand->imm.value.u;
break;
case 32:
if (formatter->imm_padding == ZYDIS_PADDING_AUTO)
{
padding = 8;
}
value = (ZyanU32)context->operand->imm.value.u;
break;
case 64:
if (formatter->imm_padding == ZYDIS_PADDING_AUTO)
{
padding = 16;
}
value = (ZyanU64)context->operand->imm.value.u;
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
ZYDIS_STRING_APPEND_NUM_U(formatter, formatter->imm_base, &buffer->string, value, padding);
return ZYAN_STATUS_SUCCESS;
}
/* ---------------------------------------------------------------------------------------------- */
/* Optional tokens */
/* ---------------------------------------------------------------------------------------------- */
ZyanStatus ZydisFormatterBasePrintSegment(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
ZyanBool printed_segment = ZYAN_FALSE;
switch (context->operand->mem.segment)
{
case ZYDIS_REGISTER_ES:
case ZYDIS_REGISTER_CS:
case ZYDIS_REGISTER_FS:
case ZYDIS_REGISTER_GS:
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.segment));
printed_segment = ZYAN_TRUE;
break;
case ZYDIS_REGISTER_SS:
if ((formatter->force_memory_segment) ||
(context->instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_SS))
{
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.segment));
printed_segment = ZYAN_TRUE;
}
break;
case ZYDIS_REGISTER_DS:
if ((formatter->force_memory_segment) ||
(context->instruction->attributes & ZYDIS_ATTRIB_HAS_SEGMENT_DS))
{
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->operand->mem.segment));
printed_segment = ZYAN_TRUE;
}
break;
default:
break;
}
if (printed_segment)
{
ZYDIS_BUFFER_APPEND(buffer, DELIM_SEGMENT);
}
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisFormatterBasePrintPrefixes(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
if (formatter->detailed_prefixes)
{
for (ZyanU8 i = 0; i < context->instruction->raw.prefix_count; ++i)
{
const ZyanU8 value = context->instruction->raw.prefixes[i].value;
switch (context->instruction->raw.prefixes[i].type)
{
case ZYDIS_PREFIX_TYPE_IGNORED:
case ZYDIS_PREFIX_TYPE_MANDATORY:
{
if ((value & 0xF0) == 0x40)
{
if (buffer->is_token_list)
{
// TODO: Case
ZYAN_CHECK(ZydisFormatterBufferAppendPredefined(buffer,
TOK_PREF_REX[value & 0x0F]));
} else
{
ZYAN_CHECK(ZydisStringAppendShortCase(&buffer->string,
STR_PREF_REX[value & 0x0F], formatter->case_prefixes));
}
} else
{
switch (value)
{
case 0xF0:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_LOCK, formatter->case_prefixes);
break;
case 0x2E:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_SEG_CS, formatter->case_prefixes);
break;
case 0x36:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_SEG_SS, formatter->case_prefixes);
break;
case 0x3E:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_SEG_DS, formatter->case_prefixes);
break;
case 0x26:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_SEG_ES, formatter->case_prefixes);
break;
case 0x64:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_SEG_FS, formatter->case_prefixes);
break;
case 0x65:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_SEG_GS, formatter->case_prefixes);
break;
default:
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_PREFIX);
ZYAN_CHECK(ZydisStringAppendHexU(&buffer->string, value, 0,
formatter->hex_uppercase, ZYAN_NULL, ZYAN_NULL));
ZYDIS_BUFFER_APPEND_TOKEN(buffer, ZYDIS_TOKEN_WHITESPACE);
ZYAN_CHECK(ZydisStringAppendShort(&buffer->string, &STR_WHITESPACE));
break;
}
}
break;
}
case ZYDIS_PREFIX_TYPE_EFFECTIVE:
switch (value)
{
case 0xF0:
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_LOCK, formatter->case_prefixes);
break;
case 0xF2:
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_XACQUIRE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_XACQUIRE, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_REPNE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_REPNE, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_BND)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_BND, formatter->case_prefixes);
}
break;
case 0xF3:
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_XRELEASE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_XRELEASE, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_REP)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_REP, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_REPE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_REPE, formatter->case_prefixes);
}
break;
default:
break;
}
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
return ZYAN_STATUS_SUCCESS;
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_XACQUIRE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_XACQUIRE, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_XRELEASE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_XRELEASE, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_LOCK)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_LOCK, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_BND)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_BND, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_NOTRACK)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_NOTRACK, formatter->case_prefixes);
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_REP)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_REP, formatter->case_prefixes);
return ZYAN_STATUS_SUCCESS;
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_REPE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_REPE, formatter->case_prefixes);
return ZYAN_STATUS_SUCCESS;
}
if (context->instruction->attributes & ZYDIS_ATTRIB_HAS_REPNE)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, PREF_REPNE, formatter->case_prefixes);
return ZYAN_STATUS_SUCCESS;
}
return ZYAN_STATUS_SUCCESS;
}
ZyanStatus ZydisFormatterBasePrintDecorator(const ZydisFormatter* formatter,
ZydisFormatterBuffer* buffer, ZydisFormatterContext* context, ZydisDecorator decorator)
{
ZYAN_ASSERT(formatter);
ZYAN_ASSERT(buffer);
ZYAN_ASSERT(context);
#if defined(ZYDIS_DISABLE_AVX512) && defined(ZYDIS_DISABLE_KNC)
ZYAN_UNUSED(formatter);
ZYAN_UNUSED(buffer);
ZYAN_UNUSED(context);
#endif
switch (decorator)
{
case ZYDIS_DECORATOR_MASK:
{
#if !defined(ZYDIS_DISABLE_AVX512) || !defined(ZYDIS_DISABLE_KNC)
if (context->instruction->avx.mask.reg != ZYDIS_REGISTER_K0)
{
if (buffer->is_token_list)
{
ZYDIS_BUFFER_APPEND(buffer, DECO_BEGIN);
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->instruction->avx.mask.reg));
ZYDIS_BUFFER_APPEND(buffer, DECO_END);
} else
{
ZYAN_CHECK(ZydisStringAppendShort(&buffer->string, &STR_DECO_BEGIN));
ZYAN_CHECK(formatter->func_print_register(formatter, buffer, context,
context->instruction->avx.mask.reg));
ZYAN_CHECK(ZydisStringAppendShort(&buffer->string, &STR_DECO_END));
}
// Only print the zeroing decorator, if the instruction is not a "zeroing masking only"
// instruction (e.g. `vcmpsd`)
if ((context->instruction->avx.mask.mode == ZYDIS_MASK_MODE_ZEROING ||
context->instruction->avx.mask.mode == ZYDIS_MASK_MODE_CONTROL_ZEROING) &&
(context->instruction->raw.evex.z))
{
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_ZERO, formatter->case_decorators);
}
}
#endif
break;
}
case ZYDIS_DECORATOR_BC:
#if !defined(ZYDIS_DISABLE_AVX512)
if (!context->instruction->avx.broadcast.is_static)
{
switch (context->instruction->avx.broadcast.mode)
{
case ZYDIS_BROADCAST_MODE_INVALID:
break;
case ZYDIS_BROADCAST_MODE_1_TO_2:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_1TO2, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_1_TO_4:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_1TO4, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_1_TO_8:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_1TO8, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_1_TO_16:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_1TO16, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_1_TO_32:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_1TO32, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_1_TO_64:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_1TO64, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_4_TO_8:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_4TO8, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_4_TO_16:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_4TO16, formatter->case_decorators);
break;
case ZYDIS_BROADCAST_MODE_8_TO_16:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_8TO16, formatter->case_decorators);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
#endif
break;
case ZYDIS_DECORATOR_RC:
#if !defined(ZYDIS_DISABLE_AVX512)
if (context->instruction->avx.has_sae)
{
switch (context->instruction->avx.rounding.mode)
{
case ZYDIS_ROUNDING_MODE_INVALID:
break;
case ZYDIS_ROUNDING_MODE_RN:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RN_SAE, formatter->case_decorators);
break;
case ZYDIS_ROUNDING_MODE_RD:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RD_SAE, formatter->case_decorators);
break;
case ZYDIS_ROUNDING_MODE_RU:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RU_SAE, formatter->case_decorators);
break;
case ZYDIS_ROUNDING_MODE_RZ:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RZ_SAE, formatter->case_decorators);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
} else
{
switch (context->instruction->avx.rounding.mode)
{
case ZYDIS_ROUNDING_MODE_INVALID:
break;
case ZYDIS_ROUNDING_MODE_RN:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RN, formatter->case_decorators);
break;
case ZYDIS_ROUNDING_MODE_RD:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RD, formatter->case_decorators);
break;
case ZYDIS_ROUNDING_MODE_RU:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RU, formatter->case_decorators);
break;
case ZYDIS_ROUNDING_MODE_RZ:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_RZ, formatter->case_decorators);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
}
#endif
break;
case ZYDIS_DECORATOR_SAE:
#if !defined(ZYDIS_DISABLE_AVX512)
if (context->instruction->avx.has_sae && !context->instruction->avx.rounding.mode)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_SAE, formatter->case_decorators);
}
#endif
break;
case ZYDIS_DECORATOR_SWIZZLE:
#if !defined(ZYDIS_DISABLE_KNC)
switch (context->instruction->avx.swizzle.mode)
{
case ZYDIS_SWIZZLE_MODE_INVALID:
case ZYDIS_SWIZZLE_MODE_DCBA:
// Nothing to do here
break;
case ZYDIS_SWIZZLE_MODE_CDAB:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_CDAB, formatter->case_decorators);
break;
case ZYDIS_SWIZZLE_MODE_BADC:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_BADC, formatter->case_decorators);
break;
case ZYDIS_SWIZZLE_MODE_DACB:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_DACB, formatter->case_decorators);
break;
case ZYDIS_SWIZZLE_MODE_AAAA:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_AAAA, formatter->case_decorators);
break;
case ZYDIS_SWIZZLE_MODE_BBBB:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_BBBB, formatter->case_decorators);
break;
case ZYDIS_SWIZZLE_MODE_CCCC:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_CCCC, formatter->case_decorators);
break;
case ZYDIS_SWIZZLE_MODE_DDDD:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_DDDD, formatter->case_decorators);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
#endif
break;
case ZYDIS_DECORATOR_CONVERSION:
#if !defined(ZYDIS_DISABLE_KNC)
switch (context->instruction->avx.conversion.mode)
{
case ZYDIS_CONVERSION_MODE_INVALID:
break;
case ZYDIS_CONVERSION_MODE_FLOAT16:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_FLOAT16, formatter->case_decorators);
break;
case ZYDIS_CONVERSION_MODE_SINT8:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_SINT8, formatter->case_decorators);
break;
case ZYDIS_CONVERSION_MODE_UINT8:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_UINT8, formatter->case_decorators);
break;
case ZYDIS_CONVERSION_MODE_SINT16:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_SINT16, formatter->case_decorators);
break;
case ZYDIS_CONVERSION_MODE_UINT16:
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_UINT16, formatter->case_decorators);
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
#endif
break;
case ZYDIS_DECORATOR_EH:
#if !defined(ZYDIS_DISABLE_KNC)
if (context->instruction->avx.has_eviction_hint)
{
ZYDIS_BUFFER_APPEND_CASE(buffer, DECO_EH, formatter->case_decorators);
}
#endif
break;
default:
return ZYAN_STATUS_INVALID_ARGUMENT;
}
return ZYAN_STATUS_SUCCESS;
}
/* ---------------------------------------------------------------------------------------------- */
/* ============================================================================================== */
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
#endif /* ENABLE(ZYDIS) */
|