Building libmpg123 for Android emits 'warning: shared library text segment...
Brought to you by:
sobukus
Using the CMake toolchain, when building libmpg123 targeting Android and x86, the following link warning is shown:
/opt/android/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: warning: shared library text segment is not shareable
/opt/android/ndk/21.4.7075529/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/i686-linux-android/4.9.x/../../../../i686-linux-android/bin/ld: error: treating warnings as errors
This is the same error discussed in https://discourse.libsdl.org/t/warning-shared-library-text-segment-is-not-shareable/23517 and fixed in https://github.com/libsdl-org/SDL_mixer/commit/4e9075f83f2b721b703a9b73314f99e0b3d500e6
By forcing MACHINE
in "external/mpg123/ports/cmake/src/libmpg123/CMakeLists.txt" to generic
, I was able to get rid of the warning.
So there are 2 ways to fix this issue:
- fix the assembly
- add a cmake option to disable assembly usage
I have attached the build log.
Is this only the case with the CMake build? I think Android worked before with the autoconf build.
I fear that people see ports/cmake for more than what it is. It came to be as vehicle to generate MSVC projects automatically. Autoconf is the main build, especially for Unix/Linux systems like Android.
So ... I am asking before diving into the linked resources: Is this a build issue or a code issue?
Last edit: Thomas Orgis 2022-10-12
I just tried building with autotools and
libmpg123.so
is built without issues there.In SDL_mixer, we made it possible to use "vendored" libraries. With CMake, this can be easily done across platforms: no need for a shell.
In libsdl's fork of mpg123, assembly for i686-Android is disabled using the following patch:
I guess it's one of these paranoia flags ...
-fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -mstackrealign -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -O2 -DNDEBUG -fPIC -MD -MT
I wonder if we have to switch to inline asm to support these compiler games. Is this no issue for Android on ARM? The NEON code is fine?
(And why has libsdl a fork of libmpg123 at all? I take pains to keep API and ABI stable. Any special patches?)
(Really, Sourceforge? When I reply by mail, you see the signature separator as headline markup?!)
Last edit: Thomas Orgis 2022-10-12
I copied the log to a bash shell and ran the commands manually. Every assembly file causes the warning/error to be emitted.
The issue is only with x86.
x86_64, arm 32- and arm 64-bit don't have any issue.
I didn't need to override any default, so I guess neon works fine.
libsdl2's fork contains our build fixes on top of your mpg123 + a
Android.mk
script.No c source has been modified.
See https://github.com/libsdl-org/mpg123/tree/v1.29.3-SDL
Is this only about getcpuflags.S? Not the actual assembly routines that do the work?
We had the textrel issue treated with bug 168, merged in 5 years ago. https://sourceforge.net/p/mpg123/bugs/168/
So it may indeed be just a little nitpick with getcpuflags.S, which is a rather trivial function.
Last edit: Thomas Orgis 2022-10-12
So … what is the actual relocation? In getcpuflags.S there is just one self-contained function. I cannot reproduce the error with a x86 build from a x86-64 Linux system with gcc 9. Is ist a quirk of the compiler/linker used in the Android NDK?
I wonder what's the source of the NDK complaint.
I tried building with the latest ndk (25.1.8937393), and this one emits a nice error.
See attachment.
Ah, that looks like the usual you-forgot-PIC-flags error. We need
-DPIC
for mangle.h to do global offset table stuff.Incidentally … there might lurk another portability issue here: Is PIC relevant for MS Windows binaries? I really don't know. Only ELF and APPLE binaries are covered.
Does
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
help on the whole build? I guess it's already active and implying-fPIC
… but we also need-DPIC
to make the preprocessor magic work.I added
at the top of
ports/cmake/src/libmpg123/CMakeLists.txt
and this reduces the error to only:dct64_sse_float.S
is compiled/assembled with the following command:(double post)
Last edit: Maarten 2022-10-12
Setting
CMAKE_POSITION_INDEPENDENT_CODE
does not matter when building a shared library. It is only important when building a static library/executable where you can choose between pic'ness. See https://cmake.org/cmake/help/latest/prop_tgt/POSITION_INDEPENDENT_CODE.htmlThe "CMake way" to add this define is to add the following:
Hm, is
-DPIC
also present when building tabinit_mmx.o?Yes, this is the command:
Just to make sure you're looking at the correct version, sdl_mixer is using mpg123 1.29.3
Never mind, I just bumped to 1.30.2 and the error is the same.
And building the same codebase using the same toolchain with the autoconf build is fine, yes? It's just the cmake build? Then the difference should be apparent from the compile logs …
As I said … we at least had the relocation stuff sorted out in 2017 for „normal“ systems (including OSX;-).
Not a solution, but changing
to
makes it building for all 4 android abi's. (and fail for ordinary Linux)
Last edit: Maarten 2022-10-12
Wait, what? So the code branch for MACH-O binaries works for Android? Why is
__ELF__
not defined, I thought Android uses ELF format. Does same trick work for you when forcing the first section with(Or just by adding
-D__ELF__
to compiler flags, I guess.) ?Maybe we can add a
|| defined(__ANDROID__)
to that branch and call it a day?Last edit: Thomas Orgis 2022-10-12
Yes, Android uses elf files. It's based on Linux after all.
So I tried the following in
dct64_sse_float.S
: