Skip to content

Commit 8d8789a

Browse files
Merge pull request #3855 from rastogishubham/76973336
Emit swift5 reflection section data in dsym bundle generated by dsymutil in the Dwarf section.
2 parents a9a5fb5 + 93c35dd commit 8d8789a

File tree

16 files changed

+1841
-11
lines changed

16 files changed

+1841
-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 binaryformat {
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 binaryformat
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::binaryformat::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: 14 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 *, binaryformat::Swift5ReflectionSectionKind::last>
234+
Swift5ReflectionSections = {};
235+
231236
public:
232237
void initMCObjectFileInfo(MCContext &MCCtx, bool PIC,
233238
bool LargeCodeModel = false);
@@ -423,6 +428,15 @@ class MCObjectFileInfo {
423428

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

431+
// Swift5 Reflection Data Sections
432+
MCSection *getSwift5ReflectionSection(
433+
llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind) {
434+
return ReflSectionKind !=
435+
llvm::binaryformat::Swift5ReflectionSectionKind::unknown
436+
? Swift5ReflectionSections[ReflSectionKind]
437+
: nullptr;
438+
}
439+
426440
private:
427441
bool PositionIndependent = false;
428442
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"
@@ -580,6 +581,9 @@ class MachOObjectFile : public ObjectFile {
580581

581582
StringRef mapDebugSectionName(StringRef Name) const override;
582583

584+
llvm::binaryformat::Swift5ReflectionSectionKind
585+
mapReflectionSectionNameToEnumValue(StringRef SectionName) const override;
586+
583587
bool hasPageZeroSegment() const { return HasPageZeroSegment; }
584588

585589
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"
@@ -291,6 +292,11 @@ class ObjectFile : public SymbolicFile {
291292
virtual void getRelocationTypeName(DataRefImpl Rel,
292293
SmallVectorImpl<char> &Result) const = 0;
293294

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

296302
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::binaryformat::Swift5ReflectionSectionKind ReflSectionKind,
309+
StringRef Buffer, 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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,18 @@ 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 \
308+
[llvm::binaryformat::Swift5ReflectionSectionKind::KIND] = \
309+
Ctx->getMachOSection(Ctx->getSwift5ReflectionSegmentName().data(), \
310+
MACHO, 0, SectionKind::getMetadata());
311+
#include "llvm/BinaryFormat/Swift.def"
312+
}
313+
302314
TLSExtraDataSection = TLSTLVSection;
303315
}
304316

llvm/lib/Object/MachOObjectFile.cpp

Lines changed: 13 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"
@@ -4682,3 +4683,15 @@ StringRef MachOObjectFile::mapDebugSectionName(StringRef Name) const {
46824683
.Case("debug_str_offs", "debug_str_offsets")
46834684
.Default(Name);
46844685
}
4686+
4687+
llvm::binaryformat::Swift5ReflectionSectionKind
4688+
MachOObjectFile::mapReflectionSectionNameToEnumValue(
4689+
StringRef SectionName) const {
4690+
#define HANDLE_SWIFT_SECTION(KIND, MACHO, ELF, COFF) \
4691+
.Case(MACHO, llvm::binaryformat::Swift5ReflectionSectionKind::KIND)
4692+
return StringSwitch<llvm::binaryformat::Swift5ReflectionSectionKind>(
4693+
SectionName)
4694+
#include "llvm/BinaryFormat/Swift.def"
4695+
.Default(llvm::binaryformat::Swift5ReflectionSectionKind::unknown);
4696+
#undef HANDLE_SWIFT_SECTION
4697+
}

0 commit comments

Comments
 (0)