Uploaded image for project: 'Swift'
  1. Swift
  2. SR-14536

Importing FoundationNetworking with -static-stdlib is broken with missing symbols on Linux

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler, Foundation
    • Labels:
      None
    • Environment:

      Ubuntu 20.04.2 LTS
      Swift version 5.4 (swift-5.4-RELEASE)
      Target: x86_64-unknown-linux-gnu

      Description

      import FoundationNetworking
      _ = URLSession.shared
      

      This code couldn't be built with -static-stdlib due to linking failure on Linux.

      This issue was introduced by the use of @_implementationOnly in swift-corelibs-foundation. FoundationNetworking shipped with Swift 5.4 imports CFURLSessionInterface with @_implementationOnly, then FoundationNetworking.swiftmodule doesn't have a dependency declaration of CFURLSessionInterface. So "import FoundationNetworking" doesn't tell the driver to link CFURLSessionInterface and CFURLSessionInterface dependent libraries.

      Here is my forum post about this issue several month ago.
      Autolinking behavior of @_implementationOnly with static linking

      Here is the command line log.

      $ swiftc -static-stdlib main.swift -v
      /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/bin/swift-frontend -frontend -c -primary-file main.swift -target x86_64-unknown-linux-gnu -disable-objc-interop -module-name main -o /tmp/main-980558.o -use-static-resource-dir
      /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/bin/swift-autolink-extract /tmp/main-980558.o -o /tmp/main-9e4f94.autolink
      /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/bin/clang -fuse-ld=gold -pie /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/lib/swift/linux/x86_64/swiftrt.o /tmp/main-980558.o -Xlinker --start-group @/tmp/main-9e4f94.autolink -Xlinker --end-group -L /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/lib/swift_static/linux @/home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/lib/swift_static/linux/static-stdlib-args.lnk --target=x86_64-unknown-linux-gnu -v -o main
      clang version 10.0.0 (git@github.com:apple/llvm-project.git 159d0efd5633014e890836a4227fb867b491eb6b)
      Target: x86_64-unknown-linux-gnu
      Thread model: posix
      InstalledDir: /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/bin
      Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
      Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
      Candidate multilib: .;@m64
      Selected multilib: .;@m64
       "/usr/bin/ld.gold" -pie -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/lib/swift_static/linux -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/lib/x86_64-linux-gnu/../../lib64 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. -L/home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/bin/../lib -L/lib -L/usr/lib /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/lib/swift/linux/x86_64/swiftrt.o /tmp/main-980558.o --start-group -lFoundationNetworking -lswift_Concurrency -lswiftCore -lFoundation -lswiftGlibc -lm -lpthread -lutil -ldl -lswiftDispatch -ldispatch -lBlocksRuntime -lDispatchStubs -lCoreFoundation -luuid -lswiftSwiftOnoneSupport --end-group -ldl -lpthread -lswiftCore -licui18nswift -licuucswift -licudataswift -lstdc++ -lm -export-dynamic --exclude-libs ALL -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crtn.o
      /home/katei/.swiftenv/versions/DEVELOPMENT-SNAPSHOT-2021-04-24-a/usr/lib/swift_static/linux/libFoundationNetworking.a(MultiHandle.swift.o):MultiHandle.swift.o:function $s20FoundationNetworking10URLSessionC12_MultiHandleC03rawE0Svvpfi: error: undefined reference to 'CFURLSessionMultiHandleInit'
      ... // and many undefined reference errors for CoreFoundation symbols
      

      For now, we can link correctly with extra "-lCFURLSessionInterface -lcurl" options.

        Attachments

          Activity

            People

            Assignee:
            yuta_saito Yuta Saito
            Reporter:
            yuta_saito Yuta Saito
            Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: