android-ndk 是否使用 --gc-sections 构建,以及如何禁用?

发布于 2024-12-01 12:00:10 字数 2681 浏览 0 评论 0原文

我正在将“iw”命令移植到 Android。这与无线相关。看来 iw 工具有一组可以使用的命令,但是来自 另一个用户网站

浏览一下 iw 源代码就会发现,iw 将所有这些东西都粘到了 一个 ELF 部分,当您链接时大部分会消失 –gc-sections。

此人也将“iw”移植到 Android,但决定通过 make 进行交叉编译,而不是构建 Android.mk 并通过 android-ndk 进行构建。他提到每当删除 --gc-sections 时:

因此,随着我的链接器命令行的消失,我终于 有一个正常运行的 Android iw

我发现这是真的。每当我通过构建 Android.mk 来构建“iw”,然后在 Android 设备上运行“iw”时,所有这些命令都会消失。

这是我的 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)

如果 android-ndk 确实以某种方式使用 --gc-sections 并从 ELF 部分中删除这些命令,我​​不知道如何阻止它这样做这。这里有人有什么建议吗?


编辑:看起来 NDK 确实正在使用这个:

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:    -Wl,--gc-sections \

编辑:我从 android-ndk 中删除了 --gc-sections-ffunction-sections 的所有实例,并且重建后就可以用了!但是,我不想修改 android-ndk,因此必须修改我的本地 Android.mk 以删除这两个标志。


编辑:我只从上面的默认 android-ndk 文件中删除了 --gc-sections ,这解决了问题。因此,-ffunction-sections 不相关。我只需要弄清楚如何在本地 Android.mk 中禁用 --gc-sections

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:    -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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

做个少女永远怀春 2024-12-08 12:00:10

您可以将行添加

LOCAL_LDLIBS += -Wl,--no-gc-sections

到 Android.mk 文件中。但结果你将同时指定 -gc-sections--no-gc-sections 因为 ndk-build 总是为可执行文件添加 -gc-sections 选项。

如果这不起作用,那么您只有从工具链中破解 setup.mk 的方法。或者您可以不使用 ndk-build 进行构建。作为替代变体,我建议使用 cmake 来构建 Android 项目。有关详细信息,请参阅 android-cmake 项目

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文