[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / llvm / tools / llvm-dwp / llvm-dwp.cpp
blobf976298dcadc2f49f5453f58bb534c1b82d929d2
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).
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/Option/ArgList.h"
27 #include "llvm/Option/Option.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileSystem.h"
30 #include "llvm/Support/InitLLVM.h"
31 #include "llvm/Support/LLVMDriver.h"
32 #include "llvm/Support/MemoryBuffer.h"
33 #include "llvm/Support/TargetSelect.h"
34 #include "llvm/Support/ToolOutputFile.h"
35 #include <optional>
37 using namespace llvm;
38 using namespace llvm::object;
40 static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
42 // Command-line option boilerplate.
43 namespace {
44 enum ID {
45 OPT_INVALID = 0, // This is not an option ID.
46 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
47 #include "Opts.inc"
48 #undef OPTION
51 #define PREFIX(NAME, VALUE) \
52 static constexpr StringLiteral NAME##_init[] = VALUE; \
53 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
54 std::size(NAME##_init) - 1);
55 #include "Opts.inc"
56 #undef PREFIX
58 using namespace llvm::opt;
59 static constexpr opt::OptTable::Info InfoTable[] = {
60 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
61 #include "Opts.inc"
62 #undef OPTION
65 class DwpOptTable : public opt::GenericOptTable {
66 public:
67 DwpOptTable() : GenericOptTable(InfoTable) {}
69 } // end anonymous namespace
71 // Options
72 static std::vector<std::string> ExecFilenames;
73 static std::string OutputFilename;
74 static bool ContinueOnCuIndexOverflow;
76 static Expected<SmallVector<std::string, 16>>
77 getDWOFilenames(StringRef ExecFilename) {
78 auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
79 if (!ErrOrObj)
80 return ErrOrObj.takeError();
82 const ObjectFile &Obj = *ErrOrObj.get().getBinary();
83 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
85 SmallVector<std::string, 16> DWOPaths;
86 for (const auto &CU : DWARFCtx->compile_units()) {
87 const DWARFDie &Die = CU->getUnitDIE();
88 std::string DWOName = dwarf::toString(
89 Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
90 if (DWOName.empty())
91 continue;
92 std::string DWOCompDir =
93 dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
94 if (!DWOCompDir.empty()) {
95 SmallString<16> DWOPath(std::move(DWOName));
96 sys::fs::make_absolute(DWOCompDir, DWOPath);
97 if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
98 DWOPaths.push_back(std::move(DWOName));
99 else
100 DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
101 } else {
102 DWOPaths.push_back(std::move(DWOName));
105 return std::move(DWOPaths);
108 static int error(const Twine &Error, const Twine &Context) {
109 errs() << Twine("while processing ") + Context + ":\n";
110 errs() << Twine("error: ") + Error + "\n";
111 return 1;
114 static Expected<Triple> readTargetTriple(StringRef FileName) {
115 auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
116 if (!ErrOrObj)
117 return ErrOrObj.takeError();
119 return ErrOrObj->getBinary()->makeTriple();
122 int llvm_dwp_main(int argc, char **argv, const llvm::ToolContext &) {
123 InitLLVM X(argc, argv);
125 DwpOptTable Tbl;
126 llvm::BumpPtrAllocator A;
127 llvm::StringSaver Saver{A};
128 opt::InputArgList Args =
129 Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
130 llvm::errs() << Msg << '\n';
131 std::exit(1);
134 if (Args.hasArg(OPT_help)) {
135 Tbl.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
136 "merge split dwarf (.dwo) files");
137 std::exit(0);
140 if (Args.hasArg(OPT_version)) {
141 llvm::cl::PrintVersionMessage();
142 std::exit(0);
145 OutputFilename = Args.getLastArgValue(OPT_outputFileName, "");
146 ContinueOnCuIndexOverflow = Args.hasArg(OPT_continueOnCuIndexOverflow);
148 for (const llvm::opt::Arg *A : Args.filtered(OPT_execFileNames))
149 ExecFilenames.emplace_back(A->getValue());
151 std::vector<std::string> DWOFilenames;
152 for (const llvm::opt::Arg *A : Args.filtered(OPT_INPUT))
153 DWOFilenames.emplace_back(A->getValue());
155 llvm::InitializeAllTargetInfos();
156 llvm::InitializeAllTargetMCs();
157 llvm::InitializeAllTargets();
158 llvm::InitializeAllAsmPrinters();
160 for (const auto &ExecFilename : ExecFilenames) {
161 auto DWOs = getDWOFilenames(ExecFilename);
162 if (!DWOs) {
163 logAllUnhandledErrors(
164 handleErrors(DWOs.takeError(),
165 [&](std::unique_ptr<ECError> EC) -> Error {
166 return createFileError(ExecFilename,
167 Error(std::move(EC)));
169 WithColor::error());
170 return 1;
172 DWOFilenames.insert(DWOFilenames.end(),
173 std::make_move_iterator(DWOs->begin()),
174 std::make_move_iterator(DWOs->end()));
177 if (DWOFilenames.empty())
178 return 0;
180 std::string ErrorStr;
181 StringRef Context = "dwarf streamer init";
183 auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
184 if (!ErrOrTriple) {
185 logAllUnhandledErrors(
186 handleErrors(ErrOrTriple.takeError(),
187 [&](std::unique_ptr<ECError> EC) -> Error {
188 return createFileError(DWOFilenames.front(),
189 Error(std::move(EC)));
191 WithColor::error());
192 return 1;
195 // Get the target.
196 const Target *TheTarget =
197 TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
198 if (!TheTarget)
199 return error(ErrorStr, Context);
200 std::string TripleName = ErrOrTriple->getTriple();
202 // Create all the MC Objects.
203 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
204 if (!MRI)
205 return error(Twine("no register info for target ") + TripleName, Context);
207 MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
208 std::unique_ptr<MCAsmInfo> MAI(
209 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
210 if (!MAI)
211 return error("no asm info for target " + TripleName, Context);
213 std::unique_ptr<MCSubtargetInfo> MSTI(
214 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
215 if (!MSTI)
216 return error("no subtarget info for target " + TripleName, Context);
218 MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
219 std::unique_ptr<MCObjectFileInfo> MOFI(
220 TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
221 MC.setObjectFileInfo(MOFI.get());
223 MCTargetOptions Options;
224 auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
225 if (!MAB)
226 return error("no asm backend for target " + TripleName, Context);
228 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
229 if (!MII)
230 return error("no instr info info for target " + TripleName, Context);
232 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
233 if (!MCE)
234 return error("no code emitter for target " + TripleName, Context);
236 // Create the output file.
237 std::error_code EC;
238 ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
239 std::optional<buffer_ostream> BOS;
240 raw_pwrite_stream *OS;
241 if (EC)
242 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
243 if (OutFile.os().supportsSeeking()) {
244 OS = &OutFile.os();
245 } else {
246 BOS.emplace(OutFile.os());
247 OS = &*BOS;
250 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
251 *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
252 MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI,
253 MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible,
254 /*DWARFMustBeAtTheEnd*/ false));
255 if (!MS)
256 return error("no object streamer for target " + TripleName, Context);
258 if (auto Err = write(*MS, DWOFilenames, ContinueOnCuIndexOverflow)) {
259 logAllUnhandledErrors(std::move(Err), WithColor::error());
260 return 1;
263 MS->finish();
264 OutFile.keep();
265 return 0;