1 //===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
10 // package files).
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/DWP/DWP.h"
14 #include "llvm/DWP/DWPError.h"
15 #include "llvm/DWP/DWPStringPool.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCInstrInfo.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/MC/MCRegisterInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/MC/MCTargetOptionsCommandFlags.h"
25 #include "llvm/MC/TargetRegistry.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/InitLLVM.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/TargetSelect.h"
31 #include "llvm/Support/ToolOutputFile.h"
32 
33 using namespace llvm;
34 using namespace llvm::object;
35 
36 static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
37 
38 cl::OptionCategory DwpCategory("Specific Options");
39 static cl::list<std::string>
40     InputFiles(cl::Positional, cl::desc("<input files>"), cl::cat(DwpCategory));
41 
42 static cl::list<std::string> ExecFilenames(
43     "e",
44     cl::desc(
45         "Specify the executable/library files to get the list of *.dwo from"),
46     cl::value_desc("filename"), cl::cat(DwpCategory));
47 
48 static cl::opt<std::string> OutputFilename(cl::Required, "o",
49                                            cl::desc("Specify the output file."),
50                                            cl::value_desc("filename"),
51                                            cl::cat(DwpCategory));
52 
53 static Expected<SmallVector<std::string, 16>>
54 getDWOFilenames(StringRef ExecFilename) {
55   auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
56   if (!ErrOrObj)
57     return ErrOrObj.takeError();
58 
59   const ObjectFile &Obj = *ErrOrObj.get().getBinary();
60   std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
61 
62   SmallVector<std::string, 16> DWOPaths;
63   for (const auto &CU : DWARFCtx->compile_units()) {
64     const DWARFDie &Die = CU->getUnitDIE();
65     std::string DWOName = dwarf::toString(
66         Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
67     if (DWOName.empty())
68       continue;
69     std::string DWOCompDir =
70         dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
71     if (!DWOCompDir.empty()) {
72       SmallString<16> DWOPath(std::move(DWOName));
73       sys::fs::make_absolute(DWOCompDir, DWOPath);
74       DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
75     } else {
76       DWOPaths.push_back(std::move(DWOName));
77     }
78   }
79   return std::move(DWOPaths);
80 }
81 
82 static int error(const Twine &Error, const Twine &Context) {
83   errs() << Twine("while processing ") + Context + ":\n";
84   errs() << Twine("error: ") + Error + "\n";
85   return 1;
86 }
87 
88 static Expected<Triple> readTargetTriple(StringRef FileName) {
89   auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
90   if (!ErrOrObj)
91     return ErrOrObj.takeError();
92 
93   return ErrOrObj->getBinary()->makeTriple();
94 }
95 
96 int main(int argc, char **argv) {
97   InitLLVM X(argc, argv);
98 
99   cl::HideUnrelatedOptions({&DwpCategory, &getColorCategory()});
100   cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n");
101 
102   llvm::InitializeAllTargetInfos();
103   llvm::InitializeAllTargetMCs();
104   llvm::InitializeAllTargets();
105   llvm::InitializeAllAsmPrinters();
106 
107   std::vector<std::string> DWOFilenames = InputFiles;
108   for (const auto &ExecFilename : ExecFilenames) {
109     auto DWOs = getDWOFilenames(ExecFilename);
110     if (!DWOs) {
111       logAllUnhandledErrors(DWOs.takeError(), WithColor::error());
112       return 1;
113     }
114     DWOFilenames.insert(DWOFilenames.end(),
115                         std::make_move_iterator(DWOs->begin()),
116                         std::make_move_iterator(DWOs->end()));
117   }
118 
119   if (DWOFilenames.empty())
120     return 0;
121 
122   std::string ErrorStr;
123   StringRef Context = "dwarf streamer init";
124 
125   auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
126   if (!ErrOrTriple) {
127     logAllUnhandledErrors(ErrOrTriple.takeError(), WithColor::error());
128     return 1;
129   }
130 
131   // Get the target.
132   const Target *TheTarget =
133       TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
134   if (!TheTarget)
135     return error(ErrorStr, Context);
136   std::string TripleName = ErrOrTriple->getTriple();
137 
138   // Create all the MC Objects.
139   std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
140   if (!MRI)
141     return error(Twine("no register info for target ") + TripleName, Context);
142 
143   MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
144   std::unique_ptr<MCAsmInfo> MAI(
145       TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
146   if (!MAI)
147     return error("no asm info for target " + TripleName, Context);
148 
149   std::unique_ptr<MCSubtargetInfo> MSTI(
150       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
151   if (!MSTI)
152     return error("no subtarget info for target " + TripleName, Context);
153 
154   MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
155   std::unique_ptr<MCObjectFileInfo> MOFI(
156       TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
157   MC.setObjectFileInfo(MOFI.get());
158 
159   MCTargetOptions Options;
160   auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
161   if (!MAB)
162     return error("no asm backend for target " + TripleName, Context);
163 
164   std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
165   if (!MII)
166     return error("no instr info info for target " + TripleName, Context);
167 
168   MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
169   if (!MCE)
170     return error("no code emitter for target " + TripleName, Context);
171 
172   // Create the output file.
173   std::error_code EC;
174   ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
175   Optional<buffer_ostream> BOS;
176   raw_pwrite_stream *OS;
177   if (EC)
178     return error(Twine(OutputFilename) + ": " + EC.message(), Context);
179   if (OutFile.os().supportsSeeking()) {
180     OS = &OutFile.os();
181   } else {
182     BOS.emplace(OutFile.os());
183     OS = BOS.getPointer();
184   }
185 
186   std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
187       *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
188       MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
189       MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
190       /*DWARFMustBeAtTheEnd*/ false));
191   if (!MS)
192     return error("no object streamer for target " + TripleName, Context);
193 
194   if (auto Err = write(*MS, DWOFilenames)) {
195     logAllUnhandledErrors(std::move(Err), WithColor::error());
196     return 1;
197   }
198 
199   MS->finish();
200   OutFile.keep();
201   return 0;
202 }
203