xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-dwp/llvm-dwp.cpp (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
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"
315f757f3fSDimitry 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.
465f757f3fSDimitry 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 
585f757f3fSDimitry Andric using namespace llvm::opt;
5906c3fb27SDimitry Andric static constexpr opt::OptTable::Info InfoTable[] = {
605f757f3fSDimitry 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;
745f757f3fSDimitry 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 
1225f757f3fSDimitry 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};
1285f757f3fSDimitry 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*cb14a3feSDimitry Andric   if (Arg *Arg = Args.getLastArg(OPT_continueOnCuIndexOverflow,
148*cb14a3feSDimitry Andric                                  OPT_continueOnCuIndexOverflow_EQ)) {
149*cb14a3feSDimitry Andric     if (Arg->getOption().matches(OPT_continueOnCuIndexOverflow)) {
150*cb14a3feSDimitry Andric       OverflowOptValue = OnCuIndexOverflow::Continue;
151*cb14a3feSDimitry Andric     } else {
152*cb14a3feSDimitry Andric       ContinueOption = Arg->getValue();
1535f757f3fSDimitry Andric       if (ContinueOption == "soft-stop") {
1545f757f3fSDimitry Andric         OverflowOptValue = OnCuIndexOverflow::SoftStop;
155*cb14a3feSDimitry Andric       } else if (ContinueOption == "continue") {
1565f757f3fSDimitry Andric         OverflowOptValue = OnCuIndexOverflow::Continue;
157*cb14a3feSDimitry Andric       } else {
158*cb14a3feSDimitry Andric         llvm::errs() << "invalid value for --continue-on-cu-index-overflow"
159*cb14a3feSDimitry Andric                      << ContinueOption << '\n';
160*cb14a3feSDimitry Andric         exit(1);
161*cb14a3feSDimitry Andric       }
1625f757f3fSDimitry Andric     }
1635f757f3fSDimitry Andric   }
16406c3fb27SDimitry Andric 
16506c3fb27SDimitry Andric   for (const llvm::opt::Arg *A : Args.filtered(OPT_execFileNames))
16606c3fb27SDimitry Andric     ExecFilenames.emplace_back(A->getValue());
16706c3fb27SDimitry Andric 
16806c3fb27SDimitry Andric   std::vector<std::string> DWOFilenames;
16906c3fb27SDimitry Andric   for (const llvm::opt::Arg *A : Args.filtered(OPT_INPUT))
17006c3fb27SDimitry Andric     DWOFilenames.emplace_back(A->getValue());
17162cfcf62SDimitry Andric 
17262cfcf62SDimitry Andric   llvm::InitializeAllTargetInfos();
17362cfcf62SDimitry Andric   llvm::InitializeAllTargetMCs();
17462cfcf62SDimitry Andric   llvm::InitializeAllTargets();
17562cfcf62SDimitry Andric   llvm::InitializeAllAsmPrinters();
17662cfcf62SDimitry Andric 
177e8d8bef9SDimitry Andric   for (const auto &ExecFilename : ExecFilenames) {
178e8d8bef9SDimitry Andric     auto DWOs = getDWOFilenames(ExecFilename);
179e8d8bef9SDimitry Andric     if (!DWOs) {
180bdd1243dSDimitry Andric       logAllUnhandledErrors(
181bdd1243dSDimitry Andric           handleErrors(DWOs.takeError(),
182bdd1243dSDimitry Andric                        [&](std::unique_ptr<ECError> EC) -> Error {
183bdd1243dSDimitry Andric                          return createFileError(ExecFilename,
184bdd1243dSDimitry Andric                                                 Error(std::move(EC)));
185bdd1243dSDimitry Andric                        }),
186bdd1243dSDimitry Andric           WithColor::error());
187e8d8bef9SDimitry Andric       return 1;
188e8d8bef9SDimitry Andric     }
189e8d8bef9SDimitry Andric     DWOFilenames.insert(DWOFilenames.end(),
190e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->begin()),
191e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->end()));
192e8d8bef9SDimitry Andric   }
193e8d8bef9SDimitry Andric 
194e8d8bef9SDimitry Andric   if (DWOFilenames.empty())
195e8d8bef9SDimitry Andric     return 0;
196e8d8bef9SDimitry Andric 
19762cfcf62SDimitry Andric   std::string ErrorStr;
19862cfcf62SDimitry Andric   StringRef Context = "dwarf streamer init";
19962cfcf62SDimitry Andric 
200e8d8bef9SDimitry Andric   auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
201e8d8bef9SDimitry Andric   if (!ErrOrTriple) {
202bdd1243dSDimitry Andric     logAllUnhandledErrors(
203bdd1243dSDimitry Andric         handleErrors(ErrOrTriple.takeError(),
204bdd1243dSDimitry Andric                      [&](std::unique_ptr<ECError> EC) -> Error {
205bdd1243dSDimitry Andric                        return createFileError(DWOFilenames.front(),
206bdd1243dSDimitry Andric                                               Error(std::move(EC)));
207bdd1243dSDimitry Andric                      }),
208bdd1243dSDimitry Andric         WithColor::error());
209e8d8bef9SDimitry Andric     return 1;
210e8d8bef9SDimitry Andric   }
21162cfcf62SDimitry Andric 
21262cfcf62SDimitry Andric   // Get the target.
21362cfcf62SDimitry Andric   const Target *TheTarget =
214e8d8bef9SDimitry Andric       TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
21562cfcf62SDimitry Andric   if (!TheTarget)
21662cfcf62SDimitry Andric     return error(ErrorStr, Context);
217e8d8bef9SDimitry Andric   std::string TripleName = ErrOrTriple->getTriple();
21862cfcf62SDimitry Andric 
21962cfcf62SDimitry Andric   // Create all the MC Objects.
22062cfcf62SDimitry Andric   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
22162cfcf62SDimitry Andric   if (!MRI)
22262cfcf62SDimitry Andric     return error(Twine("no register info for target ") + TripleName, Context);
22362cfcf62SDimitry Andric 
2245ffd83dbSDimitry Andric   MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
22562cfcf62SDimitry Andric   std::unique_ptr<MCAsmInfo> MAI(
22662cfcf62SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
22762cfcf62SDimitry Andric   if (!MAI)
22862cfcf62SDimitry Andric     return error("no asm info for target " + TripleName, Context);
22962cfcf62SDimitry Andric 
23062cfcf62SDimitry Andric   std::unique_ptr<MCSubtargetInfo> MSTI(
23162cfcf62SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
23262cfcf62SDimitry Andric   if (!MSTI)
23362cfcf62SDimitry Andric     return error("no subtarget info for target " + TripleName, Context);
23462cfcf62SDimitry Andric 
235fe6060f1SDimitry Andric   MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
236fe6060f1SDimitry Andric   std::unique_ptr<MCObjectFileInfo> MOFI(
237fe6060f1SDimitry Andric       TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
238fe6060f1SDimitry Andric   MC.setObjectFileInfo(MOFI.get());
239fe6060f1SDimitry Andric 
24062cfcf62SDimitry Andric   MCTargetOptions Options;
24162cfcf62SDimitry Andric   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
24262cfcf62SDimitry Andric   if (!MAB)
24362cfcf62SDimitry Andric     return error("no asm backend for target " + TripleName, Context);
24462cfcf62SDimitry Andric 
24562cfcf62SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
24662cfcf62SDimitry Andric   if (!MII)
24762cfcf62SDimitry Andric     return error("no instr info info for target " + TripleName, Context);
24862cfcf62SDimitry Andric 
24981ad6265SDimitry Andric   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
25062cfcf62SDimitry Andric   if (!MCE)
25162cfcf62SDimitry Andric     return error("no code emitter for target " + TripleName, Context);
25262cfcf62SDimitry Andric 
25362cfcf62SDimitry Andric   // Create the output file.
25462cfcf62SDimitry Andric   std::error_code EC;
25562cfcf62SDimitry Andric   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
256bdd1243dSDimitry Andric   std::optional<buffer_ostream> BOS;
25762cfcf62SDimitry Andric   raw_pwrite_stream *OS;
25862cfcf62SDimitry Andric   if (EC)
25962cfcf62SDimitry Andric     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
26062cfcf62SDimitry Andric   if (OutFile.os().supportsSeeking()) {
26162cfcf62SDimitry Andric     OS = &OutFile.os();
26262cfcf62SDimitry Andric   } else {
26362cfcf62SDimitry Andric     BOS.emplace(OutFile.os());
264bdd1243dSDimitry Andric     OS = &*BOS;
26562cfcf62SDimitry Andric   }
26662cfcf62SDimitry Andric 
26762cfcf62SDimitry Andric   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
268e8d8bef9SDimitry Andric       *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
26962cfcf62SDimitry Andric       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
27062cfcf62SDimitry Andric       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
27162cfcf62SDimitry Andric       /*DWARFMustBeAtTheEnd*/ false));
27262cfcf62SDimitry Andric   if (!MS)
27362cfcf62SDimitry Andric     return error("no object streamer for target " + TripleName, Context);
27462cfcf62SDimitry Andric 
2755f757f3fSDimitry Andric   if (auto Err = write(*MS, DWOFilenames, OverflowOptValue)) {
27662cfcf62SDimitry Andric     logAllUnhandledErrors(std::move(Err), WithColor::error());
27762cfcf62SDimitry Andric     return 1;
27862cfcf62SDimitry Andric   }
27962cfcf62SDimitry Andric 
28081ad6265SDimitry Andric   MS->finish();
28162cfcf62SDimitry Andric   OutFile.keep();
28262cfcf62SDimitry Andric   return 0;
28362cfcf62SDimitry Andric }
284