Menu
  • HOME
  • TAGS

How can I detect when Android x86 is emulating ARM?

Tag: android-ndk,x86,arm

I have a JNI library that runs well on most Android devices - ARMv5, ARMv7, and x86.

I'm using NEON instructions on ARMv7, but instead of cluttering the code with conditional/duplicated source, I want to detect a non-NEON ARMv7 in Java at library load time, and load the v5 library instead: slow CPU is slow.

I found a post suggesting that I look for the 'neon' feature in /proc/cpuinfo, so I'm parsing that, and loading libthing.so usually, or libthing-v5.so if the device claims to be an ARMv7 without NEON. This works nicely on ARM.

Unfortunately not only does x86 emulate an ARM /proc/cpuinfo(!), if it decides it doesn't understand NEON then it also digs out the libthing-v5.so from the armeabiv7a directory, and uses it because there isn't one in the x86 directory.

My current workaround is just to copy the x86 library to both libthing.so and libthing-v5.so so if x86 is pretending to be a NEON-free ARMv7 chip, it'll get the x86 library anyway.

Aside from cooking up a tiny standalone architecture-detecting library of my own based on Yeppp or Android's own cpufeatures, is there a way to determine the genuine local architecture from Java?


@ph0b: Herewith the output of the Razr i, showing that the emulator has decided that the application has been installed as an 'ABI2 58', and that it needs to fake out /proc/cpuinfo.

Given that both shared libraries are available from x86 as well as the armeabi* directories, I don't understand why the device has decided to be an ARM. I might ask my contact at Intel about this.

06-05 10:58:41.360 17807 18053 D dalvikvm: Trying to load lib /data/data/com.company.android/lib/libmp.so 0x42409cb0
06-05 10:58:41.360 17807 18053 D dalvikvm: Added shared lib /data/data/com.company.android/lib/libmp.so 0x42409cb0
06-05 10:58:41.370 17807 18053 D dalvikvm: No JNI_OnLoad found in /data/data/com.company.android/lib/libmp.so 0x42409cb0, skipping init
06-05 10:58:41.420 17807 18053 D         : Searching package installed with ABI2 with Uid: 10109 
06-05 10:58:41.420 17807 18053 D         : Apps with ABI2 58 accessing /proc/cpuinfo 
06-05 10:58:41.430 17807 18053 I System.out: #Here's most of /proc/cpuinfo
06-05 10:58:41.430 17807 18053 I System.out: #Thu Jun 05 10:58:41 GMT+01:00 2014
06-05 10:58:41.430 17807 18053 I System.out: Serial=0000000000000001
06-05 10:58:41.430 17807 18053 I System.out: Revision=0001
06-05 10:58:41.430 17807 18053 I System.out: CPU=revision\t\: 1
06-05 10:58:41.430 17807 18053 I System.out: BogoMIPS=1500
06-05 10:58:41.430 17807 18053 I System.out: Hardware=placeholder
06-05 10:58:41.430 17807 18053 I System.out: Features=vfp swp half thumb fastmult edsp vfpv3 
06-05 10:58:41.430 17807 18053 I System.out: Processor=ARMv7 processor rev 1 (v7l)
06-05 10:58:41.430 17807 18053 I NativeWahooLibrary: Detected ARMv7 processor rev 1 (v7l) (=ARMv7, true) with ([email protected]) vfp swp half thumb fastmult edsp vfpv3 
06-05 10:58:41.430 17807 18053 D dalvikvm: Trying to load non-neon lib /data/data/com.company.android/lib/libwahoo-v5.so 0x42409cb0

Best How To :

I doubt the x86 emulates an ARM /proc/cpuinfo !?

Anyway, in order to detect the local architecture from Java, you can rely on Build.CPU_ABI and Build.CPU_ABI2: http://developer.android.com/reference/android/os/Build.html#CPU_ABI, and then continue parsing /proc/cpuinfo to look for neon only if CPU_ABI and CPU_ABI2 are arm*/armeabi-v7a

How to do a jump based on cmpss output (floating-point compare)?

assembly,x86

cmpss and its friends are more meant to give you masks that you can do branchless computation with, you could extract it to a GPR and then test that and so on but that's all a bit verbose and unnecessary. If you want to branch, ucomiss (or comiss, depending on...

Asm x86 segmentation fault in reading from file

assembly,x86,segmentation-fault,mmap

The value in R8 at the time your program crashes is the file descriptor returned by the open syscall. Its value is probably 3 which isn't a valid address. You'll need to stores these values in a range of memory you've properly allocated. You can create a buffer in your...

Using .so files in Android Studio

android,android-studio,android-ndk,shared-libraries

You said you were using Android Studio, but by default Android Studio currently ignores your Makefiles and use its own auto-generated ones, with no support for native depencies (for now). If you deactive the built-in support and do calls to ndk-build yourself, by putting something like this inside your build.gradle:...

What is the difference between normal method call from native method call?

android,android-ndk

The NDK allows you to write code using C/C++ and then link it into your Java application. You can potentially increase the speed of your application. The downsides to the NDK are, it only compiles to specific CPUs (whereas staying in Java land means it will work on any targetted...

Automatically copy .so files from NDK library project?

android-studio,gradle,android-ndk

I does not know if ther is a way to have you .so files copied as you want whiout wirtting anything. But you could have gradle doing it for you. In your gradle file, add a task that copy those .so files where you need them to be. android {...

converting a vector of chars to int in assembly

assembly,x86,8086

Yes, there definitely is something more to it. To turn a string into a byte, you can use something like this ; INPUT esi = a null-terminated string ; OUTPUT al = the number str2byte: push cx mov cx, 0x0a00 xor al, al .loop: mul ch mov cl, byte [esi]...

Native Code: cannot use typeid with -fno-rtti

c++,osx,gcc,android-ndk,vtk

To enable C++ in the NDK, add LOCAL_CPP_FEATURES := rtti exceptions and LOCAL_CPPFLAGS += --std=c++11 to the jni/Android.mk file. By default, the NDK supports only a C++-like language. Note that there's no underscore between CPP and FLAGS. Also, I've used += because this won't overwrite other flags such as -Wall....

Android : Loading pre-built library - Circular dependency dropped

android,android-ndk,android.mk

My mistake is the value of LOCAL_MODULE_FILENAME. It must be libfromhere1 instead of fromhere1. ndk just puts .so suffix to the given name but it won't put lib prefix. Always it is better to give name by yourself than letting ndk name it for you. But I didn't understand why...

On Android Studio 1.3, NDK support not working

android,android-studio,android-ndk

Not available yet. "As announced at Google I/O, Android Studio 1.3 will include C/C++ support as well, but that is not included in the first couple of preview builds." Source: https://sites.google.com/a/android.com/tools/recent/androidstudio13preview1available ...

C float in NASM x86 assembly

c,assembly,x86,nasm

If you declare the return type func as float the result will be returned in the FPU (ST0). For returning a value in EAX you have to declare it as an integer type. For printf you have to fake a float. Example: caller.c: #include <stdio.h> #include <stdint.h> extern float asmfunc1(float);...

x86 jmp asterisk %eax

assembly,x86,jmp

jmp *%eax is AT&T syntax for jmp eax, which is one form of jmp r/m32. It will jump to the address contained in register eax: Jump near, absolute indirect, address given in r/m32. Another form of the same type of jump instruction is jmp *(%eax) which corresponds to jmp [eax]...

Set Android NDK globally in Android Studio

android,android-ndk

If you set the environment variable ANDROID_HOME to the location of your SDK and ANDROID_NDK_HOME to the location of your NDK, and delete any local.properties file, this builds projects with native code as expected on my Android Studio 1.2. I don't know why gradle clears other property setting mechanisms when...

re write-sql statement Insert OR REPLACE from java to c++ NDK Android

c++,android-ndk,android-sqlite

You can use std::to_string to build a string using your variables #include <string> std::string sql = " INSERT OR REPLACE INTO " + std::to_string(TypeContract.CTablePhotoMatch.TABLE_NAME) + "(" + ...; If any of your variables are already std::string, then you don't need to use this function you can simply use + to...

Problems compiling NDK project with Android Studio 1.3 preview

android,android-studio,gradle,stl,android-ndk

in fact, not much has changed since around january 2014 regarding NDK support in Android Studio. The 1.3 version that is out right now is only preview 3, and the amazing NDK support that has been shown at Google I/O isn't inside it yet. It should be out soon though....

Debug native code in Android Studio

android,debugging,android-studio,android-ndk

Actually, the advertised NDK support isn't available yet, even if you download the ndk-bundle and update Android Studio to the latest version in the canary channel (1.3-preview3 as of now). The SDK tools team said that the NDK support wasn't part of the first previews of Android Studio 1.3. However...

Subtraction order in assembly

assembly,x86,calling-convention,att

movl 8(%ebp), %eax brings your int a in the %EAX register. movswl 12(%ebp), %edx brings your short b in the %EDX register. Since subl %edx, %eax subtracts %EDX from %EAX you obtained (a - b)...

Read a non-atomic variable, atomically?

c++,multithreading,performance,x86,atomic

Since you tagged x86, this will be x86-specific. An increment is essentially three parts, read, add, write. The increment is not atomic, but all three steps (the add doesn't count I suppose, it's not observable anyway) of it are as long as the variable does not cross a cache line...

How to make android NDK work in windows?

android-ndk,windows-7-x64

The command you're probably looking for is ndk-build, not ndk.

Assembly (x86):

assembly,x86,bootloader,16-bit

Execution starts at the top. If you omit the jmp start then the character h will get interpreted by the CPU as if it were an instruction. Surely you see that such can not be correct? as far I was concerned, there was no distinction between code and data? There's...

Faster alternative for getPixel and getPixel in Android Bitmap?

android,performance,android-ndk,android-bitmap,renderscript

Use getPixels() to get all the pixels, modify the values in the byte[], then call setPixels() to store all the pixels at once. The overhead of calling setPixel() on each individual pixel is killing your performance. If it's still not fast enough, you can pass the array to an NDK...

Switch to user space

assembly,x86,operating-system,kernel

As bochs told you, your selector 0x08 has CPL=0 but the descriptor entry has DPL=3. Remember that the two least significant bits of the selector are the CPL. Thus, to switch to ring3, you should use 0x0b.

ASM : Trouble using int21h on real machine

assembly,x86,bootloader

You can't use MS-DOS services (INT 21h) in a bootloader. MS-DOS is an operating system, like Linux or Windows. Just like how you can't use Linux services before Linux has loaded, you can't use MS-DOS services before it has loaded. In a bootloader you're restricted to using BIOS services (or...

Play a sound file in masm32 and to stop the other sound file at the same time [closed]

winapi,assembly,x86,masm32,playsound

Read the documentation. PlaySound function pszSound A string that specifies the sound to play. The maximum length, including the null terminator, is 256 characters. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter. ... fdwSound Flags...

Linking against third-party libraries when doing cross-platform build in Visual Studio 2015

c++,android-ndk,cross-platform,clang,visual-studio-2015

First, the Opus Codec distribution comes with Visual Studio projects that are configured to build only Windows libraries, which are not cross-platform. You need to replace these projects with Cross Platform Library projects. Or better alternative: just download prebuilt libopus.a, e.g. from here. Second, you cannot use #pragma comment(lib, ...)...

subl causing Floating point exception

assembly,x86

The subl $16, %esp is a red herring. The actual issue is in print_int: movl value(%ebp), %eax jge .L1 # if value >= 0 movl doesn't set flags. You are missing a test %eax, %eax or equivalent before the jge. Learn to use a debugger. It would have pointed you...

Could this shift/jump be faster than switch…case statement?

assembly,x86,switch-statement,emulation,opcodes

If you are branching across 256 opcodes through a switch block, you're going to be doing an indirect jump which the CPU cannot predict well, and that will get you a pipeline break on every opcode. If the work to emulate an opcode is fair size, then this pipeline break...

How to upscale and render remote(RGB565) frame buffer on Android native?

android,graphics,android-ndk,android-gui,surfaceflinger

You're currently using private SurfaceFlinger APIs, which require privileged access. If you need to do that, I think you want to use the setSize() call to change the size of the window (which is independent of the size of the underlying Surface). This section in the arch doc shows how...

dlopen failed: cannot locate symbol “cblas_sdsdot” referenced by “libgsl.so”

android,c++,linux,android-ndk,gsl

I replied on your other post also regarding gsl. Please follow that procedure. I hope it will help you and you can easily use that static lib in your android app.

Error for cv::FileStorage in JNI

android,c++,opencv,android-ndk,file-storage

After a lot of debugging I found that the error was quite small The error was in the line LOCAL_LDLIBS := -llog -ldl The line should have been LOCAL_LDLIBS += -llog -ldl ...

ndk-build outputs ‘error adding symbols. File in wrong format’

android,c++,android-ndk,exiv2

you've compiled exiv2 for armv5+ devices running at least Lollipop. Here ndk-build fails because it's trying to link it from an arm64-v8a library it's building. Cross compiling without using ndk-build is hard to get right on Android, especially as you should support not only armv5, but also armv7, x86, x86_64,...

x86 asm - 12 bytes subtracted from esp. Only 8 needed

gcc,assembly,x86

It's because of the mov DWORD PTR [esp],eax Apparently, your puts and gets implementations require the argument to be pushed onto the stack. Value [ebp-0xc] is actually [esp] now, that's why that dword is reserved ahead. Why is it so? Doing it this way is more efficient, as you don't...

Android Studio 1.3 Preview NDK support

android-studio,android-ndk

Android Studio 1.3.0-Preview is indeed available through the Android Studio "Check for updates" menu. Unfortunately, all C++ features aren't available yet in this preview. They will be available in about 2 weeks. Source : Google IO dev tools keynote at 35:38...

OpenGL framebuffer android without GL_OES_packed_depth_stencil (on Nexus 7 2012)

android,c++,android-ndk,opengl-es-2.0,framebuffer

You create separate renderbuffers for depth and stencil: GLuint depthStencil[2]; glGenRenderbuffers(2, depthStencil); glBindRenderbuffer(GL_RENDERBUFFER, depthStencil[0]); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height); glBindRenderbuffer(GL_RENDERBUFFER, depthStencil[1]); glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, width, height); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthStencil[0]); glFramebufferRenderbuffer(GL_FRAMEBUFFER,...

Android NDK error bash: ../../build/intermediates/classes/debug/: Is a director

android,android-ndk

You are invoke your command under Linux. The shell interpreter BASH treat ; as the command separator. The class path separator ; is for Windows. So you can change you command to following, it will work. javah -d jni -classpath /home/king/ide/android-sdk-linux/platforms/android-22/android.jar:../../build/intermediates/classes/debug/ com.lengking.ndk.MainActivity ...

Huge performance difference in byte-array access between x64 and x86

c#,performance,x86,64bit,clr

You've made a classic mistake, attempting performance analysis on non-optimized code. Here is a complete minimal compilable example: using System; namespace SO30558357 { class Program { static void XorArray(byte[] a, byte[] b) { for (int p = 0; p< 64; p++) a[p] ^= b[p]; } static void Main(string[] args) {...

Prefetching double class member requires casting to char*?

c++,performance,optimization,x86,prefetch

If you read the description for _mm_prefetch() from the site you linked to it has : void _mm_prefetch (char const* p, int i) Fetch the line of data from memory that contains address p to a location in the cache heirarchy specified by the locality hint i. So you need...

Multiple apk versionCode distinction

android,android-ndk,multiple-apk

x86 devices are able to handle ARM libs but there is no ARM device able to handle x86 libs. So you only have to keep the version code of your x86 APK higher than the one of your ARM apk, and the right APK will go to the right device....

Reversing a string and printing it in IA32 assembly

string,assembly,x86,att,reversing

Your immediate problem is cmpb '\n', %al which is missing the $ sign required in at&t syntax to make it an immediate. As it is, '\n' is considered an address, and an invalid one at that. You should use cmpb $'\n', %al. However scanf will not place '\n' into your...

How does this instruction look in memory?

assembly,x86

IA32 processors have a default code size, in 16 bit code segments (or in real mode) is (guess) 16 bit. In 32 bit and 64 bit code segments it is 32 bit. Instructions like mov eax, 3ch are actually something like mov A, 3ch where A is the A register...

assembly function with C segfault

c,assembly,x86,sse,fpu

You have forgotten to cleanup the stack. In the prologue you have: pushl %eax pushl %ecx pushl %edx pushl %ebp movl %esp, %ebp You obviously need to undo that before you ret, such as: movl %ebp, %esp popl %ebp popl %edx popl %ecx popl %eax ret PS: I have already...

Qt does not detect Android NDK

android,c++,qt,android-ndk,android-sdk-tools

First of all I strongly recommend to install the latest version of Qt for Android (5.4.1 at present). Also you should download and install Android SDK (ver. 22+) and NDK (ver. r9+) from here. After downloading extract them. For Android SDK you should have a connection to Internet and download...

NDK application Signature Check

java,android,security,android-ndk,digital-signature

I will try to answer your first question here: Signature of your application is stored in the DEX(Dalvik executable) file of your APK. DEX files have following structure: Header Data section(contains strings, code instructions, fields, etc) Arrays of method identifiers, class identifiers, etc So, this is the beginning of the...

Android armeabi devices with API level 15+

android-ndk

AOSP itself doesn't support ARM versions below ARMv7 from Android 4.0 (API level 15), but there are custom builds that run on ARMv6. I'm not entirely sure if there are any official, certified compatible devices that run such a combination though. So in practice you should be pretty safe to...

Readelf reports program is a shared library instead of executable

c++,makefile,android-ndk,clang

clang++ keeps producing .so shared library (checked with readelf - it is indeed shared object). Is there a special switch to compiler / linker that I have forgotten? My guess: readelf is outputting Elf file type is DYN (shared object file), and you are interpreting that to mean a...

double* android ndk crash

c++,pointers,android-ndk

I see several possible causes. First of all, casting char* to double* and then accessing it through that pointer is undefined behavior. Most of time it works, but you are warned. Pointer misalignment. double is most likely supposed to be aligned to 8 bytes, and you read it through pointer...

Print string using INT 0x10 in bootsector

assembly,x86,fasm

The lodsb instruction loads the byte pointed to by the DS and SI registers but you haven't loaded either with a valid value. Since this a bootloader you also need to use the ORG directive, otherwise the assembler won't know where you code, and therefor welcome, gets loaded into memory....

Does RIP-relative addressing require 16-byte aligned?

assembly,x86,x86-64

It's the xorpd instruction. It causes and Exception of type 4, which happens when you specify an unaligned memory location with the VEX prefix before instruction (not 100% sure here). However, it's not the only one, there are about 106 more instructions that cause the same thing. ...

Wrong answer from DIV assembly

assembly,x86

div di divides the 32 bit quantity dx:ax by di. We don't know what value your bx has, but presumably it produces dx=1 (due to the adc dx, dx). So the division is going to be 0x10003 / 3 = 0x5556 remainder 1, and that's exactly what you see. PS:...

SIMD minmag and maxmag

assembly,floating-point,x86,sse,avx

Here's an alternate implementation which uses fewer instructions: static inline void maxminmag_test(__m128d & a, __m128d & b) { __m128d cmp = _mm_add_pd(a, b); // test for mean(a, b) >= 0 __m128d amin = _mm_min_pd(a, b); __m128d amax = _mm_max_pd(a, b); __m128d minmag = _mm_blendv_pd(amin, amax, cmp); __m128d maxmag = _mm_blendv_pd(amax,...

x86/C++ - Pointer To Pointer: Const being violated by compiler?

c++,visual-studio-2013,x86,shared-ptr

Thanks for all the great help with this issue, but we figured out what was going on. What was happening is that in some of the Handle constructors we needed to allocate memory for the pointer that the Handle's internal pointer-to-pointer was going to be pointing at. So what was...