Skip to content
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

[runtime] Binary section data loading for extra ELF images #6097

Merged
merged 4 commits into from Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 40 additions & 18 deletions lib/Driver/ToolChains.cpp
Expand Up @@ -1390,20 +1390,49 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back(context.Args.MakeArgString(Target));
}

bool staticExecutable = false;
bool staticStdlib = false;

if (context.Args.hasFlag(options::OPT_static_executable,
options::OPT_no_static_executable,
false)) {
staticExecutable = true;
} else if (context.Args.hasFlag(options::OPT_static_stdlib,
options::OPT_no_static_stdlib,
false)) {
staticStdlib = true;
}

SmallString<128> SharedRuntimeLibPath;
SmallString<128> StaticRuntimeLibPath;
// Path to swift_begin.o and swift_end.o.
SmallString<128> ObjectLibPath;
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, *this);

// -static-stdlib uses the static lib path for libswiftCore but
// the shared lib path for swift_begin.o and swift_end.o.
if (staticExecutable || staticStdlib) {
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
}

if (staticExecutable) {
ObjectLibPath = StaticRuntimeLibPath;
} else {
ObjectLibPath = SharedRuntimeLibPath;
}

// Add the runtime library link path, which is platform-specific and found
// relative to the compiler.
llvm::SmallString<128> RuntimeLibPath;
getRuntimeLibraryPath(RuntimeLibPath, context.Args, *this);
if (shouldProvideRPathToLinker()) {
if (!staticExecutable && shouldProvideRPathToLinker()) {
// FIXME: We probably shouldn't be adding an rpath here unless we know
// ahead of time the standard library won't be copied.
Arguments.push_back("-Xlinker");
Arguments.push_back("-rpath");
Arguments.push_back("-Xlinker");
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
}

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

// Link the standard library.
Arguments.push_back("-L");
if (context.Args.hasFlag(options::OPT_static_executable,
options::OPT_no_static_executable,
false)) {
SmallString<128> StaticRuntimeLibPath;
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);

if (staticExecutable) {
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));

SmallString<128> linkFilePath = StaticRuntimeLibPath;
Expand All @@ -1438,11 +1464,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
llvm::report_fatal_error("-static-executable not supported on this platform");
}
}
else if (context.Args.hasFlag(options::OPT_static_stdlib,
options::OPT_no_static_stdlib,
false)) {
SmallString<128> StaticRuntimeLibPath;
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
else if (staticStdlib) {
Arguments.push_back(context.Args.MakeArgString(StaticRuntimeLibPath));

SmallString<128> linkFilePath = StaticRuntimeLibPath;
Expand All @@ -1455,7 +1477,7 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
}
}
else {
Arguments.push_back(context.Args.MakeArgString(RuntimeLibPath));
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
Arguments.push_back("-lswiftCore");
}

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

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

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

// Just before the output option, allow GenericUnix toolchains to add
// additional inputs.
auto PostInputObjectPath = getPostInputObjectPath(RuntimeLibPath);
auto PostInputObjectPath = getPostInputObjectPath(ObjectLibPath);
if (!PostInputObjectPath.empty()) {
Arguments.push_back(context.Args.MakeArgString(PostInputObjectPath));
}
Expand Down
89 changes: 75 additions & 14 deletions stdlib/public/runtime/CMakeLists.txt
Expand Up @@ -18,6 +18,8 @@ if(SWIFT_RUNTIME_ENABLE_LEAK_CHECKER)
set(swift_runtime_leaks_sources Leaks.mm)
endif()

set(section_magic_compile_flags ${swift_runtime_compile_flags})

list(APPEND swift_runtime_compile_flags
"-D__SWIFT_CURRENT_DYLIB=swiftCore")

Expand Down Expand Up @@ -60,6 +62,7 @@ set(LLVM_OPTIONAL_SOURCES
MutexPThread.cpp
MutexWin32.cpp
CygwinPort.cpp
ImageInspectionInit.cpp
ImageInspectionELF.cpp
ImageInspectionStatic.cpp
StaticBinaryELF.cpp
Expand Down Expand Up @@ -129,15 +132,21 @@ foreach(sdk ${SWIFT_CONFIGURED_SDKS})
endif()
endforeach()

add_swift_library(section_magic_loader OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
ImageInspectionInit.cpp
C_COMPILE_FLAGS ${section_magic_compile_flags}
TARGET_SDKS "${ELFISH_SDKS}"
LINK_FLAGS ${swift_runtime_linker_flags}
INSTALL_IN_COMPONENT never_install)
add_swift_library(section_magic_begin OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
swift_sections.S
C_COMPILE_FLAGS ${swift_runtime_compile_flags} "-DSWIFT_BEGIN"
C_COMPILE_FLAGS ${section_magic_compile_flags} "-DSWIFT_BEGIN"
TARGET_SDKS "${ELFISH_SDKS}"
LINK_FLAGS ${swift_runtime_linker_flags}
INSTALL_IN_COMPONENT never_install)
add_swift_library(section_magic_end OBJECT_LIBRARY IS_STDLIB IS_STDLIB_CORE
swift_sections.S
C_COMPILE_FLAGS ${swift_runtime_compile_flags} "-DSWIFT_END"
C_COMPILE_FLAGS ${section_magic_compile_flags} "-DSWIFT_END"
LINK_FLAGS ${swift_runtime_linker_flags}
TARGET_SDKS "${ELFISH_SDKS}"
INSTALL_IN_COMPONENT never_install)
Expand All @@ -148,30 +157,82 @@ foreach(sdk ${ELFISH_SDKS})
set(arch_subdir "${SWIFT_SDK_${sdk}_LIB_SUBDIR}/${arch}")
set(arch_suffix "${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${arch}")

set(section_magic_begin_name "section_magic_begin-${arch_suffix}")
set(section_magic_end_name "section_magic_end-${arch_suffix}")
set(section_magic_loader_obj "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/section_magic_loader-${arch_suffix}.dir/ImageInspectionInit.cpp${CMAKE_C_OUTPUT_EXTENSION}")
set(section_magic_begin_obj "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/section_magic_begin-${arch_suffix}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}")
set(section_magic_end_obj "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/section_magic_end-${arch_suffix}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}")

if(SWIFT_ENABLE_GOLD_LINKER)
set(LD_COMMAND "gold")
else()
set(LD_COMMAND "ld")
endif()

add_custom_command_target(section_magic_${arch_suffix}_objects
add_custom_command_target(section_magic_${arch_suffix}_begin_object
COMMAND
"${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${section_magic_begin_name}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}"
# Merge ImageInspectionInit.o + swift_sections.S(BEGIN) => swift_begin.o
${LD_COMMAND} -r -o "${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
"${section_magic_begin_obj}" "${section_magic_loader_obj}"
OUTPUT
"${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
DEPENDS
"${section_magic_begin_obj}"
"${section_magic_loader_obj}")

add_custom_command_target(section_magic_${arch_suffix}_end_object
COMMAND
"${CMAKE_COMMAND}" -E copy
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/${section_magic_end_name}.dir/swift_sections.S${CMAKE_C_OUTPUT_EXTENSION}"
"${section_magic_end_obj}"
"${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
OUTPUT
"${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
"${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
DEPENDS
${section_magic_begin_name}
${section_magic_end_name})
"${section_magic_end_obj}")

list(APPEND object_target_list "${section_magic_${arch_suffix}_objects}")
list(APPEND object_target_list
"${section_magic_${arch_suffix}_begin_object}"
"${section_magic_${arch_suffix}_end_object}")

swift_install_in_component(stdlib
FILES "${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o" "${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
DESTINATION "lib/swift/${arch_subdir}")
FILES
"${SWIFTLIB_DIR}/${arch_subdir}/swift_begin.o"
"${SWIFTLIB_DIR}/${arch_subdir}/swift_end.o"
DESTINATION
"lib/swift/${arch_subdir}")

if(SWIFT_BUILD_STATIC_STDLIB)
# Static lib versions of swift_begin.o and swift_end.o
add_custom_command_target(static_section_magic_${arch_suffix}_begin_object
COMMAND
"${CMAKE_COMMAND}" -E copy
"${section_magic_begin_obj}"
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_begin.o"
OUTPUT
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_begin.o"
DEPENDS
"${section_magic_begin_obj}")

add_custom_command_target(static_section_magic_${arch_suffix}_end_object
COMMAND
"${CMAKE_COMMAND}" -E copy
"${section_magic_end_obj}"
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_end.o"
OUTPUT
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_end.o"
DEPENDS
"${section_magic_end_obj}")

list(APPEND object_target_list
"${static_section_magic_${arch_suffix}_begin_object}"
"${static_section_magic_${arch_suffix}_end_object}")

swift_install_in_component(stdlib
FILES
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_begin.o"
"${SWIFTSTATICLIB_DIR}/${arch_subdir}/swift_end.o"
DESTINATION
"lib/swift_static/${arch_subdir}")
endif()

endforeach()
endforeach()

Expand Down
1 change: 1 addition & 0 deletions stdlib/public/runtime/ImageInspection.h
Expand Up @@ -19,6 +19,7 @@
#ifndef SWIFT_RUNTIME_IMAGE_INSPECTION_H
#define SWIFT_RUNTIME_IMAGE_INSPECTION_H

#include "ImageInspectionELF.h"
#include <cstdint>

namespace swift {
Expand Down