1 //===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
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 // A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
12 //===----------------------------------------------------------------------===//
13 #include "llvm/DWP/DWP.h"
14 #include "llvm/DWP/DWPError.h"
15 #include "llvm/DWP/DWPStringPool.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
25 #include "llvm/MC/TargetRegistry.h"
26 #include "llvm/Option/ArgList.h"
27 #include "llvm/Option/Option.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/InitLLVM.h"
31 #include "llvm/Support/LLVMDriver.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/TargetSelect.h"
34 #include "llvm/Support/ToolOutputFile.h"
38 using namespace llvm::object
;
40 static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags
;
42 // Command-line option boilerplate.
45 OPT_INVALID
= 0, // This is not an option ID.
46 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
51 #define PREFIX(NAME, VALUE) \
52 static constexpr StringLiteral NAME##_init[] = VALUE; \
53 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
54 std::size(NAME##_init) - 1);
58 using namespace llvm::opt
;
59 static constexpr opt::OptTable::Info InfoTable
[] = {
60 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
65 class DwpOptTable
: public opt::GenericOptTable
{
67 DwpOptTable() : GenericOptTable(InfoTable
) {}
69 } // end anonymous namespace
72 static std::vector
<std::string
> ExecFilenames
;
73 static std::string OutputFilename
;
74 static bool ContinueOnCuIndexOverflow
;
76 static Expected
<SmallVector
<std::string
, 16>>
77 getDWOFilenames(StringRef ExecFilename
) {
78 auto ErrOrObj
= object::ObjectFile::createObjectFile(ExecFilename
);
80 return ErrOrObj
.takeError();
82 const ObjectFile
&Obj
= *ErrOrObj
.get().getBinary();
83 std::unique_ptr
<DWARFContext
> DWARFCtx
= DWARFContext::create(Obj
);
85 SmallVector
<std::string
, 16> DWOPaths
;
86 for (const auto &CU
: DWARFCtx
->compile_units()) {
87 const DWARFDie
&Die
= CU
->getUnitDIE();
88 std::string DWOName
= dwarf::toString(
89 Die
.find({dwarf::DW_AT_dwo_name
, dwarf::DW_AT_GNU_dwo_name
}), "");
92 std::string DWOCompDir
=
93 dwarf::toString(Die
.find(dwarf::DW_AT_comp_dir
), "");
94 if (!DWOCompDir
.empty()) {
95 SmallString
<16> DWOPath(std::move(DWOName
));
96 sys::fs::make_absolute(DWOCompDir
, DWOPath
);
97 if (!sys::fs::exists(DWOPath
) && sys::fs::exists(DWOName
))
98 DWOPaths
.push_back(std::move(DWOName
));
100 DWOPaths
.emplace_back(DWOPath
.data(), DWOPath
.size());
102 DWOPaths
.push_back(std::move(DWOName
));
105 return std::move(DWOPaths
);
108 static int error(const Twine
&Error
, const Twine
&Context
) {
109 errs() << Twine("while processing ") + Context
+ ":\n";
110 errs() << Twine("error: ") + Error
+ "\n";
114 static Expected
<Triple
> readTargetTriple(StringRef FileName
) {
115 auto ErrOrObj
= object::ObjectFile::createObjectFile(FileName
);
117 return ErrOrObj
.takeError();
119 return ErrOrObj
->getBinary()->makeTriple();
122 int llvm_dwp_main(int argc
, char **argv
, const llvm::ToolContext
&) {
123 InitLLVM
X(argc
, argv
);
126 llvm::BumpPtrAllocator A
;
127 llvm::StringSaver Saver
{A
};
128 opt::InputArgList Args
=
129 Tbl
.parseArgs(argc
, argv
, OPT_UNKNOWN
, Saver
, [&](StringRef Msg
) {
130 llvm::errs() << Msg
<< '\n';
134 if (Args
.hasArg(OPT_help
)) {
135 Tbl
.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
136 "merge split dwarf (.dwo) files");
140 if (Args
.hasArg(OPT_version
)) {
141 llvm::cl::PrintVersionMessage();
145 OutputFilename
= Args
.getLastArgValue(OPT_outputFileName
, "");
146 ContinueOnCuIndexOverflow
= Args
.hasArg(OPT_continueOnCuIndexOverflow
);
148 for (const llvm::opt::Arg
*A
: Args
.filtered(OPT_execFileNames
))
149 ExecFilenames
.emplace_back(A
->getValue());
151 std::vector
<std::string
> DWOFilenames
;
152 for (const llvm::opt::Arg
*A
: Args
.filtered(OPT_INPUT
))
153 DWOFilenames
.emplace_back(A
->getValue());
155 llvm::InitializeAllTargetInfos();
156 llvm::InitializeAllTargetMCs();
157 llvm::InitializeAllTargets();
158 llvm::InitializeAllAsmPrinters();
160 for (const auto &ExecFilename
: ExecFilenames
) {
161 auto DWOs
= getDWOFilenames(ExecFilename
);
163 logAllUnhandledErrors(
164 handleErrors(DWOs
.takeError(),
165 [&](std::unique_ptr
<ECError
> EC
) -> Error
{
166 return createFileError(ExecFilename
,
167 Error(std::move(EC
)));
172 DWOFilenames
.insert(DWOFilenames
.end(),
173 std::make_move_iterator(DWOs
->begin()),
174 std::make_move_iterator(DWOs
->end()));
177 if (DWOFilenames
.empty())
180 std::string ErrorStr
;
181 StringRef Context
= "dwarf streamer init";
183 auto ErrOrTriple
= readTargetTriple(DWOFilenames
.front());
185 logAllUnhandledErrors(
186 handleErrors(ErrOrTriple
.takeError(),
187 [&](std::unique_ptr
<ECError
> EC
) -> Error
{
188 return createFileError(DWOFilenames
.front(),
189 Error(std::move(EC
)));
196 const Target
*TheTarget
=
197 TargetRegistry::lookupTarget("", *ErrOrTriple
, ErrorStr
);
199 return error(ErrorStr
, Context
);
200 std::string TripleName
= ErrOrTriple
->getTriple();
202 // Create all the MC Objects.
203 std::unique_ptr
<MCRegisterInfo
> MRI(TheTarget
->createMCRegInfo(TripleName
));
205 return error(Twine("no register info for target ") + TripleName
, Context
);
207 MCTargetOptions MCOptions
= llvm::mc::InitMCTargetOptionsFromFlags();
208 std::unique_ptr
<MCAsmInfo
> MAI(
209 TheTarget
->createMCAsmInfo(*MRI
, TripleName
, MCOptions
));
211 return error("no asm info for target " + TripleName
, Context
);
213 std::unique_ptr
<MCSubtargetInfo
> MSTI(
214 TheTarget
->createMCSubtargetInfo(TripleName
, "", ""));
216 return error("no subtarget info for target " + TripleName
, Context
);
218 MCContext
MC(*ErrOrTriple
, MAI
.get(), MRI
.get(), MSTI
.get());
219 std::unique_ptr
<MCObjectFileInfo
> MOFI(
220 TheTarget
->createMCObjectFileInfo(MC
, /*PIC=*/false));
221 MC
.setObjectFileInfo(MOFI
.get());
223 MCTargetOptions Options
;
224 auto MAB
= TheTarget
->createMCAsmBackend(*MSTI
, *MRI
, Options
);
226 return error("no asm backend for target " + TripleName
, Context
);
228 std::unique_ptr
<MCInstrInfo
> MII(TheTarget
->createMCInstrInfo());
230 return error("no instr info info for target " + TripleName
, Context
);
232 MCCodeEmitter
*MCE
= TheTarget
->createMCCodeEmitter(*MII
, MC
);
234 return error("no code emitter for target " + TripleName
, Context
);
236 // Create the output file.
238 ToolOutputFile
OutFile(OutputFilename
, EC
, sys::fs::OF_None
);
239 std::optional
<buffer_ostream
> BOS
;
240 raw_pwrite_stream
*OS
;
242 return error(Twine(OutputFilename
) + ": " + EC
.message(), Context
);
243 if (OutFile
.os().supportsSeeking()) {
246 BOS
.emplace(OutFile
.os());
250 std::unique_ptr
<MCStreamer
> MS(TheTarget
->createMCObjectStreamer(
251 *ErrOrTriple
, MC
, std::unique_ptr
<MCAsmBackend
>(MAB
),
252 MAB
->createObjectWriter(*OS
), std::unique_ptr
<MCCodeEmitter
>(MCE
), *MSTI
,
253 MCOptions
.MCRelaxAll
, MCOptions
.MCIncrementalLinkerCompatible
,
254 /*DWARFMustBeAtTheEnd*/ false));
256 return error("no object streamer for target " + TripleName
, Context
);
258 if (auto Err
= write(*MS
, DWOFilenames
, ContinueOnCuIndexOverflow
)) {
259 logAllUnhandledErrors(std::move(Err
), WithColor::error());