Skip to content

Commit 9ea988f

Browse files
authored
Merge pull request #12758 from compnerd/ELF-registration
ELF registration Restructuring
2 parents 20d7701 + 28dffe7 commit 9ea988f

28 files changed

+418
-886
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -716,19 +716,6 @@ function(_add_swift_library_single target name)
716716
set(SWIFTLIB_SINGLE_API_NOTES "${module_name}")
717717
endif()
718718

719-
# On platforms that use ELF binaries we add markers for metadata sections in
720-
# the shared libraries using these object files. This wouldn't be necessary
721-
# if the link was done by the swift binary: rdar://problem/19007002
722-
if(SWIFTLIB_SINGLE_TARGET_LIBRARY AND
723-
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
724-
if("${libkind}" STREQUAL "SHARED")
725-
set(arch_subdir "${SWIFTLIB_DIR}/${SWIFTLIB_SINGLE_SUBDIR}")
726-
727-
set(SWIFT_SECTIONS_OBJECT_BEGIN "${arch_subdir}/swift_begin.o")
728-
set(SWIFT_SECTIONS_OBJECT_END "${arch_subdir}/swift_end.o")
729-
endif()
730-
endif()
731-
732719
if("${SWIFTLIB_SINGLE_SDK}" STREQUAL "WINDOWS")
733720
swift_windows_include_for_arch(${SWIFTLIB_SINGLE_ARCHITECTURE} SWIFTLIB_INCLUDE)
734721
swift_windows_generate_sdk_vfs_overlay(SWIFTLIB_SINGLE_VFS_OVERLAY_FLAGS)
@@ -828,12 +815,15 @@ function(_add_swift_library_single target name)
828815
endif()
829816

830817
add_library("${target}" ${libkind}
831-
${SWIFT_SECTIONS_OBJECT_BEGIN}
832-
${SWIFTLIB_SINGLE_SOURCES}
833-
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}
834-
${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}
835-
${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES}
836-
${SWIFT_SECTIONS_OBJECT_END})
818+
${SWIFTLIB_SINGLE_SOURCES}
819+
${SWIFTLIB_SINGLE_EXTERNAL_SOURCES}
820+
${INCORPORATED_OBJECT_LIBRARIES_EXPRESSIONS}
821+
${SWIFTLIB_SINGLE_XCODE_WORKAROUND_SOURCES})
822+
if("${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF" AND SWIFTLIB_TARGET_LIBRARY)
823+
if("${libkind}" STREQUAL "SHARED")
824+
target_sources(${target} PRIVATE $<TARGET_OBJECTS:swiftImageRegistrationObject-${SWIFT_SDK_${sdk}_LIB_SUBDIR}-${SWIFTLIB_SINGLE_ARCHITECTURE}>)
825+
endif()
826+
endif()
837827
_set_target_prefix_and_suffix("${target}" "${libkind}" "${SWIFTLIB_SINGLE_SDK}")
838828

839829
if(SWIFTLIB_SINGLE_TARGET_LIBRARY)
@@ -872,17 +862,6 @@ function(_add_swift_library_single target name)
872862
set_property(TARGET "${target}" PROPERTY NO_SONAME ON)
873863
endif()
874864

875-
# The section metadata objects are generated sources, and we need to tell CMake
876-
# not to expect to find them prior to their generation.
877-
if(SWIFTLIB_SINGLE_TARGET_LIBRARY AND
878-
"${SWIFT_SDK_${SWIFTLIB_SINGLE_SDK}_OBJECT_FORMAT}" STREQUAL "ELF")
879-
if("${libkind}" STREQUAL "SHARED")
880-
set_source_files_properties(${SWIFT_SECTIONS_OBJECT_BEGIN} PROPERTIES GENERATED 1)
881-
set_source_files_properties(${SWIFT_SECTIONS_OBJECT_END} PROPERTIES GENERATED 1)
882-
add_dependencies("${target}" section_magic)
883-
endif()
884-
endif()
885-
886865
llvm_update_compile_flags(${target})
887866

888867
set_output_directory(${target}

include/swift/Reflection/Records.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ class FieldRecord {
6363
return MangledTypeName;
6464
}
6565

66-
std::string getMangledTypeName() const {
67-
return MangledTypeName.get();
66+
std::string getMangledTypeName(uintptr_t Offset) const {
67+
return MangledTypeName.get() + Offset;
6868
}
6969

70-
std::string getFieldName() const {
70+
std::string getFieldName(uintptr_t Offset) const {
7171
if (FieldName)
72-
return FieldName.get();
72+
return FieldName.get() + Offset;
7373
return "";
7474
}
7575

@@ -188,16 +188,16 @@ class FieldDescriptor {
188188
return MangledTypeName;
189189
}
190190

191-
std::string getMangledTypeName() const {
192-
return MangledTypeName.get();
191+
std::string getMangledTypeName(uintptr_t Offset) const {
192+
return MangledTypeName.get() + Offset;
193193
}
194194

195195
bool hasSuperclass() const {
196196
return Superclass;
197197
}
198198

199-
std::string getSuperclass() const {
200-
return Superclass.get();
199+
std::string getSuperclass(uintptr_t Offset) const {
200+
return Superclass.get() + Offset;
201201
}
202202
};
203203

@@ -241,12 +241,12 @@ class AssociatedTypeRecord {
241241
const RelativeDirectPointer<const char> SubstitutedTypeName;
242242

243243
public:
244-
std::string getName() const {
245-
return Name.get();
244+
std::string getName(uintptr_t Offset) const {
245+
return Name.get() + Offset;
246246
}
247247

248-
std::string getMangledSubstitutedTypeName() const {
249-
return SubstitutedTypeName.get();
248+
std::string getMangledSubstitutedTypeName(uintptr_t Offset) const {
249+
return SubstitutedTypeName.get() + Offset;
250250
}
251251
};
252252

@@ -319,12 +319,12 @@ struct AssociatedTypeDescriptor {
319319
return const_iterator { End, End };
320320
}
321321

322-
std::string getMangledProtocolTypeName() const {
323-
return ProtocolTypeName.get();
322+
std::string getMangledProtocolTypeName(uintptr_t Offset) const {
323+
return ProtocolTypeName.get() + Offset;
324324
}
325325

326-
std::string getMangledConformingTypeName() const {
327-
return ConformingTypeName.get();
326+
std::string getMangledConformingTypeName(uintptr_t Offset) const {
327+
return ConformingTypeName.get() + Offset;
328328
}
329329
};
330330

@@ -377,8 +377,8 @@ class BuiltinTypeDescriptor {
377377
return TypeName;
378378
}
379379

380-
std::string getMangledTypeName() const {
381-
return TypeName.get();
380+
std::string getMangledTypeName(uintptr_t Offset) const {
381+
return TypeName.get() + Offset;
382382
}
383383
};
384384

@@ -424,8 +424,8 @@ class CaptureTypeRecord {
424424
return MangledTypeName;
425425
}
426426

427-
std::string getMangledTypeName() const {
428-
return MangledTypeName.get();
427+
std::string getMangledTypeName(uintptr_t Offset) const {
428+
return MangledTypeName.get() + Offset;
429429
}
430430
};
431431

@@ -470,16 +470,16 @@ class MetadataSourceRecord {
470470
return MangledTypeName;
471471
}
472472

473-
std::string getMangledTypeName() const {
474-
return MangledTypeName.get();
473+
std::string getMangledTypeName(uintptr_t Offset) const {
474+
return MangledTypeName.get() + Offset;
475475
}
476476

477477
bool hasMangledMetadataSource() const {
478478
return MangledMetadataSource;
479479
}
480480

481-
std::string getMangledMetadataSource() const {
482-
return MangledMetadataSource.get();
481+
std::string getMangledMetadataSource(uintptr_t Offset) const {
482+
return MangledMetadataSource.get() + Offset;
483483
}
484484
};
485485

include/swift/Reflection/ReflectionContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class ReflectionContext
147147
if (CD == nullptr)
148148
return nullptr;
149149

150-
auto Info = getBuilder().getClosureContextInfo(*CD);
150+
auto Info = getBuilder().getClosureContextInfo(*CD, 0);
151151

152152
return getClosureContextInfo(ObjectAddress, Info);
153153
}

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,36 @@ using CaptureSection = ReflectionSection<CaptureDescriptorIterator>;
7373
using GenericSection = ReflectionSection<const void *>;
7474

7575
struct ReflectionInfo {
76-
FieldSection fieldmd;
77-
AssociatedTypeSection assocty;
78-
BuiltinTypeSection builtin;
79-
CaptureSection capture;
80-
GenericSection typeref;
81-
GenericSection reflstr;
76+
struct {
77+
FieldSection Metadata;
78+
uintptr_t SectionOffset;
79+
} Field;
80+
81+
struct {
82+
AssociatedTypeSection Metadata;
83+
uintptr_t SectionOffset;
84+
} AssociatedType;
85+
86+
struct {
87+
BuiltinTypeSection Metadata;
88+
uintptr_t SectionOffset;
89+
} Builtin;
90+
91+
struct {
92+
CaptureSection Metadata;
93+
uintptr_t SectionOffset;
94+
} Capture;
95+
96+
struct {
97+
GenericSection Metadata;
98+
uintptr_t SectionOffset;
99+
} TypeReference;
100+
101+
struct {
102+
GenericSection Metadata;
103+
uintptr_t SectionOffset;
104+
} ReflectionString;
105+
82106
uintptr_t LocalStartAddress;
83107
uintptr_t RemoteStartAddress;
84108
};
@@ -322,10 +346,12 @@ class TypeRefBuilder {
322346
lookupSuperclass(const TypeRef *TR);
323347

324348
/// Load unsubstituted field types for a nominal type.
325-
const FieldDescriptor *getFieldTypeInfo(const TypeRef *TR);
349+
std::pair<const FieldDescriptor *, uintptr_t>
350+
getFieldTypeInfo(const TypeRef *TR);
326351

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

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

338364
/// Get the unsubstituted capture types for a closure context.
339-
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD);
365+
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD,
366+
uintptr_t Offset);
340367

341368
///
342369
/// Dumping typerefs, field declarations, associated types

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,35 @@ typedef struct swift_reflection_section {
3535
/// \brief Represents the set of Swift reflection sections of an image.
3636
/// Not all sections may be present.
3737
typedef struct swift_reflection_info {
38-
swift_reflection_section_t fieldmd;
39-
swift_reflection_section_t assocty;
40-
swift_reflection_section_t builtin;
41-
swift_reflection_section_t capture;
42-
swift_reflection_section_t typeref;
43-
swift_reflection_section_t reflstr;
38+
struct {
39+
swift_reflection_section_t section;
40+
uintptr_t offset;
41+
} field;
42+
43+
struct {
44+
swift_reflection_section_t section;
45+
uintptr_t offset;
46+
} associated_types;
47+
48+
struct {
49+
swift_reflection_section_t section;
50+
uintptr_t offset;
51+
} builtin_types;
52+
53+
struct {
54+
swift_reflection_section_t section;
55+
uintptr_t offset;
56+
} capture;
57+
58+
struct {
59+
swift_reflection_section_t section;
60+
uintptr_t offset;
61+
} type_references;
62+
63+
struct {
64+
swift_reflection_section_t section;
65+
uintptr_t offset;
66+
} reflection_strings;
4467

4568
// Start address in local and remote address spaces.
4669
uintptr_t LocalStartAddress;

lib/Driver/ToolChains.cpp

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,26 +1533,6 @@ bool toolchains::GenericUnix::shouldProvideRPathToLinker() const {
15331533
return true;
15341534
}
15351535

1536-
std::string toolchains::GenericUnix::getPreInputObjectPath(
1537-
StringRef RuntimeLibraryPath) const {
1538-
// On Linux and FreeBSD and Haiku (really, ELF binaries) we need to add objects
1539-
// to provide markers and size for the metadata sections.
1540-
SmallString<128> PreInputObjectPath = RuntimeLibraryPath;
1541-
llvm::sys::path::append(PreInputObjectPath,
1542-
swift::getMajorArchitectureName(getTriple()));
1543-
llvm::sys::path::append(PreInputObjectPath, "swift_begin.o");
1544-
return PreInputObjectPath.str();
1545-
}
1546-
1547-
std::string toolchains::GenericUnix::getPostInputObjectPath(
1548-
StringRef RuntimeLibraryPath) const {
1549-
SmallString<128> PostInputObjectPath = RuntimeLibraryPath;
1550-
llvm::sys::path::append(PostInputObjectPath,
1551-
swift::getMajorArchitectureName(getTriple()));
1552-
llvm::sys::path::append(PostInputObjectPath, "swift_end.o");
1553-
return PostInputObjectPath.str();
1554-
}
1555-
15561536
ToolChain::InvocationInfo
15571537
toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
15581538
const JobContext &context) const {
@@ -1630,22 +1610,10 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
16301610
}
16311611

16321612
SmallString<128> SharedRuntimeLibPath;
1633-
SmallString<128> StaticRuntimeLibPath;
1634-
// Path to swift_begin.o and swift_end.o.
1635-
SmallString<128> ObjectLibPath;
16361613
getRuntimeLibraryPath(SharedRuntimeLibPath, context.Args, *this);
16371614

1638-
// -static-stdlib uses the static lib path for libswiftCore but
1639-
// the shared lib path for swift_begin.o and swift_end.o.
1640-
if (staticExecutable || staticStdlib) {
1641-
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
1642-
}
1643-
1644-
if (staticExecutable) {
1645-
ObjectLibPath = StaticRuntimeLibPath;
1646-
} else {
1647-
ObjectLibPath = SharedRuntimeLibPath;
1648-
}
1615+
SmallString<128> StaticRuntimeLibPath;
1616+
getRuntimeStaticLibraryPath(StaticRuntimeLibPath, context.Args, *this);
16491617

16501618
// Add the runtime library link path, which is platform-specific and found
16511619
// relative to the compiler.
@@ -1658,10 +1626,12 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
16581626
Arguments.push_back(context.Args.MakeArgString(SharedRuntimeLibPath));
16591627
}
16601628

1661-
auto PreInputObjectPath = getPreInputObjectPath(ObjectLibPath);
1662-
if (!PreInputObjectPath.empty()) {
1663-
Arguments.push_back(context.Args.MakeArgString(PreInputObjectPath));
1664-
}
1629+
SmallString<128> swiftrtPath = SharedRuntimeLibPath;
1630+
llvm::sys::path::append(swiftrtPath,
1631+
swift::getMajorArchitectureName(getTriple()));
1632+
llvm::sys::path::append(swiftrtPath, "swiftrt.o");
1633+
Arguments.push_back(context.Args.MakeArgString(swiftrtPath));
1634+
16651635
addPrimaryInputsOfType(Arguments, context.Inputs, types::TY_Object);
16661636
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
16671637

@@ -1756,13 +1726,6 @@ toolchains::GenericUnix::constructInvocation(const LinkJobAction &job,
17561726
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
17571727
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
17581728

1759-
// Just before the output option, allow GenericUnix toolchains to add
1760-
// additional inputs.
1761-
auto PostInputObjectPath = getPostInputObjectPath(ObjectLibPath);
1762-
if (!PostInputObjectPath.empty()) {
1763-
Arguments.push_back(context.Args.MakeArgString(PostInputObjectPath));
1764-
}
1765-
17661729
// This should be the last option, for convenience in checking output.
17671730
Arguments.push_back("-o");
17681731
Arguments.push_back(context.Output.getPrimaryOutputFilename().c_str());
@@ -1795,14 +1758,3 @@ std::string toolchains::Cygwin::getTargetForLinker() const {
17951758
return "";
17961759
}
17971760

1798-
std::string toolchains::Cygwin::getPreInputObjectPath(
1799-
StringRef RuntimeLibraryPath) const {
1800-
// Cygwin does not add "begin" and "end" objects.
1801-
return "";
1802-
}
1803-
1804-
std::string toolchains::Cygwin::getPostInputObjectPath(
1805-
StringRef RuntimeLibraryPath) const {
1806-
// Cygwin does not add "begin" and "end" objects.
1807-
return "";
1808-
}

0 commit comments

Comments
 (0)