Skip to content

Commit 3b25795

Browse files
committed
Restore -static-executable on Linux.
- Add back StaticBinaryELF.cpp which provides swift::lookupSymbol() for statically linked ELF binaries. - Build libswiftImageInspectionStatic.a from StaticBinaryELF.cpp - Update static-executable-args.lnk and replace libswiftImageInspectionShared with libswiftImageInspectionStatic. - Fix linkage to pthreads to avoid weak linkage issues in a static executable. - Add driver test for '-static-executable' on Linux to validate that output binary is statically linked.
1 parent b3f5d61 commit 3b25795

File tree

6 files changed

+401
-51
lines changed

6 files changed

+401
-51
lines changed

lib/Driver/UnixToolChains.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,6 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
189189
Arguments.push_back(context.Args.MakeArgString(A->getValue()));
190190
}
191191

192-
if (getTriple().getOS() == llvm::Triple::Linux &&
193-
job.getKind() == LinkKind::Executable) {
194-
Arguments.push_back("-pie");
195-
}
196-
197192
bool staticExecutable = false;
198193
bool staticStdlib = false;
199194

@@ -205,6 +200,11 @@ toolchains::GenericUnix::constructInvocation(const DynamicLinkJobAction &job,
205200
staticStdlib = true;
206201
}
207202

203+
if (getTriple().getOS() == llvm::Triple::Linux &&
204+
job.getKind() == LinkKind::Executable && !staticExecutable) {
205+
Arguments.push_back("-pie");
206+
}
207+
208208
SmallVector<std::string, 4> RuntimeLibPaths;
209209
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath,
210210
/*Shared=*/!(staticExecutable || staticStdlib));

stdlib/public/runtime/CMakeLists.txt

+42-31
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ endif(LLVM_ENABLE_ASSERTIONS)
7878

7979
# Acknowledge that the following sources are known.
8080
set(LLVM_OPTIONAL_SOURCES
81+
StaticBinaryELF.cpp
8182
SwiftRT-COFF.cpp
8283
SwiftRT-ELF.cpp
8384
${swift_runtime_sources}
@@ -102,35 +103,43 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
102103
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
103104
INSTALL_IN_COMPONENT stdlib)
104105

105-
foreach(arch IN LISTS SWIFT_SDK_${sdk}_ARCHITECTURES)
106-
set(FragileSupportLibrary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch})
107-
set(LibraryLocation ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${arch})
108-
add_custom_command_target(swift_image_inspection_${arch}_static
109-
COMMAND
110-
"${CMAKE_COMMAND}" -E copy $<TARGET_FILE:${FragileSupportLibrary}> ${LibraryLocation}
111-
OUTPUT
112-
"${LibraryLocation}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}"
113-
DEPENDS
114-
${FragileSupportLibrary})
115-
add_dependencies(stdlib ${FragileSupportLibrary})
116-
swift_install_in_component(FILES $<TARGET_FILE:${FragileSupportLibrary}>
117-
DESTINATION "lib/swift_static/${lowercase_sdk}/${arch}"
118-
COMPONENT stdlib)
119-
endforeach()
106+
add_swift_target_library(swiftImageInspectionStatic TARGET_LIBRARY STATIC
107+
StaticBinaryELF.cpp ImageInspectionELF.cpp
108+
C_COMPILE_FLAGS ${swift_runtime_library_compile_flags} -DELF_STATIC_LIB
109+
LINK_FLAGS ${swift_runtime_linker_flags}
110+
INSTALL_IN_COMPONENT stdlib)
120111

121-
set(FragileSupportLibraryPrimary swiftImageInspectionShared-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFT_PRIMARY_VARIANT_ARCH})
122-
set(LibraryLocationPrimary ${SWIFTSTATICLIB_DIR}/${lowercase_sdk})
123-
add_custom_command_target(swift_image_inspection_static_primary_arch
124-
COMMAND
125-
"${CMAKE_COMMAND}" -E copy $<TARGET_FILE:${FragileSupportLibraryPrimary}> ${LibraryLocationPrimary}
126-
OUTPUT
127-
"${LibraryLocationPrimary}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspectionShared${CMAKE_STATIC_LIBRARY_SUFFIX}"
128-
DEPENDS
129-
${FragileSupportLibraryPrimary})
130-
add_dependencies(stdlib ${FragileSupportLibraryPrimary})
131-
swift_install_in_component(FILES $<TARGET_FILE:${FragileSupportLibraryPrimary}>
132-
DESTINATION "lib/swift_static/${lowercase_sdk}"
133-
COMPONENT stdlib)
112+
foreach(linktype Shared Static)
113+
foreach(arch IN LISTS SWIFT_SDK_${sdk}_ARCHITECTURES)
114+
set(FragileSupportLibrary swiftImageInspection${linktype}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch})
115+
set(LibraryLocation ${SWIFTSTATICLIB_DIR}/${lowercase_sdk}/${arch})
116+
add_custom_command_target(swift_image_inspection_${linktype}_${arch}_static
117+
COMMAND
118+
"${CMAKE_COMMAND}" -E copy $<TARGET_FILE:${FragileSupportLibrary}> ${LibraryLocation}
119+
OUTPUT
120+
"${LibraryLocation}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspection${linktype}${CMAKE_STATIC_LIBRARY_SUFFIX}"
121+
DEPENDS
122+
${FragileSupportLibrary})
123+
add_dependencies(stdlib ${FragileSupportLibrary})
124+
swift_install_in_component(FILES $<TARGET_FILE:${FragileSupportLibrary}>
125+
DESTINATION "lib/swift_static/${lowercase_sdk}/${arch}"
126+
COMPONENT stdlib)
127+
endforeach()
128+
129+
set(FragileSupportLibraryPrimary swiftImageInspection${linktype}-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFT_PRIMARY_VARIANT_ARCH})
130+
set(LibraryLocationPrimary ${SWIFTSTATICLIB_DIR}/${lowercase_sdk})
131+
add_custom_command_target(swift_image_inspection_${linktype}_primary_arch
132+
COMMAND
133+
"${CMAKE_COMMAND}" -E copy $<TARGET_FILE:${FragileSupportLibraryPrimary}> ${LibraryLocationPrimary}
134+
OUTPUT
135+
"${LibraryLocationPrimary}/${CMAKE_STATIC_LIBRARY_PREFIX}swiftImageInspection${linktype}${CMAKE_STATIC_LIBRARY_SUFFIX}"
136+
DEPENDS
137+
${FragileSupportLibraryPrimary})
138+
add_dependencies(stdlib ${FragileSupportLibraryPrimary})
139+
swift_install_in_component(FILES $<TARGET_FILE:${FragileSupportLibraryPrimary}>
140+
DESTINATION "lib/swift_static/${lowercase_sdk}"
141+
COMPONENT stdlib)
142+
endforeach()
134143

135144
# Generate the static-executable-args.lnk file used for ELF systems (eg linux)
136145
set(linkfile "${lowercase_sdk}/static-executable-args.lnk")
@@ -149,10 +158,12 @@ if(SWIFT_BUILD_STATIC_STDLIB AND "${sdk}" STREQUAL "LINUX")
149158
DESTINATION "lib/swift_static/${lowercase_sdk}"
150159
COMPONENT stdlib)
151160
add_custom_target(static_binary_magic ALL DEPENDS ${static_binary_lnk_file_list})
152-
foreach(arch IN LISTS SWIFT_SDK_LINUX_ARCHITECTURES)
153-
add_dependencies(static_binary_magic ${swift_image_inspection_${arch}_static})
161+
foreach(linktype Shared Static)
162+
foreach(arch IN LISTS SWIFT_SDK_LINUX_ARCHITECTURES)
163+
add_dependencies(static_binary_magic ${swift_image_inspection_${linktype}_${arch}_static})
164+
endforeach()
165+
add_dependencies(static_binary_magic ${swift_image_inspection_${linktype}_primary_arch})
154166
endforeach()
155-
add_dependencies(static_binary_magic ${swift_image_inspection_static_primary_arch})
156167
add_dependencies(stdlib static_binary_magic)
157168

158169
add_swift_target_library(swiftImageInspectionSharedObject OBJECT_LIBRARY

stdlib/public/runtime/ImageInspectionELF.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222

2323
#include "ImageInspection.h"
2424
#include "ImageInspectionELF.h"
25+
26+
#ifndef ELF_STATIC_LIB
2527
#include <dlfcn.h>
28+
#endif
2629

2730
using namespace swift;
2831

@@ -56,6 +59,7 @@ void swift::initializeProtocolLookup() {
5659
sections = sections->next;
5760
}
5861
}
62+
5963
void swift::initializeProtocolConformanceLookup() {
6064
const swift::MetadataSections *sections = registered;
6165
while (true) {
@@ -89,10 +93,8 @@ void swift::initializeTypeMetadataRecordLookup() {
8993
void swift::initializeDynamicReplacementLookup() {
9094
}
9195

92-
// As ELF images are loaded, ImageInspectionInit:sectionDataInit() will call
93-
// addNewDSOImage() with an address in the image that can later be used via
94-
// dladdr() to dlopen() the image after the appropriate initialize*Lookup()
95-
// function has been called.
96+
// As ELF images are loaded, SwiftRT-ELF:swift_image_constructor() will call
97+
// addNewDSOImage() with a pointer to the MetadataSections in the image.
9698
SWIFT_RUNTIME_EXPORT
9799
void swift_addNewDSOImage(const void *addr) {
98100
const swift::MetadataSections *sections =
@@ -131,6 +133,12 @@ void swift_addNewDSOImage(const void *addr) {
131133
}
132134
}
133135

136+
#ifndef ELF_STATIC_LIB
137+
138+
// For shared executables only, static executables use the version defined in
139+
// StaticBinaryELF.cpp.
140+
// libswiftImageInspectionShared.a contains the below function and
141+
// libswiftImageInspectionStatic.a contains the version in StaticBinaryELF.cpp
134142
int swift::lookupSymbol(const void *address, SymbolInfo *info) {
135143
Dl_info dlinfo;
136144
if (dladdr(address, &dlinfo) == 0) {
@@ -144,6 +152,8 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
144152
return 1;
145153
}
146154

155+
#endif
156+
147157
// This is only used for backward deployment hooks, which we currently only support for
148158
// MachO. Add a stub here to make sure it still compiles.
149159
void *swift::lookupSection(const char *segment, const char *section, size_t *outSize) {

0 commit comments

Comments
 (0)