Skip to content

Remove lookupSymbol() and have all callers use SymbolInfo::lookup() instead #62552

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
Dec 14, 2022
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
4 changes: 2 additions & 2 deletions stdlib/public/SwiftShims/swift/shims/MetadataSections.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ struct MetadataSections {
/// reported when the section was registered with the Swift runtime.
///
/// The value of this field is equivalent to the value of
/// \c SymbolInfo::baseAddress as returned from \c lookupSymbol() for a symbol
/// in the image that contains these sections.
/// \c SymbolInfo::baseAddress as returned from \c SymbolInfo::lookup() for a
/// symbol in the image that contains these sections.
///
/// For Mach-O images, set this field to \c __dso_handle (i.e. the Mach header
/// for the image.) For ELF images, set it to \c __dso_handle (the runtime
Expand Down
14 changes: 6 additions & 8 deletions stdlib/public/runtime/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,16 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName,
void swift::dumpStackTraceEntry(unsigned index, void *framePC,
bool shortOutput) {
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
SymbolInfo syminfo;

// 0 is failure for lookupSymbol
if (0 == lookupSymbol(framePC, &syminfo)) {
auto syminfo = SymbolInfo::lookup(framePC);
if (!syminfo.has_value()) {
return;
}

// If lookupSymbol succeeded then fileName is non-null. Thus, we find the
// If SymbolInfo:lookup succeeded then fileName is non-null. Thus, we find the
// library name here. Avoid using StringRef::rsplit because its definition
// is not provided in the header so that it requires linking with
// libSupport.a.
llvm::StringRef libraryName{syminfo.getFilename()};
llvm::StringRef libraryName{syminfo->getFilename()};
libraryName = libraryName.substr(libraryName.rfind('/')).substr(1);

// Next we get the symbol name that we are going to use in our backtrace.
Expand All @@ -165,12 +163,12 @@ void swift::dumpStackTraceEntry(unsigned index, void *framePC,
// we just get HexAddr + 0.
uintptr_t symbolAddr = uintptr_t(framePC);
bool foundSymbol =
getSymbolNameAddr(libraryName, syminfo, symbolName, symbolAddr);
getSymbolNameAddr(libraryName, syminfo.value(), symbolName, symbolAddr);
ptrdiff_t offset = 0;
if (foundSymbol) {
offset = ptrdiff_t(uintptr_t(framePC) - symbolAddr);
} else {
auto baseAddress = syminfo.getBaseAddress();
auto baseAddress = syminfo->getBaseAddress();
offset = ptrdiff_t(uintptr_t(framePC) - uintptr_t(baseAddress));
symbolAddr = uintptr_t(framePC);
symbolName = "<unavailable>";
Expand Down
18 changes: 8 additions & 10 deletions stdlib/public/runtime/ImageInspectionCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ static void fixupMetadataSectionBaseAddress(swift::MetadataSections *sections) {
if (fixupNeeded) {
// We need to fix up the base address. We'll need a known-good address in
// the same image: `sections` itself will work nicely.
swift::SymbolInfo symbolInfo;
if (lookupSymbol(sections, &symbolInfo) && symbolInfo.getBaseAddress()) {
sections->baseAddress.store(symbolInfo.getBaseAddress(),
auto symbolInfo = SymbolInfo::lookup(sections);
if (symbolInfo.has_value() && symbolInfo->getBaseAddress()) {
sections->baseAddress.store(symbolInfo->getBaseAddress(),
std::memory_order_relaxed);
}
}
Expand Down Expand Up @@ -190,10 +190,9 @@ const swift::MetadataSections *swift_getMetadataSection(size_t index) {
SWIFT_RUNTIME_EXPORT
const char *
swift_getMetadataSectionName(const swift::MetadataSections *section) {
swift::SymbolInfo info;
if (lookupSymbol(section, &info)) {
if (info.getFilename()) {
return info.getFilename();
if (auto info = SymbolInfo::lookup(section)) {
if (info->getFilename()) {
return info->getFilename();
}
}
return "";
Expand All @@ -203,9 +202,8 @@ SWIFT_RUNTIME_EXPORT
void swift_getMetadataSectionBaseAddress(const swift::MetadataSections *section,
void const **out_actual,
void const **out_expected) {
swift::SymbolInfo info;
if (lookupSymbol(section, &info)) {
*out_actual = info.getBaseAddress();
if (auto info = SymbolInfo::lookup(section)) {
*out_actual = info->getBaseAddress();
} else {
*out_actual = nullptr;
}
Expand Down
5 changes: 2 additions & 3 deletions stdlib/public/runtime/MetadataLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,9 +1105,8 @@ _gatherGenericParameters(const ContextDescriptor *context,

str += "_gatherGenericParameters: context: ";

SymbolInfo contextInfo;
if (lookupSymbol(context, &contextInfo)) {
str += contextInfo.getSymbolName();
if (auto contextInfo = SymbolInfo::lookup(context)) {
str += contextInfo->getSymbolName();
str += " ";
}

Expand Down
11 changes: 6 additions & 5 deletions stdlib/public/runtime/ProtocolConformance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,13 @@ static const char *class_getName(const ClassMetadata* type) {
}

template<> void ProtocolConformanceDescriptor::dump() const {
SymbolInfo info;
llvm::Optional<SymbolInfo> info;
auto symbolName = [&](const void *addr) -> const char * {
int ok = lookupSymbol(addr, &info);
if (!ok || !info.getSymbolName())
return "<unknown addr>";
return info.getSymbolName();
info = SymbolInfo::lookup(addr);
if (info.has_value() && info->getSymbolName()) {
return info->getSymbolName();
}
return "<unknown addr>";
};

switch (auto kind = getTypeKind()) {
Expand Down
9 changes: 5 additions & 4 deletions stdlib/public/runtime/ReflectionMirror.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,12 @@ id swift_reflectionMirror_quickLookObject(OpaqueValue *value, const Metadata *T)

SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
const char *swift_keyPath_copySymbolName(void *address) {
SymbolInfo info;
if (lookupSymbol(address, &info) && info.getSymbolName()) {
return strdup(info.getSymbolName());
if (auto info = SymbolInfo::lookup(address)) {
if (info->getSymbolName()) {
return strdup(info->getSymbolName());
}
}
return 0;
return nullptr;
}

SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
Expand Down
21 changes: 0 additions & 21 deletions stdlib/public/runtime/SymbolInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,6 @@ struct SymbolInfo {
static llvm::Optional<SymbolInfo> lookup(const void *address);
};

/// Look up a symbol by address and store the result in a locally-declared
/// \c SymbolInfo value.
///
/// \param address The address where the symbol is located.
/// \param outInfo On successful return, populated with information about the
/// symbol at \a address. On failure, unspecified.
///
/// \returns On success, a non-zero integer. On failure, zero.
///
/// \note This function will be replaced with \c SymbolInfo::lookup() in a
/// future update.
static inline int lookupSymbol(const void *address, SymbolInfo *outInfo) {
auto info = SymbolInfo::lookup(address);
if (info.has_value()) {
*outInfo = info.value();
return 1;
} else {
return 0;
}
}

} // end namespace swift

#endif