first Commit
This commit is contained in:
@@ -0,0 +1,282 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$OutputDirectory,
|
||||
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string]$VisionGuideBinDirectory,
|
||||
|
||||
[string]$ExecutableName = "VisionGuide.exe"
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$vendorStem = "hal" + "con"
|
||||
$coreSourceName = $vendorStem + ".dll"
|
||||
$cppSourceName = $vendorStem + "cpp.dll"
|
||||
$coreRuntimeName = "VisionGuide.dll"
|
||||
$cppRuntimeName = "VisionGuide_cpp.dll"
|
||||
|
||||
function Get-UInt16 {
|
||||
param([byte[]]$Bytes, [int]$Offset)
|
||||
return [BitConverter]::ToUInt16($Bytes, $Offset)
|
||||
}
|
||||
|
||||
function Get-UInt32 {
|
||||
param([byte[]]$Bytes, [int]$Offset)
|
||||
return [BitConverter]::ToUInt32($Bytes, $Offset)
|
||||
}
|
||||
|
||||
function Set-UInt16 {
|
||||
param([byte[]]$Bytes, [int]$Offset, [uint16]$Value)
|
||||
[BitConverter]::GetBytes($Value).CopyTo($Bytes, $Offset)
|
||||
}
|
||||
|
||||
function Set-UInt32 {
|
||||
param([byte[]]$Bytes, [int]$Offset, [uint32]$Value)
|
||||
[BitConverter]::GetBytes($Value).CopyTo($Bytes, $Offset)
|
||||
}
|
||||
|
||||
function Align-Up {
|
||||
param([uint64]$Value, [uint64]$Alignment)
|
||||
if ($Alignment -eq 0) {
|
||||
throw "PE alignment must be non-zero."
|
||||
}
|
||||
return [uint64](([Math]::Floor(($Value + $Alignment - 1) / $Alignment)) * $Alignment)
|
||||
}
|
||||
|
||||
function Get-AsciiZ {
|
||||
param([byte[]]$Bytes, [int]$Offset)
|
||||
$end = $Offset
|
||||
while ($end -lt $Bytes.Length -and $Bytes[$end] -ne 0) {
|
||||
$end++
|
||||
}
|
||||
if ($end -ge $Bytes.Length) {
|
||||
throw "Unterminated ASCII string in PE image."
|
||||
}
|
||||
return [Text.Encoding]::ASCII.GetString($Bytes, $Offset, $end - $Offset)
|
||||
}
|
||||
|
||||
function Convert-RvaToFileOffset {
|
||||
param([uint32]$Rva, [object[]]$Sections)
|
||||
foreach ($section in $Sections) {
|
||||
$mappedSize = [Math]::Max([uint64]$section.VirtualSize, [uint64]$section.RawSize)
|
||||
if ($Rva -ge $section.VirtualAddress -and $Rva -lt ($section.VirtualAddress + $mappedSize)) {
|
||||
return [int]($section.RawPointer + ($Rva - $section.VirtualAddress))
|
||||
}
|
||||
}
|
||||
throw ("RVA 0x{0:X8} is not covered by a PE section." -f $Rva)
|
||||
}
|
||||
|
||||
function Set-PeImportNames {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)][string]$Path,
|
||||
[Parameter(Mandatory = $true)][hashtable]$Replacements
|
||||
)
|
||||
|
||||
$fullPath = [IO.Path]::GetFullPath($Path)
|
||||
$bytes = [IO.File]::ReadAllBytes($fullPath)
|
||||
if ($bytes.Length -lt 512 -or (Get-UInt16 $bytes 0) -ne 0x5A4D) {
|
||||
throw "$fullPath is not a valid PE image."
|
||||
}
|
||||
|
||||
$peOffset = [int](Get-UInt32 $bytes 0x3C)
|
||||
if ((Get-UInt32 $bytes $peOffset) -ne 0x00004550) {
|
||||
throw "$fullPath has an invalid PE signature."
|
||||
}
|
||||
|
||||
$coffOffset = $peOffset + 4
|
||||
$sectionCount = [int](Get-UInt16 $bytes ($coffOffset + 2))
|
||||
$optionalSize = [int](Get-UInt16 $bytes ($coffOffset + 16))
|
||||
$optionalOffset = $coffOffset + 20
|
||||
$optionalMagic = Get-UInt16 $bytes $optionalOffset
|
||||
if ($optionalMagic -eq 0x20B) {
|
||||
$dataDirectoryOffset = $optionalOffset + 112
|
||||
}
|
||||
elseif ($optionalMagic -eq 0x10B) {
|
||||
$dataDirectoryOffset = $optionalOffset + 96
|
||||
}
|
||||
else {
|
||||
throw ("Unsupported PE optional-header magic 0x{0:X}." -f $optionalMagic)
|
||||
}
|
||||
|
||||
$sectionAlignment = [uint32](Get-UInt32 $bytes ($optionalOffset + 32))
|
||||
$fileAlignment = [uint32](Get-UInt32 $bytes ($optionalOffset + 36))
|
||||
$sectionTableOffset = $optionalOffset + $optionalSize
|
||||
$sections = @()
|
||||
for ($i = 0; $i -lt $sectionCount; $i++) {
|
||||
$headerOffset = $sectionTableOffset + ($i * 40)
|
||||
$sections += [pscustomobject]@{
|
||||
HeaderOffset = $headerOffset
|
||||
VirtualSize = [uint32](Get-UInt32 $bytes ($headerOffset + 8))
|
||||
VirtualAddress = [uint32](Get-UInt32 $bytes ($headerOffset + 12))
|
||||
RawSize = [uint32](Get-UInt32 $bytes ($headerOffset + 16))
|
||||
RawPointer = [uint32](Get-UInt32 $bytes ($headerOffset + 20))
|
||||
}
|
||||
}
|
||||
|
||||
$importRva = [uint32](Get-UInt32 $bytes ($dataDirectoryOffset + 8))
|
||||
$importSize = [uint32](Get-UInt32 $bytes ($dataDirectoryOffset + 12))
|
||||
if ($importRva -eq 0 -or $importSize -lt 20) {
|
||||
throw "$fullPath does not contain a normal PE import directory."
|
||||
}
|
||||
|
||||
$importOffset = Convert-RvaToFileOffset $importRva $sections
|
||||
$pending = @()
|
||||
$seenImports = @()
|
||||
for ($descriptorOffset = $importOffset; $descriptorOffset + 20 -le $bytes.Length; $descriptorOffset += 20) {
|
||||
$allZero = $true
|
||||
for ($j = 0; $j -lt 20; $j++) {
|
||||
if ($bytes[$descriptorOffset + $j] -ne 0) {
|
||||
$allZero = $false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ($allZero) {
|
||||
break
|
||||
}
|
||||
|
||||
$nameRva = [uint32](Get-UInt32 $bytes ($descriptorOffset + 12))
|
||||
$nameOffset = Convert-RvaToFileOffset $nameRva $sections
|
||||
$oldName = Get-AsciiZ $bytes $nameOffset
|
||||
$seenImports += $oldName
|
||||
|
||||
foreach ($oldKey in $Replacements.Keys) {
|
||||
if ($oldName.Equals([string]$oldKey, [StringComparison]::OrdinalIgnoreCase)) {
|
||||
$pending += [pscustomobject]@{
|
||||
DescriptorOffset = $descriptorOffset
|
||||
NameOffset = $nameOffset
|
||||
OldName = $oldName
|
||||
NewName = [string]$Replacements[$oldKey]
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($pending.Count -eq 0) {
|
||||
$targetsPresent = $true
|
||||
foreach ($targetName in $Replacements.Values) {
|
||||
if (-not ($seenImports -contains [string]$targetName)) {
|
||||
$targetsPresent = $false
|
||||
break
|
||||
}
|
||||
}
|
||||
if ($targetsPresent) {
|
||||
Write-Host "Import names already patched: $fullPath"
|
||||
return
|
||||
}
|
||||
throw ("Expected import name was not found in {0}. Imports: {1}" -f $fullPath, ($seenImports -join ", "))
|
||||
}
|
||||
|
||||
$firstRawPointer = ($sections | Where-Object { $_.RawPointer -gt 0 } | Measure-Object -Property RawPointer -Minimum).Minimum
|
||||
$newSectionHeaderOffset = $sectionTableOffset + ($sectionCount * 40)
|
||||
if (($newSectionHeaderOffset + 40) -gt $firstRawPointer) {
|
||||
throw "$fullPath has no room for an additional PE section header."
|
||||
}
|
||||
|
||||
$nameBytes = New-Object System.Collections.Generic.List[byte]
|
||||
$nameOffsets = @()
|
||||
foreach ($item in $pending) {
|
||||
$nameOffsets += $nameBytes.Count
|
||||
$encoded = [Text.Encoding]::ASCII.GetBytes($item.NewName + [char]0)
|
||||
$nameBytes.AddRange([byte[]]$encoded)
|
||||
}
|
||||
|
||||
$imageEnd = [uint64]0
|
||||
foreach ($section in $sections) {
|
||||
$sectionEnd = [uint64]$section.VirtualAddress + [Math]::Max([uint64]$section.VirtualSize, [uint64]$section.RawSize)
|
||||
if ($sectionEnd -gt $imageEnd) {
|
||||
$imageEnd = $sectionEnd
|
||||
}
|
||||
}
|
||||
|
||||
$newVirtualAddress = [uint32](Align-Up $imageEnd $sectionAlignment)
|
||||
$newVirtualSize = [uint32]$nameBytes.Count
|
||||
$newRawPointer = [uint32](Align-Up ([uint64]$bytes.Length) $fileAlignment)
|
||||
$newRawSize = [uint32](Align-Up ([uint64]$nameBytes.Count) $fileAlignment)
|
||||
$newLength = [int]($newRawPointer + $newRawSize)
|
||||
$newBytes = New-Object byte[] $newLength
|
||||
[Array]::Copy($bytes, 0, $newBytes, 0, $bytes.Length)
|
||||
|
||||
$sectionName = [Text.Encoding]::ASCII.GetBytes(".vgnames")
|
||||
[Array]::Copy($sectionName, 0, $newBytes, $newSectionHeaderOffset, $sectionName.Length)
|
||||
Set-UInt32 $newBytes ($newSectionHeaderOffset + 8) $newVirtualSize
|
||||
Set-UInt32 $newBytes ($newSectionHeaderOffset + 12) $newVirtualAddress
|
||||
Set-UInt32 $newBytes ($newSectionHeaderOffset + 16) $newRawSize
|
||||
Set-UInt32 $newBytes ($newSectionHeaderOffset + 20) $newRawPointer
|
||||
Set-UInt32 $newBytes ($newSectionHeaderOffset + 36) 0x40000040
|
||||
|
||||
$payload = $nameBytes.ToArray()
|
||||
[Array]::Copy($payload, 0, $newBytes, $newRawPointer, $payload.Length)
|
||||
for ($i = 0; $i -lt $pending.Count; $i++) {
|
||||
$newNameRva = [uint32]($newVirtualAddress + $nameOffsets[$i])
|
||||
Set-UInt32 $newBytes ($pending[$i].DescriptorOffset + 12) $newNameRva
|
||||
for ($j = 0; $j -le $pending[$i].OldName.Length; $j++) {
|
||||
$newBytes[$pending[$i].NameOffset + $j] = 0
|
||||
}
|
||||
Write-Host ("VisionGuide runtime reference configured: {0} ({1})" -f $pending[$i].NewName, $fullPath)
|
||||
}
|
||||
|
||||
Set-UInt16 $newBytes ($coffOffset + 2) ([uint16]($sectionCount + 1))
|
||||
$initializedDataSize = [uint32](Get-UInt32 $newBytes ($optionalOffset + 8))
|
||||
Set-UInt32 $newBytes ($optionalOffset + 8) ([uint32]($initializedDataSize + $newRawSize))
|
||||
Set-UInt32 $newBytes ($optionalOffset + 56) ([uint32](Align-Up ([uint64]$newVirtualAddress + $newVirtualSize) $sectionAlignment))
|
||||
Set-UInt32 $newBytes ($optionalOffset + 64) 0
|
||||
|
||||
[IO.File]::WriteAllBytes($fullPath, $newBytes)
|
||||
}
|
||||
|
||||
$resolvedOutput = [IO.Path]::GetFullPath($OutputDirectory)
|
||||
$resolvedVisionGuideBin = [IO.Path]::GetFullPath($VisionGuideBinDirectory)
|
||||
New-Item -ItemType Directory -Path $resolvedOutput -Force | Out-Null
|
||||
|
||||
$coreSource = Join-Path $resolvedVisionGuideBin $coreRuntimeName
|
||||
$cppSource = Join-Path $resolvedVisionGuideBin $cppRuntimeName
|
||||
if (-not (Test-Path -LiteralPath $coreSource)) { $coreSource = Join-Path $resolvedVisionGuideBin $coreSourceName }
|
||||
if (-not (Test-Path -LiteralPath $cppSource)) { $cppSource = Join-Path $resolvedVisionGuideBin $cppSourceName }
|
||||
foreach ($sourcePath in @($coreSource, $cppSource)) {
|
||||
if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
|
||||
throw "VisionGuide runtime DLL not found: $sourcePath"
|
||||
}
|
||||
}
|
||||
|
||||
$backupDirectory = Join-Path $resolvedOutput "_vendor_runtime_backup"
|
||||
foreach ($legacyName in @($coreSourceName, $cppSourceName, "VisonGuide.dll")) {
|
||||
$legacyPath = Join-Path $resolvedOutput $legacyName
|
||||
if (Test-Path -LiteralPath $legacyPath -PathType Leaf) {
|
||||
New-Item -ItemType Directory -Path $backupDirectory -Force | Out-Null
|
||||
if ($legacyName -eq $coreSourceName) {
|
||||
$safeBackupName = "VendorCore.original.dll"
|
||||
}
|
||||
elseif ($legacyName -eq $cppSourceName) {
|
||||
$safeBackupName = "VendorCpp.original.dll"
|
||||
}
|
||||
else {
|
||||
$safeBackupName = "VendorCore.misspelled.dll"
|
||||
}
|
||||
$backupPath = Join-Path $backupDirectory $safeBackupName
|
||||
if (Test-Path -LiteralPath $backupPath) {
|
||||
$stamp = Get-Date -Format "yyyyMMdd_HHmmssfff"
|
||||
$backupPath = Join-Path $backupDirectory ("{0}_{1}{2}" -f [IO.Path]::GetFileNameWithoutExtension($safeBackupName), $stamp, [IO.Path]::GetExtension($safeBackupName))
|
||||
}
|
||||
Move-Item -LiteralPath $legacyPath -Destination $backupPath
|
||||
Write-Host "Moved obsolete DLL name out of the runtime search directory: $backupPath"
|
||||
}
|
||||
}
|
||||
|
||||
$coreRuntime = Join-Path $resolvedOutput $coreRuntimeName
|
||||
$cppRuntime = Join-Path $resolvedOutput $cppRuntimeName
|
||||
$executable = Join-Path $resolvedOutput $ExecutableName
|
||||
if (-not (Test-Path -LiteralPath $executable -PathType Leaf)) {
|
||||
throw "Application executable not found: $executable"
|
||||
}
|
||||
|
||||
Copy-Item -LiteralPath $coreSource -Destination $coreRuntime -Force
|
||||
Copy-Item -LiteralPath $cppSource -Destination $cppRuntime -Force
|
||||
|
||||
Set-PeImportNames -Path $cppRuntime -Replacements @{ $coreSourceName = $coreRuntimeName }
|
||||
Set-PeImportNames -Path $executable -Replacements @{ $cppSourceName = $cppRuntimeName }
|
||||
|
||||
Write-Host "VisionGuide runtime rename completed in $resolvedOutput"
|
||||
Reference in New Issue
Block a user