does android-ndk build with --gc-sections, and how to disable?
I am porting the 'iw' command to Android. This is wireless related. It appears that the iw tool has a set of commands that can be used, however from another user's site:
A glance at the iw source revealed that iw sticks all that stuff into an ELF section which mostly disappears when you link with –gc-sections.
This person was porting 'iw' to Android also, but decided to do cross-compilation through make instead of building an Android.mk and building through android-ndk. He mentions that whenever --gc-sections
is removed:
So with that exorcised from my linker command line, I finally have a functioning Android iw
I am finding this to be true. Whenever I build 'iw' by constructing an Android.mk, and then running 'iw' on my Android device, all of these commands disappear.
Here is my Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:= \
iw.c \
genl.c \
event.c \
info.c \
phy.c \
interface.c \
ibss.c \
station.c \
survey.c \
util.c \
mesh.c \
mpath.c \
scan.c \
reg.c \
version.c \
reason.c \
status.c \
connect.c \
link.c \
offch.c \
ps.c \
cqm.c \
bitrate.c \
wowlan.c \
roc.c \
sections.c
LOCAL_C_INCLUDES += $(LOCAL_PATH)/../lowpan/include \
$(LOCAL_PATH)/../libnl/include
LOCAL_CFLAGS += -g
LOCAL_CFLAGS += -fPIC -DPIC
LOCAL_STATIC_LIBRARIES := libnls libnl
ifeq ($(TARGET_BUILD_TYPE),release)
LOCAL_CFLAGS += -g
endif
LOCAL_MODULE:= iw
include $(BUILD_EXECUTABLE)
If it is true that the android-ndk is somehow using --gc-sections
and removing these commands from an ELF section, I cannot figure out how to stop it from doing this. Does anyone have any suggestions here?
Edit: It does look like the NDK is using this:
build/core/default-build-commands.mk: -Wl,--gc-sections \
build/tools/toolchain-patches/build/0001-Options-brought-in-from-core-combo-for-IA.patch:+ -fexceptions -ffunction-sections -finline-functions \
build/tools/toolchain-patches/build/0001-Options-brought-in-from-core-combo-for-IA.patch:+ -Wl,-z,noexecstack -Wl,--gc-sections -nostdlib \
build/tools/toolchain-patches/build/0001-Options-brought-in-from-core-combo-for-IA.patch:+ -fexceptions -frtti -fstrict-aliasing -ffunction-sections -finline-functions \
build/tools/toolchain-patches/build/0001-Options-brought-in-from-core-combo-for-IA.patch:+ -ffunction-sections -funwind-tables -fmessage-length=0 \
toolchains/arm-linux-androideabi-4.4.3/setup.mk: -ffunction-sections \
toolchains/arm-linux-androideabi-4.4.3/setup.mk: -Wl,--gc-sections \
toolchains/x86-4.4.3/setup.mk: -ffunction-sections \
toolchains/x86-4.4.3/setup.mk: -开发者_JS百科Wl,--gc-sections \
EDIT: I removed all instances of --gc-sections
and -ffunction-sections
from the android-ndk, and after rebuilding it works! However, I'd prefer not to modify the android-ndk, so there must be a modification to my local Android.mk to remove these two flags.
EDIT: I removed only --gc-sections
from the above default android-ndk files, and that solved the problem. So, -ffunction-sections
is not related. I just need to figure out how to disable --gc-sections
in a local Android.mk
You can add line
LOCAL_LDLIBS += -Wl,--no-gc-sections
to your Android.mk file. But as result you will have both -gc-sections
and --no-gc-sections
specified at the same time because ndk-build
always adds -gc-sections
options for executables.
If this does not work then you have only way to hack setup.mk
from your toolchain. Or you can build without ndk-build. As alternative variant I can suggest to use cmake for building Android projects. See android-cmake project for details
精彩评论