xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-dwp/llvm-dwp.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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"
2606c3fb27SDimitry Andric #include "llvm/Option/ArgList.h"
2706c3fb27SDimitry 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"
31*5f757f3fSDimitry Andric #include "llvm/Support/LLVMDriver.h"
3281ad6265SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
3362cfcf62SDimitry Andric #include "llvm/Support/TargetSelect.h"
3462cfcf62SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
35bdd1243dSDimitry Andric #include <optional>
3662cfcf62SDimitry Andric 
3762cfcf62SDimitry Andric using namespace llvm;
3862cfcf62SDimitry Andric using namespace llvm::object;
3962cfcf62SDimitry Andric 
405ffd83dbSDimitry Andric static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
415ffd83dbSDimitry Andric 
4206c3fb27SDimitry Andric // Command-line option boilerplate.
4306c3fb27SDimitry Andric namespace {
4406c3fb27SDimitry Andric enum ID {
4506c3fb27SDimitry Andric   OPT_INVALID = 0, // This is not an option ID.
46*5f757f3fSDimitry Andric #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
4706c3fb27SDimitry Andric #include "Opts.inc"
4806c3fb27SDimitry Andric #undef OPTION
4906c3fb27SDimitry Andric };
5062cfcf62SDimitry Andric 
5106c3fb27SDimitry Andric #define PREFIX(NAME, VALUE)                                                    \
5206c3fb27SDimitry Andric   static constexpr StringLiteral NAME##_init[] = VALUE;                        \
5306c3fb27SDimitry Andric   static constexpr ArrayRef<StringLiteral> NAME(NAME##_init,                   \
5406c3fb27SDimitry Andric                                                 std::size(NAME##_init) - 1);
5506c3fb27SDimitry Andric #include "Opts.inc"
5606c3fb27SDimitry Andric #undef PREFIX
5762cfcf62SDimitry Andric 
58*5f757f3fSDimitry Andric using namespace llvm::opt;
5906c3fb27SDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
60*5f757f3fSDimitry Andric #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
6106c3fb27SDimitry Andric #include "Opts.inc"
6206c3fb27SDimitry Andric #undef OPTION
6306c3fb27SDimitry Andric };
6406c3fb27SDimitry Andric 
6506c3fb27SDimitry Andric class DwpOptTable : public opt::GenericOptTable {
6606c3fb27SDimitry Andric public:
6706c3fb27SDimitry Andric   DwpOptTable() : GenericOptTable(InfoTable) {}
6806c3fb27SDimitry Andric };
6906c3fb27SDimitry Andric } // end anonymous namespace
7006c3fb27SDimitry Andric 
7106c3fb27SDimitry Andric // Options
7206c3fb27SDimitry Andric static std::vector<std::string> ExecFilenames;
7306c3fb27SDimitry Andric static std::string OutputFilename;
74*5f757f3fSDimitry Andric static std::string ContinueOption;
7562cfcf62SDimitry Andric 
7662cfcf62SDimitry Andric static Expected<SmallVector<std::string, 16>>
7762cfcf62SDimitry Andric getDWOFilenames(StringRef ExecFilename) {
7862cfcf62SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
7962cfcf62SDimitry Andric   if (!ErrOrObj)
8062cfcf62SDimitry Andric     return ErrOrObj.takeError();
8162cfcf62SDimitry Andric 
8262cfcf62SDimitry Andric   const ObjectFile &Obj = *ErrOrObj.get().getBinary();
8362cfcf62SDimitry Andric   std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
8462cfcf62SDimitry Andric 
8562cfcf62SDimitry Andric   SmallVector<std::string, 16> DWOPaths;
8662cfcf62SDimitry Andric   for (const auto &CU : DWARFCtx->compile_units()) {
8762cfcf62SDimitry Andric     const DWARFDie &Die = CU->getUnitDIE();
8862cfcf62SDimitry Andric     std::string DWOName = dwarf::toString(
8962cfcf62SDimitry Andric         Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
9062cfcf62SDimitry Andric     if (DWOName.empty())
9162cfcf62SDimitry Andric       continue;
9262cfcf62SDimitry Andric     std::string DWOCompDir =
9362cfcf62SDimitry Andric         dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
9462cfcf62SDimitry Andric     if (!DWOCompDir.empty()) {
95d409305fSDimitry Andric       SmallString<16> DWOPath(std::move(DWOName));
96d409305fSDimitry Andric       sys::fs::make_absolute(DWOCompDir, DWOPath);
97bdd1243dSDimitry Andric       if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
98bdd1243dSDimitry Andric         DWOPaths.push_back(std::move(DWOName));
99bdd1243dSDimitry Andric       else
10062cfcf62SDimitry Andric         DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
10162cfcf62SDimitry Andric     } else {
10262cfcf62SDimitry Andric       DWOPaths.push_back(std::move(DWOName));
10362cfcf62SDimitry Andric     }
10462cfcf62SDimitry Andric   }
10562cfcf62SDimitry Andric   return std::move(DWOPaths);
10662cfcf62SDimitry Andric }
10762cfcf62SDimitry Andric 
10862cfcf62SDimitry Andric static int error(const Twine &Error, const Twine &Context) {
10962cfcf62SDimitry Andric   errs() << Twine("while processing ") + Context + ":\n";
11062cfcf62SDimitry Andric   errs() << Twine("error: ") + Error + "\n";
11162cfcf62SDimitry Andric   return 1;
11262cfcf62SDimitry Andric }
11362cfcf62SDimitry Andric 
114e8d8bef9SDimitry Andric static Expected<Triple> readTargetTriple(StringRef FileName) {
115e8d8bef9SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
116e8d8bef9SDimitry Andric   if (!ErrOrObj)
117e8d8bef9SDimitry Andric     return ErrOrObj.takeError();
118e8d8bef9SDimitry Andric 
119e8d8bef9SDimitry Andric   return ErrOrObj->getBinary()->makeTriple();
120e8d8bef9SDimitry Andric }
121e8d8bef9SDimitry Andric 
122*5f757f3fSDimitry Andric int llvm_dwp_main(int argc, char **argv, const llvm::ToolContext &) {
12362cfcf62SDimitry Andric   InitLLVM X(argc, argv);
12462cfcf62SDimitry Andric 
12506c3fb27SDimitry Andric   DwpOptTable Tbl;
12606c3fb27SDimitry Andric   llvm::BumpPtrAllocator A;
12706c3fb27SDimitry Andric   llvm::StringSaver Saver{A};
128*5f757f3fSDimitry Andric   OnCuIndexOverflow OverflowOptValue = OnCuIndexOverflow::HardStop;
12906c3fb27SDimitry Andric   opt::InputArgList Args =
13006c3fb27SDimitry Andric       Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
13106c3fb27SDimitry Andric         llvm::errs() << Msg << '\n';
13206c3fb27SDimitry Andric         std::exit(1);
13306c3fb27SDimitry Andric       });
13406c3fb27SDimitry Andric 
13506c3fb27SDimitry Andric   if (Args.hasArg(OPT_help)) {
13606c3fb27SDimitry Andric     Tbl.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
13706c3fb27SDimitry Andric                   "merge split dwarf (.dwo) files");
13806c3fb27SDimitry Andric     std::exit(0);
13906c3fb27SDimitry Andric   }
14006c3fb27SDimitry Andric 
14106c3fb27SDimitry Andric   if (Args.hasArg(OPT_version)) {
14206c3fb27SDimitry Andric     llvm::cl::PrintVersionMessage();
14306c3fb27SDimitry Andric     std::exit(0);
14406c3fb27SDimitry Andric   }
14506c3fb27SDimitry Andric 
14606c3fb27SDimitry Andric   OutputFilename = Args.getLastArgValue(OPT_outputFileName, "");
147*5f757f3fSDimitry Andric   if (Args.hasArg(OPT_continueOnCuIndexOverflow)) {
148*5f757f3fSDimitry Andric     ContinueOption =
149*5f757f3fSDimitry Andric         Args.getLastArgValue(OPT_continueOnCuIndexOverflow, "continue");
150*5f757f3fSDimitry Andric     if (ContinueOption == "soft-stop") {
151*5f757f3fSDimitry Andric       OverflowOptValue = OnCuIndexOverflow::SoftStop;
152*5f757f3fSDimitry Andric     } else {
153*5f757f3fSDimitry Andric       OverflowOptValue = OnCuIndexOverflow::Continue;
154*5f757f3fSDimitry Andric     }
155*5f757f3fSDimitry Andric   }
15606c3fb27SDimitry Andric 
15706c3fb27SDimitry Andric   for (const llvm::opt::Arg *A : Args.filtered(OPT_execFileNames))
15806c3fb27SDimitry Andric     ExecFilenames.emplace_back(A->getValue());
15906c3fb27SDimitry Andric 
16006c3fb27SDimitry Andric   std::vector<std::string> DWOFilenames;
16106c3fb27SDimitry Andric   for (const llvm::opt::Arg *A : Args.filtered(OPT_INPUT))
16206c3fb27SDimitry Andric     DWOFilenames.emplace_back(A->getValue());
16362cfcf62SDimitry Andric 
16462cfcf62SDimitry Andric   llvm::InitializeAllTargetInfos();
16562cfcf62SDimitry Andric   llvm::InitializeAllTargetMCs();
16662cfcf62SDimitry Andric   llvm::InitializeAllTargets();
16762cfcf62SDimitry Andric   llvm::InitializeAllAsmPrinters();
16862cfcf62SDimitry Andric 
169e8d8bef9SDimitry Andric   for (const auto &ExecFilename : ExecFilenames) {
170e8d8bef9SDimitry Andric     auto DWOs = getDWOFilenames(ExecFilename);
171e8d8bef9SDimitry Andric     if (!DWOs) {
172bdd1243dSDimitry Andric       logAllUnhandledErrors(
173bdd1243dSDimitry Andric           handleErrors(DWOs.takeError(),
174bdd1243dSDimitry Andric                        [&](std::unique_ptr<ECError> EC) -> Error {
175bdd1243dSDimitry Andric                          return createFileError(ExecFilename,
176bdd1243dSDimitry Andric                                                 Error(std::move(EC)));
177bdd1243dSDimitry Andric                        }),
178bdd1243dSDimitry Andric           WithColor::error());
179e8d8bef9SDimitry Andric       return 1;
180e8d8bef9SDimitry Andric     }
181e8d8bef9SDimitry Andric     DWOFilenames.insert(DWOFilenames.end(),
182e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->begin()),
183e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->end()));
184e8d8bef9SDimitry Andric   }
185e8d8bef9SDimitry Andric 
186e8d8bef9SDimitry Andric   if (DWOFilenames.empty())
187e8d8bef9SDimitry Andric     return 0;
188e8d8bef9SDimitry Andric 
18962cfcf62SDimitry Andric   std::string ErrorStr;
19062cfcf62SDimitry Andric   StringRef Context = "dwarf streamer init";
19162cfcf62SDimitry Andric 
192e8d8bef9SDimitry Andric   auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
193e8d8bef9SDimitry Andric   if (!ErrOrTriple) {
194bdd1243dSDimitry Andric     logAllUnhandledErrors(
195bdd1243dSDimitry Andric         handleErrors(ErrOrTriple.takeError(),
196bdd1243dSDimitry Andric                      [&](std::unique_ptr<ECError> EC) -> Error {
197bdd1243dSDimitry Andric                        return createFileError(DWOFilenames.front(),
198bdd1243dSDimitry Andric                                               Error(std::move(EC)));
199bdd1243dSDimitry Andric                      }),
200bdd1243dSDimitry Andric         WithColor::error());
201e8d8bef9SDimitry Andric     return 1;
202e8d8bef9SDimitry Andric   }
20362cfcf62SDimitry Andric 
20462cfcf62SDimitry Andric   // Get the target.
20562cfcf62SDimitry Andric   const Target *TheTarget =
206e8d8bef9SDimitry Andric       TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
20762cfcf62SDimitry Andric   if (!TheTarget)
20862cfcf62SDimitry Andric     return error(ErrorStr, Context);
209e8d8bef9SDimitry Andric   std::string TripleName = ErrOrTriple->getTriple();
21062cfcf62SDimitry Andric 
21162cfcf62SDimitry Andric   // Create all the MC Objects.
21262cfcf62SDimitry Andric   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
21362cfcf62SDimitry Andric   if (!MRI)
21462cfcf62SDimitry Andric     return error(Twine("no register info for target ") + TripleName, Context);
21562cfcf62SDimitry Andric 
2165ffd83dbSDimitry Andric   MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
21762cfcf62SDimitry Andric   std::unique_ptr<MCAsmInfo> MAI(
21862cfcf62SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
21962cfcf62SDimitry Andric   if (!MAI)
22062cfcf62SDimitry Andric     return error("no asm info for target " + TripleName, Context);
22162cfcf62SDimitry Andric 
22262cfcf62SDimitry Andric   std::unique_ptr<MCSubtargetInfo> MSTI(
22362cfcf62SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
22462cfcf62SDimitry Andric   if (!MSTI)
22562cfcf62SDimitry Andric     return error("no subtarget info for target " + TripleName, Context);
22662cfcf62SDimitry Andric 
227fe6060f1SDimitry Andric   MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
228fe6060f1SDimitry Andric   std::unique_ptr<MCObjectFileInfo> MOFI(
229fe6060f1SDimitry Andric       TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
230fe6060f1SDimitry Andric   MC.setObjectFileInfo(MOFI.get());
231fe6060f1SDimitry Andric 
23262cfcf62SDimitry Andric   MCTargetOptions Options;
23362cfcf62SDimitry Andric   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
23462cfcf62SDimitry Andric   if (!MAB)
23562cfcf62SDimitry Andric     return error("no asm backend for target " + TripleName, Context);
23662cfcf62SDimitry Andric 
23762cfcf62SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
23862cfcf62SDimitry Andric   if (!MII)
23962cfcf62SDimitry Andric     return error("no instr info info for target " + TripleName, Context);
24062cfcf62SDimitry Andric 
24181ad6265SDimitry Andric   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
24262cfcf62SDimitry Andric   if (!MCE)
24362cfcf62SDimitry Andric     return error("no code emitter for target " + TripleName, Context);
24462cfcf62SDimitry Andric 
24562cfcf62SDimitry Andric   // Create the output file.
24662cfcf62SDimitry Andric   std::error_code EC;
24762cfcf62SDimitry Andric   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
248bdd1243dSDimitry Andric   std::optional<buffer_ostream> BOS;
24962cfcf62SDimitry Andric   raw_pwrite_stream *OS;
25062cfcf62SDimitry Andric   if (EC)
25162cfcf62SDimitry Andric     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
25262cfcf62SDimitry Andric   if (OutFile.os().supportsSeeking()) {
25362cfcf62SDimitry Andric     OS = &OutFile.os();
25462cfcf62SDimitry Andric   } else {
25562cfcf62SDimitry Andric     BOS.emplace(OutFile.os());
256bdd1243dSDimitry Andric     OS = &*BOS;
25762cfcf62SDimitry Andric   }
25862cfcf62SDimitry Andric 
25962cfcf62SDimitry Andric   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
260e8d8bef9SDimitry Andric       *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
26162cfcf62SDimitry Andric       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
26262cfcf62SDimitry Andric       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
26362cfcf62SDimitry Andric       /*DWARFMustBeAtTheEnd*/ false));
26462cfcf62SDimitry Andric   if (!MS)
26562cfcf62SDimitry Andric     return error("no object streamer for target " + TripleName, Context);
26662cfcf62SDimitry Andric 
267*5f757f3fSDimitry Andric   if (auto Err = write(*MS, DWOFilenames, OverflowOptValue)) {
26862cfcf62SDimitry Andric     logAllUnhandledErrors(std::move(Err), WithColor::error());
26962cfcf62SDimitry Andric     return 1;
27062cfcf62SDimitry Andric   }
27162cfcf62SDimitry Andric 
27281ad6265SDimitry Andric   MS->finish();
27362cfcf62SDimitry Andric   OutFile.keep();
27462cfcf62SDimitry Andric   return 0;
27562cfcf62SDimitry Andric }
276