Skip to content

Commit

Permalink
basic support for symbol names for CFG functions. Minor other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zodiacon committed Feb 14, 2017
1 parent 8aa50d1 commit 5648aed
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 27 deletions.
37 changes: 21 additions & 16 deletions PEExplorer.Core/LoadConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Diagnostics.Runtime.Utilities;
using DebugHelp;
using Microsoft.Diagnostics.Runtime.Utilities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
Expand Down Expand Up @@ -37,25 +38,29 @@ internal LoadConfiguration(PEFileParser pefile, ref IMAGE_LOAD_CONFIG_DIRECTORY6
public ulong CFGFunctionCount => _is32bit ? _config32.GuardCFFunctionCount : _config64.GuardCFFunctionCount;

public async Task<IEnumerable<ExportedSymbol>> GetCFGFunctions() {
var locator = new DefaultSymbolLocator();
string pdbFileName;
Guid pdbGuid;
int pdbAge;
if (_pefile.File.GetPdbSignature(out pdbFileName, out pdbGuid, out pdbAge)) {
pdbFileName = await locator.FindPdbAsync(pdbFileName, pdbGuid, pdbAge);
if (pdbFileName != null) {

}
}
var va = CFGFunctionTable - _pefile.Header.ImageBase;
int count = (int)CFGFunctionCount;
var symbols = new ExportedSymbol[count];

var offset = _pefile.Header.RvaToFileOffset((int)va);
for (int i = 0; i < count; i++) {
var address = _pefile.Accessor.ReadUInt32(offset);
symbols[i] = new ExportedSymbol { Address = address };
offset += 4;
int lastIndex = -1;

using(var handler = SymbolHandler.Create(SymbolOptions.PublicsOnly | SymbolOptions.UndecorateNames | SymbolOptions.Debug)) {
var dllBase = await handler.TryLoadSymbolsForModuleAsync(_pefile.Filename);

for(int i = 0; i < count; i++) {
var address = _pefile.Accessor.ReadUInt32(offset);
string name = null;
if(dllBase != 0) {
ulong disp;
var symbol = new SymbolInfo();
if(handler.TryGetSymbolFromAddress(address + dllBase, ref symbol, out disp) && (lastIndex < 0 || symbol.Name != symbols[lastIndex].Name)) {
name = symbol.Name;
lastIndex = i;
}
}
symbols[i] = new ExportedSymbol { Address = address, Name = name };
offset += 5;
}
}

return symbols;
Expand Down
6 changes: 6 additions & 0 deletions PEExplorer.Core/PEExplorer.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\DebugHelp\DebugHelp\DebugHelp.csproj">
<Project>{DB1D8FEA-43A9-4C62-9EB3-E541A3550C3B}</Project>
<Name>DebugHelp</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
6 changes: 6 additions & 0 deletions PEExplorer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PEExplorer", "PEExplorer\PE
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PEExplorer.Core", "PEExplorer.Core\PEExplorer.Core.csproj", "{F5A21CBF-3DA6-4F54-A2BF-085325D2996F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DebugHelp", "..\DebugHelp\DebugHelp\DebugHelp.csproj", "{DB1D8FEA-43A9-4C62-9EB3-E541A3550C3B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{F5A21CBF-3DA6-4F54-A2BF-085325D2996F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5A21CBF-3DA6-4F54-A2BF-085325D2996F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5A21CBF-3DA6-4F54-A2BF-085325D2996F}.Release|Any CPU.Build.0 = Release|Any CPU
{DB1D8FEA-43A9-4C62-9EB3-E541A3550C3B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB1D8FEA-43A9-4C62-9EB3-E541A3550C3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB1D8FEA-43A9-4C62-9EB3-E541A3550C3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB1D8FEA-43A9-4C62-9EB3-E541A3550C3B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
4 changes: 4 additions & 0 deletions PEExplorer/PEExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@
<Resource Include="Icons\export1.ico" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\DebugHelp\DebugHelp\DebugHelp.csproj">
<Project>{db1d8fea-43a9-4c62-9eb3-e541a3550c3b}</Project>
<Name>DebugHelp</Name>
</ProjectReference>
<ProjectReference Include="..\PEExplorer.Core\PEExplorer.Core.csproj">
<Project>{f5a21cbf-3da6-4f54-a2bf-085325d2996f}</Project>
<Name>PEExplorer.Core</Name>
Expand Down
19 changes: 10 additions & 9 deletions PEExplorer/ViewModels/InstructionViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
using SharpDisasm;

namespace PEExplorer.ViewModels {
class InstructionViewModel {
public Instruction Instruction { get; }
class InstructionViewModel {
public Instruction Instruction { get; }

public InstructionViewModel(Instruction instruction) {
Instruction = instruction;
}
public InstructionViewModel(Instruction instruction) {
Instruction = instruction;
}

public string Bytes => string.Join(" ", Instruction.Bytes);
public string Bytes => string.Join(" ", Instruction.Bytes);

public string Address => Instruction.Offset.ToString("X8");
public string Address => Instruction.Offset.ToString("X8");

public string Text => Instruction.ToString().ToUpper();
}
public string Text => Instruction.ToString().Substring(8).ToUpper();

}
}
2 changes: 1 addition & 1 deletion PEExplorer/Views/DisassemblyView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<ListBox Grid.Row="1" ItemsSource="{Binding Instructions}" SelectionMode="Single" FontFamily="Consolas" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Instruction}" FontWeight="Bold" Margin="4,0"/>
<TextBlock Text="{Binding Text}" FontWeight="Bold" Margin="4,0"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Expand Down
2 changes: 1 addition & 1 deletion PEExplorer/Views/MainView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding Icon}" Width="24" Height="24" />
<TextBlock Text="{Binding Text}" VerticalAlignment="Center" FontSize="14" Margin="4,0,0,0"/>
<TextBlock Text="{Binding Text}" VerticalAlignment="Center" FontSize="12" Margin="4,0,0,0"/>
<Button Content="{z:Image /icons/close.ico, Width=16, Height=16}" Padding="0" Opacity=".7"
Command="{Binding DataContext.CloseTabCommand, ElementName=_Tabs}" Margin="6,0,0,0" BorderThickness="0" Focusable="False" Background="Transparent"
CommandParameter="{Binding}" Visibility="{Binding CanClose, Converter={StaticResource bool2vis}}"/>
Expand Down

0 comments on commit 5648aed

Please sign in to comment.