Need help with configuring Android.mk file ( AOSP + OPTEE )

Hello, I’m currently trying to use the hello_world sample for OP-TEE and developing a simple Android app which calls the hello_world application. I’m running into some problems with the Android.mk file:

Currently, my Android.mk file is:

################################################################################
# Android optee-hello-world makefile                                           #
################################################################################

LOCAL_PATH := $(call my-dir)
CFG_TEEC_PUBLIC_INCLUDE = /home/jsherman/devel/optee/optee_client/public

################################################################################
# Build hello world                                                            #
################################################################################
include $(CLEAR_VARS)

LOCAL_SHARED_LIBRARIES := libteec
$(info $(LOCAL_SHARED_LIBRARIES))

LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := ndktest.c

LOCAL_C_INCLUDES := /home/jsherman/devel/optee/optee_hello_world/ta/include \
		$(CFG_TEEC_PUBLIC_INCLUDE) \
		

include $(BUILD_SHARED_LIBRARY)

Basically I just inserted the hello_world.c code into a JNI function. When I try and run the ndk-build function, I get the following error:

/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:19: undefined reference to'TEEC_InitializeContext'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:27: undefined reference to 'TEEC_OpenSession'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:51: undefined reference to 'TEEC_InvokeCommand'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:66: undefined reference to 'TEEC_CloseSession'
/home/jsherman/AndroidStudioProjects/NDKTest/jni/ndktest.c:68: undefined reference to 'TEEC_FinalizeContext'

I’m guessing I’m not correctly linking the libteec.so library, but I’m not entirely sure.

Answered in https://github.com/OP-TEE/optee_os/issues/913.

Hi,
I am trying to execute a TA from the android media framework ( frameworks/av/drm/mediadrm/plugins/clearkey ) using TEEC_InitializeContext, TEEC_OpenSession, TEEC_CloseSession, TEEC_FinalizeContext, TEEC_InvokeCommand APIs. In order to do so I am trying to link libdrmclearkeyplugin.so with the libteec.so.

But I am not able to link that libteec.so with libdrmclearkeyplugin.so and getting the undefined reference errors.
Provided the libteec in the LOCAL_SHARED_LIBRARIES section of the Android.mk file under frameworks/av/drm/mediadrm/plugins/clearkey and also provided the tee_client_api.h file path in the LOCAL_C_INCLUDES section.

Please help me in resolving this issue.

I am using https://github.com/linaro-swg/optee_android_manifest/tree/hikey-n-4.9-240

---------------------------error log-------------------------------------------
FAILED: /bin/bash -c “prebuilts/misc/linux-x86/ccache/ccache prebuilts/clang/host/linux-x86/clang-2690385/bin/clang++ -nostdlib -Wl,-soname,libdrmclearkeyplugin.so -Wl,–gc-sections -shared -Lout/target/product/hikey/obj_arm/lib out/target/product/hikey/obj_arm/lib/crtbegin_so.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/AesCtrDecryptor.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/ClearKeyUUID.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/CreatePluginFactories.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/CryptoFactory.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/CryptoPlugin.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/DrmFactory.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/DrmPlugin.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/InitDataParser.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/JsonWebKey.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/Session.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/SessionLibrary.o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/Utils.o -Wl,–whole-archive -Wl,–no-whole-archive out/target/product/hikey/obj_arm/STATIC_LIBRARIES/libjsmn_intermediates/libjsmn.a out/target/product/hikey/obj_arm/STATIC_LIBRARIES/libunwind_llvm_intermediates/libunwind_llvm.a out/target/product/hikey/obj_arm/STATIC_LIBRARIES/libcompiler_rt-extras_intermediates/libcompiler_rt-extras.a prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/…/lib/gcc/arm-linux-androideabi/4.9/…/…/…/…/arm-linux-androideabi/lib/libatomic.a prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/bin/…/lib/gcc/arm-linux-androideabi/4.9/libgcc.a -lcrypto -llog -lstagefright_foundation -lutils -lteec -lc++ -ldl -lc -lm -o out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libdrmclearkeyplugin_intermediates/LINKED/libdrmclearkeyplugin.so -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,–build-id=md5 -Wl,–warn-shared-textrel -Wl,–fatal-warnings -Wl,–icf=safe -Wl,–hash-style=gnu -Wl,–no-undefined-version -Wl,–no-fix-cortex-a8 -target arm-linux-androideabi -Bprebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.9/arm-linux-androideabi/bin -Wl,–exclude-libs,libunwind_llvm.a -Wl,–no-undefined out/target/product/hikey/obj_arm/lib/crtend_so.o”
frameworks/av/drm/mediadrm/plugins/clearkey/Session.cpp:37: error: undefined reference to ‘TEEC_InitializeContext(char const*, TEEC_Context*)’
frameworks/av/drm/mediadrm/plugins/clearkey/Session.cpp:42: error: undefined reference to ‘TEEC_OpenSession(TEEC_Context*, TEEC_Session*, TEEC_UUID const*, unsigned int, void const*, TEEC_Operation*, unsigned int*)’
frameworks/av/drm/mediadrm/plugins/clearkey/Session.cpp:54: error: undefined reference to ‘TEEC_CloseSession(TEEC_Session*)’
frameworks/av/drm/mediadrm/plugins/clearkey/Session.cpp:55: error: undefined reference to ‘TEEC_FinalizeContext(TEEC_Context*)’
frameworks/av/drm/mediadrm/plugins/clearkey/Session.cpp:216: error: undefined reference to ‘TEEC_InvokeCommand(TEEC_Session*, unsigned int, TEEC_Operation*, unsigned int*)’
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
-----------------------------error log-----------------------------------

find . -name libteec.so
./out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libteec_intermediates/PACKED/libteec.so
./out/target/product/hikey/obj_arm/SHARED_LIBRARIES/libteec_intermediates/LINKED/libteec.so
./out/target/product/hikey/obj_arm/lib/libteec.so
./out/target/product/hikey/obj/SHARED_LIBRARIES/libteec_intermediates/PACKED/libteec.so
./out/target/product/hikey/obj/SHARED_LIBRARIES/libteec_intermediates/LINKED/libteec.so
./out/target/product/hikey/obj/lib/libteec.so
./out/target/product/hikey/system/lib64/libteec.so
./out/target/product/hikey/system/lib/libteec.so
./out/target/product/hikey/symbols/system/lib64/libteec.so
./out/target/product/hikey/symbols/system/lib/libteec.so

objdump -T ./out/target/product/hikey/system/lib64/libteec.so | grep TEEC_
000000000000161c g DF .text 0000000000000098 Base TEEC_CloseSession
0000000000000f88 g DF .text 0000000000000010 Base TEEC_FinalizeContext
0000000000000e28 g DF .text 0000000000000160 Base TEEC_InitializeContext
00000000000016b4 g DF .text 000000000000017c Base TEEC_InvokeCommand
00000000000018fc g DF .text 0000000000000110 Base TEEC_RegisterSharedMemory
0000000000001a0c g DF .text 00000000000000c0 Base TEEC_RegisterSharedMemoryFileDescriptor
0000000000001bdc g DF .text 0000000000000068 Base TEEC_ReleaseSharedMemory
0000000000001830 g DF .text 00000000000000cc Base TEEC_RequestCancellation
0000000000001acc g DF .text 0000000000000110 Base TEEC_AllocateSharedMemory
0000000000000f98 g DF .text 00000000000001bc Base TEEC_OpenSession

Answered in https://github.com/OP-TEE/optee_os/issues/2135. Basically tee_client_api.h needs to include an extern "C" statement.

@vchong Thanks for the help.

i’ve already wrote extern c in my .h file. I’m currently trying to get it to work with the android settings app. my file structure is like this.

settings
+--jni 
     +--android.mk
     +--hellojni.c

and in the android make at settings i’m including my hellojni.c file in this manner.

ZPATH := $(LOCAL_PATH)
include $(ZPATH)/jni/Android.mk
LOCAL_JNI_SHARED_LIBRARIES := libhello-jni

I was able to get that working. I had a simple example where jni would return the string I tried the few methods for getting the libteec.so working. my header file has extern c in it. I tried not using prebuilt libraries in the includes. here is my make file within the jni folder

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libteec
LOCAL_SRC_FILES := /storage/optee_android_manifest/out/target/product/hikey960/vendor/lib/libteec.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := libhello-jni
LOCAL_SRC_FILES := hello-jni.c

LOCAL_C_INCLUDES +=
$(JNI_H_INCLUDE)

LOCAL_CFLAGS += -O0 -g3

include $(BUILD_SHARED_LIBRARY)