-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] undefined reference to `__write_chk' #1179
Comments
is -DANDROID_PLATFORM not the right way to declare that you're targeting API level 23 with cmake? here's what line 174 of bits/fortify/unistd.h looks like, so we shouldn't be making the call at API level 23:
|
Should be right according to ANDROID_PLATFORM. No error if replace |
i assume this is telling cmake to statically link libz? it actually looks like there's a mistake in how we built the libz for API level 23:
|
yeah, looks like we only have one libz.a per architecture (which i already believed to be true), but that we didn't build it at the oldest supported API level (which is surprising to me)?
|
(oddly they don't appear to be built with the usual ELF note that would tell me what API level we thought we were targeting.) |
The static libs in the sysroot are mostly only for static executables (libc.a isn't going to help in JNI), so they're only built for the latest API level. For libz we may want to do differently. @huangqinjin are you using the static libz intentionally, or is that just by accident because it's what CMake chose for you with |
Where "they" is libz.a? They can't be. The note file is part of the CRT object. libz.a isn't linked yet. |
no, i looked at the corresponding .so files --- which i realize now don't really "correspond" in any useful sense, since they're just stubs. |
Ah. I think in that case it's because the stubs aren't built as NDK libraries, so they're not built with the NDK CRT objects. Like you say, they're just stubs so it doesn't make much of a difference. |
Same error when targeting 21 API level with zlib.a |
If you're building a static executable you need to target the maximum supported API level for that NDK. For r21 that means 29. If you're not building a static executable, don't use libz.a; use libz.so. |
I am building a statically linked shared library. So I statically link OpenCV into my library, which itself is a .so file to be loaded by my app. I have to support Android API level 18 and later. Everything worked fine until NDK 21.0.6113669, when I also started to get the mentioned So according to this solution, I would have to compile for API level 29, which compiles and links, but crashes at runtime (when loading the shared library) with an unknown reference to So what to do? |
I got the same issue and I had to back to the ndk version 19.2.5345600 to make it work around. Note: Make sure that you update the ndk configuration in
Hope this helps. |
@Elv1zz Target API 18 and use libz.so, not libz.a. |
According to find_library search process 6 and CMake source code of
And variables set in Android toolchain:
So CMake found One solution is to prepend Another solution, is it possible to remove |
Sure looks that way. Will dup this to that one.
Not without breaking a different set of users :( I'd really like to remove both from the NDK proper and expose them as an AAR instead, since relying on the system's libz.so means getting an old libz.so on old devices, but I suspect that migration will take a long time to avoid breaking people (and I haven't made such an AAR yet). |
That's true. We never use the platform libz for production. But I should repeat that the easiest cure for the meanwhile is to drop
|
Yep, definitely using plain old |
I don't agree. The standard way in CMake world is to use If we would like to benefit from CMake's cross-platform ablity for NDK development, we need follow the CMake way. |
Yeah, just to be clear, no argument against the idiomatic approach working on Android :) We'll get it fixed, I just can't be sure on the timeline. I suspect this one is a fair amount of work to do right. |
@huangqinjin I believe you are right in the long run. But actually the correct way to treat this would be to switch to a prefab external up-to-date libz, as @DanAlbert suggested above. My suggestion is but an immediate workaround. |
I found same libz.a issue with undefined __chk_write and __chk_read during opencv build (cmake). |
@timofey-retailnext the best is not to rely on system libz. I believe you can choose an external libz for opencv build. |
#929 should be fixed in NDK r22beta1, though, right? so you should automatically get libz.so there rather than libz.a, and that should just work? |
The fix for #929 had to be reverted because it broke AGP. Unsure when we'll be able to fix it again.
I'm not clear on what's broken. File a bug? |
should we just remove libz.a? i wonder whether it's causing far more trouble than it's worth. who's using it on purpose, and for what? |
Static executables won't have a choice. Removing it might break things like toybox? Unsure if it uses libz or not. |
but your static toybox is going to be a bit broken anyway. there's no static liblog (which is an NDK library) let alone libcutils (which isn't). and like alex said, you can always include your own libz build if that's actually deliberate. it's the "how many people are being hurt by this?" side of the question i'm unclear on... |
What lib should normally provide __write_chk and __read_chk at link time ? it is linked locally to an .SO (and thus resolves these undefined symbols) ? if yes, then it should be done for .A too ... (or .A to be thrown away completely). |
they're in libc, as part of the fortify implementation. __write_chk: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/fortify.cpp#448 __read_chk: https://android.googlesource.com/platform/bionic/+/master/libc/bionic/fortify.cpp#201 |
Currently I use the first method I mentioned above and it requires
|
Let's move this discussion to the main bug: #929 |
1. Set CMAKE_FIND_ROOT_PATH unconditionally. Revise the implementation from commit a7f41a7 (Android: Fix find_* search order within NDK for unified toolchains, 2020-10-13). In the old implementation, if people set CMAKE_FIND_ROOT_PATH, CMAKE_ANDROID_NDK won't be added to find root. And all paths added to CMAKE_SYSTEM_*_PATH below will be rerooted to the user specified root. 2. Add api level specific library path to CMAKE_SYSTEM_PREFIX_PATH. As the discussion in [1], some people want the paths added by UnixPaths.cmake. They install their libraries according to GNUInstallDirs [2]. As a result, we cannot clear CMAKE_SYSTEM_PREFIX_PATH. It includes /usr so no matter what we specify in CMAKE_SYSTEM_LIBRARY_PATH, /usr/lib/<arch> will be searched first. The author also pointed out a way to solve this issue [3]. In addition to other paths, CMake also searches <root>/<prefix> [4]. So we can add the API specific lib path to the beginning of CMAKE_SYSTEM_PREFIX_PATH, to have it searched first. [1] https://android-review.googlesource.com/c/platform/ndk/+/1486800 [2] https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html [3] android/ndk#1179 (comment) [4] https://gitlab.kitware.com/cmake/cmake/-/blob/11425041f04fd0945480b8f9e9933d1549b93981/Source/cmSearchPath.cxx#L202
A lot ! |
It's already fixed in r23. See the comment before yours for the up to date info. |
I'll upgrade and try again, thanks. |
ndk r22d claimed to fix similar issue (#1391 - missing cpu_features). but __write_chk/__read_chk issues is still there. r23-beta2 also has it. and r21e. |
that was unrelated: the |
This comment has been minimized.
This comment has been minimized.
The problem reported here (CMake picking the static library by default instead of the shared library) has already been fixed.
This builds just fine with r23. As does the
If there's still a problem we don't know about it and you need to file a new bug with a repro case. |
Description
CMakeLists.txt
main.cpp
No error with NDK 20.1.5948944 or NDK API level 24 and above.
Environment Details
The text was updated successfully, but these errors were encountered: