Skip to content

Commit 0a1db26

Browse files
committed
fixed DarthTon#112; RVAtoVA refactored
1 parent 04808dc commit 0a1db26

File tree

4 files changed

+50
-42
lines changed

4 files changed

+50
-42
lines changed

src/BlackBone/Include/Macro.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// Rebase address
88
#define MAKE_PTR(T, pRVA, base) (T)((ptr_t)pRVA + (ptr_t)base)
99
#define REBASE(pRVA, baseOld, baseNew) ((ptr_t)pRVA - (ptr_t)baseOld + (ptr_t)baseNew)
10-
#define REBASE2(T, rva, baseOld, baseNew) (T)((uintptr_t)rva - (uintptr_t)baseOld + (uintptr_t)baseNew)
1110

1211
// Field offset info
1312
#define FIELD_OFFSET2(type, field) ((LONG)(LONG_PTR)&(((type)0)->field))

src/BlackBone/ManualMap/MMap.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -818,16 +818,16 @@ NTSTATUS MMap::EnableExceptions( ImageContext* pImage )
818818
if (!_process.nativeLdr().InsertInvertedFunctionTable( pImage->imgMem.ptr<void*>(), pImage->peImage.imageSize(), safeseh ))
819819
{
820820
// Retry with documented method
821+
auto expTableRVA = pImage->peImage.DirectoryAddress( IMAGE_DIRECTORY_ENTRY_EXCEPTION, pe::RVA );
821822
size_t size = pImage->peImage.DirectorySize( IMAGE_DIRECTORY_ENTRY_EXCEPTION );
822-
auto pExpTable = reinterpret_cast<PIMAGE_RUNTIME_FUNCTION_ENTRY>(pImage->peImage.DirectoryAddress( IMAGE_DIRECTORY_ENTRY_EXCEPTION ));
823823

824824
// Invoke RtlAddFunctionTable
825-
if (pExpTable)
825+
if (expTableRVA)
826826
{
827827
AsmJitHelper a;
828828
uint64_t result = 0;
829-
830-
pImage->pExpTableAddr = REBASE( pExpTable, pImage->peImage.base(), pImage->imgMem.ptr<ptr_t>() );
829+
830+
pImage->pExpTableAddr = expTableRVA + pImage->imgMem.ptr<ptr_t>();
831831
auto pAddTable = _process.modules().GetExport(
832832
_process.modules().GetModule( L"ntdll.dll", LdrList, pImage->peImage.mType() ),
833833
"RtlAddFunctionTable"
@@ -847,9 +847,6 @@ NTSTATUS MMap::EnableExceptions( ImageContext* pImage )
847847
auto status = _process.remote().ExecInWorkerThread( a->make(), a->getCodeSize(), result );
848848
if (!NT_SUCCESS( status ))
849849
return status;
850-
851-
return (pImage->flags & CreateLdrRef) ? STATUS_SUCCESS :
852-
MExcept::CreateVEH( pImage->imgMem.ptr<uintptr_t>(), pImage->peImage.imageSize(), pImage->peImage.mType(), partial );
853850
}
854851
// No exception table
855852
else

src/BlackBone/PE/PEImage.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -383,64 +383,69 @@ void PEImage::GetExports( vecExports& exports )
383383
/// <param name="index">Directory index</param>
384384
/// <param name="keepRelative">Keep address relative to image base</param>
385385
/// <returns>Directory address</returns>
386-
size_t PEImage::DirectoryAddress( int index, bool keepRelative /*= false*/ ) const
386+
uintptr_t PEImage::DirectoryAddress( int index, AddressType type /*= VA*/ ) const
387387
{
388388
// Sanity check
389389
if (index < 0 || index >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
390390
return 0;
391391

392-
const IMAGE_DATA_DIRECTORY* idd = _is64 ? _pImageHdr64->OptionalHeader.DataDirectory
393-
: _pImageHdr32->OptionalHeader.DataDirectory;
392+
const auto idd = _is64 ? _pImageHdr64->OptionalHeader.DataDirectory : _pImageHdr32->OptionalHeader.DataDirectory;
393+
return idd[index].VirtualAddress == 0 ? 0 : ResolveRVAToVA( idd[index].VirtualAddress, type );
394+
}
394395

395-
if (idd[index].VirtualAddress == 0)
396+
/// <summary>
397+
/// Get data directory size
398+
/// </summary>
399+
/// <param name="index">Data directory index</param>
400+
/// <returns>Data directory size</returns>
401+
size_t PEImage::DirectorySize( int index ) const
402+
{
403+
// Sanity check
404+
if (index < 0 || index >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
396405
return 0;
397-
else
398-
return ResolveRVAToVA( idd[index].VirtualAddress, keepRelative );
406+
407+
const IMAGE_DATA_DIRECTORY* idd = _is64 ? _pImageHdr64->OptionalHeader.DataDirectory : _pImageHdr32->OptionalHeader.DataDirectory;
408+
return idd[index].VirtualAddress != 0 ? static_cast<size_t>(idd[index].Size) : 0;
399409
}
400410

411+
401412
/// <summary>
402413
/// Resolve virtual memory address to physical file offset
403414
/// </summary>
404415
/// <param name="Rva">Memory address</param>
405416
/// <param name="keepRelative">Keep address relative to file start</param>
406417
/// <returns>Resolved address</returns>
407-
uintptr_t PEImage::ResolveRVAToVA( uintptr_t Rva, bool keepRelative /*= false*/ ) const
418+
uintptr_t PEImage::ResolveRVAToVA( uintptr_t Rva, AddressType type /*= VA*/ ) const
408419
{
409-
if (_isPlainData)
420+
switch (type)
410421
{
411-
for (auto& sec : _sections)
422+
case blackbone::pe::RVA:
423+
return Rva;
424+
425+
case blackbone::pe::VA:
426+
case blackbone::pe::RPA:
427+
if (_isPlainData)
412428
{
413-
if (Rva >= sec.VirtualAddress && Rva < sec.VirtualAddress + sec.Misc.VirtualSize)
429+
for (auto& sec : _sections)
414430
{
415-
if (keepRelative)
416-
return (Rva - sec.VirtualAddress + sec.PointerToRawData);
417-
else
418-
return reinterpret_cast<uintptr_t>(_pFileBase) + (Rva - sec.VirtualAddress + sec.PointerToRawData);
431+
if (Rva >= sec.VirtualAddress && Rva < sec.VirtualAddress + sec.Misc.VirtualSize)
432+
if (type == VA)
433+
return reinterpret_cast<uintptr_t>(_pFileBase) + Rva - sec.VirtualAddress + sec.PointerToRawData;
434+
else
435+
return Rva - sec.VirtualAddress + sec.PointerToRawData;
419436
}
437+
438+
return 0;
420439
}
440+
else
441+
return (type == VA) ? (reinterpret_cast<uintptr_t>(_pFileBase) + Rva) : Rva;
421442

443+
default:
422444
return 0;
423445
}
424-
else
425-
return (keepRelative ? Rva : (reinterpret_cast<uintptr_t>(_pFileBase) + Rva));
426-
}
427446

428-
/// <summary>
429-
/// Get data directory size
430-
/// </summary>
431-
/// <param name="index">Data directory index</param>
432-
/// <returns>Data directory size</returns>
433-
size_t PEImage::DirectorySize( int index ) const
434-
{
435-
// Sanity check
436-
if (index < 0 || index >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES)
437-
return 0;
438-
439-
const IMAGE_DATA_DIRECTORY* idd = _is64 ? _pImageHdr64->OptionalHeader.DataDirectory : _pImageHdr32->OptionalHeader.DataDirectory;
440-
return idd[index].VirtualAddress != 0 ? static_cast<size_t>(idd[index].Size) : 0;
441447
}
442448

443-
444449
/// <summary>
445450
/// Retrieve image TLS callbacks
446451
/// Callbacks are rebased for target image

src/BlackBone/PE/PEImage.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ namespace blackbone
2323
namespace pe
2424
{
2525

26+
enum AddressType
27+
{
28+
RVA, // Relative virtual
29+
VA, // Absolute virtual
30+
RPA, // Relative physical
31+
};
32+
2633
// Relocation block information
2734
struct RelocData
2835
{
@@ -150,7 +157,7 @@ class PEImage
150157
/// <param name="index">Directory index</param>
151158
/// <param name="keepRelative">Keep address relative to image base</param>
152159
/// <returns>Directory address</returns>
153-
BLACKBONE_API size_t DirectoryAddress( int index, bool keepRelative = false ) const;
160+
BLACKBONE_API uintptr_t DirectoryAddress( int index, AddressType type = VA ) const;
154161

155162
/// <summary>
156163
/// Get data directory size
@@ -163,9 +170,9 @@ class PEImage
163170
/// Resolve virtual memory address to physical file offset
164171
/// </summary>
165172
/// <param name="Rva">Memory address</param>
166-
/// <param name="keepRelative">Keep address relative to file start</param>
173+
/// <param name="type">Address type to return</param>
167174
/// <returns>Resolved address</returns>
168-
BLACKBONE_API uintptr_t ResolveRVAToVA( uintptr_t Rva, bool keepRelative = false ) const;
175+
BLACKBONE_API uintptr_t ResolveRVAToVA( uintptr_t Rva, AddressType type = VA ) const;
169176

170177
/// <summary>
171178
/// Get image path

0 commit comments

Comments
 (0)