How to diagnose failures in dlopen () on iOS devices

I have a C library that is used in an iOS application. This library uses dlopen() to access extension functions. The code works fine on the iOS simulator (both in 32-bit and 64-bit modes); however, when I run the same code on real iPhone5 (ARM64), I get the following error:

 dlopen(/private/var/mobile/Containers/Bundle/Application/EDEAE282-AE96-45CA-9A4F-D70EE532FB93/foobar.app/lib/time.so, 2): no suitable image found. Did find: /private/var/mobile/Containers/Bundle/Application/EDEAE282-AE96-45CA-9A4F-D70EE532FB93/foobar.app/lib/time.so: mmap() error 1 at address=0x101CE4000, size=0x00004000 segment=__TEXT in Segment::map() mapping /private/var/mobile/Containers/Bundle/Application/EDEAE282-AE96-45CA-9A4F-D70EE532FB93/foobar.app/lib/time.so 

As far as I can figure out, the file exists and was found successfully. It is also not just a problem of compiling the wrong architecture; lipo -info reports that time.so :

 Non-fat file: time.so is architecture: arm64 

and file tells time.so as Mach-O 64-bit bundle .

Two questions that I have:

  • What do the "no matching images" and "mmap () error 1" error messages mean?

  • How can I fix (or even diagnose) what is happening here? I went through the C code, and the dlopen() call fails with a return value of NULL and set an error message, so the step-by-step code will not give me more information.

(As a background, I'm trying to pass Python to iOS, the dlopen() call is used to load custom code modules. time.so is one of these modules).

+5
source share
2 answers

I ran into the same problem. When I dlopen framework, Xcode says dlopen mmap() error 1 .

Make sure you create a framework and host application with the same identifier .

+1
source

Question 1: That no suitable image was found

[Answer] You tried to install the LD_LIBRARY_PATH export, or instead set -rpath

Question 2: What does the error message "mmap () error 1" mean.

[Answer]

One possibility: In principle, the key point of dlopen is to establish a memory mapping between your library in the disk file in system RAM.

"mmap () error 1 at address = 0x101CE4000, size = 0x00004000"

means that the OS will map the file to the virtual memory address 0x101CE4000 (which is page aligned), and the size is 4 pages (16384 bytes).

since such an attempt failed, I suspect that there is enough free memory in your system bass to carry the display.

Another possibility:

mmap used in dlopen can use the MAP_FIXED flag to perform the mapping, so it is possible that it will complete the condition that the kernel selects a virtual address, which should be mmaped, but its property is not executable.

On the man page in mmap:

"The prot argument asks for PROT_EXEC, but the displayed area belongs to the file system file that no-exec was installed."

If you can modify the dlopen () file, simply remove the MAP_FIXED flag, if any.

In general, it is interesting to understand what an error value (non-return value) is set via mmap (). You can do this using strace, which shows why the system call is not working.

0
source

Source: https://habr.com/ru/post/1214331/


All Articles