zyantific / zydis

Fast and lightweight x86/x86-64 disassembler and code generation library
https://zydis.re
MIT License
3.47k stars 438 forks source link

ZydisEncoder specify instruction length #534

Closed qcold closed 2 weeks ago

qcold commented 2 weeks ago

How to specify instruction operand size explicitly? I did it with ZYDIS_OPERAND_SIZE_HINT_32, am i right? How can i do that in another way? e.g i wanna encode sub reg, imm32, but i get sub reg, imm8

static ZydisOperandSizeHint sizeToHint(int size) {
  switch (size) {
    case 8:
      return ZYDIS_OPERAND_SIZE_HINT_64;
    case 4:
      return ZYDIS_OPERAND_SIZE_HINT_32;
    case 2:
      return ZYDIS_OPERAND_SIZE_HINT_16;
    case 1:
      return ZYDIS_OPERAND_SIZE_HINT_8;
  }
}
mappzor commented 2 weeks ago

Operand size hints are used in very rare cases (e.g. enter, leave) when operand size cannot be determined automatically. See documentation for more details. In your case encoder will determine smallest possible encoding automatically, so if your immediate fits into imm8, it's the smallest possible encoding, so it's going to be prioritized.

qcold commented 2 weeks ago

Operand size hints are used in very rare cases (e.g. enter, leave) when operand size cannot be determined automatically. See documentation for more details. In your case encoder will determine smallest possible encoding automatically, so if your immediate fits into imm8, it's the smallest possible encoding, so it's going to be prioritized.

could i disable such a feature or should i edit the code?

mappzor commented 2 weeks ago

You can't disable that. API is semantic-driven and encoder is tasked with providing size-optimal output. If you need to fill some extra space e.g. during re-encoding I recommend using ZydisEncoderNopFill.