xref: /freebsd-src/contrib/llvm-project/llvm/tools/llvm-dwp/llvm-dwp.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
265ffd83dbSDimitry Andric #include "llvm/Support/CommandLine.h"
2762cfcf62SDimitry Andric #include "llvm/Support/FileSystem.h"
2862cfcf62SDimitry Andric #include "llvm/Support/InitLLVM.h"
2981ad6265SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
3062cfcf62SDimitry Andric #include "llvm/Support/TargetSelect.h"
3162cfcf62SDimitry Andric #include "llvm/Support/ToolOutputFile.h"
32*bdd1243dSDimitry Andric #include <optional>
3362cfcf62SDimitry Andric 
3462cfcf62SDimitry Andric using namespace llvm;
3562cfcf62SDimitry Andric using namespace llvm::object;
3662cfcf62SDimitry Andric 
375ffd83dbSDimitry Andric static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
385ffd83dbSDimitry Andric 
3962cfcf62SDimitry Andric cl::OptionCategory DwpCategory("Specific Options");
4081ad6265SDimitry Andric static cl::list<std::string>
4181ad6265SDimitry Andric     InputFiles(cl::Positional, cl::desc("<input files>"), cl::cat(DwpCategory));
4262cfcf62SDimitry Andric 
4362cfcf62SDimitry Andric static cl::list<std::string> ExecFilenames(
4481ad6265SDimitry Andric     "e",
4581ad6265SDimitry Andric     cl::desc(
4681ad6265SDimitry Andric         "Specify the executable/library files to get the list of *.dwo from"),
4762cfcf62SDimitry Andric     cl::value_desc("filename"), cl::cat(DwpCategory));
4862cfcf62SDimitry Andric 
4962cfcf62SDimitry Andric static cl::opt<std::string> OutputFilename(cl::Required, "o",
5062cfcf62SDimitry Andric                                            cl::desc("Specify the output file."),
5162cfcf62SDimitry Andric                                            cl::value_desc("filename"),
5262cfcf62SDimitry Andric                                            cl::cat(DwpCategory));
5362cfcf62SDimitry Andric 
5462cfcf62SDimitry Andric static Expected<SmallVector<std::string, 16>>
5562cfcf62SDimitry Andric getDWOFilenames(StringRef ExecFilename) {
5662cfcf62SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
5762cfcf62SDimitry Andric   if (!ErrOrObj)
5862cfcf62SDimitry Andric     return ErrOrObj.takeError();
5962cfcf62SDimitry Andric 
6062cfcf62SDimitry Andric   const ObjectFile &Obj = *ErrOrObj.get().getBinary();
6162cfcf62SDimitry Andric   std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
6262cfcf62SDimitry Andric 
6362cfcf62SDimitry Andric   SmallVector<std::string, 16> DWOPaths;
6462cfcf62SDimitry Andric   for (const auto &CU : DWARFCtx->compile_units()) {
6562cfcf62SDimitry Andric     const DWARFDie &Die = CU->getUnitDIE();
6662cfcf62SDimitry Andric     std::string DWOName = dwarf::toString(
6762cfcf62SDimitry Andric         Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
6862cfcf62SDimitry Andric     if (DWOName.empty())
6962cfcf62SDimitry Andric       continue;
7062cfcf62SDimitry Andric     std::string DWOCompDir =
7162cfcf62SDimitry Andric         dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
7262cfcf62SDimitry Andric     if (!DWOCompDir.empty()) {
73d409305fSDimitry Andric       SmallString<16> DWOPath(std::move(DWOName));
74d409305fSDimitry Andric       sys::fs::make_absolute(DWOCompDir, DWOPath);
75*bdd1243dSDimitry Andric       if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
76*bdd1243dSDimitry Andric         DWOPaths.push_back(std::move(DWOName));
77*bdd1243dSDimitry Andric       else
7862cfcf62SDimitry Andric         DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
7962cfcf62SDimitry Andric     } else {
8062cfcf62SDimitry Andric       DWOPaths.push_back(std::move(DWOName));
8162cfcf62SDimitry Andric     }
8262cfcf62SDimitry Andric   }
8362cfcf62SDimitry Andric   return std::move(DWOPaths);
8462cfcf62SDimitry Andric }
8562cfcf62SDimitry Andric 
8662cfcf62SDimitry Andric static int error(const Twine &Error, const Twine &Context) {
8762cfcf62SDimitry Andric   errs() << Twine("while processing ") + Context + ":\n";
8862cfcf62SDimitry Andric   errs() << Twine("error: ") + Error + "\n";
8962cfcf62SDimitry Andric   return 1;
9062cfcf62SDimitry Andric }
9162cfcf62SDimitry Andric 
92e8d8bef9SDimitry Andric static Expected<Triple> readTargetTriple(StringRef FileName) {
93e8d8bef9SDimitry Andric   auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
94e8d8bef9SDimitry Andric   if (!ErrOrObj)
95e8d8bef9SDimitry Andric     return ErrOrObj.takeError();
96e8d8bef9SDimitry Andric 
97e8d8bef9SDimitry Andric   return ErrOrObj->getBinary()->makeTriple();
98e8d8bef9SDimitry Andric }
99e8d8bef9SDimitry Andric 
10062cfcf62SDimitry Andric int main(int argc, char **argv) {
10162cfcf62SDimitry Andric   InitLLVM X(argc, argv);
10262cfcf62SDimitry Andric 
103fe6060f1SDimitry Andric   cl::HideUnrelatedOptions({&DwpCategory, &getColorCategory()});
10462cfcf62SDimitry Andric   cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n");
10562cfcf62SDimitry Andric 
10662cfcf62SDimitry Andric   llvm::InitializeAllTargetInfos();
10762cfcf62SDimitry Andric   llvm::InitializeAllTargetMCs();
10862cfcf62SDimitry Andric   llvm::InitializeAllTargets();
10962cfcf62SDimitry Andric   llvm::InitializeAllAsmPrinters();
11062cfcf62SDimitry Andric 
111e8d8bef9SDimitry Andric   std::vector<std::string> DWOFilenames = InputFiles;
112e8d8bef9SDimitry Andric   for (const auto &ExecFilename : ExecFilenames) {
113e8d8bef9SDimitry Andric     auto DWOs = getDWOFilenames(ExecFilename);
114e8d8bef9SDimitry Andric     if (!DWOs) {
115*bdd1243dSDimitry Andric       logAllUnhandledErrors(
116*bdd1243dSDimitry Andric           handleErrors(DWOs.takeError(),
117*bdd1243dSDimitry Andric                        [&](std::unique_ptr<ECError> EC) -> Error {
118*bdd1243dSDimitry Andric                          return createFileError(ExecFilename,
119*bdd1243dSDimitry Andric                                                 Error(std::move(EC)));
120*bdd1243dSDimitry Andric                        }),
121*bdd1243dSDimitry Andric           WithColor::error());
122e8d8bef9SDimitry Andric       return 1;
123e8d8bef9SDimitry Andric     }
124e8d8bef9SDimitry Andric     DWOFilenames.insert(DWOFilenames.end(),
125e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->begin()),
126e8d8bef9SDimitry Andric                         std::make_move_iterator(DWOs->end()));
127e8d8bef9SDimitry Andric   }
128e8d8bef9SDimitry Andric 
129e8d8bef9SDimitry Andric   if (DWOFilenames.empty())
130e8d8bef9SDimitry Andric     return 0;
131e8d8bef9SDimitry Andric 
13262cfcf62SDimitry Andric   std::string ErrorStr;
13362cfcf62SDimitry Andric   StringRef Context = "dwarf streamer init";
13462cfcf62SDimitry Andric 
135e8d8bef9SDimitry Andric   auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
136e8d8bef9SDimitry Andric   if (!ErrOrTriple) {
137*bdd1243dSDimitry Andric     logAllUnhandledErrors(
138*bdd1243dSDimitry Andric         handleErrors(ErrOrTriple.takeError(),
139*bdd1243dSDimitry Andric                      [&](std::unique_ptr<ECError> EC) -> Error {
140*bdd1243dSDimitry Andric                        return createFileError(DWOFilenames.front(),
141*bdd1243dSDimitry Andric                                               Error(std::move(EC)));
142*bdd1243dSDimitry Andric                      }),
143*bdd1243dSDimitry Andric         WithColor::error());
144e8d8bef9SDimitry Andric     return 1;
145e8d8bef9SDimitry Andric   }
14662cfcf62SDimitry Andric 
14762cfcf62SDimitry Andric   // Get the target.
14862cfcf62SDimitry Andric   const Target *TheTarget =
149e8d8bef9SDimitry Andric       TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
15062cfcf62SDimitry Andric   if (!TheTarget)
15162cfcf62SDimitry Andric     return error(ErrorStr, Context);
152e8d8bef9SDimitry Andric   std::string TripleName = ErrOrTriple->getTriple();
15362cfcf62SDimitry Andric 
15462cfcf62SDimitry Andric   // Create all the MC Objects.
15562cfcf62SDimitry Andric   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
15662cfcf62SDimitry Andric   if (!MRI)
15762cfcf62SDimitry Andric     return error(Twine("no register info for target ") + TripleName, Context);
15862cfcf62SDimitry Andric 
1595ffd83dbSDimitry Andric   MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
16062cfcf62SDimitry Andric   std::unique_ptr<MCAsmInfo> MAI(
16162cfcf62SDimitry Andric       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
16262cfcf62SDimitry Andric   if (!MAI)
16362cfcf62SDimitry Andric     return error("no asm info for target " + TripleName, Context);
16462cfcf62SDimitry Andric 
16562cfcf62SDimitry Andric   std::unique_ptr<MCSubtargetInfo> MSTI(
16662cfcf62SDimitry Andric       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
16762cfcf62SDimitry Andric   if (!MSTI)
16862cfcf62SDimitry Andric     return error("no subtarget info for target " + TripleName, Context);
16962cfcf62SDimitry Andric 
170fe6060f1SDimitry Andric   MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
171fe6060f1SDimitry Andric   std::unique_ptr<MCObjectFileInfo> MOFI(
172fe6060f1SDimitry Andric       TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
173fe6060f1SDimitry Andric   MC.setObjectFileInfo(MOFI.get());
174fe6060f1SDimitry Andric 
17562cfcf62SDimitry Andric   MCTargetOptions Options;
17662cfcf62SDimitry Andric   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
17762cfcf62SDimitry Andric   if (!MAB)
17862cfcf62SDimitry Andric     return error("no asm backend for target " + TripleName, Context);
17962cfcf62SDimitry Andric 
18062cfcf62SDimitry Andric   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
18162cfcf62SDimitry Andric   if (!MII)
18262cfcf62SDimitry Andric     return error("no instr info info for target " + TripleName, Context);
18362cfcf62SDimitry Andric 
18481ad6265SDimitry Andric   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
18562cfcf62SDimitry Andric   if (!MCE)
18662cfcf62SDimitry Andric     return error("no code emitter for target " + TripleName, Context);
18762cfcf62SDimitry Andric 
18862cfcf62SDimitry Andric   // Create the output file.
18962cfcf62SDimitry Andric   std::error_code EC;
19062cfcf62SDimitry Andric   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
191*bdd1243dSDimitry Andric   std::optional<buffer_ostream> BOS;
19262cfcf62SDimitry Andric   raw_pwrite_stream *OS;
19362cfcf62SDimitry Andric   if (EC)
19462cfcf62SDimitry Andric     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
19562cfcf62SDimitry Andric   if (OutFile.os().supportsSeeking()) {
19662cfcf62SDimitry Andric     OS = &OutFile.os();
19762cfcf62SDimitry Andric   } else {
19862cfcf62SDimitry Andric     BOS.emplace(OutFile.os());
199*bdd1243dSDimitry Andric     OS = &*BOS;
20062cfcf62SDimitry Andric   }
20162cfcf62SDimitry Andric 
20262cfcf62SDimitry Andric   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
203e8d8bef9SDimitry Andric       *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
20462cfcf62SDimitry Andric       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
20562cfcf62SDimitry Andric       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
20662cfcf62SDimitry Andric       /*DWARFMustBeAtTheEnd*/ false));
20762cfcf62SDimitry Andric   if (!MS)
20862cfcf62SDimitry Andric     return error("no object streamer for target " + TripleName, Context);
20962cfcf62SDimitry Andric 
21062cfcf62SDimitry Andric   if (auto Err = write(*MS, DWOFilenames)) {
21162cfcf62SDimitry Andric     logAllUnhandledErrors(std::move(Err), WithColor::error());
21262cfcf62SDimitry Andric     return 1;
21362cfcf62SDimitry Andric   }
21462cfcf62SDimitry Andric 
21581ad6265SDimitry Andric   MS->finish();
21662cfcf62SDimitry Andric   OutFile.keep();
21762cfcf62SDimitry Andric   return 0;
21862cfcf62SDimitry Andric }
219