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/LLVMDriver.h"
31 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/TargetSelect.h"
33 #include "llvm/Support/ToolOutputFile.h"
37 using namespace llvm::object
;
39 static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags
;
41 // Command-line option boilerplate.
44 OPT_INVALID
= 0, // This is not an option ID.
45 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
50 #define PREFIX(NAME, VALUE) \
51 static constexpr StringLiteral NAME##_init[] = VALUE; \
52 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
53 std::size(NAME##_init) - 1);
57 using namespace llvm::opt
;
58 static constexpr opt::OptTable::Info InfoTable
[] = {
59 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
64 class DwpOptTable
: public opt::GenericOptTable
{
66 DwpOptTable() : GenericOptTable(InfoTable
) {}
68 } // end anonymous namespace
71 static std::vector
<std::string
> ExecFilenames
;
72 static std::string OutputFilename
;
73 static std::string ContinueOption
;
75 static Expected
<SmallVector
<std::string
, 16>>
76 getDWOFilenames(StringRef ExecFilename
) {
77 auto ErrOrObj
= object::ObjectFile::createObjectFile(ExecFilename
);
79 return ErrOrObj
.takeError();
81 const ObjectFile
&Obj
= *ErrOrObj
.get().getBinary();
82 std::unique_ptr
<DWARFContext
> DWARFCtx
= DWARFContext::create(Obj
);
84 SmallVector
<std::string
, 16> DWOPaths
;
85 for (const auto &CU
: DWARFCtx
->compile_units()) {
86 const DWARFDie
&Die
= CU
->getUnitDIE();
87 std::string DWOName
= dwarf::toString(
88 Die
.find({dwarf::DW_AT_dwo_name
, dwarf::DW_AT_GNU_dwo_name
}), "");
91 std::string DWOCompDir
=
92 dwarf::toString(Die
.find(dwarf::DW_AT_comp_dir
), "");
93 if (!DWOCompDir
.empty()) {
94 SmallString
<16> DWOPath(DWOName
);
95 sys::fs::make_absolute(DWOCompDir
, DWOPath
);
96 if (!sys::fs::exists(DWOPath
) && sys::fs::exists(DWOName
))
97 DWOPaths
.push_back(std::move(DWOName
));
99 DWOPaths
.emplace_back(DWOPath
.data(), DWOPath
.size());
101 DWOPaths
.push_back(std::move(DWOName
));
104 return std::move(DWOPaths
);
107 static int error(const Twine
&Error
, const Twine
&Context
) {
108 errs() << Twine("while processing ") + Context
+ ":\n";
109 errs() << Twine("error: ") + Error
+ "\n";
113 static Expected
<Triple
> readTargetTriple(StringRef FileName
) {
114 auto ErrOrObj
= object::ObjectFile::createObjectFile(FileName
);
116 return ErrOrObj
.takeError();
118 return ErrOrObj
->getBinary()->makeTriple();
121 int llvm_dwp_main(int argc
, char **argv
, const llvm::ToolContext
&) {
123 llvm::BumpPtrAllocator A
;
124 llvm::StringSaver Saver
{A
};
125 OnCuIndexOverflow OverflowOptValue
= OnCuIndexOverflow::HardStop
;
126 opt::InputArgList Args
=
127 Tbl
.parseArgs(argc
, argv
, OPT_UNKNOWN
, Saver
, [&](StringRef Msg
) {
128 llvm::errs() << Msg
<< '\n';
132 if (Args
.hasArg(OPT_help
)) {
133 Tbl
.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
134 "merge split dwarf (.dwo) files");
138 if (Args
.hasArg(OPT_version
)) {
139 llvm::cl::PrintVersionMessage();
143 OutputFilename
= Args
.getLastArgValue(OPT_outputFileName
, "");
144 if (Arg
*Arg
= Args
.getLastArg(OPT_continueOnCuIndexOverflow
,
145 OPT_continueOnCuIndexOverflow_EQ
)) {
146 if (Arg
->getOption().matches(OPT_continueOnCuIndexOverflow
)) {
147 OverflowOptValue
= OnCuIndexOverflow::Continue
;
149 ContinueOption
= Arg
->getValue();
150 if (ContinueOption
== "soft-stop") {
151 OverflowOptValue
= OnCuIndexOverflow::SoftStop
;
152 } else if (ContinueOption
== "continue") {
153 OverflowOptValue
= OnCuIndexOverflow::Continue
;
155 llvm::errs() << "invalid value for --continue-on-cu-index-overflow"
156 << ContinueOption
<< '\n';
162 for (const llvm::opt::Arg
*A
: Args
.filtered(OPT_execFileNames
))
163 ExecFilenames
.emplace_back(A
->getValue());
165 std::vector
<std::string
> DWOFilenames
;
166 for (const llvm::opt::Arg
*A
: Args
.filtered(OPT_INPUT
))
167 DWOFilenames
.emplace_back(A
->getValue());
169 llvm::InitializeAllTargetInfos();
170 llvm::InitializeAllTargetMCs();
171 llvm::InitializeAllTargets();
172 llvm::InitializeAllAsmPrinters();
174 for (const auto &ExecFilename
: ExecFilenames
) {
175 auto DWOs
= getDWOFilenames(ExecFilename
);
177 logAllUnhandledErrors(
178 handleErrors(DWOs
.takeError(),
179 [&](std::unique_ptr
<ECError
> EC
) -> Error
{
180 return createFileError(ExecFilename
,
181 Error(std::move(EC
)));
186 DWOFilenames
.insert(DWOFilenames
.end(),
187 std::make_move_iterator(DWOs
->begin()),
188 std::make_move_iterator(DWOs
->end()));
191 if (DWOFilenames
.empty()) {
192 WithColor::defaultWarningHandler(make_error
<DWPError
>(
193 "executable file does not contain any references to dwo files"));
197 std::string ErrorStr
;
198 StringRef Context
= "dwarf streamer init";
200 auto ErrOrTriple
= readTargetTriple(DWOFilenames
.front());
202 logAllUnhandledErrors(
203 handleErrors(ErrOrTriple
.takeError(),
204 [&](std::unique_ptr
<ECError
> EC
) -> Error
{
205 return createFileError(DWOFilenames
.front(),
206 Error(std::move(EC
)));
213 const Target
*TheTarget
=
214 TargetRegistry::lookupTarget("", *ErrOrTriple
, ErrorStr
);
216 return error(ErrorStr
, Context
);
217 std::string TripleName
= ErrOrTriple
->getTriple();
219 // Create all the MC Objects.
220 std::unique_ptr
<MCRegisterInfo
> MRI(TheTarget
->createMCRegInfo(TripleName
));
222 return error(Twine("no register info for target ") + TripleName
, Context
);
224 MCTargetOptions MCOptions
= llvm::mc::InitMCTargetOptionsFromFlags();
225 std::unique_ptr
<MCAsmInfo
> MAI(
226 TheTarget
->createMCAsmInfo(*MRI
, TripleName
, MCOptions
));
228 return error("no asm info for target " + TripleName
, Context
);
230 std::unique_ptr
<MCSubtargetInfo
> MSTI(
231 TheTarget
->createMCSubtargetInfo(TripleName
, "", ""));
233 return error("no subtarget info for target " + TripleName
, Context
);
235 MCContext
MC(*ErrOrTriple
, MAI
.get(), MRI
.get(), MSTI
.get());
236 std::unique_ptr
<MCObjectFileInfo
> MOFI(
237 TheTarget
->createMCObjectFileInfo(MC
, /*PIC=*/false));
238 MC
.setObjectFileInfo(MOFI
.get());
240 MCTargetOptions Options
;
241 auto MAB
= TheTarget
->createMCAsmBackend(*MSTI
, *MRI
, Options
);
243 return error("no asm backend for target " + TripleName
, Context
);
245 std::unique_ptr
<MCInstrInfo
> MII(TheTarget
->createMCInstrInfo());
247 return error("no instr info info for target " + TripleName
, Context
);
249 MCCodeEmitter
*MCE
= TheTarget
->createMCCodeEmitter(*MII
, MC
);
251 return error("no code emitter for target " + TripleName
, Context
);
253 // Create the output file.
255 ToolOutputFile
OutFile(OutputFilename
, EC
, sys::fs::OF_None
);
256 std::optional
<buffer_ostream
> BOS
;
257 raw_pwrite_stream
*OS
;
259 return error(Twine(OutputFilename
) + ": " + EC
.message(), Context
);
260 if (OutFile
.os().supportsSeeking()) {
263 BOS
.emplace(OutFile
.os());
267 std::unique_ptr
<MCStreamer
> MS(TheTarget
->createMCObjectStreamer(
268 *ErrOrTriple
, MC
, std::unique_ptr
<MCAsmBackend
>(MAB
),
269 MAB
->createObjectWriter(*OS
), std::unique_ptr
<MCCodeEmitter
>(MCE
),
272 return error("no object streamer for target " + TripleName
, Context
);
274 if (auto Err
= write(*MS
, DWOFilenames
, OverflowOptValue
)) {
275 logAllUnhandledErrors(std::move(Err
), WithColor::error());