Skip to content

Commit f516c4d

Browse files
authored
Merge pull request #32 from filipnavara/unit_tests
Update unit tests and fix bugs to make them pass again
2 parents 4338772 + a6384ed commit f516c4d

7 files changed

Lines changed: 51 additions & 37 deletions

File tree

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010

1111
jobs:
1212
build:
13-
runs-on: ubuntu-20.04
13+
runs-on: ubuntu-22.04
1414

1515
steps:
1616
- name: Checkout

src/LibObjectFile.Tests/Dwarf/DwarfTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public void TestDebugLineHelloWorld()
8080
{
8181
var cppName = "helloworld";
8282
var cppExe = $"{cppName}_debug";
83-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -o {cppExe}");
83+
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -o {cppExe}");
8484

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

144144
ElfObjectFile elf;
145145
using (var inStream = File.OpenRead(libShared))
@@ -198,7 +198,7 @@ public void TestDebugLineSmall()
198198
{
199199
var cppName = "small";
200200
var cppObj = $"{cppName}_debug.o";
201-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -c -o {cppObj}");
201+
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
202202
ElfObjectFile elf;
203203
using (var inStream = File.OpenRead(cppObj))
204204
{
@@ -256,7 +256,7 @@ public void TestDebugLineMultipleFunctions()
256256
{
257257
var cppName = "multiple_functions";
258258
var cppObj = $"{cppName}_debug.o";
259-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -c -o {cppObj}");
259+
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
260260

261261
ElfObjectFile elf;
262262
using (var inStream = File.OpenRead(cppObj))
@@ -314,7 +314,7 @@ public void TestDebugInfoSmall()
314314
{
315315
var cppName = "small";
316316
var cppObj = $"{cppName}_debug.o";
317-
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -g -c -o {cppObj}");
317+
LinuxUtil.RunLinuxExe("gcc", $"{cppName}.cpp -gdwarf-4 -c -o {cppObj}");
318318

319319
ElfObjectFile elf;
320320
using (var inStream = File.OpenRead(cppObj))

src/LibObjectFile.Tests/LinuxUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static string ReadElf(string file, string arguments = "-W -a")
1717
return RunLinuxExe("readelf", $"{file} {arguments}");
1818
}
1919

20-
public static string RunLinuxExe(string exe, string arguments, string distribution = "Ubuntu-20.04")
20+
public static string RunLinuxExe(string exe, string arguments, string distribution = "Ubuntu")
2121
{
2222
if (exe == null) throw new ArgumentNullException(nameof(exe));
2323
if (arguments == null) throw new ArgumentNullException(nameof(arguments));

src/LibObjectFile/Elf/ElfObjectFile.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,23 @@ public unsafe void UpdateLayout(DiagnosticBag diagnostics)
253253
shstrTable.Reset();
254254

255255
// Prepare all section names (to calculate the name indices and the size of the SectionNames)
256-
for (var j = 0; j < sections.Count; j++)
256+
// Do it in two passes to generate optimal string table
257+
for (var pass = 0; pass < 2; pass++)
257258
{
258-
var otherSection = sections[j];
259-
if ((j == 0 && otherSection.Type == ElfSectionType.Null)) continue;
260-
if (otherSection.IsShadow) continue;
261-
otherSection.Name = otherSection.Name.WithIndex(shstrTable.GetOrCreateIndex(otherSection.Name));
259+
for (var j = 0; j < sections.Count; j++)
260+
{
261+
var otherSection = sections[j];
262+
if ((j == 0 && otherSection.Type == ElfSectionType.Null)) continue;
263+
if (otherSection.IsShadow) continue;
264+
if (pass == 0)
265+
{
266+
shstrTable.ReserveString(otherSection.Name);
267+
}
268+
else
269+
{
270+
otherSection.Name = otherSection.Name.WithIndex(shstrTable.GetOrCreateIndex(otherSection.Name));
271+
}
272+
}
262273
}
263274
}
264275

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

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

281-
Layout.TotalSize = offset + (ulong)VisibleSectionCount * Layout.SizeOfSectionHeaderEntry;
292+
Layout.TotalSize = Layout.OffsetOfSectionHeaderTable + (ulong)VisibleSectionCount * Layout.SizeOfSectionHeaderEntry;
282293
}
283294

284295
// Update program headers with offsets from auto layout

src/LibObjectFile/Elf/ElfPrinter.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ public static void PrintElfHeader(ElfObjectFile elf, TextWriter writer)
6868
writer.WriteLine($" Size of program headers: {elf.Layout.SizeOfProgramHeaderEntry} (bytes)");
6969
writer.WriteLine($" Number of program headers: {elf.Segments.Count}");
7070
writer.WriteLine($" Size of section headers: {elf.Layout.SizeOfSectionHeaderEntry} (bytes)");
71-
writer.WriteLine($" Number of section headers: {elf.VisibleSectionCount}");
71+
if (elf.VisibleSectionCount >= ElfNative.SHN_LORESERVE || elf.VisibleSectionCount == 0)
72+
{
73+
writer.WriteLine($" Number of section headers: 0 ({elf.VisibleSectionCount})");
74+
}
75+
else
76+
{
77+
writer.WriteLine($" Number of section headers: {elf.VisibleSectionCount}");
78+
}
7279
writer.WriteLine($" Section header string table index: {elf.SectionHeaderStringTable?.SectionIndex ?? 0}");
7380
}
7481

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

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

108-
if (elf.Sections.Count == 0)
109-
{
110-
writer.WriteLine();
111-
writer.WriteLine("There are no sections to group in this file.");
112-
return;
113-
}
114-
115115
writer.WriteLine();
116116
writer.WriteLine("There are no section groups in this file.");
117117
// TODO
@@ -252,8 +252,15 @@ public static void PrintUnwind(ElfObjectFile elf, TextWriter writer)
252252
if (elf == null) throw new ArgumentNullException(nameof(elf));
253253
if (writer == null) throw new ArgumentNullException(nameof(writer));
254254

255-
writer.WriteLine();
256-
writer.WriteLine($"The decoding of unwind sections for machine type {GetElfArch(elf.Arch)} is not currently supported.");
255+
if (elf.Arch == ElfArchEx.I386 || elf.Arch == ElfArchEx.X86_64)
256+
{
257+
writer.WriteLine("No processor specific unwind information to decode");
258+
}
259+
else
260+
{
261+
writer.WriteLine();
262+
writer.WriteLine($"The decoding of unwind sections for machine type {GetElfArch(elf.Arch)} is not currently supported.");
263+
}
257264
}
258265

259266

src/LibObjectFile/Elf/ElfSegmentRange.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,7 @@ public ulong Size
9999
return 0;
100100
}
101101

102-
var parent = BeginSection.Parent;
103-
ulong size = 0;
104-
for (uint i = BeginSection.Index; i < EndSection.Index; i++)
105-
{
106-
var section = parent.Sections[(int)i];
107-
if (section.HasContent)
108-
{
109-
size += section.Size;
110-
}
111-
}
112-
102+
ulong size = EndSection.Offset - BeginSection.Offset;
113103
size -= BeginOffset;
114104
size += EndOffset < 0 ? (ulong)((long)EndSection.Size + EndOffset + 1) : (ulong)(EndOffset + 1);
115105
return size;

src/LibObjectFile/Elf/ElfWriter{TEncoder}.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,16 @@ private void WriteSections()
227227
private void WriteSectionHeaderTable()
228228
{
229229
var offset = (ulong)Stream.Position - _startOfFile;
230-
if (offset != Layout.OffsetOfSectionHeaderTable)
230+
var diff = Layout.OffsetOfSectionHeaderTable - offset;
231+
if (diff < 0 || diff > 8)
231232
{
232233
throw new InvalidOperationException("Internal error. Unexpected offset for SectionHeaderTable");
233234
}
235+
else if (diff != 0)
236+
{
237+
// Alignment
238+
Stream.Write(stackalloc byte[(int)diff]);
239+
}
234240

235241
// Then write all regular sections
236242
var sections = ObjectFile.Sections;

0 commit comments

Comments
 (0)