33using System . IO ;
44using System . Text ;
55
6- using ELFSharp . ELF ;
7- using ELFSharp . ELF . Sections ;
8-
96using Xamarin . Android . Tools ;
107using 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 ) {
0 commit comments