Skip to content

Commit 4ce1f3d

Browse files
Emit swift5 reflection section data in dsym bundle generated by dsymutil in the Dwarf section.
Add support for Swift reflection metadata to dsymutil. This patch adds support for copying Swift reflection metadata (__swift5_.* sections) from .o files to into the symbol-rich binary in the output .dSYM. The functionality is automatically enabled only if a .o file has reflection metadata sections and the binary doesn't. When copying dsymutil moves the section from the __TEXT segment to the __DWARF segment. rdar://76973336 Differential Revision: https://reviews.llvm.org/D115007
1 parent c0861fc commit 4ce1f3d

File tree

16 files changed

+1838
-11
lines changed

16 files changed

+1838
-11
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===- llvm/BinaryFormat/Swift.def - Swift definitions ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Macros for running through Swift enumerators.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#if !(defined HANDLE_SWIFT_SECTION)
14+
#error "Missing macro definition of HANDLE_SWIFT_SECTION"
15+
#endif
16+
17+
#ifndef HANDLE_SWIFT_SECTION
18+
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF)
19+
#endif
20+
21+
HANDLE_SWIFT_SECTION(fieldmd, "__swift5_fieldmd", "swift5_fieldmd", ".sw5flmd")
22+
HANDLE_SWIFT_SECTION(assocty, "__swift5_assocty", "swift5_assocty", ".sw5asty")
23+
HANDLE_SWIFT_SECTION(builtin, "__swift5_builtin", "swift5_builtin", ".sw5bltn")
24+
HANDLE_SWIFT_SECTION(capture, "__swift5_capture", "swift5_capture", ".sw5cptr")
25+
HANDLE_SWIFT_SECTION(typeref, "__swift5_typeref", "swift5_typeref", ".sw5tyrf")
26+
HANDLE_SWIFT_SECTION(reflstr, "__swift5_reflstr", "swift5_reflstr", ".sw5rfst")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===-- llvm/BinaryFormat/Swift.h ---Swift Constants-------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
8+
#ifndef LLVM_BINARYFORMAT_SWIFT_H
9+
#define LLVM_BINARYFORMAT_SWIFT_H
10+
11+
namespace llvm {
12+
namespace swift {
13+
14+
enum Swift5ReflectionSectionKind {
15+
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) KIND,
16+
#include "llvm/BinaryFormat/Swift.def"
17+
#undef HANDLE_SWIFT_SECTION
18+
unknown,
19+
last = unknown
20+
};
21+
} // end of namespace swift
22+
} // end of namespace llvm
23+
24+
#endif

llvm/include/llvm/DWARFLinker/DWARFStreamer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_DWARFLINKER_DWARFSTREAMER_H
1010
#define LLVM_DWARFLINKER_DWARFSTREAMER_H
1111

12+
#include "llvm/BinaryFormat/Swift.h"
1213
#include "llvm/CodeGen/AccelTable.h"
1314
#include "llvm/CodeGen/AsmPrinter.h"
1415
#include "llvm/DWARFLinker/DWARFLinker.h"
@@ -48,7 +49,7 @@ class DwarfStreamer : public DwarfEmitter {
4849
: OutFile(OutFile), OutFileType(OutFileType), Translator(Translator),
4950
ErrorHandler(Error), WarningHandler(Warning) {}
5051

51-
bool init(Triple TheTriple);
52+
bool init(Triple TheTriple, StringRef Swift5ReflectionSegmentName);
5253

5354
/// Dump the file to the disk.
5455
void finish();
@@ -85,6 +86,11 @@ class DwarfStreamer : public DwarfEmitter {
8586
/// Emit the swift_ast section stored in \p Buffer.
8687
void emitSwiftAST(StringRef Buffer);
8788

89+
/// Emit the swift reflection section stored in \p Buffer.
90+
void emitSwiftReflectionSection(
91+
llvm::swift::Swift5ReflectionSectionKind ReflSectionKind,
92+
StringRef Buffer, uint32_t Alignment, uint32_t Size);
93+
8894
/// Emit debug_ranges for \p FuncRange by translating the
8995
/// original \p Entries.
9096
void emitRangesEntries(

llvm/include/llvm/MC/MCContext.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ namespace llvm {
8080
private:
8181
Environment Env;
8282

83+
/// The name of the Segment where Swift5 Reflection Section data will be
84+
/// outputted
85+
StringRef Swift5ReflectionSegmentName;
86+
8387
/// The triple for this object.
8488
Triple TT;
8589

@@ -399,13 +403,17 @@ namespace llvm {
399403
const MCRegisterInfo *MRI, const MCSubtargetInfo *MSTI,
400404
const SourceMgr *Mgr = nullptr,
401405
MCTargetOptions const *TargetOpts = nullptr,
402-
bool DoAutoReset = true);
406+
bool DoAutoReset = true,
407+
StringRef Swift5ReflSegmentName = {});
403408
MCContext(const MCContext &) = delete;
404409
MCContext &operator=(const MCContext &) = delete;
405410
~MCContext();
406411

407412
Environment getObjectFileType() const { return Env; }
408413

414+
const StringRef &getSwift5ReflectionSegmentName() const {
415+
return Swift5ReflectionSegmentName;
416+
}
409417
const Triple &getTargetTriple() const { return TT; }
410418
const SourceMgr *getSourceManager() const { return SrcMgr; }
411419

llvm/include/llvm/MC/MCObjectFileInfo.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/ADT/DenseMap.h"
1717
#include "llvm/ADT/Triple.h"
18+
#include "llvm/BinaryFormat/Swift.h"
1819
#include "llvm/MC/MCSymbol.h"
1920
#include "llvm/Support/CodeGen.h"
2021
#include "llvm/Support/VersionTuple.h"
@@ -228,6 +229,10 @@ class MCObjectFileInfo {
228229
MCSection *ReadOnly8Section = nullptr;
229230
MCSection *ReadOnly16Section = nullptr;
230231

232+
// Swift5 Reflection Data Sections
233+
std::array<MCSection *, swift::Swift5ReflectionSectionKind::last>
234+
Swift5ReflectionSections = {};
235+
231236
public:
232237
void initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
233238
bool LargeCodeModel = false);
@@ -423,6 +428,14 @@ class MCObjectFileInfo {
423428

424429
bool isPositionIndependent() const { return PositionIndependent; }
425430

431+
// Swift5 Reflection Data Sections
432+
MCSection *getSwift5ReflectionSection(
433+
llvm::swift::Swift5ReflectionSectionKind ReflSectionKind) {
434+
return ReflSectionKind != llvm::swift::Swift5ReflectionSectionKind::unknown
435+
? Swift5ReflectionSections[ReflSectionKind]
436+
: nullptr;
437+
}
438+
426439
private:
427440
bool PositionIndependent = false;
428441
MCContext *Ctx = nullptr;

llvm/include/llvm/Object/MachO.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/ADT/Triple.h"
2323
#include "llvm/ADT/iterator_range.h"
2424
#include "llvm/BinaryFormat/MachO.h"
25+
#include "llvm/BinaryFormat/Swift.h"
2526
#include "llvm/MC/SubtargetFeature.h"
2627
#include "llvm/Object/Binary.h"
2728
#include "llvm/Object/ObjectFile.h"
@@ -583,6 +584,9 @@ class MachOObjectFile : public ObjectFile {
583584

584585
StringRef mapDebugSectionName(StringRef Name) const override;
585586

587+
llvm::swift::Swift5ReflectionSectionKind
588+
mapReflectionSectionNameToEnumValue(StringRef SectionName) const override;
589+
586590
bool hasPageZeroSegment() const { return HasPageZeroSegment; }
587591

588592
static bool classof(const Binary *v) {

llvm/include/llvm/Object/ObjectFile.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/Triple.h"
1919
#include "llvm/ADT/iterator_range.h"
2020
#include "llvm/BinaryFormat/Magic.h"
21+
#include "llvm/BinaryFormat/Swift.h"
2122
#include "llvm/Object/Binary.h"
2223
#include "llvm/Object/Error.h"
2324
#include "llvm/Object/SymbolicFile.h"
@@ -290,6 +291,11 @@ class ObjectFile : public SymbolicFile {
290291
virtual void getRelocationTypeName(DataRefImpl Rel,
291292
SmallVectorImpl<char> &Result) const = 0;
292293

294+
virtual llvm::swift::Swift5ReflectionSectionKind
295+
mapReflectionSectionNameToEnumValue(StringRef SectionName) const {
296+
return llvm::swift::Swift5ReflectionSectionKind::unknown;
297+
};
298+
293299
Expected<uint64_t> getSymbolValue(DataRefImpl Symb) const;
294300

295301
public:

llvm/lib/DWARFLinker/DWARFStreamer.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727

2828
namespace llvm {
2929

30-
bool DwarfStreamer::init(Triple TheTriple) {
30+
bool DwarfStreamer::init(Triple TheTriple,
31+
StringRef Swift5ReflectionSegmentName) {
3132
std::string ErrorStr;
3233
std::string TripleName;
3334
StringRef Context = "dwarf streamer init";
@@ -54,8 +55,9 @@ bool DwarfStreamer::init(Triple TheTriple) {
5455
if (!MSTI)
5556
return error("no subtarget info for target " + TripleName, Context), false;
5657

57-
MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get()));
58-
MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false));
58+
MC.reset(new MCContext(TheTriple, MAI.get(), MRI.get(), MSTI.get(), nullptr,
59+
nullptr, true, Swift5ReflectionSegmentName));
60+
MOFI.reset(TheTarget->createMCObjectFileInfo(*MC, /*PIC=*/false, false));
5961
MC->setObjectFileInfo(MOFI.get());
6062

6163
MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, MCOptions);
@@ -302,6 +304,18 @@ void DwarfStreamer::emitSwiftAST(StringRef Buffer) {
302304
MS->emitBytes(Buffer);
303305
}
304306

307+
void DwarfStreamer::emitSwiftReflectionSection(
308+
llvm::swift::Swift5ReflectionSectionKind ReflSectionKind, StringRef Buffer,
309+
uint32_t Alignment, uint32_t Size) {
310+
MCSection *ReflectionSection =
311+
MOFI->getSwift5ReflectionSection(ReflSectionKind);
312+
if (ReflectionSection == nullptr)
313+
return;
314+
ReflectionSection->setAlignment(Align(Alignment));
315+
MS->SwitchSection(ReflectionSection);
316+
MS->emitBytes(Buffer);
317+
}
318+
305319
/// Emit the debug_range section contents for \p FuncRange by
306320
/// translating the original \p Entries. The debug_range section
307321
/// format is totally trivial, consisting just of pairs of address

llvm/lib/MC/MCContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ static void defaultDiagHandler(const SMDiagnostic &SMD, bool, const SourceMgr &,
6767
MCContext::MCContext(const Triple &TheTriple, const MCAsmInfo *mai,
6868
const MCRegisterInfo *mri, const MCSubtargetInfo *msti,
6969
const SourceMgr *mgr, MCTargetOptions const *TargetOpts,
70-
bool DoAutoReset)
71-
: TT(TheTriple), SrcMgr(mgr), InlineSrcMgr(nullptr),
72-
DiagHandler(defaultDiagHandler), MAI(mai), MRI(mri), MSTI(msti),
73-
Symbols(Allocator), UsedNames(Allocator),
70+
bool DoAutoReset, StringRef Swift5ReflSegmentName)
71+
: Swift5ReflectionSegmentName(Swift5ReflSegmentName), TT(TheTriple),
72+
SrcMgr(mgr), InlineSrcMgr(nullptr), DiagHandler(defaultDiagHandler),
73+
MAI(mai), MRI(mri), MSTI(msti), Symbols(Allocator), UsedNames(Allocator),
7474
InlineAsmUsedLabelNames(Allocator),
7575
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
7676
AutoReset(DoAutoReset), TargetOptions(TargetOpts) {

llvm/lib/MC/MCObjectFileInfo.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,17 @@ void MCObjectFileInfo::initMachOMCObjectFileInfo(const Triple &T) {
299299
RemarksSection = Ctx->getMachOSection(
300300
"__LLVM", "__remarks", MachO::S_ATTR_DEBUG, SectionKind::getMetadata());
301301

302+
// The architecture of dsymutil makes it very difficult to copy the Swift
303+
// reflection metadata sections into the __TEXT segment, so dsymutil creates
304+
// these sections in the __DWARF segment instead.
305+
if (!Ctx->getSwift5ReflectionSegmentName().empty()) {
306+
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \
307+
Swift5ReflectionSections[llvm::swift::Swift5ReflectionSectionKind::KIND] = \
308+
Ctx->getMachOSection(Ctx->getSwift5ReflectionSegmentName().data(), \
309+
MACHO, 0, SectionKind::getMetadata());
310+
#include "llvm/BinaryFormat/Swift.def"
311+
}
312+
302313
TLSExtraDataSection = TLSTLVSection;
303314
}
304315

llvm/lib/Object/MachOObjectFile.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/ADT/Triple.h"
2121
#include "llvm/ADT/Twine.h"
2222
#include "llvm/BinaryFormat/MachO.h"
23+
#include "llvm/BinaryFormat/Swift.h"
2324
#include "llvm/Object/Error.h"
2425
#include "llvm/Object/MachO.h"
2526
#include "llvm/Object/ObjectFile.h"
@@ -4765,3 +4766,14 @@ MachOObjectFile::findDsymObjectMembers(StringRef Path) {
47654766
Path.str().c_str());
47664767
return ObjectPaths;
47674768
}
4769+
4770+
llvm::swift::Swift5ReflectionSectionKind
4771+
MachOObjectFile::mapReflectionSectionNameToEnumValue(
4772+
StringRef SectionName) const {
4773+
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \
4774+
.Case(MACHO, llvm::swift::Swift5ReflectionSectionKind::KIND)
4775+
return StringSwitch<llvm::swift::Swift5ReflectionSectionKind>(SectionName)
4776+
#include "llvm/BinaryFormat/Swift.def"
4777+
.Default(llvm::swift::Swift5ReflectionSectionKind::unknown);
4778+
#undef HANDLE_SWIFT_SECTION
4779+
}

0 commit comments

Comments
 (0)