Skip to content

Commit bada5eb

Browse files
authored
[llvm-nm] Fix how inlined dylibs are reported from tbd files (#134498)
An Inlined library is a dylib that is reexported from an umbrella or top-level library. When this is encoded in tbd files, ensure we are reading the symbol table from the inlined library when `--add-inlinedinfo` is used as opposed to the top-level lib. resolves: rdar://147767733
1 parent 43bb6ba commit bada5eb

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

llvm/include/llvm/Object/TapiUniversal.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ class TapiUniversal : public Binary {
110110
static bool classof(const Binary *v) { return v->isTapiUniversal(); }
111111

112112
private:
113+
/// Attributes of a library that is inlined into a single TBD file.
113114
struct Library {
114-
StringRef InstallName;
115-
MachO::Architecture Arch;
115+
const StringRef InstallName;
116+
const MachO::Architecture Arch;
117+
const std::optional<size_t> DocumentIdx;
116118
};
117119

118120
std::unique_ptr<MachO::InterfaceFile> ParsedFile;

llvm/lib/Object/TapiUniversal.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,34 @@ TapiUniversal::TapiUniversal(MemoryBufferRef Source, Error &Err)
2929
}
3030
ParsedFile = std::move(Result.get());
3131

32-
auto FlattenObjectInfo = [this](const auto &File) {
32+
auto FlattenObjectInfo = [this](const auto &File,
33+
std::optional<size_t> DocIdx = std::nullopt) {
3334
StringRef Name = File->getInstallName();
3435
for (const Architecture Arch : File->getArchitectures())
35-
Libraries.emplace_back(Library({Name, Arch}));
36+
Libraries.emplace_back(Library({Name, Arch, DocIdx}));
3637
};
37-
3838
FlattenObjectInfo(ParsedFile);
3939
// Get inlined documents from tapi file.
40+
size_t DocIdx = 0;
4041
for (const std::shared_ptr<InterfaceFile> &File : ParsedFile->documents())
41-
FlattenObjectInfo(File);
42+
FlattenObjectInfo(File, DocIdx++);
4243
}
4344

4445
TapiUniversal::~TapiUniversal() = default;
4546

4647
Expected<std::unique_ptr<TapiFile>>
4748
TapiUniversal::ObjectForArch::getAsObjectFile() const {
48-
return std::make_unique<TapiFile>(Parent->getMemoryBufferRef(),
49-
*Parent->ParsedFile,
50-
Parent->Libraries[Index].Arch);
49+
const auto &InlinedDocuments = Parent->ParsedFile->documents();
50+
const Library &CurrLib = Parent->Libraries[Index];
51+
assert(
52+
(isTopLevelLib() || (CurrLib.DocumentIdx.has_value() &&
53+
(InlinedDocuments.size() > *CurrLib.DocumentIdx))) &&
54+
"Index into documents exceeds the container for them");
55+
InterfaceFile *IF = isTopLevelLib()
56+
? Parent->ParsedFile.get()
57+
: InlinedDocuments[*CurrLib.DocumentIdx].get();
58+
return std::make_unique<TapiFile>(Parent->getMemoryBufferRef(), *IF,
59+
CurrLib.Arch);
5160
}
5261

5362
Expected<std::unique_ptr<TapiUniversal>>

llvm/test/tools/llvm-nm/tapi-files.test

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,8 @@ V3-NEXT: 0000000000000000 S _OBJC_METACLASS_$_NSString
4545
V3-NEXT: 0000000000000000 S _sym1
4646
V3-NEXT: 0000000000000000 S _sym2
4747
V3: /usr/lib/liba.dylib (for architecture x86_64):
48-
V3-NEXT: 0000000000000000 S _OBJC_CLASS_$_NSBlockPredicate
49-
V3-NEXT: 0000000000000000 S _OBJC_CLASS_$_NSString
50-
V3-NEXT: 0000000000000000 S _OBJC_EHTYPE_$_NSString
51-
V3-NEXT: 0000000000000000 S _OBJC_IVAR_$_NSBlockPredicate._block
52-
V3-NEXT: 0000000000000000 S _OBJC_METACLASS_$_NSBlockPredicate
53-
V3-NEXT: 0000000000000000 S _OBJC_METACLASS_$_NSString
54-
V3-NEXT: 0000000000000000 S _sym1
55-
V3-NEXT: 0000000000000000 S _sym2
48+
V3-NEXT: 0000000000000000 S _sym10
49+
V3-NEXT: 0000000000000000 S _sym11
5650

5751
V4: /u/l/libFoo.dylib (for architecture i386):
5852
V4-NEXT: 00000000 S _sym1

0 commit comments

Comments
 (0)