1 //===- DWARFEmitterImpl.cpp -----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DWARFEmitterImpl.h"
10 #include "llvm/DWARFLinker/DWARFLinkerCompileUnit.h"
11 #include "llvm/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCObjectWriter.h"
14 #include "llvm/MC/MCSubtargetInfo.h"
15 #include "llvm/MC/MCTargetOptions.h"
16 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
17 #include "llvm/MC/TargetRegistry.h"
18 #include "llvm/Support/FormattedStream.h"
21 namespace dwarflinker_parallel
{
23 Error
DwarfEmitterImpl::init(Triple TheTriple
,
24 StringRef Swift5ReflectionSegmentName
) {
26 std::string TripleName
;
29 const Target
*TheTarget
=
30 TargetRegistry::lookupTarget(TripleName
, TheTriple
, ErrorStr
);
32 return createStringError(std::errc::invalid_argument
, ErrorStr
.c_str());
33 TripleName
= TheTriple
.getTriple();
35 // Create all the MC Objects.
36 MRI
.reset(TheTarget
->createMCRegInfo(TripleName
));
38 return createStringError(std::errc::invalid_argument
,
39 "no register info for target %s",
42 MCTargetOptions MCOptions
= mc::InitMCTargetOptionsFromFlags();
43 MAI
.reset(TheTarget
->createMCAsmInfo(*MRI
, TripleName
, MCOptions
));
45 return createStringError(std::errc::invalid_argument
,
46 "no asm info for target %s", TripleName
.c_str());
48 MSTI
.reset(TheTarget
->createMCSubtargetInfo(TripleName
, "", ""));
50 return createStringError(std::errc::invalid_argument
,
51 "no subtarget info for target %s",
54 MC
.reset(new MCContext(TheTriple
, MAI
.get(), MRI
.get(), MSTI
.get(), nullptr,
55 nullptr, true, Swift5ReflectionSegmentName
));
56 MOFI
.reset(TheTarget
->createMCObjectFileInfo(*MC
, /*PIC=*/false, false));
57 MC
->setObjectFileInfo(MOFI
.get());
59 MAB
= TheTarget
->createMCAsmBackend(*MSTI
, *MRI
, MCOptions
);
61 return createStringError(std::errc::invalid_argument
,
62 "no asm backend for target %s",
65 MII
.reset(TheTarget
->createMCInstrInfo());
67 return createStringError(std::errc::invalid_argument
,
68 "no instr info info for target %s",
71 MCE
= TheTarget
->createMCCodeEmitter(*MII
, *MC
);
73 return createStringError(std::errc::invalid_argument
,
74 "no code emitter for target %s",
77 switch (OutFileType
) {
78 case DWARFLinker::OutputFileType::Assembly
: {
79 MIP
= TheTarget
->createMCInstPrinter(TheTriple
, MAI
->getAssemblerDialect(),
81 MS
= TheTarget
->createAsmStreamer(
82 *MC
, std::make_unique
<formatted_raw_ostream
>(OutFile
), true, true, MIP
,
83 std::unique_ptr
<MCCodeEmitter
>(MCE
), std::unique_ptr
<MCAsmBackend
>(MAB
),
87 case DWARFLinker::OutputFileType::Object
: {
88 MS
= TheTarget
->createMCObjectStreamer(
89 TheTriple
, *MC
, std::unique_ptr
<MCAsmBackend
>(MAB
),
90 MAB
->createObjectWriter(OutFile
), std::unique_ptr
<MCCodeEmitter
>(MCE
),
91 *MSTI
, MCOptions
.MCRelaxAll
, MCOptions
.MCIncrementalLinkerCompatible
,
92 /*DWARFMustBeAtTheEnd*/ false);
98 return createStringError(std::errc::invalid_argument
,
99 "no object streamer for target %s",
102 // Finally create the AsmPrinter we'll use to emit the DIEs.
103 TM
.reset(TheTarget
->createTargetMachine(TripleName
, "", "", TargetOptions(),
106 return createStringError(std::errc::invalid_argument
,
107 "no target machine for target %s",
110 Asm
.reset(TheTarget
->createAsmPrinter(*TM
, std::unique_ptr
<MCStreamer
>(MS
)));
112 return createStringError(std::errc::invalid_argument
,
113 "no asm printer for target %s",
115 Asm
->setDwarfUsesRelocationsAcrossSections(false);
117 RangesSectionSize
= 0;
118 RngListsSectionSize
= 0;
120 LocListsSectionSize
= 0;
122 FrameSectionSize
= 0;
123 DebugInfoSectionSize
= 0;
124 MacInfoSectionSize
= 0;
125 MacroSectionSize
= 0;
127 return Error::success();
130 } // end of namespace dwarflinker_parallel