xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-dwp/llvm-dwp.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
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"
225ffd83dbSDimitry Andric #include "llvm/MC/MCTargetOptionsCommandFlags.h"
23*349cc55cSDimitry Andric #include "llvm/MC/TargetRegistry.h"
245ffd83dbSDimitry Andric #include "llvm/Support/CommandLine.h"
2562cfcf62SDimitry Andric #include "llvm/Support/FileSystem.h"
2662cfcf62SDimitry Andric #include "llvm/Support/InitLLVM.h"
2762cfcf62SDimitry Andric #include "llvm/Support/TargetSelect.h"
2862cfcf62SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
2962cfcf62SDimitry Andric 
3062cfcf62SDimitry Andric using namespace llvm;
3162cfcf62SDimitry Andric using namespace llvm::object;
3262cfcf62SDimitry Andric 
335ffd83dbSDimitry Andric static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
345ffd83dbSDimitry Andric 
3562cfcf62SDimitry Andric cl::OptionCategory DwpCategory("Specific Options");
3662cfcf62SDimitry Andric static cl::list<std::string> InputFiles(cl::Positional, cl::ZeroOrMore,
3762cfcf62SDimitry Andric                                         cl::desc("<input files>"),
3862cfcf62SDimitry Andric                                         cl::cat(DwpCategory));
3962cfcf62SDimitry Andric 
4062cfcf62SDimitry Andric static cl::list<std::string> ExecFilenames(
4162cfcf62SDimitry Andric     "e", cl::ZeroOrMore,
4262cfcf62SDimitry Andric     cl::desc("Specify the executable/library files to get the list of *.dwo from"),
4362cfcf62SDimitry Andric     cl::value_desc("filename"), cl::cat(DwpCategory));
4462cfcf62SDimitry Andric 
4562cfcf62SDimitry Andric static cl::opt<std::string> OutputFilename(cl::Required, "o",
4662cfcf62SDimitry Andric                                            cl::desc("Specify the output file."),
4762cfcf62SDimitry Andric                                            cl::value_desc("filename"),
4862cfcf62SDimitry Andric                                            cl::cat(DwpCategory));
4962cfcf62SDimitry Andric 
5062cfcf62SDimitry Andric static Expected<SmallVector<std::string, 16>>
5162cfcf62SDimitry Andric getDWOFilenames(StringRef ExecFilename) {
5262cfcf62SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
5362cfcf62SDimitry Andric   if (!ErrOrObj)
5462cfcf62SDimitry Andric     return ErrOrObj.takeError();
5562cfcf62SDimitry Andric 
5662cfcf62SDimitry Andric   const ObjectFile &Obj = *ErrOrObj.get().getBinary();
5762cfcf62SDimitry Andric   std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
5862cfcf62SDimitry Andric 
5962cfcf62SDimitry Andric   SmallVector<std::string, 16> DWOPaths;
6062cfcf62SDimitry Andric   for (const auto &CU : DWARFCtx->compile_units()) {
6162cfcf62SDimitry Andric     const DWARFDie &Die = CU->getUnitDIE();
6262cfcf62SDimitry Andric     std::string DWOName = dwarf::toString(
6362cfcf62SDimitry Andric         Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
6462cfcf62SDimitry Andric     if (DWOName.empty())
6562cfcf62SDimitry Andric       continue;
6662cfcf62SDimitry Andric     std::string DWOCompDir =
6762cfcf62SDimitry Andric         dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
6862cfcf62SDimitry Andric     if (!DWOCompDir.empty()) {
69d409305fSDimitry Andric       SmallString<16> DWOPath(std::move(DWOName));
70d409305fSDimitry Andric       sys::fs::make_absolute(DWOCompDir, DWOPath);
7162cfcf62SDimitry Andric       DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
7262cfcf62SDimitry Andric     } else {
7362cfcf62SDimitry Andric       DWOPaths.push_back(std::move(DWOName));
7462cfcf62SDimitry Andric     }
7562cfcf62SDimitry Andric   }
7662cfcf62SDimitry Andric   return std::move(DWOPaths);
7762cfcf62SDimitry Andric }
7862cfcf62SDimitry Andric 
7962cfcf62SDimitry Andric static int error(const Twine &Error, const Twine &Context) {
8062cfcf62SDimitry Andric   errs() << Twine("while processing ") + Context + ":\n";
8162cfcf62SDimitry Andric   errs() << Twine("error: ") + Error + "\n";
8262cfcf62SDimitry Andric   return 1;
8362cfcf62SDimitry Andric }
8462cfcf62SDimitry Andric 
85e8d8bef9SDimitry Andric static Expected<Triple> readTargetTriple(StringRef FileName) {
86e8d8bef9SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
87e8d8bef9SDimitry Andric   if (!ErrOrObj)
88e8d8bef9SDimitry Andric     return ErrOrObj.takeError();
89e8d8bef9SDimitry Andric 
90e8d8bef9SDimitry Andric   return ErrOrObj->getBinary()->makeTriple();
91e8d8bef9SDimitry Andric }
92e8d8bef9SDimitry Andric 
9362cfcf62SDimitry Andric int main(int argc, char **argv) {
9462cfcf62SDimitry Andric   InitLLVM X(argc, argv);
9562cfcf62SDimitry Andric 
96fe6060f1SDimitry Andric   cl::HideUnrelatedOptions({&DwpCategory, &getColorCategory()});
9762cfcf62SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n");
9862cfcf62SDimitry Andric 
9962cfcf62SDimitry Andric   llvm::InitializeAllTargetInfos();
10062cfcf62SDimitry Andric   llvm::InitializeAllTargetMCs();
10162cfcf62SDimitry Andric   llvm::InitializeAllTargets();
10262cfcf62SDimitry Andric   llvm::InitializeAllAsmPrinters();
10362cfcf62SDimitry Andric 
104e8d8bef9SDimitry Andric   std::vector<std::string> DWOFilenames = InputFiles;
105e8d8bef9SDimitry Andric   for (const auto &ExecFilename : ExecFilenames) {
106e8d8bef9SDimitry Andric     auto DWOs = getDWOFilenames(ExecFilename);
107e8d8bef9SDimitry Andric     if (!DWOs) {
108e8d8bef9SDimitry Andric       logAllUnhandledErrors(DWOs.takeError(), WithColor::error());
109e8d8bef9SDimitry Andric       return 1;
110e8d8bef9SDimitry Andric     }
111e8d8bef9SDimitry Andric     DWOFilenames.insert(DWOFilenames.end(),
112e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->begin()),
113e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->end()));
114e8d8bef9SDimitry Andric   }
115e8d8bef9SDimitry Andric 
116e8d8bef9SDimitry Andric   if (DWOFilenames.empty())
117e8d8bef9SDimitry Andric     return 0;
118e8d8bef9SDimitry Andric 
11962cfcf62SDimitry Andric   std::string ErrorStr;
12062cfcf62SDimitry Andric   StringRef Context = "dwarf streamer init";
12162cfcf62SDimitry Andric 
122e8d8bef9SDimitry Andric   auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
123e8d8bef9SDimitry Andric   if (!ErrOrTriple) {
124e8d8bef9SDimitry Andric     logAllUnhandledErrors(ErrOrTriple.takeError(), WithColor::error());
125e8d8bef9SDimitry Andric     return 1;
126e8d8bef9SDimitry Andric   }
12762cfcf62SDimitry Andric 
12862cfcf62SDimitry Andric   // Get the target.
12962cfcf62SDimitry Andric   const Target *TheTarget =
130e8d8bef9SDimitry Andric       TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
13162cfcf62SDimitry Andric   if (!TheTarget)
13262cfcf62SDimitry Andric     return error(ErrorStr, Context);
133e8d8bef9SDimitry Andric   std::string TripleName = ErrOrTriple->getTriple();
13462cfcf62SDimitry Andric 
13562cfcf62SDimitry Andric   // Create all the MC Objects.
13662cfcf62SDimitry Andric   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
13762cfcf62SDimitry Andric   if (!MRI)
13862cfcf62SDimitry Andric     return error(Twine("no register info for target ") + TripleName, Context);
13962cfcf62SDimitry Andric 
1405ffd83dbSDimitry Andric   MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
14162cfcf62SDimitry Andric   std::unique_ptr<MCAsmInfo> MAI(
14262cfcf62SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
14362cfcf62SDimitry Andric   if (!MAI)
14462cfcf62SDimitry Andric     return error("no asm info for target " + TripleName, Context);
14562cfcf62SDimitry Andric 
14662cfcf62SDimitry Andric   std::unique_ptr<MCSubtargetInfo> MSTI(
14762cfcf62SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
14862cfcf62SDimitry Andric   if (!MSTI)
14962cfcf62SDimitry Andric     return error("no subtarget info for target " + TripleName, Context);
15062cfcf62SDimitry Andric 
151fe6060f1SDimitry Andric   MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
152fe6060f1SDimitry Andric   std::unique_ptr<MCObjectFileInfo> MOFI(
153fe6060f1SDimitry Andric       TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
154fe6060f1SDimitry Andric   MC.setObjectFileInfo(MOFI.get());
155fe6060f1SDimitry Andric 
15662cfcf62SDimitry Andric   MCTargetOptions Options;
15762cfcf62SDimitry Andric   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
15862cfcf62SDimitry Andric   if (!MAB)
15962cfcf62SDimitry Andric     return error("no asm backend for target " + TripleName, Context);
16062cfcf62SDimitry Andric 
16162cfcf62SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
16262cfcf62SDimitry Andric   if (!MII)
16362cfcf62SDimitry Andric     return error("no instr info info for target " + TripleName, Context);
16462cfcf62SDimitry Andric 
16562cfcf62SDimitry Andric   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
16662cfcf62SDimitry Andric   if (!MCE)
16762cfcf62SDimitry Andric     return error("no code emitter for target " + TripleName, Context);
16862cfcf62SDimitry Andric 
16962cfcf62SDimitry Andric   // Create the output file.
17062cfcf62SDimitry Andric   std::error_code EC;
17162cfcf62SDimitry Andric   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
17262cfcf62SDimitry Andric   Optional<buffer_ostream> BOS;
17362cfcf62SDimitry Andric   raw_pwrite_stream *OS;
17462cfcf62SDimitry Andric   if (EC)
17562cfcf62SDimitry Andric     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
17662cfcf62SDimitry Andric   if (OutFile.os().supportsSeeking()) {
17762cfcf62SDimitry Andric     OS = &OutFile.os();
17862cfcf62SDimitry Andric   } else {
17962cfcf62SDimitry Andric     BOS.emplace(OutFile.os());
18062cfcf62SDimitry Andric     OS = BOS.getPointer();
18162cfcf62SDimitry Andric   }
18262cfcf62SDimitry Andric 
18362cfcf62SDimitry Andric   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
184e8d8bef9SDimitry Andric       *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
18562cfcf62SDimitry Andric       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
18662cfcf62SDimitry Andric       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
18762cfcf62SDimitry Andric       /*DWARFMustBeAtTheEnd*/ false));
18862cfcf62SDimitry Andric   if (!MS)
18962cfcf62SDimitry Andric     return error("no object streamer for target " + TripleName, Context);
19062cfcf62SDimitry Andric 
19162cfcf62SDimitry Andric   if (auto Err = write(*MS, DWOFilenames)) {
19262cfcf62SDimitry Andric     logAllUnhandledErrors(std::move(Err), WithColor::error());
19362cfcf62SDimitry Andric     return 1;
19462cfcf62SDimitry Andric   }
19562cfcf62SDimitry Andric 
19662cfcf62SDimitry Andric   MS->Finish();
19762cfcf62SDimitry Andric   OutFile.keep();
19862cfcf62SDimitry Andric   return 0;
19962cfcf62SDimitry Andric }
200