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