Skip to content

Restore -static-executable on Linux. #15183

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

Closed
wants to merge 2 commits into from

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Mar 12, 2018

  • Build libswiftImageInspectionStatic.a from
    ImageInspectionELF.cpp and StaticBinaryELF.cpp

  • Update static-executable-args.lnk and replace
    libswiftImageInspectionShared with libswiftImageInspectionStatic.

This fixes -static-executable on Linux to build a static binary (when linked with appropiate libicu .a libs`

@spevans spevans requested a review from compnerd March 12, 2018 19:14
@compnerd
Copy link
Member

Hmm, why not leave this as is, and just modify the swiftrt.o to have a static version which doesn't invoke the register method? That way, this can fully be reincorporated into the runtime, and the driver can make the appropriate choice.

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment, I suppose that the notification might work better this way.

@spevans
Copy link
Contributor Author

spevans commented Mar 23, 2018

@compnerd Sorry for the delay in replying, are you suggesting something like create a swiftrt-static.o which doesn't have the call to swift_addNewDSOImage and then modify lib/Driver/ToolChains.cpp to link that version instead, when a static executable is built?

@spevans spevans force-pushed the pr_static_executable_fix branch from 9026cf6 to 176f2ad Compare March 24, 2018 20:35
@compnerd
Copy link
Member

@spevans, yes, effectively that. However, thinking more about this, we can be even more clever about this. On the static builds, you still need to link to the (static) runtime. The static runtime can include the object file, and we can create a reference to it, causing the linker to preserve the object from the runtime itself and avoid having to have the driver push the file into the actual linker invocation.

@spevans
Copy link
Contributor Author

spevans commented Mar 28, 2018

@compnerd In the most recent version I created ImageInspectionStatic-ELF.cpp which is effectively SwiftRT-ELF.cpp with the constructor and swift_addNewDSOImage removed. I also removed linking SwiftRT.o from lib/Driver/Toolchains.cpp for static executables. I think this matches what you are suggesting.

@compnerd
Copy link
Member

While functionally equivalent, this introduces yet another copy of the table of sections. It is another place where we need to update that list. If we compile the single file with a macro we can get away with a single place for all of ELF, which I think is better.

@compnerd compnerd dismissed their stale review March 31, 2018 23:43

Addresses the functional change, stylistic changes remain

@compnerd
Copy link
Member

@swift-ci please test

@spevans spevans force-pushed the pr_static_executable_fix branch from 43f9658 to e9774ac Compare April 2, 2018 20:09
@spevans
Copy link
Contributor Author

spevans commented Apr 3, 2018

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Apr 3, 2018

Build failed
Swift Test OS X Platform
Git Sha - 43f9658447a208e537f511c70a8e8bd5d7e26336

@swift-ci
Copy link
Contributor

swift-ci commented Apr 3, 2018

Build failed
Swift Test Linux Platform
Git Sha - 43f9658447a208e537f511c70a8e8bd5d7e26336

}
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this just a duplication of the swift_addNewDSOImage? Why not just use that?

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it might be possible to reduce some duplication of the code which would be better.

@spevans spevans force-pushed the pr_static_executable_fix branch 2 times, most recently from cea1288 to b5d0def Compare November 7, 2018 23:46
@spevans
Copy link
Contributor Author

spevans commented Nov 7, 2018

@swift-ci test linux

@swift-ci
Copy link
Contributor

swift-ci commented Nov 8, 2018

Build failed
Swift Test Linux Platform
Git Sha - b5d0def2dbada20c9a4b188ede1124a4af7815f9

@spevans
Copy link
Contributor Author

spevans commented Nov 8, 2018

@swift-ci test linux

@swift-ci
Copy link
Contributor

swift-ci commented Nov 8, 2018

Build failed
Swift Test Linux Platform
Git Sha - b5d0def2dbada20c9a4b188ede1124a4af7815f9

@spevans spevans force-pushed the pr_static_executable_fix branch from b5d0def to 1d2ea19 Compare November 9, 2018 17:07
@spevans
Copy link
Contributor Author

spevans commented Nov 9, 2018

@swift-ci test linux

@spevans
Copy link
Contributor Author

spevans commented Nov 10, 2018

@swift-ci Please smoke test OS X platform

@spevans
Copy link
Contributor Author

spevans commented Nov 12, 2018

@compnerd I know this PR is quite old but I have updated it including a test and removed the duplicate sections I added in SwfitRT-ELF.cpp. Could you take another look at it please?

@aemino
Copy link

aemino commented Dec 22, 2018

Any updates on this PR?

@frol
Copy link

frol commented Apr 7, 2019

I have bumped into this issue and filed a bug: https://bugs.swift.org/browse/SR-10328.

@spevans
Copy link
Contributor Author

spevans commented Apr 8, 2019

@frol Unfortunately this PR still has some issues on Ubuntu18.04, relating to glibc and pthreads so it still needs some more work

@jckarter
Copy link
Contributor

jckarter commented Apr 8, 2019

While functionally equivalent, this introduces yet another copy of the table of sections. It is another place where we need to update that list. If we compile the single file with a macro we can get away with a single place for all of ELF, which I think is better.

As I think you were alluding to, in a fully-static binary there really isn't any need for a table of sections at all, since we ought to be able to link the runtime directly against the beginnings and ends of the sections from the main program.

@miguelangel-dev
Copy link

Just a quick question, I was using -static-executable in macox, with sdk iphonesimulator and target x86_64-apple-ios12.2-simulator- it stops to work (I think from Swift 5) and it throws the next error:

llvm error: -static-executable is not supported on darwin

This PR will solve this error?

@spevans
Copy link
Contributor Author

spevans commented May 29, 2019

Apple platforms do not support statically linked executables, see https://developer.apple.com/library/content/qa/qa1118/_index.html

spevans added 2 commits May 31, 2019 09:02
- Build libswiftImageInspectionStatic.a from
  SwiftRT-ELF.cpp and StaticBinaryELF.cpp

- Update static-executable-args.lnk and replace
  libswiftImageInspectionShared with libswiftImageInspectionStatic.

- Add driver tests for '-static-executable' on Linux to validate
  that output binary is statically linked.

- Fix linkage to pthreads and SwiftRT-ELF.o in a static executable
- If an address does not fall inside a symbol's region then return the
  symbol with the smallest gap between then end of the symbol's region
  and the address.
@spevans spevans force-pushed the pr_static_executable_fix branch from 1d2ea19 to c77d3a6 Compare May 31, 2019 08:30
@spevans
Copy link
Contributor Author

spevans commented May 31, 2019

Please test with following PR:

swiftlang/swift-integration-tests#59
@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 1d2ea193d1ed54bd80141200452106d8720699ee

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 1d2ea193d1ed54bd80141200452106d8720699ee

@spevans
Copy link
Contributor Author

spevans commented May 31, 2019

Please test with following PR:

swiftlang/swift-integration-tests#59
@swift-ci test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - c77d3a6

@spevans
Copy link
Contributor Author

spevans commented May 31, 2019

Please test with following PR:

swiftlang/swift-integration-tests#59
@swift-ci test linux

@CodaFi
Copy link
Contributor

CodaFi commented Dec 2, 2019

@spevans It's been a while and this has gathered quite a few conflicts. Is this still something you're interested in pursuing?

@aemino
Copy link

aemino commented Dec 2, 2019

Would restoring the static executable flag for Linux allow statically-linked toolchains on Linux? Because if so, I think that would be incredibly helpful for producing cross-distro compatible Linux toolchains.

@spevans
Copy link
Contributor Author

spevans commented Dec 2, 2019

@CodaFi I'll close this off for now, I need to get --static-stdlib working with Foundation first anyway and this PR had issues on Ubutnu18.04 if I recall (it worked ok on 16.04)

@aemino This PR was for producing statically linked binaries as the output of swiftc not actually building statically linked versions of the compiler itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants