2

got this strange behaviour with standalone Android NDK r10e Toolchain (built with --toolchain=x86-clang3.6 switch). Environment variables for cross compilation have been set before running makefile, SYSROOT points to Android toolchain location, CXX equals i686-linux-android-clang++. Basically, I have a bunch of cpp files that I would like to compile to Android executable. Sadly, 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?

Makefile: (main.cpp contains main function)

CFLAGS=-c -Wall -std=c++11 -Wextra --sysroot=${SYSROOT} -march=i686
LDFLAGS=-lc -lc++_shared -L${SYSROOT}/usr/lib
SOURCES=main.cpp File1.cpp File2.cpp File3.cpp File4.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=test

all: $(SOURCES) $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS) ${CXX} $(LDFLAGS) $(OBJECTS) -o $@
.cpp.o: ${CXX} $(CFLAGS) $< -o $@
clean: rm *.o test
jww
  • 97,681
  • 90
  • 411
  • 885
cleaweag
  • 23
  • 3
  • 1
    Are you sure you've copied your makefile here correctly? To me it looks like this makefile will most likely give you _don't know how to build_ errors. There should be a newline/TAB after the prerequisites / before the recipe, or else they should be separated by `;`. – MadScientist May 31 '15 at 14:05
  • @MadScientist That is right, TABs are missing here, probably cut it out when pasting. – cleaweag Jun 01 '15 at 22:11
  • Missing TABs is quite common on StackOverflow. Missing newlines not so much ... :) – MadScientist Jun 02 '15 at 04:22

1 Answers1

1

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 shared object :)

You probably did something like:

readelf -l test | grep -i "file type"

In reality, that's an artifact of Position Independent Executable (PIE) and readelf reporting.

The important part is readelf is reporting DYN, and not reporting EXE. EXE means it lacks PIE, and that usually triggers a security related defect.


PIE was added at Android 4.1, but it was optional. PIE is required for Android 5.0 and above. From Security Enhancements in Android 5.0:

non-PIE linker support removed. Android now requires all dynamically linked executables to support PIE (position-independent executables). This enhances Android’s address space layout randomization (ASLR) implementation.


Also see Position Independent Executables on Stack Overflow. It discusses PIE and Android.

Community
  • 1
  • 1
jww
  • 97,681
  • 90
  • 411
  • 885
  • Thst's exactly what I did. What's more, after copying compiled file and running it from adb shell, it just works as expected. Thank you for pointing me into the right direction :) – cleaweag Jun 01 '15 at 22:17