Skip to content

ELF registration Restructuring #12758

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

Merged
merged 3 commits into from
Nov 30, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -716,19 +716,6 @@ function(_add_swift_library_single target name)
set(SWIFTLIB_SINGLE_API_NOTES "${module_name}")
endif()

# On platforms that use ELF binaries we add markers for metadata sections in
# the shared libraries using these object files. This wouldn't be necessary
# if the link was done by the swift binary: rdar://problem/19007002
if(SWIFTLIB_SINGLE_TARGET_LIBRARY AND
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
if("${libkind}" STREQUAL "SHARED")
set(arch_subdir "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}")

set(SWIFT_SECTIONS_OBJECT_BEGIN "${arch_subdir}/swift_begin.o")
set(SWIFT_SECTIONS_OBJECT_END "${arch_subdir}/swift_end.o")
endif()
endif()

if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS")
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
swift_windows_generate_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY_FLAGS)
Expand Down Expand Up @@ -828,12 +815,15 @@ function(_add_swift_library_single target name)
endif()

add_library("${target}" ${libkind}
${SWIFT_SECTIONS_OBJECT_BEGIN}
${SWIFTLIB_SINGLE_SOURCES}
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}
${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}
${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES}
${SWIFT_SECTIONS_OBJECT_END})
${SWIFTLIB_SINGLE_SOURCES}
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}
${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}
${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES})
if("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" AND SWIFTLIB_TARGET_LIBRARY)
if("${libkind}" STREQUAL "SHARED")
target_sources(${target} PRIVATE $<TARGET_OBJECTS:swiftImageRegistrationObject-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}>)
endif()
endif()
_set_target_prefix_and_suffix("${target}" "${libkind}" "${SWIFTLIB_SINGLE_SDK}")

if(SWIFTLIB_SINGLE_TARGET_LIBRARY)
Expand Down Expand Up @@ -872,17 +862,6 @@ function(_add_swift_library_single target name)
set_property(TARGET "${target}" PROPERTY NO_SONAME ON)
endif()

# The section metadata objects are generated sources, and we need to tell CMake
# not to expect to find them prior to their generation.
if(SWIFTLIB_SINGLE_TARGET_LIBRARY AND
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
if("${libkind}" STREQUAL "SHARED")
set_source_files_properties(${SWIFT_SECTIONS_OBJECT_BEGIN} PROPERTIES GENERATED 1)
set_source_files_properties(${SWIFT_SECTIONS_OBJECT_END} PROPERTIES GENERATED 1)
add_dependencies("${target}" section_magic)
endif()
endif()

llvm_update_compile_flags(${target})

set_output_directory(${target}
Expand Down
48 changes: 24 additions & 24 deletions include/swift/Reflection/Records.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ class FieldRecord {
return MangledTypeName;
}

std::string getMangledTypeName() const {
return MangledTypeName.get();
std::string getMangledTypeName(uintptr_t Offset) const {
return MangledTypeName.get() + Offset;
}

std::string getFieldName() const {
std::string getFieldName(uintptr_t Offset) const {
if (FieldName)
return FieldName.get();
return FieldName.get() + Offset;
return "";
}

Expand Down Expand Up @@ -188,16 +188,16 @@ class FieldDescriptor {
return MangledTypeName;
}

std::string getMangledTypeName() const {
return MangledTypeName.get();
std::string getMangledTypeName(uintptr_t Offset) const {
return MangledTypeName.get() + Offset;
}

bool hasSuperclass() const {
return Superclass;
}

std::string getSuperclass() const {
return Superclass.get();
std::string getSuperclass(uintptr_t Offset) const {
return Superclass.get() + Offset;
}
};

Expand Down Expand Up @@ -241,12 +241,12 @@ class AssociatedTypeRecord {
const RelativeDirectPointer<const char> SubstitutedTypeName;

public:
std::string getName() const {
return Name.get();
std::string getName(uintptr_t Offset) const {
return Name.get() + Offset;
}

std::string getMangledSubstitutedTypeName() const {
return SubstitutedTypeName.get();
std::string getMangledSubstitutedTypeName(uintptr_t Offset) const {
return SubstitutedTypeName.get() + Offset;
}
};

Expand Down Expand Up @@ -319,12 +319,12 @@ struct AssociatedTypeDescriptor {
return const_iterator { End, End };
}

std::string getMangledProtocolTypeName() const {
return ProtocolTypeName.get();
std::string getMangledProtocolTypeName(uintptr_t Offset) const {
return ProtocolTypeName.get() + Offset;
}

std::string getMangledConformingTypeName() const {
return ConformingTypeName.get();
std::string getMangledConformingTypeName(uintptr_t Offset) const {
return ConformingTypeName.get() + Offset;
}
};

Expand Down Expand Up @@ -377,8 +377,8 @@ class BuiltinTypeDescriptor {
return TypeName;
}

std::string getMangledTypeName() const {
return TypeName.get();
std::string getMangledTypeName(uintptr_t Offset) const {
return TypeName.get() + Offset;
}
};

Expand Down Expand Up @@ -424,8 +424,8 @@ class CaptureTypeRecord {
return MangledTypeName;
}

std::string getMangledTypeName() const {
return MangledTypeName.get();
std::string getMangledTypeName(uintptr_t Offset) const {
return MangledTypeName.get() + Offset;
}
};

Expand Down Expand Up @@ -470,16 +470,16 @@ class MetadataSourceRecord {
return MangledTypeName;
}

std::string getMangledTypeName() const {
return MangledTypeName.get();
std::string getMangledTypeName(uintptr_t Offset) const {
return MangledTypeName.get() + Offset;
}

bool hasMangledMetadataSource() const {
return MangledMetadataSource;
}

std::string getMangledMetadataSource() const {
return MangledMetadataSource.get();
std::string getMangledMetadataSource(uintptr_t Offset) const {
return MangledMetadataSource.get() + Offset;
}
};

Expand Down
2 changes: 1 addition & 1 deletion include/swift/Reflection/ReflectionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ReflectionContext
if (CD == nullptr)
return nullptr;

auto Info = getBuilder().getClosureContextInfo(*CD);
auto Info = getBuilder().getClosureContextInfo(*CD, 0);

return getClosureContextInfo(ObjectAddress, Info);
}
Expand Down
45 changes: 36 additions & 9 deletions include/swift/Reflection/TypeRefBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,36 @@ using CaptureSection = ReflectionSection<CaptureDescriptorIterator>;
using GenericSection = ReflectionSection<const void *>;

struct ReflectionInfo {
FieldSection fieldmd;
AssociatedTypeSection assocty;
BuiltinTypeSection builtin;
CaptureSection capture;
GenericSection typeref;
GenericSection reflstr;
struct {
FieldSection Metadata;
uintptr_t SectionOffset;
} Field;

struct {
AssociatedTypeSection Metadata;
uintptr_t SectionOffset;
} AssociatedType;

struct {
BuiltinTypeSection Metadata;
uintptr_t SectionOffset;
} Builtin;

struct {
CaptureSection Metadata;
uintptr_t SectionOffset;
} Capture;

struct {
GenericSection Metadata;
uintptr_t SectionOffset;
} TypeReference;

struct {
GenericSection Metadata;
uintptr_t SectionOffset;
} ReflectionString;

uintptr_t LocalStartAddress;
uintptr_t RemoteStartAddress;
};
Expand Down Expand Up @@ -322,10 +346,12 @@ class TypeRefBuilder {
lookupSuperclass(const TypeRef *TR);

/// Load unsubstituted field types for a nominal type.
const FieldDescriptor *getFieldTypeInfo(const TypeRef *TR);
std::pair<const FieldDescriptor *, uintptr_t>
getFieldTypeInfo(const TypeRef *TR);

/// Get the parsed and substituted field types for a nominal type.
bool getFieldTypeRefs(const TypeRef *TR, const FieldDescriptor *FD,
bool getFieldTypeRefs(const TypeRef *TR,
const std::pair<const FieldDescriptor *, uintptr_t> &FD,
std::vector<FieldTypeInfo> &Fields);

/// Get the primitive type lowering for a builtin type.
Expand All @@ -336,7 +362,8 @@ class TypeRefBuilder {
const CaptureDescriptor *getCaptureDescriptor(uintptr_t RemoteAddress);

/// Get the unsubstituted capture types for a closure context.
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD);
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD,
uintptr_t Offset);

///
/// Dumping typerefs, field declarations, associated types
Expand Down
35 changes: 29 additions & 6 deletions include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,35 @@ typedef struct swift_reflection_section {
/// \brief Represents the set of Swift reflection sections of an image.
/// Not all sections may be present.
typedef struct swift_reflection_info {
swift_reflection_section_t fieldmd;
swift_reflection_section_t assocty;
swift_reflection_section_t builtin;
swift_reflection_section_t capture;
swift_reflection_section_t typeref;
swift_reflection_section_t reflstr;
struct {
swift_reflection_section_t section;
uintptr_t offset;
} field;

struct {
swift_reflection_section_t section;
uintptr_t offset;
} associated_types;

struct {
swift_reflection_section_t section;
uintptr_t offset;
} builtin_types;

struct {
swift_reflection_section_t section;
uintptr_t offset;
} capture;

struct {
swift_reflection_section_t section;
uintptr_t offset;
} type_references;

struct {
swift_reflection_section_t section;
uintptr_t offset;
} reflection_strings;

// Start address in local and remote address spaces.
uintptr_t LocalStartAddress;
Expand Down
64 changes: 8 additions & 56 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1530,26 +1530,6 @@ bool toolchains::GenericUnix::shouldProvideRPathToLinker() const {
return true;
}

std::string toolchains::GenericUnix::getPreInputObjectPath(
StringRef RuntimeLibraryPath) const {
// On Linux and FreeBSD and Haiku (really, ELF binaries) we need to add objects
// to provide markers and size for the metadata sections.
SmallString<128> PreInputObjectPath = RuntimeLibraryPath;
llvm::sys::path::append(PreInputObjectPath,
swift::getMajorArchitectureName(getTriple()));
llvm::sys::path::append(PreInputObjectPath, "swift_begin.o");
return PreInputObjectPath.str();
}

std::string toolchains::GenericUnix::getPostInputObjectPath(
StringRef RuntimeLibraryPath) const {
SmallString<128> PostInputObjectPath = RuntimeLibraryPath;
llvm::sys::path::append(PostInputObjectPath,
swift::getMajorArchitectureName(getTriple()));
llvm::sys::path::append(PostInputObjectPath, "swift_end.o");
return PostInputObjectPath.str();
}

ToolChain::InvocationInfo
toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
const JobContext &context) const {
Expand Down Expand Up @@ -1627,22 +1607,10 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
}

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;
}
SmallString<128> StaticRuntimeLibPath;
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);

// Add the runtime library link path, which is platform-specific and found
// relative to the compiler.
Expand All @@ -1655,10 +1623,12 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
}

auto PreInputObjectPath = getPreInputObjectPath(ObjectLibPath);
if (!PreInputObjectPath.empty()) {
Arguments.push_back(context.Args.MakeArgString(PreInputObjectPath));
}
SmallString<128> swiftrtPath = SharedRuntimeLibPath;
llvm::sys::path::append(swiftrtPath,
swift::getMajorArchitectureName(getTriple()));
llvm::sys::path::append(swiftrtPath, "swiftrt.o");
Arguments.push_back(context.Args.MakeArgString(swiftrtPath));

addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
addInputsOfType(Arguments, context.InputActions, types::TY_Object);

Expand Down Expand Up @@ -1753,13 +1723,6 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);

// Just before the output option, allow GenericUnix toolchains to add
// additional inputs.
auto PostInputObjectPath = getPostInputObjectPath(ObjectLibPath);
if (!PostInputObjectPath.empty()) {
Arguments.push_back(context.Args.MakeArgString(PostInputObjectPath));
}

// This should be the last option, for convenience in checking output.
Arguments.push_back("-o");
Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str());
Expand Down Expand Up @@ -1792,14 +1755,3 @@ std::string toolchains::Cygwin::getTargetForLinker() const {
return "";
}

std::string toolchains::Cygwin::getPreInputObjectPath(
StringRef RuntimeLibraryPath) const {
// Cygwin does not add "begin" and "end" objects.
return "";
}

std::string toolchains::Cygwin::getPostInputObjectPath(
StringRef RuntimeLibraryPath) const {
// Cygwin does not add "begin" and "end" objects.
return "";
}
Loading