Repository navigation
Description
| Previous ID | SR-3819 |
| Radar | None |
| Original Reporter | @weissi |
| Type | Bug |
Attachment: Download
Additional Detail from JIRA
| Votes | 6 |
| Component/s | Compiler |
| Labels | Bug, Driver, Linux |
| Assignee | None |
| Priority | Medium |
md5: 52de48fd6048234a1c72d6a42d29e78d
Issue Description:
If ICU isn't compiled with --disable-dyload it'll need -ldl to be linked. That's a problem in Swift when producing a static executable:
$ echo 'print("Hello World")' > test.swift && swiftc -static-executable test.swift
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/libicuuc.a(putil.ao):function uprv_dl_open_55: error: undefined reference to 'dlopen'
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/libicuuc.a(putil.ao):function uprv_dlsym_func_55: error: undefined reference to 'dlsym'
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/libicuuc.a(putil.ao):function uprv_dl_close_55: error: undefined reference to 'dlclose'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
<unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
My Swift compiler is version
Swift version 3.1-dev (LLVM 5c165fb715, Clang e540ba0c30, Swift 3d3fdecbb4)
Target: x86_64-unknown-linux-gnu
which is master of 2017-02-01.
I already verified that it can be fixed the following way:
/tmp/jw-swift/usr/bin/swiftc -o main.o -c main.swift
"/usr/bin/ld.gold" "-z" "relro" "--hash-style=gnu" "--build-id" "-m" "elf_x86_64" "-static" "-o" "test" "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crt1.o" "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crti.o" "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/crtbeginT.o" "-L/tmp/jw-swift/usr/lib/swift_static/linux" "-L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0" "-L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu" "-L/lib/x86_64-linux-gnu" "-L/lib/../lib64" "-L/usr/lib/x86_64-linux-gnu" "-L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../.." "-L/usr/lib/llvm-3.8/bin/../lib" "-L/lib" "-L/usr/lib" "/tmp/jw-swift/usr/lib/swift_static/linux/x86_64/swift_begin.o" "main.o" "-ldl" "-lswiftCore" "-lswiftImageInspectionStatic" "--defsym=__import_pthread_self=pthread_self" "--defsym=__import_pthread_once=pthread_once" "--defsym=__import_pthread_key_create=pthread_key_create" "-lpthread" "-licui18n" "-licuuc" "-licudata" "-lswiftCore" "-lswiftSwiftOnoneSupport" "/tmp/jw-swift/usr/lib/swift_static/linux/x86_64/swift_end.o" "-lstdc++" "-lm" "--start-group" "-lgcc" "-lgcc_eh" "-lc" -ldl "--end-group" "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/crtend.o" "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.4.0/../../../x86_64-linux-gnu/crtn.o"
the only difference between this and the standard invocation is the -ldl inbetween the --start-group and --end-group linker flags.
I'd be happy to fix this but I'm unsure where exactly to do that. Any pointers?
Notes
$ icu-config --ldflags
-L/usr/lib/x86_64-linux-gnu -licui18n -licuuc -licudata
$ icu-config --ldflags-system
-ldl -lm
-
I'm running Ubuntu 16.04
-
it's not enough to just pass
-ldltoswiftc(or-Xlinker -ldl) -
the
-ldlNEEDS to be in betweenld.gold's--start-groupand--end-groupand I'm unsure how to get it there -
the addition of
-ldlshould obviously only happen if ICU is compiled without--disable-dyload.
Activity
belkadan commentedon Feb 1, 2017
The link args come from utils/static-executable-args.lnk, which is copied into place by CMake. We could do some configure-time tests to figure out whether
-ldlis necessary; do you think that would be sufficient? I'd rather not have the compiler call out to icu-config every time it needs to link a static executable.weissi commentedon Feb 1, 2017
@belkadan yes, I had seen that file and it would be enough to put
-ldlin there but then it would even do that if your system's ICU is compiled with--disable-dyload.But thinking about it, it probably improves the current situation because 1) it's unlikely that your ICU is compiled with
--disable-dyloadand 2) we can't make fully working static binaries anyway because glibc doesn't support that properly (and libFoundation.a also isn't a thing (yet)). I'll make a pull request...weissi commentedon Feb 1, 2017
@belkadan #7188
belkadan commentedon Feb 1, 2017
I was thinking rather than update the file directly, we would change it to use CMake's configure_file feature, which allows us to insert substitutions. I'm fine with CMake calling icu-config.
belkadan commentedon Feb 1, 2017
I suppose that's not great for binary distributions, though.
weissi commentedon Feb 1, 2017
Sorry about that. But yes, as you say it shouldn't really be checked on the system that builds swiftc. And I'm guessing most users use the binary distribution...
Hmm, unsure what to do really.
spevans commentedon Feb 2, 2017
For static binaries (using the
-static-executableoption) you should not link inlibdlwhich is obviously a problem given howlibicuis compiled on ubuntu.To work around this I added a
--libicuoption to the swift build script which will compile uplibicuusing the correctconfigureoptions to produce static versions of the libraries that dont requirelibdland put them inlib/swift_static/linuxand shared versions in thelib/swift/linux. This means that if building a binary distribution you could provide versions oflibicusuitable for static linking without worrying about how the distribution provided package has configured it.My intention with
utils/static-executable-args.lnkwas that it could be overridden by a package maintainer or built from a script at build time if building on a different OS eg android or FreeBSD, the static file is just there as an initial version for ubuntu.weissi commentedon Feb 2, 2017
@spevans, I do agree that
-ldlfor static binaries is not quite right (but linking glibc statically is so too). What about the following: If Swift is compiled with--libicuwe do not put-ldlinutils/static-executable-args.lnkbut if it's not we do (as it's as you say an initial version for Ubuntu).spevans commentedon Feb 3, 2017
You are correct that if Swift is compiled with
--libicuthen-ldlshould not be added. The easiest way to do this is probably to have a script likeutils/gen-static-stdlib-link-argsthat does the same forstatic-executable-args.lnk(or expandutils/gen-static-stdlib-link-argsto generate both). This is called from https://github.com/apple/swift/blob/master/lib/Driver/CMakeLists.txt#L24 and has knowledge of whether--libicuwas used. This script can also be expanded in the future to handle other ELF platforms that may have different library requirements.If the only reason to create static binaries using
-ldlis to get thelibicustatically linked into the executable then the-static-stdliboption should already do this on ubuntu as thestatic-stdlib-args.lnkfile contains full paths to the ICU .a library filesThe alternative is to not copy/create the
static-executable-args.lnkfile at all when--libicuhas not been used.swiftcsees the lack oflib/swift_static/linux/static-executable-args.lnkas an indication that static linking isnt supported and gives the following error:This is probably the correct action to take when libicu hasnt been built to be fully static.
swift-ci commentedon Feb 27, 2018
Comment by jamlee (JIRA)
this problem is up to me.
MaxDesiatov commentedon Nov 4, 2018
After #19860 has been merged, can this issue be closed now?
spevans commentedon Nov 4, 2018
No, it requires the fix in #15183 which just needs an update for PR comments but otherwise works:
weissi commentedon Nov 8, 2018
@spevans cool, thanks! Mind checking
nm test | grep ' dl'? The bug was about us needed the static version of libdl (yes, that's a thing). I added that in #7188 and it's still there:https://github.com/apple/swift/blob/master/utils/static-executable-args.lnk#L15
So if this is truly fixed, then we should remove
-ldlfromutils/static-executable-args.lnkagain and then we can close this issue.spevans commentedon Nov 8, 2018
Ok, firstly libICU is now compiled without use of
dlopen()Note however that glibc still has its own internal dl* functions but none of the
-ldlonesweissi commentedon Nov 8, 2018
@spevans awesome, thanks! Yes, the glibc ones we can't fix without dropping glibc which is a bigger task not for today. Should we then just remove
-ldlfromutils/static-executable-args.lnk?spevans commentedon Nov 8, 2018
Removing
-ldlis done as part of #15183 which fixes up-static-executableto work correctly (its currently broken inmaster) and also adds a regression test to catch future breakage.finagolfin commentedon Jan 16, 2025
@weissi, can this be closed now that the linux toolchain no longer uses separate ICU libraries?
weissi commentedon Jan 16, 2025
@finagolfin
FoundationXMLstill needs libxml2 and libicu, not sure if that counts?but when I install
libxml2againwhich then makes
./testwork.tl;dr: The toolchains still requires libicu in certain situations. Not sure about
FoundationInternationalization.finagolfin commentedon Jan 16, 2025
OK, but this issue isn't whether FoundationXML needs libicu, but whether building on linux generally works. For that niche case of linking FoundationXML dynamically, you say it's working, whereas I don't think we support building FoundationXML with static executables.
So where's the problem now?
weissi commentedon Jan 16, 2025
That's fair. Let's close.