Search code examples
androidandroid-ndkarmarmv7trusted-computing

How to call arm assembly from C source files?


I have found a number of good tutorials on compiling assembly code with Android NDK. But they do not have information on how to call assembly instructions from C source files, which I believe is possible. I think I have seen similar tutorials for it online. My question is if I can have a C source file, which issues Assembly calls. I also want to be able to compile with NDK. But I like to avoid using Android Studio and JNI; one reason being I do not have any Java code. And I have already validated I can compile and run C source files using NDK. So basically I know how to compile C source files, and assembly files using NDK. I have validated that C code runs fine on my phone. But I am not sure how to go about calling assembly instructions from C source files for the arm architecture. I keep getting the following error message when I try to compile a simple source file:

/tmp/ccwua4Gd.s: Assembler messages:
/tmp/ccwua4Gd.s:18: Error: selected processor does not support Thumb mode `smc #0'

Here is the file:

#include <stdio.h>

__asm__(" smc #0");

int main(void)
{
  /*Do something*/
  return 0;
}

The issue, by the way, does not seem to be thumb vs. arm related. I did try LOCAL_ARM_MODE := arm statement in my Android.mk, but it did not fix anything. I just get the similar error for ARM instruction.

Update: I still get that error. Here is a copy of my Android.mk I did not have to specify arm version or arm/thumb specification in an earlier C source file in order to execute on the device. I have read about thumb and the pros and cons associated with the option, and how to specify support for it. This error however persists even when I put in configure the ABI. The one thing I have not tried is adding the .arm extension to my C source files. I saw a reference to it in an earlier post, and could not figure out why that would make a difference. I believe the OP, in that thread, implied that this would fix the error. Can anyone elaborate on that? Has anyone used it? What is the effect of doing that vs. the Android.mk statements relevant to the architecture? Thanks

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS) 
LOCAL_MODULE := hello  
APP_ABI := armeabi-v7a
FILE_LIST := $(wildcard $(LOCAL_PATH)/*.c)
$(warning here is what is in FILE_LIST after an attempt to collect all the C files$(FILE_LIST))
FILE_LIST += $(wildcard $(LOCAL_PATH)/*.s)
$(warning here is what is in FILE_LIST after an attempt to collect all the S files$(FILE_LIST))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
include $(BUILD_EXECUTABLE)

Another update - I tried adding the .arm suffix to my C source files changing foo.c to foo.c.arm, but even that did not address the issue. Any help is greatly appreciated. Here is what I am doing now in my Android.mk file:

FILE_LIST := $(patsubst %.c,%.c.arm,$(wildcard $(LOCAL_PATH)/*.c))

Here is an updated version of the error. It just changes the word "thumb" to "arm"

/tmp/ccTK9siq.s: Assembler messages:
/tmp/ccTK9siq.s:17: Error: selected processor does not support ARM mode `smc #0'

Hey I did a little more research, and found this link about my problem. I updated my Android.mk file with LOCAL_CFLAGS := -mcpu=cortex-a8 as instructed in the page. The problem did not go away, but I got an informative message stating: warning: switch -mcpu=cortex-a8 conflicts with -march=armv5te switch [enabled by default] So the commands suggested below did not really take effect. Does anyone know if I should try to change the default settings of ndk-build and how? After all it is a warning; not an error. So I am not sure if it took my configuration or I need to do something else. Also now I am getting an error about missing the only header file, stdio.h, which I was not getting before. This kind of tells me that ndk-build just handed the build process to make after that warning. Can anyone provide guidance or solution to this?

073015 update - I realized there is a difference between application.mk and Android.mk. After configuring the applicaiton.mk with the parameters stated, I was able to change the architecture to armv7-a but I get the same error; look below:

jni/Android.mk:6: This is in NDK_ROOT /home/sansari/android/android-ndk-r10d
[armeabi-v7a] Assembly       : hello_tz <= main_normal.filtered.s
./obj/local/armeabi-v7a/objs-debug/hello_tz/main_normal.filtered.s: Assembler messages:
./obj/local/armeabi-v7a/objs-debug/hello_tz/main_normal.filtered.s:16: Error: **selected processor does not support ARM mode `smc #0'**
make: *** [obj/local/armeabi-v7a/objs-debug/hello_tz/main_normal.o] Error 1

Is there any other config I need to make to get the functionality I want?


Solution

  • The problem is that you're injecting assembly that is for ARMv6 or above. If you do not specify the ABI in your NDK Application.mk, the tools assume ARMv5.