gradle __stack_chk_fail_local和-fno-stack-protector -如何让它工作?

8yparm6h  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(111)

更新:
我刚刚发现这个问题特别是与我的项目中包含的预构建库(libxml2)有关。它在构建时启用了堆栈保护,因此依赖于__stack_chk_fail_local方法。我现在也用-fno-stack-protector重建了这个库,一切正常。
把这个问题留在这里,以防其他人也在这个问题上犯错。
原问题:
当NDK更新到版本22时,我的Android应用程序的x86版本停止工作,并出现错误:
未定义的隐藏符号:__stack_chk_fail_local
这似乎是一件很常见的事情,每个人都说要么回到NDK 21,要么将-fno-stack-protector添加到cFlags。
我当时从我的构建中删除了x86,但从那以后,我收到了旧Chromebook用户的投诉,我的应用程序部分崩溃(由于使用ARM仿真)-甚至Crashlytics本身也因仿真(armeabi-v7a/libcrashlytics-handler.so" has unexpected e_machine: 40 (EM_ARM))而崩溃,所以我没有收到崩溃报告。因此,我想添加x86支持回来,同时仍然使用最新的NDK。
我已经将其添加到build. gradle的android.defaultConfig块中:

externalNativeBuild.cmake.cFlags "-fno-stack-protector"

字符串
但是当我查看它运行的命令行时,我看到它仍然在命令的开头添加“-fstack-protector-strong”,并且我的额外标志被添加到末尾:
D:\Android\sdk\ndk\23.1.7779620\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe--target = i686-none-linux-android21--gcc-toolchain = D:/Android/sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64--sysroot = D:/Android/sdk/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot-fPIC-g-DANDROID-fdata-sections-ffunction-sections-funwind-tables**-fstack-protector-strong**-no-canonical-prefixes-mstackrealign-D_FORTIFY_SOURCE = 2-Wformat-Werror = format-security**-fno-stack-protector**...
其他文章似乎建议添加no-stack-protector应该会自动删除stack-protector-strong,但事实并非如此。
我已经搜索了其他有这个问题的人,但找不到任何东西。我已经搜索了NDK问题列表,没有看到它提到。
我还能试试别的吗

whlutmcx

whlutmcx1#

你可能有一些用stack-protector编译的预建库。我通过将以下代码添加到我的一个cpp文件中来定义符号,从而使它得以编译。

extern "C" {
void __stack_chk_fail(void)
{
    abort();
}
/* On some architectures, this helps needless PIC pointer setup
   that would be needed just for the __stack_chk_fail call.  */

void __stack_chk_fail_local (void)
{
    __stack_chk_fail ();
}
}

字符串
或者,你可以从这里复制功能https://codebrowser.dev/glibc/glibc/debug/stack_chk_fail.c.html基本上,应用程序应该崩溃时,该函数被调用。

相关问题