Skip to content

Commit

Permalink
chore: Code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Feb 18, 2024
1 parent c8a97d4 commit fe90ad9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/ElfSymbolNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace Hosihikari.FastElfQuery;

public class ElfSymbolNotFoundException : Exception
public sealed class ElfSymbolNotFoundException : Exception
{
public string SymbolName { get; }

internal ElfSymbolNotFoundException(string name)
: base($"Symbol {name} not found")
{
SymbolName = name;
}
}

public string SymbolName { get; }
}
44 changes: 28 additions & 16 deletions src/ElfSymbolQueryTable.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
using System.Runtime.CompilerServices;
using ELFSharp.ELF;
using ELFSharp.ELF;
using ELFSharp.ELF.Sections;
using System.Runtime.CompilerServices;

namespace Hosihikari.FastElfQuery;

public class ElfSymbolQueryTable
public sealed class ElfSymbolQueryTable
{
#region ---Private Fields---

private readonly SortedDictionary<int, int> _table = new();

#endregion

#region ---Constructors---

public ElfSymbolQueryTable(string path)
{
LoadFromElf(path);
}

#endregion

#region ---Private Methods---

private static IEnumerable<(string name, ulong offset)> EnumerableSymbolsFromElf(IELF elf)
{
var symbolTable = (ISymbolTable)elf.GetSection(".symtab");
var symbolEntries = symbolTable.Entries.Cast<SymbolEntry<ulong>>();
foreach (var symbolEntry in symbolEntries)
ISymbolTable symbolTable = (ISymbolTable)elf.GetSection(".symtab");
IEnumerable<SymbolEntry<ulong>> symbolEntries = symbolTable.Entries.Cast<SymbolEntry<ulong>>();
foreach (SymbolEntry<ulong> symbolEntry in symbolEntries)
{
var name = symbolEntry.Name;
string name = symbolEntry.Name;
if (!string.IsNullOrWhiteSpace(name))
{
yield return (name, symbolEntry.Value);
Expand All @@ -31,8 +40,8 @@ public ElfSymbolQueryTable(string path)
private void LoadFromElf(string path)
{
_table.Clear();
using var elf = ELFReader.Load(path);
foreach (var (name, offset) in EnumerableSymbolsFromElf(elf))
using IELF elf = ELFReader.Load(path);
foreach ((string name, ulong offset) in EnumerableSymbolsFromElf(elf))
{
switch (name)
{
Expand All @@ -51,6 +60,7 @@ private void LoadFromElf(string path)
case "_ZL32TextProcessingEventOriginEnumMapB5cxx11":
continue;
}

//cast ulong to int to optimize memory usage
Add(name, (int)offset);
}
Expand All @@ -64,33 +74,35 @@ private void Add(string name, int offset)
#if UnknownSymbol
if (!
#endif
_table.TryAdd(name.GetHashCode(), offset)
_table.TryAdd(name.GetHashCode(), offset)
#if UnknownSymbol
)
{
Console.WriteLine("duplicated symbol {0}, offset {1}", name, offset);
}
#else
;
;
#endif
}

#endregion
#region ---Private Fields---
private readonly SortedDictionary<int, int> _table = new();
#endregion

#region ---Public Methods---

public int Query(string symbolName)
{
if (_table.TryGetValue(symbolName.GetHashCode(), out var offset))
if (_table.TryGetValue(symbolName.GetHashCode(), out int offset))
{
return offset;
}

throw new ElfSymbolNotFoundException(symbolName);
}

public bool TryQuery(string symbolName, out int offset)
{
return _table.TryGetValue(symbolName.GetHashCode(), out offset);
}

#endregion
}
}
1 change: 0 additions & 1 deletion src/Hosihikari.FastElfQuery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit fe90ad9

Please sign in to comment.