From d11fe85599a370d00da16154f3d25e147cb2a695 Mon Sep 17 00:00:00 2001 From: Andrei Vlad LUTAS Date: Fri, 24 Jul 2020 23:11:36 +0300 Subject: [PATCH] Improved linear address computation in bdshemu. --- bdshemu/bdshemu.c | 21 ++++++++++++++------- docs/source/index.rst | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/bdshemu/bdshemu.c b/bdshemu/bdshemu.c index 0e9a53e..23a487f 100644 --- a/bdshemu/bdshemu.c +++ b/bdshemu/bdshemu.c @@ -788,12 +788,6 @@ ShemuComputeLinearAddress( { uint64_t gla = 0; - // Memory operands usually have a segment. - if (Operand->Info.Memory.HasSeg) - { - gla += ShemuGetSegBase(Context, Operand->Info.Memory.Seg); - } - if (Operand->Info.Memory.HasBase) { gla += ShemuGetGprValue(Context, Operand->Info.Memory.Base, Operand->Info.Memory.BaseSize, false); @@ -857,11 +851,24 @@ ShemuComputeLinearAddress( gla &= 0xFFFFFFFF; break; case ND_ADDR_16: - gla &= 0xFFFFF; + gla &= 0xFFFF; default: break; } + // Memory operands usually have a segment. Note that we don't care about any segment checks, since we're most + // likely be provided with flat segments. If checks should be needed, dedicated callbacks should be added. + if (Operand->Info.Memory.HasSeg) + { + gla += ShemuGetSegBase(Context, Operand->Info.Memory.Seg); + + if (Context->Mode != ND_CODE_64) + { + // Truncate to 32 bit outside 64 bit. + gla &= 0xFFFFFFFF; + } + } + return gla; } diff --git a/docs/source/index.rst b/docs/source/index.rst index a6bc6ea..5e1d1e1 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -376,7 +376,7 @@ Determining if the instruction is a branch of any kind if (ix.RipAccess & ND_ACCESS_ANY_WRITE) // Instruction writes RIP. -Computing the linear address of a memory operand +Computing the virtual address of a memory operand ------------------------------------------------ .. code-block:: c