@@ -21,34 +21,65 @@ public class ElfSimpleTests : ElfTestBase
21
21
[ DynamicData ( nameof ( GetLinuxBins ) , DynamicDataSourceType . Method ) ]
22
22
public void TestLinuxFile ( string file )
23
23
{
24
- if ( ! OperatingSystem . IsLinux ( ) )
24
+ if ( string . IsNullOrEmpty ( file ) )
25
25
{
26
- Assert . Inconclusive ( "This test can only run on Linux" ) ;
26
+ Assert . Inconclusive ( "This test can only run on Linux or on Windows with WSL " ) ;
27
27
return ;
28
28
}
29
29
30
30
using var stream = File . OpenRead ( file ) ;
31
31
if ( ! ElfFile . IsElf ( stream ) ) return ;
32
+
32
33
var elf = ElfFile . Read ( stream ) ;
34
+ stream . Position = 0 ;
35
+ var elfOriginal = ElfFile . Read ( stream ) ; // elfOriginal is not written back, so it's layout is not updated
33
36
34
- // TODO: check for errors
37
+ var originalBuffer = File . ReadAllBytes ( file ) ;
35
38
36
- //var writer = new StringWriter();
37
- //writer.WriteLine("---------------------------------------------------------------------------------------");
38
- //writer.WriteLine($"{file}");
39
- //elf.Print(writer);
40
- //writer.WriteLine();
39
+ var copyStream = new MemoryStream ( ) ;
40
+ elf . Write ( copyStream ) ;
41
+
42
+ for ( var i = 0 ; i < elfOriginal . Content . Count ; i ++ )
43
+ {
44
+ var content = elf . Content [ i ] ;
45
+ var contentOriginal = elfOriginal . Content [ i ] ;
46
+ Assert . AreEqual ( contentOriginal . Position , content . Position , $ "Invalid position for content { content } ") ;
47
+ Assert . AreEqual ( contentOriginal . Size , content . Size , $ "Invalid size for content { content } ") ;
48
+ }
49
+
50
+ for ( var i = 0 ; i < elfOriginal . Segments . Count ; i ++ )
51
+ {
52
+ var segment = elf . Segments [ i ] ;
53
+ var segmentOriginal = elfOriginal . Segments [ i ] ;
54
+ Assert . AreEqual ( segmentOriginal . Position , segment . Position , $ "Invalid position for segment { segment } ") ;
55
+ Assert . AreEqual ( segmentOriginal . Size , segment . Size , $ "Invalid size for segment { segment } ") ;
56
+ }
57
+
58
+ ByteArrayAssert . AreEqual ( originalBuffer , copyStream . ToArray ( ) ) ;
41
59
}
42
60
43
61
public static IEnumerable < object [ ] > GetLinuxBins ( )
44
62
{
63
+ var wslDirectory = @"\\wsl$\Ubuntu\usr\bin" ;
45
64
if ( OperatingSystem . IsLinux ( ) )
46
65
{
47
66
foreach ( var file in Directory . EnumerateFiles ( @"/usr/bin" ) )
48
67
{
49
68
yield return new object [ ] { file } ;
50
69
}
51
70
}
71
+ else if ( OperatingSystem . IsWindows ( ) && Directory . Exists ( wslDirectory ) )
72
+ {
73
+ foreach ( var file in Directory . EnumerateFiles ( wslDirectory ) )
74
+ {
75
+ var fileInfo = new FileInfo ( file ) ;
76
+ // Skip symbolic links as loading them will fail
77
+ if ( ( fileInfo . Attributes & FileAttributes . ReparsePoint ) == 0 )
78
+ {
79
+ yield return new object [ ] { file } ;
80
+ }
81
+ }
82
+ }
52
83
else
53
84
{
54
85
yield return new object [ ] { string . Empty } ;
@@ -96,7 +127,7 @@ public void TryReadFailed()
96
127
public void SimpleEmptyWithDefaultSections ( )
97
128
{
98
129
var elf = new ElfFile ( ElfArch . X86_64 ) ;
99
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
130
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
100
131
AssertReadElf ( elf , "empty_default.elf" ) ;
101
132
}
102
133
@@ -121,9 +152,9 @@ public void SimpleCodeSection()
121
152
codeStream . Position = 0 ;
122
153
123
154
var codeSection = new ElfStreamSection ( ElfSectionSpecialType . Text , codeStream ) ;
124
- elf . Content . Add ( codeSection ) ;
125
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
126
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
155
+ elf . Add ( codeSection ) ;
156
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
157
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
127
158
128
159
AssertReadElf ( elf , "test.elf" ) ;
129
160
}
@@ -136,16 +167,15 @@ public void TestBss()
136
167
var stream = new MemoryStream ( ) ;
137
168
stream . Write ( new byte [ ] { 1 , 2 , 3 , 4 } ) ;
138
169
stream . Position = 0 ;
139
- var codeSection = new ElfStreamSection ( ElfSectionSpecialType . Text , stream ) ;
140
- elf . Content . Add ( codeSection ) ;
141
- var bssSection = new ElfStreamSection ( ElfSectionSpecialType . Bss )
170
+
171
+ var codeSection = elf . Add ( new ElfStreamSection ( ElfSectionSpecialType . Text , stream ) ) ;
172
+ var bssSection = elf . Add ( new ElfStreamSection ( ElfSectionSpecialType . Bss )
142
173
{
143
- Alignment = 1024
144
- } ;
145
- elf . Content . Add ( bssSection ) ;
174
+ FileAlignment = 1024
175
+ } ) ;
146
176
147
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
148
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
177
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
178
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
149
179
150
180
var diagnostics = new DiagnosticBag ( ) ;
151
181
elf . UpdateLayout ( diagnostics ) ;
@@ -166,10 +196,10 @@ public void SimpleCodeSectionAndSymbolSection()
166
196
codeStream . Position = 0 ;
167
197
168
198
var codeSection = new ElfStreamSection ( ElfSectionSpecialType . Text , codeStream ) ;
169
- elf . Content . Add ( codeSection ) ;
199
+ elf . Add ( codeSection ) ;
170
200
171
201
var stringSection = new ElfStringTable ( ) ;
172
- elf . Content . Add ( stringSection ) ;
202
+ elf . Add ( stringSection ) ;
173
203
174
204
var symbolSection = new ElfSymbolTable ( )
175
205
{
@@ -198,9 +228,9 @@ public void SimpleCodeSectionAndSymbolSection()
198
228
}
199
229
}
200
230
} ;
201
- elf . Content . Add ( symbolSection ) ;
202
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
203
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
231
+ elf . Add ( symbolSection ) ;
232
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
233
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
204
234
205
235
AssertReadElf ( elf , "test2.elf" ) ;
206
236
}
@@ -216,22 +246,22 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSection()
216
246
var codeSection = new ElfStreamSection ( ElfSectionSpecialType . Text , codeStream )
217
247
{
218
248
VirtualAddress = 0x1000 ,
219
- Alignment = 4096
249
+ VirtualAddressAlignment = 4096
220
250
} ;
221
- elf . Content . Add ( codeSection ) ;
251
+ elf . Add ( codeSection ) ;
222
252
223
253
var dataStream = new MemoryStream ( ) ;
224
254
dataStream . Write ( new byte [ 1024 ] ) ;
225
255
226
256
var dataSection = new ElfStreamSection ( ElfSectionSpecialType . ReadOnlyData , dataStream )
227
257
{
228
258
VirtualAddress = 0x2000 ,
229
- Alignment = 4096
259
+ VirtualAddressAlignment = 4096
230
260
} ;
231
- elf . Content . Add ( dataSection ) ;
261
+ elf . Add ( dataSection ) ;
232
262
233
263
var stringSection = new ElfStringTable ( ) ;
234
- elf . Content . Add ( stringSection ) ;
264
+ elf . Add ( stringSection ) ;
235
265
236
266
var symbolSection = new ElfSymbolTable ( )
237
267
{
@@ -260,10 +290,10 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSection()
260
290
}
261
291
}
262
292
} ;
263
- elf . Content . Add ( symbolSection ) ;
293
+ elf . Add ( symbolSection ) ;
264
294
265
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
266
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
295
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
296
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
267
297
268
298
elf . Segments . Add ( new ElfSegment ( )
269
299
{
@@ -274,7 +304,7 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSection()
274
304
Flags = ElfSegmentFlagsCore . Readable | ElfSegmentFlagsCore . Executable ,
275
305
Size = 4096 ,
276
306
SizeInMemory = 4096 ,
277
- Alignment = 4096 ,
307
+ VirtualAddressAlignment = 4096 ,
278
308
} ) ;
279
309
280
310
elf . Segments . Add ( new ElfSegment ( )
@@ -286,7 +316,7 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSection()
286
316
Flags = ElfSegmentFlagsCore . Readable | ElfSegmentFlagsCore . Writable ,
287
317
Size = 1024 ,
288
318
SizeInMemory = 1024 ,
289
- Alignment = 4096 ,
319
+ VirtualAddressAlignment = 4096 ,
290
320
} ) ;
291
321
292
322
AssertReadElf ( elf , "test3.elf" ) ;
@@ -304,22 +334,22 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSectionAndRelocation()
304
334
var codeSection = new ElfStreamSection ( ElfSectionSpecialType . Text , codeStream )
305
335
{
306
336
VirtualAddress = 0x1000 ,
307
- Alignment = 4096
337
+ VirtualAddressAlignment = 4096
308
338
} ;
309
- elf . Content . Add ( codeSection ) ;
339
+ elf . Add ( codeSection ) ;
310
340
311
341
var dataStream = new MemoryStream ( ) ;
312
342
dataStream . Write ( new byte [ 1024 ] ) ;
313
343
314
344
var dataSection = new ElfStreamSection ( ElfSectionSpecialType . ReadOnlyData , dataStream )
315
345
{
316
346
VirtualAddress = 0x2000 ,
317
- Alignment = 4096
347
+ VirtualAddressAlignment = 4096
318
348
} ;
319
- elf . Content . Add ( dataSection ) ;
349
+ elf . Add ( dataSection ) ;
320
350
321
351
var stringSection = new ElfStringTable ( ) ;
322
- elf . Content . Add ( stringSection ) ;
352
+ elf . Add ( stringSection ) ;
323
353
324
354
var symbolSection = new ElfSymbolTable ( )
325
355
{
@@ -348,7 +378,7 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSectionAndRelocation()
348
378
}
349
379
}
350
380
} ;
351
- elf . Content . Add ( symbolSection ) ;
381
+ elf . Add ( symbolSection ) ;
352
382
353
383
elf . Segments . Add (
354
384
new ElfSegment ( )
@@ -360,7 +390,7 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSectionAndRelocation()
360
390
Flags = ElfSegmentFlagsCore . Readable | ElfSegmentFlagsCore . Executable ,
361
391
Size = 4096 ,
362
392
SizeInMemory = 4096 ,
363
- Alignment = 4096 ,
393
+ VirtualAddressAlignment = 4096 ,
364
394
}
365
395
) ;
366
396
@@ -374,7 +404,7 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSectionAndRelocation()
374
404
Flags = ElfSegmentFlagsCore . Readable | ElfSegmentFlagsCore . Writable ,
375
405
Size = 1024 ,
376
406
SizeInMemory = 1024 ,
377
- Alignment = 4096 ,
407
+ VirtualAddressAlignment = 4096 ,
378
408
}
379
409
) ;
380
410
@@ -399,10 +429,10 @@ public void SimpleProgramHeaderAndCodeSectionAndSymbolSectionAndRelocation()
399
429
}
400
430
}
401
431
} ;
402
- elf . Content . Add ( relocTable ) ;
432
+ elf . Add ( relocTable ) ;
403
433
404
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
405
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
434
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
435
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
406
436
407
437
AssertReadElf ( elf , "test4.elf" ) ;
408
438
}
@@ -454,12 +484,12 @@ public void TestAlignedSection()
454
484
455
485
var codeSection = new ElfStreamSection ( ElfSectionSpecialType . Text , codeStream )
456
486
{
457
- Alignment = 0x1000 ,
487
+ FileAlignment = 0x1000 ,
458
488
} ;
459
- elf . Content . Add ( codeSection ) ;
489
+ elf . Add ( codeSection ) ;
460
490
461
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
462
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
491
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
492
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
463
493
464
494
var diagnostics = elf . Verify ( ) ;
465
495
Assert . IsFalse ( diagnostics . HasErrors ) ;
@@ -482,20 +512,20 @@ public void TestManySections()
482
512
for ( int i = 0 ; i < ushort . MaxValue ; i ++ )
483
513
{
484
514
var section = new ElfStreamSection ( ElfSectionSpecialType . Data ) { Name = $ ".section{ i } " } ;
485
- elf . Content . Add ( section ) ;
515
+ elf . Add ( section ) ;
486
516
symbolTable . Entries . Add ( new ElfSymbol { Type = ElfSymbolType . Section , SectionLink = section } ) ;
487
517
}
488
518
489
- elf . Content . Add ( stringTable ) ;
490
- elf . Content . Add ( symbolTable ) ;
491
- elf . Content . Add ( new ElfSectionHeaderStringTable ( ) ) ;
492
- elf . Content . Add ( new ElfSectionHeaderTable ( ) ) ;
519
+ elf . Add ( stringTable ) ;
520
+ elf . Add ( symbolTable ) ;
521
+ elf . Add ( new ElfSectionHeaderStringTable ( ) ) ;
522
+ elf . Add ( new ElfSectionHeaderTable ( ) ) ;
493
523
494
524
var diagnostics = elf . Verify ( ) ;
495
525
Assert . IsTrue ( diagnostics . HasErrors ) ;
496
526
Assert . AreEqual ( DiagnosticId . ELF_ERR_MissingSectionHeaderIndices , diagnostics . Messages [ 0 ] . Id ) ;
497
527
498
- elf . Content . Add ( new ElfSymbolTableSectionHeaderIndices { Link = symbolTable } ) ;
528
+ elf . Add ( new ElfSymbolTableSectionHeaderIndices { Link = symbolTable } ) ;
499
529
diagnostics = elf . Verify ( ) ;
500
530
Assert . IsFalse ( diagnostics . HasErrors ) ;
501
531
0 commit comments