xref: /freebsd/contrib/llvm-project/llvm/tools/llvm-dwp/llvm-dwp.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
162cfcf62SDimitry Andric //===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
262cfcf62SDimitry Andric //
362cfcf62SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
462cfcf62SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
562cfcf62SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
662cfcf62SDimitry Andric //
762cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
862cfcf62SDimitry Andric //
962cfcf62SDimitry Andric // A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
1062cfcf62SDimitry Andric // package files).
1162cfcf62SDimitry Andric //
1262cfcf62SDimitry Andric //===----------------------------------------------------------------------===//
13fe6060f1SDimitry Andric #include "llvm/DWP/DWP.h"
14fe6060f1SDimitry Andric #include "llvm/DWP/DWPError.h"
15fe6060f1SDimitry Andric #include "llvm/DWP/DWPStringPool.h"
1662cfcf62SDimitry Andric #include "llvm/MC/MCAsmBackend.h"
1762cfcf62SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
1862cfcf62SDimitry Andric #include "llvm/MC/MCCodeEmitter.h"
1962cfcf62SDimitry Andric #include "llvm/MC/MCContext.h"
2062cfcf62SDimitry Andric #include "llvm/MC/MCInstrInfo.h"
2162cfcf62SDimitry Andric #include "llvm/MC/MCObjectWriter.h"
2281ad6265SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
2381ad6265SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
245ffd83dbSDimitry Andric #include "llvm/MC/MCTargetOptionsCommandFlags.h"
25349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
26*06c3fb27SDimitry Andric #include "llvm/Option/ArgList.h"
27*06c3fb27SDimitry Andric #include "llvm/Option/Option.h"
285ffd83dbSDimitry Andric #include "llvm/Support/CommandLine.h"
2962cfcf62SDimitry Andric #include "llvm/Support/FileSystem.h"
3062cfcf62SDimitry Andric #include "llvm/Support/InitLLVM.h"
3181ad6265SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
3262cfcf62SDimitry Andric #include "llvm/Support/TargetSelect.h"
3362cfcf62SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
34bdd1243dSDimitry Andric #include <optional>
3562cfcf62SDimitry Andric 
3662cfcf62SDimitry Andric using namespace llvm;
3762cfcf62SDimitry Andric using namespace llvm::object;
3862cfcf62SDimitry Andric 
395ffd83dbSDimitry Andric static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
405ffd83dbSDimitry Andric 
41*06c3fb27SDimitry Andric // Command-line option boilerplate.
42*06c3fb27SDimitry Andric namespace {
43*06c3fb27SDimitry Andric enum ID {
44*06c3fb27SDimitry Andric   OPT_INVALID = 0, // This is not an option ID.
45*06c3fb27SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
46*06c3fb27SDimitry Andric                HELPTEXT, METAVAR, VALUES)                                      \
47*06c3fb27SDimitry Andric   OPT_##ID,
48*06c3fb27SDimitry Andric #include "Opts.inc"
49*06c3fb27SDimitry Andric #undef OPTION
50*06c3fb27SDimitry Andric };
5162cfcf62SDimitry Andric 
52*06c3fb27SDimitry Andric #define PREFIX(NAME, VALUE)                                                    \
53*06c3fb27SDimitry Andric   static constexpr StringLiteral NAME##_init[] = VALUE;                        \
54*06c3fb27SDimitry Andric   static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
55*06c3fb27SDimitry Andric                                                 std::size(NAME##_init) - 1);
56*06c3fb27SDimitry Andric #include "Opts.inc"
57*06c3fb27SDimitry Andric #undef PREFIX
5862cfcf62SDimitry Andric 
59*06c3fb27SDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
60*06c3fb27SDimitry Andric #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM,  \
61*06c3fb27SDimitry Andric                HELPTEXT, METAVAR, VALUES)                                      \
62*06c3fb27SDimitry Andric   {                                                                            \
63*06c3fb27SDimitry Andric       PREFIX,      NAME,      HELPTEXT,                                        \
64*06c3fb27SDimitry Andric       METAVAR,     OPT_##ID,  opt::Option::KIND##Class,                        \
65*06c3fb27SDimitry Andric       PARAM,       FLAGS,     OPT_##GROUP,                                     \
66*06c3fb27SDimitry Andric       OPT_##ALIAS, ALIASARGS, VALUES},
67*06c3fb27SDimitry Andric #include "Opts.inc"
68*06c3fb27SDimitry Andric #undef OPTION
69*06c3fb27SDimitry Andric };
70*06c3fb27SDimitry Andric 
71*06c3fb27SDimitry Andric class DwpOptTable : public opt::GenericOptTable {
72*06c3fb27SDimitry Andric public:
73*06c3fb27SDimitry Andric   DwpOptTable() : GenericOptTable(InfoTable) {}
74*06c3fb27SDimitry Andric };
75*06c3fb27SDimitry Andric } // end anonymous namespace
76*06c3fb27SDimitry Andric 
77*06c3fb27SDimitry Andric // Options
78*06c3fb27SDimitry Andric static std::vector<std::string> ExecFilenames;
79*06c3fb27SDimitry Andric static std::string OutputFilename;
80*06c3fb27SDimitry Andric static bool ContinueOnCuIndexOverflow;
8162cfcf62SDimitry Andric 
8262cfcf62SDimitry Andric static Expected<SmallVector<std::string, 16>>
8362cfcf62SDimitry Andric getDWOFilenames(StringRef ExecFilename) {
8462cfcf62SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
8562cfcf62SDimitry Andric   if (!ErrOrObj)
8662cfcf62SDimitry Andric     return ErrOrObj.takeError();
8762cfcf62SDimitry Andric 
8862cfcf62SDimitry Andric   const ObjectFile &Obj = *ErrOrObj.get().getBinary();
8962cfcf62SDimitry Andric   std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
9062cfcf62SDimitry Andric 
9162cfcf62SDimitry Andric   SmallVector<std::string, 16> DWOPaths;
9262cfcf62SDimitry Andric   for (const auto &CU : DWARFCtx->compile_units()) {
9362cfcf62SDimitry Andric     const DWARFDie &Die = CU->getUnitDIE();
9462cfcf62SDimitry Andric     std::string DWOName = dwarf::toString(
9562cfcf62SDimitry Andric         Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
9662cfcf62SDimitry Andric     if (DWOName.empty())
9762cfcf62SDimitry Andric       continue;
9862cfcf62SDimitry Andric     std::string DWOCompDir =
9962cfcf62SDimitry Andric         dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
10062cfcf62SDimitry Andric     if (!DWOCompDir.empty()) {
101d409305fSDimitry Andric       SmallString<16> DWOPath(std::move(DWOName));
102d409305fSDimitry Andric       sys::fs::make_absolute(DWOCompDir, DWOPath);
103bdd1243dSDimitry Andric       if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
104bdd1243dSDimitry Andric         DWOPaths.push_back(std::move(DWOName));
105bdd1243dSDimitry Andric       else
10662cfcf62SDimitry Andric         DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
10762cfcf62SDimitry Andric     } else {
10862cfcf62SDimitry Andric       DWOPaths.push_back(std::move(DWOName));
10962cfcf62SDimitry Andric     }
11062cfcf62SDimitry Andric   }
11162cfcf62SDimitry Andric   return std::move(DWOPaths);
11262cfcf62SDimitry Andric }
11362cfcf62SDimitry Andric 
11462cfcf62SDimitry Andric static int error(const Twine &Error, const Twine &Context) {
11562cfcf62SDimitry Andric   errs() << Twine("while processing ") + Context + ":\n";
11662cfcf62SDimitry Andric   errs() << Twine("error: ") + Error + "\n";
11762cfcf62SDimitry Andric   return 1;
11862cfcf62SDimitry Andric }
11962cfcf62SDimitry Andric 
120e8d8bef9SDimitry Andric static Expected<Triple> readTargetTriple(StringRef FileName) {
121e8d8bef9SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
122e8d8bef9SDimitry Andric   if (!ErrOrObj)
123e8d8bef9SDimitry Andric     return ErrOrObj.takeError();
124e8d8bef9SDimitry Andric 
125e8d8bef9SDimitry Andric   return ErrOrObj->getBinary()->makeTriple();
126e8d8bef9SDimitry Andric }
127e8d8bef9SDimitry Andric 
12862cfcf62SDimitry Andric int main(int argc, char **argv) {
12962cfcf62SDimitry Andric   InitLLVM X(argc, argv);
13062cfcf62SDimitry Andric 
131*06c3fb27SDimitry Andric   DwpOptTable Tbl;
132*06c3fb27SDimitry Andric   llvm::BumpPtrAllocator A;
133*06c3fb27SDimitry Andric   llvm::StringSaver Saver{A};
134*06c3fb27SDimitry Andric   opt::InputArgList Args =
135*06c3fb27SDimitry Andric       Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
136*06c3fb27SDimitry Andric         llvm::errs() << Msg << '\n';
137*06c3fb27SDimitry Andric         std::exit(1);
138*06c3fb27SDimitry Andric       });
139*06c3fb27SDimitry Andric 
140*06c3fb27SDimitry Andric   if (Args.hasArg(OPT_help)) {
141*06c3fb27SDimitry Andric     Tbl.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
142*06c3fb27SDimitry Andric                   "merge split dwarf (.dwo) files");
143*06c3fb27SDimitry Andric     std::exit(0);
144*06c3fb27SDimitry Andric   }
145*06c3fb27SDimitry Andric 
146*06c3fb27SDimitry Andric   if (Args.hasArg(OPT_version)) {
147*06c3fb27SDimitry Andric     llvm::cl::PrintVersionMessage();
148*06c3fb27SDimitry Andric     std::exit(0);
149*06c3fb27SDimitry Andric   }
150*06c3fb27SDimitry Andric 
151*06c3fb27SDimitry Andric   OutputFilename = Args.getLastArgValue(OPT_outputFileName, "");
152*06c3fb27SDimitry Andric   ContinueOnCuIndexOverflow = Args.hasArg(OPT_continueOnCuIndexOverflow);
153*06c3fb27SDimitry Andric 
154*06c3fb27SDimitry Andric   for (const llvm::opt::Arg *A : Args.filtered(OPT_execFileNames))
155*06c3fb27SDimitry Andric     ExecFilenames.emplace_back(A->getValue());
156*06c3fb27SDimitry Andric 
157*06c3fb27SDimitry Andric   std::vector<std::string> DWOFilenames;
158*06c3fb27SDimitry Andric   for (const llvm::opt::Arg *A : Args.filtered(OPT_INPUT))
159*06c3fb27SDimitry Andric     DWOFilenames.emplace_back(A->getValue());
16062cfcf62SDimitry Andric 
16162cfcf62SDimitry Andric   llvm::InitializeAllTargetInfos();
16262cfcf62SDimitry Andric   llvm::InitializeAllTargetMCs();
16362cfcf62SDimitry Andric   llvm::InitializeAllTargets();
16462cfcf62SDimitry Andric   llvm::InitializeAllAsmPrinters();
16562cfcf62SDimitry Andric 
166e8d8bef9SDimitry Andric   for (const auto &ExecFilename : ExecFilenames) {
167e8d8bef9SDimitry Andric     auto DWOs = getDWOFilenames(ExecFilename);
168e8d8bef9SDimitry Andric     if (!DWOs) {
169bdd1243dSDimitry Andric       logAllUnhandledErrors(
170bdd1243dSDimitry Andric           handleErrors(DWOs.takeError(),
171bdd1243dSDimitry Andric                        [&](std::unique_ptr<ECError> EC) -> Error {
172bdd1243dSDimitry Andric                          return createFileError(ExecFilename,
173bdd1243dSDimitry Andric                                                 Error(std::move(EC)));
174bdd1243dSDimitry Andric                        }),
175bdd1243dSDimitry Andric           WithColor::error());
176e8d8bef9SDimitry Andric       return 1;
177e8d8bef9SDimitry Andric     }
178e8d8bef9SDimitry Andric     DWOFilenames.insert(DWOFilenames.end(),
179e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->begin()),
180e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->end()));
181e8d8bef9SDimitry Andric   }
182e8d8bef9SDimitry Andric 
183e8d8bef9SDimitry Andric   if (DWOFilenames.empty())
184e8d8bef9SDimitry Andric     return 0;
185e8d8bef9SDimitry Andric 
18662cfcf62SDimitry Andric   std::string ErrorStr;
18762cfcf62SDimitry Andric   StringRef Context = "dwarf streamer init";
18862cfcf62SDimitry Andric 
189e8d8bef9SDimitry Andric   auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
190e8d8bef9SDimitry Andric   if (!ErrOrTriple) {
191bdd1243dSDimitry Andric     logAllUnhandledErrors(
192bdd1243dSDimitry Andric         handleErrors(ErrOrTriple.takeError(),
193bdd1243dSDimitry Andric                      [&](std::unique_ptr<ECError> EC) -> Error {
194bdd1243dSDimitry Andric                        return createFileError(DWOFilenames.front(),
195bdd1243dSDimitry Andric                                               Error(std::move(EC)));
196bdd1243dSDimitry Andric                      }),
197bdd1243dSDimitry Andric         WithColor::error());
198e8d8bef9SDimitry Andric     return 1;
199e8d8bef9SDimitry Andric   }
20062cfcf62SDimitry Andric 
20162cfcf62SDimitry Andric   // Get the target.
20262cfcf62SDimitry Andric   const Target *TheTarget =
203e8d8bef9SDimitry Andric       TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
20462cfcf62SDimitry Andric   if (!TheTarget)
20562cfcf62SDimitry Andric     return error(ErrorStr, Context);
206e8d8bef9SDimitry Andric   std::string TripleName = ErrOrTriple->getTriple();
20762cfcf62SDimitry Andric 
20862cfcf62SDimitry Andric   // Create all the MC Objects.
20962cfcf62SDimitry Andric   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
21062cfcf62SDimitry Andric   if (!MRI)
21162cfcf62SDimitry Andric     return error(Twine("no register info for target ") + TripleName, Context);
21262cfcf62SDimitry Andric 
2135ffd83dbSDimitry Andric   MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
21462cfcf62SDimitry Andric   std::unique_ptr<MCAsmInfo> MAI(
21562cfcf62SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
21662cfcf62SDimitry Andric   if (!MAI)
21762cfcf62SDimitry Andric     return error("no asm info for target " + TripleName, Context);
21862cfcf62SDimitry Andric 
21962cfcf62SDimitry Andric   std::unique_ptr<MCSubtargetInfo> MSTI(
22062cfcf62SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
22162cfcf62SDimitry Andric   if (!MSTI)
22262cfcf62SDimitry Andric     return error("no subtarget info for target " + TripleName, Context);
22362cfcf62SDimitry Andric 
224fe6060f1SDimitry Andric   MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
225fe6060f1SDimitry Andric   std::unique_ptr<MCObjectFileInfo> MOFI(
226fe6060f1SDimitry Andric       TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
227fe6060f1SDimitry Andric   MC.setObjectFileInfo(MOFI.get());
228fe6060f1SDimitry Andric 
22962cfcf62SDimitry Andric   MCTargetOptions Options;
23062cfcf62SDimitry Andric   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
23162cfcf62SDimitry Andric   if (!MAB)
23262cfcf62SDimitry Andric     return error("no asm backend for target " + TripleName, Context);
23362cfcf62SDimitry Andric 
23462cfcf62SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
23562cfcf62SDimitry Andric   if (!MII)
23662cfcf62SDimitry Andric     return error("no instr info info for target " + TripleName, Context);
23762cfcf62SDimitry Andric 
23881ad6265SDimitry Andric   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
23962cfcf62SDimitry Andric   if (!MCE)
24062cfcf62SDimitry Andric     return error("no code emitter for target " + TripleName, Context);
24162cfcf62SDimitry Andric 
24262cfcf62SDimitry Andric   // Create the output file.
24362cfcf62SDimitry Andric   std::error_code EC;
24462cfcf62SDimitry Andric   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
245bdd1243dSDimitry Andric   std::optional<buffer_ostream> BOS;
24662cfcf62SDimitry Andric   raw_pwrite_stream *OS;
24762cfcf62SDimitry Andric   if (EC)
24862cfcf62SDimitry Andric     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
24962cfcf62SDimitry Andric   if (OutFile.os().supportsSeeking()) {
25062cfcf62SDimitry Andric     OS = &OutFile.os();
25162cfcf62SDimitry Andric   } else {
25262cfcf62SDimitry Andric     BOS.emplace(OutFile.os());
253bdd1243dSDimitry Andric     OS = &*BOS;
25462cfcf62SDimitry Andric   }
25562cfcf62SDimitry Andric 
25662cfcf62SDimitry Andric   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
257e8d8bef9SDimitry Andric       *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
25862cfcf62SDimitry Andric       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
25962cfcf62SDimitry Andric       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
26062cfcf62SDimitry Andric       /*DWARFMustBeAtTheEnd*/ false));
26162cfcf62SDimitry Andric   if (!MS)
26262cfcf62SDimitry Andric     return error("no object streamer for target " + TripleName, Context);
26362cfcf62SDimitry Andric 
264*06c3fb27SDimitry Andric   if (auto Err = write(*MS, DWOFilenames, ContinueOnCuIndexOverflow)) {
26562cfcf62SDimitry Andric     logAllUnhandledErrors(std::move(Err), WithColor::error());
26662cfcf62SDimitry Andric     return 1;
26762cfcf62SDimitry Andric   }
26862cfcf62SDimitry Andric 
26981ad6265SDimitry Andric   MS->finish();
27062cfcf62SDimitry Andric   OutFile.keep();
27162cfcf62SDimitry Andric   return 0;
27262cfcf62SDimitry Andric }
273