Bug 60531 - Could not AOT the assembly - ipad4.4-11.1 - MT3001
Summary: Could not AOT the assembly - ipad4.4-11.1 - MT3001
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 11.3 (xcode9.1)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2017-11-06 16:52 UTC by Lovisa
Modified: 2017-11-14 18:09 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Binding Library (7.37 MB, application/zip)
2017-11-08 15:34 UTC, Lovisa

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Lovisa 2017-11-06 16:52:56 UTC
I've got two binding libraries, in two separated projects, linked to an iOS project and I get this error
"MTOUCH : error MT5216: Native linking failed for '/iOStst/iOStst/obj/iPhone/Debug/device-builds/ipad4.4-11.1/mtouch-cache/arm64/libPayExBinding.dll.dylib'. Please file a bug report at http://bugzilla.xamarin.com

The project is located at https://github.com/lovisaju/iOSLibrary

The error only occurs when debugging on iPad, not in the simulator and I've tested every settings I can think of at this moment.

Few preferences:

Xcode 9.1

Visual Studio Community 2017 for Mac
Version 7.2.2 (build 7)

	Mono (2017-06/1f4613aa1ac) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.5
SDK Build Tools Version: 25.0.3

Version: (Visual Studio Community)

Operating System
Mac OS X 10.13.0
Comment 1 Alex Soto [MSFT] 2017-11-07 16:29:11 UTC
Hello, I have tried to run your project but I get some build errors

> UI/FrontPage/FrontPageController.xib : error : The file 'UI/FrontPage/FrontPageController.xib' does not exist.
> UI/Base/ProgressView/ProgressView.xib : error : The file 'UI/Base/ProgressView/ProgressView.xib' does not exist.
> UI/Base/Button/ctr.xib : error : The file 'UI/Base/Button/ctr.xib' does not exist.

Can you review your sample and let us know when it is fixed to show your actual issue? Thanks!
Comment 2 Lovisa 2017-11-07 16:57:50 UTC
Thanks for the info.. I've fixed it

It seems the references didn't agree with my changes earlier :)
Comment 3 Alex Soto [MSFT] 2017-11-07 21:59:45 UTC
The PayExBinding is missing some frameworks (Security CoreGraphcs) that are referenced by iSMP.a, so if you have the binding project source of PayExBinding.dll change the LinkWith attribute from this:

[LinkWith ("iSMP.a", Frameworks = "ExternalAccessory SystemConfiguration", LinkerFlags = "-ObjC", SmartLink = true, ForceLoad = true)]

to this:

[LinkWith ("iSMP.a", Frameworks = "ExternalAccessory SystemConfiguration Security CoreGraphcs", LinkerFlags = "-ObjC", SmartLink = true, ForceLoad = true)]

Please let me know if this fixes your issue, I am not able to test because the PayExBinding binding is not available in your repository.
Comment 4 Alex Soto [MSFT] 2017-11-07 22:00:56 UTC
Whoops a typo, this is the correct one

> [LinkWith ("iSMP.a", Frameworks = "ExternalAccessory SystemConfiguration Security CoreGraphics", LinkerFlags = "-ObjC", SmartLink = true, ForceLoad = true)]

let me know if this helps.
Comment 5 Lovisa 2017-11-08 09:14:15 UTC
Hi Alex,

Thank you for the info.  It did however not resolve the issue.

Do you want me to change the project files in the repository accordingly?
Comment 6 Lovisa 2017-11-08 11:36:11 UTC
Updated the project in the GitHub repository

Altered the settings for the libraries

The settings for the library in Plugin2 is

[assembly: LinkWith ("libValitorBundle.a", 
                     Frameworks = "ExternalAccessory SystemConfiguration Security CoreGraphics",
                     LinkerFlags = "-ObjC",
                     IsCxx = true,
                     SmartLink = true, 
                     ForceLoad = true)]

in the settings for library in Plugin1 it's

assembly: LinkWith("iSMP.a",
                    Frameworks = "ExternalAccessory SystemConfiguration Security CoreGraphics",
                    LinkerFlags = "-ObjC",
                    IsCxx = true,
                    SmartLink = true,
                    ForceLoad = true)]
Comment 7 Alex Soto [MSFT] 2017-11-08 15:13:28 UTC
Thanks, making progress here

> Structure field of type SByte[] can't be marshalled as LPArray

Any chance you could put the binding projects somewhere so I can have a look at them? especially the ValitorBinding which is the one causing the problem.
Comment 8 Lovisa 2017-11-08 15:34:09 UTC
Created attachment 25606 [details]
Binding Library

Added binding library in a zip file, this is the one giving us the headache.
Comment 9 Lovisa 2017-11-14 16:32:12 UTC
Some additional information, don't know if it affects the issue

Configuration: Debug
Platform: iPhone (options: iPhoneSimulator/iPhone)

iOS Bild Config
* SDK version: Default
* Linker behavior: Link Framework SDKs Only
* Supported architectures: ARMv7 + ARM64

Checked with:
 * Enable incremental builds
 * Enable device-specific builds
Comment 10 Alex Soto [MSFT] 2017-11-14 16:42:25 UTC
Ahh sorry, missed your last reply, testing ASAP
Comment 11 Alex Soto [MSFT] 2017-11-14 17:45:22 UTC

Like I suspected, this is not an issue with Xamarin.iOS but with the binding code of ValitorBinding, it looks to be just ObjectiveSharpie output which needs a little of auditing before it can result in something that it is working. ObjectiveSharpie is a scaffolding tool and the output must be audited before use. In most cases, the output needs to be modified to produce working binaries. 

That being said the issue is happening because there is no way for the AOT compiler to know how it should marshal `sbyte[]` inside structs like the following one taken as an example:

public struct ICDeviceInformation
    public nint serialNumber;
    public nint reference;
    public sbyte[] protocol;

When interoping with native libraries especially with c/c++ arrays you need to know beforehand the size of the array so you can actually read its contents, in the above case, there is no way of knowing the array size for `protocol` and the aot compiler cannot produce the needed code to marshal it into a managed array and this is the reason you get the following error:

> Structure field of type SByte[] can't be marshalled as LPArray

Most of the time when you are marshalling fixed arrays you can provide this information using the `MarshalAsAttribute` but when the size isn't fixed you must manually marshal this field into a managed array in order to know if you dealing with a fixed array or not you would need to refer to the library docs. I would suggest reading the Interop with Native Libraries[1] documentation.

[1]: http://www.mono-project.com/docs/advanced/pinvoke/
Comment 12 Lovisa 2017-11-14 18:09:57 UTC
Hi Alex

Thank you so much for your help and the time you gave checking the issue.

Kind regards