Skip to content

Commit 5c993b4

Browse files
committed
[runtime] Remove swift_addNewDSOImage() call from static ELF binaries
- Create separate swift_begin.o/swift_end.o for lib/swift and lib/swift_static. The static swift_begin.o does not call swift_addNewDSOImage() at startup. - Update ToolChains.cpp to use the correct swift_begin.o/swift_end.o files for the `-static-stdlib` and `-static-executable` options.
1 parent 30436f4 commit 5c993b4

File tree

3 files changed

+80
-25
lines changed

3 files changed

+80
-25
lines changed

lib/Driver/ToolChains.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,20 +1390,49 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
13901390
Arguments.push_back(context.Args.MakeArgString(Target));
13911391
}
13921392

1393+
bool staticExecutable = false;
1394+
bool staticStdlib = false;
1395+
1396+
if (context.Args.hasFlag(options::OPT_static_executable,
1397+
options::OPT_no_static_executable,
1398+
false)) {
1399+
staticExecutable = true;
1400+
} else if (context.Args.hasFlag(options::OPT_static_stdlib,
1401+
options::OPT_no_static_stdlib,
1402+
false)) {
1403+
staticStdlib = true;
1404+
}
1405+
1406+
SmallString<128> SharedRuntimeLibPath;
1407+
SmallString<128> StaticRuntimeLibPath;
1408+
// Path to swift_begin.o and swift_end.o.
1409+
SmallString<128> ObjectLibPath;
1410+
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, *this);
1411+
1412+
// -static-stdlib uses the static lib path for libswiftCore but
1413+
// the shared lib path for swift_begin.o and swift_end.o.
1414+
if (staticExecutable || staticStdlib) {
1415+
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1416+
}
1417+
1418+
if (staticExecutable) {
1419+
ObjectLibPath = StaticRuntimeLibPath;
1420+
} else {
1421+
ObjectLibPath = SharedRuntimeLibPath;
1422+
}
1423+
13931424
// Add the runtime library link path, which is platform-specific and found
13941425
// relative to the compiler.
1395-
llvm::SmallString<128> RuntimeLibPath;
1396-
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this);
1397-
if (shouldProvideRPathToLinker()) {
1426+
if (!staticExecutable && shouldProvideRPathToLinker()) {
13981427
// FIXME: We probably shouldn't be adding an rpath here unless we know
13991428
// ahead of time the standard library won't be copied.
14001429
Arguments.push_back("-Xlinker");
14011430
Arguments.push_back("-rpath");
14021431
Arguments.push_back("-Xlinker");
1403-
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1432+
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
14041433
}
14051434

1406-
auto PreInputObjectPath = getPreInputObjectPath(RuntimeLibPath);
1435+
auto PreInputObjectPath = getPreInputObjectPath(ObjectLibPath);
14071436
if (!PreInputObjectPath.empty()) {
14081437
Arguments.push_back(context.Args.MakeArgString(PreInputObjectPath));
14091438
}
@@ -1421,11 +1450,8 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
14211450

14221451
// Link the standard library.
14231452
Arguments.push_back("-L");
1424-
if (context.Args.hasFlag(options::OPT_static_executable,
1425-
options::OPT_no_static_executable,
1426-
false)) {
1427-
SmallString<128> StaticRuntimeLibPath;
1428-
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1453+
1454+
if (staticExecutable) {
14291455
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
14301456

14311457
SmallString<128> linkFilePath = StaticRuntimeLibPath;
@@ -1438,11 +1464,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
14381464
llvm::report_fatal_error("-static-executable not supported on this platform");
14391465
}
14401466
}
1441-
else if (context.Args.hasFlag(options::OPT_static_stdlib,
1442-
options::OPT_no_static_stdlib,
1443-
false)) {
1444-
SmallString<128> StaticRuntimeLibPath;
1445-
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1467+
else if (staticStdlib) {
14461468
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));
14471469

14481470
SmallString<128> linkFilePath = StaticRuntimeLibPath;
@@ -1455,7 +1477,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
14551477
}
14561478
}
14571479
else {
1458-
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
1480+
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
14591481
Arguments.push_back("-lswiftCore");
14601482
}
14611483

@@ -1464,7 +1486,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
14641486
Arguments.push_back(context.Args.MakeArgString("--target=" + getTriple().str()));
14651487

14661488
if (context.Args.hasArg(options::OPT_profile_generate)) {
1467-
SmallString<128> LibProfile(RuntimeLibPath);
1489+
SmallString<128> LibProfile(SharedRuntimeLibPath);
14681490
llvm::sys::path::remove_filename(LibProfile); // remove platform name
14691491
llvm::sys::path::append(LibProfile, "clang", "lib");
14701492

@@ -1488,7 +1510,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
14881510

14891511
// Just before the output option, allow GenericUnix toolchains to add
14901512
// additional inputs.
1491-
auto PostInputObjectPath = getPostInputObjectPath(RuntimeLibPath);
1513+
auto PostInputObjectPath = getPostInputObjectPath(ObjectLibPath);
14921514
if (!PostInputObjectPath.empty()) {
14931515
Arguments.push_back(context.Args.MakeArgString(PostInputObjectPath));
14941516
}

stdlib/public/runtime/CMakeLists.txt

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,46 @@ foreach(sdk ${ELFISH_SDKS})
193193
"${section_magic_${arch_suffix}_end_object}")
194194

195195
swift_install_in_component(stdlib
196-
FILES "${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o" "${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
197-
DESTINATION "lib/swift/${arch_subdir}")
196+
FILES
197+
"${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
198+
"${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
199+
DESTINATION
200+
"lib/swift/${arch_subdir}")
201+
202+
if(SWIFT_BUILD_STATIC_STDLIB)
203+
# Static lib versions of swift_begin.o and swift_end.o
204+
add_custom_command_target(static_section_magic_${arch_suffix}_begin_object
205+
COMMAND
206+
"${CMAKE_COMMAND}" -E copy
207+
"${section_magic_begin_obj}"
208+
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_begin.o"
209+
OUTPUT
210+
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_begin.o"
211+
DEPENDS
212+
"${section_magic_begin_obj}")
213+
214+
add_custom_command_target(static_section_magic_${arch_suffix}_end_object
215+
COMMAND
216+
"${CMAKE_COMMAND}" -E copy
217+
"${section_magic_end_obj}"
218+
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_end.o"
219+
OUTPUT
220+
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_end.o"
221+
DEPENDS
222+
"${section_magic_end_obj}")
223+
224+
list(APPEND object_target_list
225+
"${static_section_magic_${arch_suffix}_begin_object}"
226+
"${static_section_magic_${arch_suffix}_end_object}")
227+
228+
swift_install_in_component(stdlib
229+
FILES
230+
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_begin.o"
231+
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_end.o"
232+
DESTINATION
233+
"lib/swift_static/${arch_subdir}")
234+
endif()
235+
198236
endforeach()
199237
endforeach()
200238

stdlib/public/runtime/ImageInspectionStatic.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ extern const Section typeMetadataStart asm(".swift2_type_metadata_start");
2828

2929
using namespace swift;
3030

31-
// Called from ImageInspectionInit
32-
void
33-
swift::addNewDSOImage(const void *addr) {
34-
}
35-
3631
static SectionInfo
3732
getSectionInfo(const Section *section) {
3833
SectionInfo info;

0 commit comments

Comments
 (0)