diff --git a/src/ElfSymbolNotFoundException.cs b/src/ElfSymbolNotFoundException.cs index da975d4..1c1e1fb 100644 --- a/src/ElfSymbolNotFoundException.cs +++ b/src/ElfSymbolNotFoundException.cs @@ -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; } +} \ No newline at end of file diff --git a/src/ElfSymbolQueryTable.cs b/src/ElfSymbolQueryTable.cs index af75af2..98c8400 100644 --- a/src/ElfSymbolQueryTable.cs +++ b/src/ElfSymbolQueryTable.cs @@ -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 _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>(); - foreach (var symbolEntry in symbolEntries) + ISymbolTable symbolTable = (ISymbolTable)elf.GetSection(".symtab"); + IEnumerable> symbolEntries = symbolTable.Entries.Cast>(); + foreach (SymbolEntry symbolEntry in symbolEntries) { - var name = symbolEntry.Name; + string name = symbolEntry.Name; if (!string.IsNullOrWhiteSpace(name)) { yield return (name, symbolEntry.Value); @@ -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) { @@ -51,6 +60,7 @@ private void LoadFromElf(string path) case "_ZL32TextProcessingEventOriginEnumMapB5cxx11": continue; } + //cast ulong to int to optimize memory usage Add(name, (int)offset); } @@ -64,27 +74,28 @@ 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 _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); } @@ -92,5 +103,6 @@ public bool TryQuery(string symbolName, out int offset) { return _table.TryGetValue(symbolName.GetHashCode(), out offset); } + #endregion -} +} \ No newline at end of file diff --git a/src/Hosihikari.FastElfQuery.csproj b/src/Hosihikari.FastElfQuery.csproj index e9f69b2..d4345a1 100644 --- a/src/Hosihikari.FastElfQuery.csproj +++ b/src/Hosihikari.FastElfQuery.csproj @@ -4,7 +4,6 @@ net8.0 enable enable - true