Skip to content

Commit 59c52cb

Browse files
committed
Let's see what that'll do
1 parent 90835c8 commit 59c52cb

4 files changed

Lines changed: 129 additions & 71 deletions

File tree

‎src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/ArchiveAssemblyHelper.cs‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,44 @@ public ArchiveAssemblyHelper (string archivePath, bool useAssemblyStores = true,
6464
ret = ReadZipEntry (path, arch, uncompressIfNecessary);
6565
}
6666

67-
ret?.Seek (0, SeekOrigin.Begin);
68-
return ret;
67+
if (ret == null) {
68+
return null;
69+
}
70+
71+
ret.Seek (0, SeekOrigin.Begin);
72+
(ulong elfPayloadOffset, ulong elfPayloadSize, ELFPayloadError error) = Xamarin.Android.AssemblyStore.Utils.FindELFPayloadSectionOffsetAndSize (ret);
73+
74+
if (error != ELFPayloadError.None) {
75+
string message = error switch {
76+
ELFPayloadError.NotELF => $"Entry '{path}' is not a valid ELF binary",
77+
ELFPayloadError.LoadFailed => $"Entry '{path}' could not be loaded",
78+
ELFPayloadError.NotSharedLibrary => $"Entry '{path}' is not a shared ELF library",
79+
ELFPayloadError.NotLittleEndian => $"Entry '{path}' is not a little-endian ELF image",
80+
ELFPayloadError.NoPayloadSection => $"Entry '{path}' does not contain the 'payload' section",
81+
_ => $"Unknown ELF payload section error for entry '{path}': {error}"
82+
};
83+
Console.WriteLine (message);
84+
} else {
85+
Console.WriteLine ($"Extracted content from ELF image '{path}'");
86+
}
87+
88+
ret.Seek (0, SeekOrigin.Begin);
89+
if (elfPayloadOffset == 0) {
90+
return ret;
91+
}
92+
93+
// Make a copy of JUST the payload section, so that it contains only the data the tests expect and support
94+
var payload = new MemoryStream ();
95+
var data = buffers.Rent (16384);
96+
int nRead = 0;
97+
98+
while ((nRead = ret.Read (data, 0, data.Length)) > 0) {
99+
payload.Write (data, 0, nRead);
100+
}
101+
payload.Flush ();
102+
ret.Dispose ();
103+
104+
return payload;
69105
}
70106

71107
Stream? ReadZipEntry (string path, AndroidTargetArch arch, bool uncompressIfNecessary)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Xamarin.Android.AssemblyStore;
2+
3+
enum ELFPayloadError
4+
{
5+
None,
6+
NotELF,
7+
LoadFailed,
8+
NotSharedLibrary,
9+
NotLittleEndian,
10+
NoPayloadSection,
11+
}

‎tools/assembly-store-reader-mk2/AssemblyStore/StoreReader_V2.cs‎

Lines changed: 14 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
using System.IO;
44
using System.Text;
55

6-
using ELFSharp.ELF;
7-
using ELFSharp.ELF.Sections;
8-
96
using Xamarin.Android.Tools;
107
using Xamarin.Android.Tasks;
118

@@ -94,8 +91,20 @@ protected override bool IsSupported ()
9491

9592
uint magic = reader.ReadUInt32 ();
9693
if (magic == Utils.ELF_MAGIC) {
97-
elfOffset = FindELFPayloadSectionOffset ();
98-
if (elfOffset >= 0) {
94+
ELFPayloadError error;
95+
(elfOffset, _, error) = Utils.FindELFPayloadSectionOffsetAndSize (StoreStream);
96+
97+
if (error != ELFPayloadError.None) {
98+
string message = error switch {
99+
ELFPayloadError.NotELF => $"Store '{StorePath}' is not a valid ELF binary",
100+
ELFPayloadError.LoadFailed => $"Store '{StorePath}' could not be loaded",
101+
ELFPayloadError.NotSharedLibrary => $"Store '{StorePath}' is not a shared ELF library",
102+
ELFPayloadError.NotLittleEndian => $"Store '{StorePath}' is not a little-endian ELF image",
103+
ELFPayloadError.NoPayloadSection => $"Store '{StorePath}' does not contain the 'payload' section",
104+
_ => $"Unknown ELF payload section error for store '{StorePath}': {error}"
105+
};
106+
Log.Debug (message);
107+
} else if (elfOffset >= 0) {
99108
StoreStream.Seek ((long)elfOffset, SeekOrigin.Begin);
100109
magic = reader.ReadUInt32 ();
101110
}
@@ -120,70 +129,6 @@ protected override bool IsSupported ()
120129
return true;
121130
}
122131

123-
ulong FindELFPayloadSectionOffset ()
124-
{
125-
StoreStream.Seek (0, SeekOrigin.Begin);
126-
Class elfClass = ELFReader.CheckELFType (StoreStream);
127-
if (elfClass == Class.NotELF) {
128-
Log.Debug ($"Store '{StorePath}' is not a valid ELF binary");
129-
return 0;
130-
}
131-
132-
if (!ELFReader.TryLoad (StoreStream, shouldOwnStream: false, out IELF? elf)) {
133-
return LogErrorAndReturn ($"Store '{StorePath}' could not be loaded", elf);
134-
}
135-
136-
if (elf.Type != FileType.SharedObject) {
137-
return LogErrorAndReturn ($"Store '{StorePath}' is not a shared ELF library", elf);
138-
}
139-
140-
if (elf.Endianess != ELFSharp.Endianess.LittleEndian) {
141-
return LogErrorAndReturn ($"Store '{StorePath}' is not a little-endian ELF image", elf);
142-
}
143-
144-
if (!elf.TryGetSection ("payload", out ISection? payloadSection)) {
145-
return LogErrorAndReturn ($"Store '{StorePath}' does not contain the 'payload' section", elf);
146-
}
147-
148-
bool is64 = elf.Machine switch {
149-
Machine.ARM => false,
150-
Machine.Intel386 => false,
151-
152-
Machine.AArch64 => true,
153-
Machine.AMD64 => true,
154-
155-
_ => throw new NotSupportedException ($"Unsupported ELF architecture '{elf.Machine}'")
156-
};
157-
158-
ulong offset;
159-
if (is64) {
160-
offset = GetDataOffset64 ((Section<ulong>)payloadSection);
161-
} else {
162-
offset = GetDataOffset32 ((Section<uint>)payloadSection);
163-
}
164-
165-
elf.Dispose ();
166-
return offset;
167-
168-
ulong GetDataOffset64 (Section<ulong> payload)
169-
{
170-
return payload.Offset;
171-
}
172-
173-
ulong GetDataOffset32 (Section<uint> payload)
174-
{
175-
return (ulong)payload.Offset;
176-
}
177-
178-
ulong LogErrorAndReturn (string message, IELF? elf)
179-
{
180-
Log.Debug (message);
181-
elf?.Dispose ();
182-
183-
return 0;
184-
}
185-
}
186-
187132
protected override void Prepare ()
188133
{
189134
if (header == null) {

‎tools/assembly-store-reader-mk2/AssemblyStore/Utils.cs‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.IO;
33
using System.Buffers;
44

5+
using ELFSharp.ELF;
6+
using ELFSharp.ELF.Sections;
57
using Xamarin.Tools.Zip;
68

79
namespace Xamarin.Android.AssemblyStore;
@@ -27,6 +29,70 @@ static class Utils
2729

2830
public static readonly ArrayPool<byte> BytePool = ArrayPool<byte>.Shared;
2931

32+
public static (ulong offset, ulong size, ELFPayloadError error) FindELFPayloadSectionOffsetAndSize (Stream stream)
33+
{
34+
stream.Seek (0, SeekOrigin.Begin);
35+
Class elfClass = ELFReader.CheckELFType (stream);
36+
if (elfClass == Class.NotELF) {
37+
return ReturnError (null, ELFPayloadError.NotELF);
38+
}
39+
40+
if (!ELFReader.TryLoad (stream, shouldOwnStream: false, out IELF? elf)) {
41+
return ReturnError (elf, ELFPayloadError.LoadFailed);
42+
}
43+
44+
if (elf.Type != FileType.SharedObject) {
45+
return ReturnError (elf, ELFPayloadError.NotSharedLibrary);
46+
}
47+
48+
if (elf.Endianess != ELFSharp.Endianess.LittleEndian) {
49+
return ReturnError (elf, ELFPayloadError.NotLittleEndian);
50+
}
51+
52+
if (!elf.TryGetSection ("payload", out ISection? payloadSection)) {
53+
return ReturnError (elf, ELFPayloadError.NoPayloadSection);
54+
}
55+
56+
bool is64 = elf.Machine switch {
57+
Machine.ARM => false,
58+
Machine.Intel386 => false,
59+
60+
Machine.AArch64 => true,
61+
Machine.AMD64 => true,
62+
63+
_ => throw new NotSupportedException ($"Unsupported ELF architecture '{elf.Machine}'")
64+
};
65+
66+
ulong offset;
67+
ulong size;
68+
69+
if (is64) {
70+
(offset, size) = GetOffsetAndSize64 ((Section<ulong>)payloadSection);
71+
} else {
72+
(offset, size) = GetOffsetAndSize32 ((Section<uint>)payloadSection);
73+
}
74+
75+
elf.Dispose ();
76+
return (offset, size, ELFPayloadError.None);
77+
78+
(ulong offset, ulong size) GetOffsetAndSize64 (Section<ulong> payload)
79+
{
80+
return (payload.Offset, payload.Size);
81+
}
82+
83+
(ulong offset, ulong size) GetOffsetAndSize32 (Section<uint> payload)
84+
{
85+
return ((ulong)payload.Offset, (ulong)payload.Size);
86+
}
87+
88+
(ulong offset, ulong size, ELFPayloadError error) ReturnError (IELF? elf, ELFPayloadError error)
89+
{
90+
elf?.Dispose ();
91+
92+
return (0, 0, error);
93+
}
94+
}
95+
3096
public static (FileFormat format, FileInfo? info) DetectFileFormat (string path)
3197
{
3298
if (String.IsNullOrEmpty (path)) {

0 commit comments

Comments
 (0)