Decode ELF GNU ABI tag and build ID.

This commit is contained in:
2018-03-13 18:33:05 +00:00
parent 3945a53fce
commit bb6b17800f
6 changed files with 99 additions and 21 deletions

View File

@@ -4,7 +4,6 @@ using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace libexeinfo
{
@@ -13,9 +12,9 @@ namespace libexeinfo
List<Architecture> architectures;
Elf64_Ehdr Header;
string interpreter;
Dictionary<string, ElfNote> notes;
string[] sectionNames;
Elf64_Shdr[] sections;
Dictionary<string, ElfNote> notes;
/// <summary>
/// Initializes a new instance of the <see cref="T:libexeinfo.ELF" /> class.
@@ -58,7 +57,7 @@ namespace libexeinfo
public IEnumerable<Architecture> Architectures => architectures;
public OperatingSystem RequiredOperatingSystem { get; private set; }
public IEnumerable<string> Strings { get; private set; }
public IEnumerable<Segment> Segments { get; }
public IEnumerable<Segment> Segments { get; private set; }
/// <summary>
/// The <see cref="FileStream" /> that contains the executable represented by this instance
@@ -235,10 +234,13 @@ namespace libexeinfo
pos += (int)namesz;
switch(Header.ei_class) {
case eiClass.ELFCLASS32: pos += pos % 4;
switch(Header.ei_class)
{
case eiClass.ELFCLASS32:
pos += pos % 4;
break;
case eiClass.ELFCLASS64: pos += pos % 8;
case eiClass.ELFCLASS64:
pos += pos % 8;
break;
}
@@ -248,6 +250,18 @@ namespace libexeinfo
}
}
if(notes.TryGetValue(".note.ABI-tag", out ElfNote abiTag))
{
GnuAbiTag gnuAbiTag = DecodeGnuAbiTag(abiTag, IsBigEndian);
if(gnuAbiTag != null)
RequiredOperatingSystem = new OperatingSystem
{
Name = $"{gnuAbiTag.system}",
MajorVersion = (int)gnuAbiTag.major,
MinorVersion = (int)gnuAbiTag.minor
};
}
if(strings.Count > 0)
{
strings.Remove(null);
@@ -255,6 +269,8 @@ namespace libexeinfo
strings.Sort();
Strings = strings.Distinct();
}
Segments = segments;
}
static Elf64_Ehdr UpBits(byte[] buffer, bool bigEndian)

View File

@@ -941,5 +941,10 @@ namespace libexeinfo
/// <summary>PA-RISC 2.0</summary>
EFA_PARISC_2_0 = 0x0214
}
enum GnuAbiSystem : uint
{
Linux = 0
}
}
}

37
libexeinfo/ELF/GNU.cs Normal file
View File

@@ -0,0 +1,37 @@
using System;
using System.Text;
namespace libexeinfo
{
public partial class ELF
{
static GnuAbiTag DecodeGnuAbiTag(ElfNote note, bool bigEndian)
{
if(note.name != "GNU") return null;
return bigEndian
? new GnuAbiTag
{
system = (GnuAbiSystem)Swapping.Swap(BitConverter.ToUInt32(note.contents, 0)),
major = Swapping.Swap(BitConverter.ToUInt32(note.contents, 4)),
minor = Swapping.Swap(BitConverter.ToUInt32(note.contents, 8)),
revision = Swapping.Swap(BitConverter.ToUInt32(note.contents, 0))
}
: new GnuAbiTag
{
system = (GnuAbiSystem)BitConverter.ToUInt32(note.contents, 0),
major = BitConverter.ToUInt32(note.contents, 4),
minor = BitConverter.ToUInt32(note.contents, 8),
revision = BitConverter.ToUInt32(note.contents, 0)
};
}
static string DecodeGnuBuildId(ElfNote note)
{
StringBuilder sb = new StringBuilder();
foreach(byte b in note.contents) { sb.AppendFormat("{0:x2}", b); }
return sb.ToString();
}
}
}

View File

@@ -83,6 +83,17 @@ namespace libexeinfo
sb.AppendFormat("\t\t\tLength: {0} bytes", note.contents.Length).AppendLine();
}
if(notes.TryGetValue(".note.ABI-tag", out ElfNote abiTag))
{
GnuAbiTag gnuAbiTag = DecodeGnuAbiTag(abiTag, IsBigEndian);
if(gnuAbiTag != null)
sb.AppendFormat("\tGNU ABI tag: Requires {0} version {1}.{2}.{3}", gnuAbiTag.system,
gnuAbiTag.major, gnuAbiTag.minor, gnuAbiTag.revision).AppendLine();
}
if(notes.TryGetValue(".note.gnu.build-id", out ElfNote buildId))
sb.AppendFormat("\tGNU build ID: {0}", DecodeGnuBuildId(buildId));
return sb.ToString();
}
}

View File

@@ -376,5 +376,13 @@ namespace libexeinfo
public uint type;
public byte[] contents;
}
class GnuAbiTag
{
public GnuAbiSystem system;
public uint major;
public uint minor;
public uint revision;
}
}
}

View File

@@ -57,6 +57,7 @@
<Compile Include="ELF\Consts.cs" />
<Compile Include="ELF\ELF.cs" />
<Compile Include="ELF\Enums.cs" />
<Compile Include="ELF\GNU.cs" />
<Compile Include="ELF\Info.cs" />
<Compile Include="ELF\Structs.cs" />
<Compile Include="Enums.cs" />