Skip to content

Commit b8b7b5f

Browse files
authored
Merge pull request #6097 from spevans/pr_solib_constructor
[runtime] Binary section data loading for extra ELF images
2 parents fd91396 + 7d2a9aa commit b8b7b5f

File tree

7 files changed

+285
-78
lines changed

7 files changed

+285
-78
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: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ if(SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
1818
set(swift_runtime_leaks_sources Leaks.mm)
1919
endif()
2020

21+
set(section_magic_compile_flags ${swift_runtime_compile_flags})
22+
2123
list(APPEND swift_runtime_compile_flags
2224
"-D__SWIFT_CURRENT_DYLIB=swiftCore")
2325

@@ -60,6 +62,7 @@ set(LLVM_OPTIONAL_SOURCES
6062
MutexPThread.cpp
6163
MutexWin32.cpp
6264
CygwinPort.cpp
65+
ImageInspectionInit.cpp
6366
ImageInspectionELF.cpp
6467
ImageInspectionStatic.cpp
6568
StaticBinaryELF.cpp
@@ -129,15 +132,21 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS})
129132
endif()
130133
endforeach()
131134

135+
add_swift_library(section_magic_loader OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
136+
ImageInspectionInit.cpp
137+
C_COMPILE_FLAGS ${section_magic_compile_flags}
138+
TARGET_SDKS "${ELFISH_SDKS}"
139+
LINK_FLAGS ${swift_runtime_linker_flags}
140+
INSTALL_IN_COMPONENT never_install)
132141
add_swift_library(section_magic_begin OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
133142
swift_sections.S
134-
C_COMPILE_FLAGS ${swift_runtime_compile_flags} "-DSWIFT_BEGIN"
143+
C_COMPILE_FLAGS ${section_magic_compile_flags} "-DSWIFT_BEGIN"
135144
TARGET_SDKS "${ELFISH_SDKS}"
136145
LINK_FLAGS ${swift_runtime_linker_flags}
137146
INSTALL_IN_COMPONENT never_install)
138147
add_swift_library(section_magic_end OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
139148
swift_sections.S
140-
C_COMPILE_FLAGS ${swift_runtime_compile_flags} "-DSWIFT_END"
149+
C_COMPILE_FLAGS ${section_magic_compile_flags} "-DSWIFT_END"
141150
LINK_FLAGS ${swift_runtime_linker_flags}
142151
TARGET_SDKS "${ELFISH_SDKS}"
143152
INSTALL_IN_COMPONENT never_install)
@@ -148,30 +157,82 @@ foreach(sdk ${ELFISH_SDKS})
148157
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
149158
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")
150159

151-
set(section_magic_begin_name "section_magic_begin-${arch_suffix}")
152-
set(section_magic_end_name "section_magic_end-${arch_suffix}")
160+
set(section_magic_loader_obj "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/section_magic_loader-${arch_suffix}.dir/ImageInspectionInit.cpp${CMAKE_C_OUTPUT_EXTENSION}")
161+
set(section_magic_begin_obj "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/section_magic_begin-${arch_suffix}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}")
162+
set(section_magic_end_obj "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/section_magic_end-${arch_suffix}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}")
163+
164+
if(SWIFT_ENABLE_GOLD_LINKER)
165+
set(LD_COMMAND "gold")
166+
else()
167+
set(LD_COMMAND "ld")
168+
endif()
153169

154-
add_custom_command_target(section_magic_${arch_suffix}_objects
170+
add_custom_command_target(section_magic_${arch_suffix}_begin_object
155171
COMMAND
156-
"${CMAKE_COMMAND}" -E copy
157-
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${section_magic_begin_name}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}"
172+
# Merge ImageInspectionInit.o + swift_sections.S(BEGIN) => swift_begin.o
173+
${LD_COMMAND} -r -o "${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
174+
"${section_magic_begin_obj}" "${section_magic_loader_obj}"
175+
OUTPUT
158176
"${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
177+
DEPENDS
178+
"${section_magic_begin_obj}"
179+
"${section_magic_loader_obj}")
180+
181+
add_custom_command_target(section_magic_${arch_suffix}_end_object
159182
COMMAND
160183
"${CMAKE_COMMAND}" -E copy
161-
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${section_magic_end_name}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}"
184+
"${section_magic_end_obj}"
162185
"${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
163186
OUTPUT
164-
"${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
165187
"${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
166188
DEPENDS
167-
${section_magic_begin_name}
168-
${section_magic_end_name})
189+
"${section_magic_end_obj}")
169190

170-
list(APPEND object_target_list "${section_magic_${arch_suffix}_objects}")
191+
list(APPEND object_target_list
192+
"${section_magic_${arch_suffix}_begin_object}"
193+
"${section_magic_${arch_suffix}_end_object}")
171194

172195
swift_install_in_component(stdlib
173-
FILES "${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o" "${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
174-
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+
175236
endforeach()
176237
endforeach()
177238

stdlib/public/runtime/ImageInspection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SWIFT_RUNTIME_IMAGE_INSPECTION_H
2020
#define SWIFT_RUNTIME_IMAGE_INSPECTION_H
2121

22+
#include "ImageInspectionELF.h"
2223
#include <cstdint>
2324

2425
namespace swift {

0 commit comments

Comments
 (0)