Skip to content

Commit

Permalink
Merge pull request #32 from filipnavara/unit_tests
Browse files Browse the repository at this point in the history
Update unit tests and fix bugs to make them pass again
  • Loading branch information
xoofx authored Oct 7, 2023
2 parents 4338772 + a6384ed commit f516c4d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

jobs:
build:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

steps:
- name: Checkout
Expand Down
10 changes: 5 additions & 5 deletions src/LibObjectFile.Tests/Dwarf/DwarfTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void TestDebugLineHelloWorld()
{
var cppName = "helloworld";
var cppExe = $"{cppName}_debug";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -o {cppExe}");
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -o {cppExe}");

ElfObjectFile elf;
using (var inStream = File.OpenRead(cppExe))
Expand Down Expand Up @@ -139,7 +139,7 @@ public void TestDebugLineLibMultipleObjs()
{
var cppName = "lib";
var libShared = $"{cppName}_debug.so";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}_a.cpp {cppName}_b.cpp -g -shared -o {libShared}");
LinuxUtil.RunLinuxExe("gcc", $"{cppName}_a.cpp {cppName}_b.cpp -gdwarf-4 -shared -o {libShared}");

ElfObjectFile elf;
using (var inStream = File.OpenRead(libShared))
Expand Down Expand Up @@ -198,7 +198,7 @@ public void TestDebugLineSmall()
{
var cppName = "small";
var cppObj = $"{cppName}_debug.o";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -c -o {cppObj}");
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
ElfObjectFile elf;
using (var inStream = File.OpenRead(cppObj))
{
Expand Down Expand Up @@ -256,7 +256,7 @@ public void TestDebugLineMultipleFunctions()
{
var cppName = "multiple_functions";
var cppObj = $"{cppName}_debug.o";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -c -o {cppObj}");
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");

ElfObjectFile elf;
using (var inStream = File.OpenRead(cppObj))
Expand Down Expand Up @@ -314,7 +314,7 @@ public void TestDebugInfoSmall()
{
var cppName = "small";
var cppObj = $"{cppName}_debug.o";
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -c -o {cppObj}");
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");

ElfObjectFile elf;
using (var inStream = File.OpenRead(cppObj))
Expand Down
2 changes: 1 addition & 1 deletion src/LibObjectFile.Tests/LinuxUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static string ReadElf(string file, string arguments = "-W -a")
return RunLinuxExe("readelf", $"{file} {arguments}");
}

public static string RunLinuxExe(string exe, string arguments, string distribution = "Ubuntu-20.04")
public static string RunLinuxExe(string exe, string arguments, string distribution = "Ubuntu")
{
if (exe == null) throw new ArgumentNullException(nameof(exe));
if (arguments == null) throw new ArgumentNullException(nameof(arguments));
Expand Down
25 changes: 18 additions & 7 deletions src/LibObjectFile/Elf/ElfObjectFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,23 @@ public unsafe void UpdateLayout(DiagnosticBag diagnostics)
shstrTable.Reset();

// Prepare all section names (to calculate the name indices and the size of the SectionNames)
for (var j = 0; j < sections.Count; j++)
// Do it in two passes to generate optimal string table
for (var pass = 0; pass < 2; pass++)
{
var otherSection = sections[j];
if ((j == 0 && otherSection.Type == ElfSectionType.Null)) continue;
if (otherSection.IsShadow) continue;
otherSection.Name = otherSection.Name.WithIndex(shstrTable.GetOrCreateIndex(otherSection.Name));
for (var j = 0; j < sections.Count; j++)
{
var otherSection = sections[j];
if ((j == 0 && otherSection.Type == ElfSectionType.Null)) continue;
if (otherSection.IsShadow) continue;
if (pass == 0)
{
shstrTable.ReserveString(otherSection.Name);
}
else
{
otherSection.Name = otherSection.Name.WithIndex(shstrTable.GetOrCreateIndex(otherSection.Name));
}
}
}
}

Expand All @@ -276,9 +287,9 @@ public unsafe void UpdateLayout(DiagnosticBag diagnostics)
}

// The Section Header Table will be put just after all the sections
Layout.OffsetOfSectionHeaderTable = offset;
Layout.OffsetOfSectionHeaderTable = AlignHelper.AlignToUpper(offset, FileClass == ElfFileClass.Is32 ? 4u : 8u);

Layout.TotalSize = offset + (ulong)VisibleSectionCount * Layout.SizeOfSectionHeaderEntry;
Layout.TotalSize = Layout.OffsetOfSectionHeaderTable + (ulong)VisibleSectionCount * Layout.SizeOfSectionHeaderEntry;
}

// Update program headers with offsets from auto layout
Expand Down
29 changes: 18 additions & 11 deletions src/LibObjectFile/Elf/ElfPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ public static void PrintElfHeader(ElfObjectFile elf, TextWriter writer)
writer.WriteLine($" Size of program headers: {elf.Layout.SizeOfProgramHeaderEntry} (bytes)");
writer.WriteLine($" Number of program headers: {elf.Segments.Count}");
writer.WriteLine($" Size of section headers: {elf.Layout.SizeOfSectionHeaderEntry} (bytes)");
writer.WriteLine($" Number of section headers: {elf.VisibleSectionCount}");
if (elf.VisibleSectionCount >= ElfNative.SHN_LORESERVE || elf.VisibleSectionCount == 0)
{
writer.WriteLine($" Number of section headers: 0 ({elf.VisibleSectionCount})");
}
else
{
writer.WriteLine($" Number of section headers: {elf.VisibleSectionCount}");
}
writer.WriteLine($" Section header string table index: {elf.SectionHeaderStringTable?.SectionIndex ?? 0}");
}

Expand Down Expand Up @@ -97,21 +104,14 @@ public static void PrintSectionHeaders(ElfObjectFile elf, TextWriter writer)
W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
L (link order), O (extra OS processing required), G (group), T (TLS),
C (compressed), x (unknown), o (OS specific), E (exclude),
l (large), p (processor specific)");
D (mbind), l (large), p (processor specific)");
}

public static void PrintSectionGroups(ElfObjectFile elf, TextWriter writer)
{
if (elf == null) throw new ArgumentNullException(nameof(elf));
if (writer == null) throw new ArgumentNullException(nameof(writer));

if (elf.Sections.Count == 0)
{
writer.WriteLine();
writer.WriteLine("There are no sections to group in this file.");
return;
}

writer.WriteLine();
writer.WriteLine("There are no section groups in this file.");
// TODO
Expand Down Expand Up @@ -252,8 +252,15 @@ public static void PrintUnwind(ElfObjectFile elf, TextWriter writer)
if (elf == null) throw new ArgumentNullException(nameof(elf));
if (writer == null) throw new ArgumentNullException(nameof(writer));

writer.WriteLine();
writer.WriteLine($"The decoding of unwind sections for machine type {GetElfArch(elf.Arch)} is not currently supported.");
if (elf.Arch == ElfArchEx.I386 || elf.Arch == ElfArchEx.X86_64)
{
writer.WriteLine("No processor specific unwind information to decode");
}
else
{
writer.WriteLine();
writer.WriteLine($"The decoding of unwind sections for machine type {GetElfArch(elf.Arch)} is not currently supported.");
}
}


Expand Down
12 changes: 1 addition & 11 deletions src/LibObjectFile/Elf/ElfSegmentRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,7 @@ public ulong Size
return 0;
}

var parent = BeginSection.Parent;
ulong size = 0;
for (uint i = BeginSection.Index; i < EndSection.Index; i++)
{
var section = parent.Sections[(int)i];
if (section.HasContent)
{
size += section.Size;
}
}

ulong size = EndSection.Offset - BeginSection.Offset;
size -= BeginOffset;
size += EndOffset < 0 ? (ulong)((long)EndSection.Size + EndOffset + 1) : (ulong)(EndOffset + 1);
return size;
Expand Down
8 changes: 7 additions & 1 deletion src/LibObjectFile/Elf/ElfWriter{TEncoder}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,16 @@ private void WriteSections()
private void WriteSectionHeaderTable()
{
var offset = (ulong)Stream.Position - _startOfFile;
if (offset != Layout.OffsetOfSectionHeaderTable)
var diff = Layout.OffsetOfSectionHeaderTable - offset;
if (diff < 0 || diff > 8)
{
throw new InvalidOperationException("Internal error. Unexpected offset for SectionHeaderTable");
}
else if (diff != 0)
{
// Alignment
Stream.Write(stackalloc byte[(int)diff]);
}

// Then write all regular sections
var sections = ObjectFile.Sections;
Expand Down

0 comments on commit f516c4d

Please sign in to comment.