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" 22*81ad6265SDimitry Andric #include "llvm/MC/MCRegisterInfo.h" 23*81ad6265SDimitry 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" 29*81ad6265SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 3062cfcf62SDimitry Andric #include "llvm/Support/TargetSelect.h" 3162cfcf62SDimitry Andric #include "llvm/Support/ToolOutputFile.h" 3262cfcf62SDimitry Andric 3362cfcf62SDimitry Andric using namespace llvm; 3462cfcf62SDimitry Andric using namespace llvm::object; 3562cfcf62SDimitry Andric 365ffd83dbSDimitry Andric static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags; 375ffd83dbSDimitry Andric 3862cfcf62SDimitry Andric cl::OptionCategory DwpCategory("Specific Options"); 39*81ad6265SDimitry Andric static cl::list<std::string> 40*81ad6265SDimitry Andric InputFiles(cl::Positional, cl::desc("<input files>"), cl::cat(DwpCategory)); 4162cfcf62SDimitry Andric 4262cfcf62SDimitry Andric static cl::list<std::string> ExecFilenames( 43*81ad6265SDimitry Andric "e", 44*81ad6265SDimitry Andric cl::desc( 45*81ad6265SDimitry Andric "Specify the executable/library files to get the list of *.dwo from"), 4662cfcf62SDimitry Andric cl::value_desc("filename"), cl::cat(DwpCategory)); 4762cfcf62SDimitry Andric 4862cfcf62SDimitry Andric static cl::opt<std::string> OutputFilename(cl::Required, "o", 4962cfcf62SDimitry Andric cl::desc("Specify the output file."), 5062cfcf62SDimitry Andric cl::value_desc("filename"), 5162cfcf62SDimitry Andric cl::cat(DwpCategory)); 5262cfcf62SDimitry Andric 5362cfcf62SDimitry Andric static Expected<SmallVector<std::string, 16>> 5462cfcf62SDimitry Andric getDWOFilenames(StringRef ExecFilename) { 5562cfcf62SDimitry Andric auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename); 5662cfcf62SDimitry Andric if (!ErrOrObj) 5762cfcf62SDimitry Andric return ErrOrObj.takeError(); 5862cfcf62SDimitry Andric 5962cfcf62SDimitry Andric const ObjectFile &Obj = *ErrOrObj.get().getBinary(); 6062cfcf62SDimitry Andric std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj); 6162cfcf62SDimitry Andric 6262cfcf62SDimitry Andric SmallVector<std::string, 16> DWOPaths; 6362cfcf62SDimitry Andric for (const auto &CU : DWARFCtx->compile_units()) { 6462cfcf62SDimitry Andric const DWARFDie &Die = CU->getUnitDIE(); 6562cfcf62SDimitry Andric std::string DWOName = dwarf::toString( 6662cfcf62SDimitry Andric Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), ""); 6762cfcf62SDimitry Andric if (DWOName.empty()) 6862cfcf62SDimitry Andric continue; 6962cfcf62SDimitry Andric std::string DWOCompDir = 7062cfcf62SDimitry Andric dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), ""); 7162cfcf62SDimitry Andric if (!DWOCompDir.empty()) { 72d409305fSDimitry Andric SmallString<16> DWOPath(std::move(DWOName)); 73d409305fSDimitry Andric sys::fs::make_absolute(DWOCompDir, DWOPath); 7462cfcf62SDimitry Andric DWOPaths.emplace_back(DWOPath.data(), DWOPath.size()); 7562cfcf62SDimitry Andric } else { 7662cfcf62SDimitry Andric DWOPaths.push_back(std::move(DWOName)); 7762cfcf62SDimitry Andric } 7862cfcf62SDimitry Andric } 7962cfcf62SDimitry Andric return std::move(DWOPaths); 8062cfcf62SDimitry Andric } 8162cfcf62SDimitry Andric 8262cfcf62SDimitry Andric static int error(const Twine &Error, const Twine &Context) { 8362cfcf62SDimitry Andric errs() << Twine("while processing ") + Context + ":\n"; 8462cfcf62SDimitry Andric errs() << Twine("error: ") + Error + "\n"; 8562cfcf62SDimitry Andric return 1; 8662cfcf62SDimitry Andric } 8762cfcf62SDimitry Andric 88e8d8bef9SDimitry Andric static Expected<Triple> readTargetTriple(StringRef FileName) { 89e8d8bef9SDimitry Andric auto ErrOrObj = object::ObjectFile::createObjectFile(FileName); 90e8d8bef9SDimitry Andric if (!ErrOrObj) 91e8d8bef9SDimitry Andric return ErrOrObj.takeError(); 92e8d8bef9SDimitry Andric 93e8d8bef9SDimitry Andric return ErrOrObj->getBinary()->makeTriple(); 94e8d8bef9SDimitry Andric } 95e8d8bef9SDimitry Andric 9662cfcf62SDimitry Andric int main(int argc, char **argv) { 9762cfcf62SDimitry Andric InitLLVM X(argc, argv); 9862cfcf62SDimitry Andric 99fe6060f1SDimitry Andric cl::HideUnrelatedOptions({&DwpCategory, &getColorCategory()}); 10062cfcf62SDimitry Andric cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n"); 10162cfcf62SDimitry Andric 10262cfcf62SDimitry Andric llvm::InitializeAllTargetInfos(); 10362cfcf62SDimitry Andric llvm::InitializeAllTargetMCs(); 10462cfcf62SDimitry Andric llvm::InitializeAllTargets(); 10562cfcf62SDimitry Andric llvm::InitializeAllAsmPrinters(); 10662cfcf62SDimitry Andric 107e8d8bef9SDimitry Andric std::vector<std::string> DWOFilenames = InputFiles; 108e8d8bef9SDimitry Andric for (const auto &ExecFilename : ExecFilenames) { 109e8d8bef9SDimitry Andric auto DWOs = getDWOFilenames(ExecFilename); 110e8d8bef9SDimitry Andric if (!DWOs) { 111e8d8bef9SDimitry Andric logAllUnhandledErrors(DWOs.takeError(), WithColor::error()); 112e8d8bef9SDimitry Andric return 1; 113e8d8bef9SDimitry Andric } 114e8d8bef9SDimitry Andric DWOFilenames.insert(DWOFilenames.end(), 115e8d8bef9SDimitry Andric std::make_move_iterator(DWOs->begin()), 116e8d8bef9SDimitry Andric std::make_move_iterator(DWOs->end())); 117e8d8bef9SDimitry Andric } 118e8d8bef9SDimitry Andric 119e8d8bef9SDimitry Andric if (DWOFilenames.empty()) 120e8d8bef9SDimitry Andric return 0; 121e8d8bef9SDimitry Andric 12262cfcf62SDimitry Andric std::string ErrorStr; 12362cfcf62SDimitry Andric StringRef Context = "dwarf streamer init"; 12462cfcf62SDimitry Andric 125e8d8bef9SDimitry Andric auto ErrOrTriple = readTargetTriple(DWOFilenames.front()); 126e8d8bef9SDimitry Andric if (!ErrOrTriple) { 127e8d8bef9SDimitry Andric logAllUnhandledErrors(ErrOrTriple.takeError(), WithColor::error()); 128e8d8bef9SDimitry Andric return 1; 129e8d8bef9SDimitry Andric } 13062cfcf62SDimitry Andric 13162cfcf62SDimitry Andric // Get the target. 13262cfcf62SDimitry Andric const Target *TheTarget = 133e8d8bef9SDimitry Andric TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr); 13462cfcf62SDimitry Andric if (!TheTarget) 13562cfcf62SDimitry Andric return error(ErrorStr, Context); 136e8d8bef9SDimitry Andric std::string TripleName = ErrOrTriple->getTriple(); 13762cfcf62SDimitry Andric 13862cfcf62SDimitry Andric // Create all the MC Objects. 13962cfcf62SDimitry Andric std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 14062cfcf62SDimitry Andric if (!MRI) 14162cfcf62SDimitry Andric return error(Twine("no register info for target ") + TripleName, Context); 14262cfcf62SDimitry Andric 1435ffd83dbSDimitry Andric MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags(); 14462cfcf62SDimitry Andric std::unique_ptr<MCAsmInfo> MAI( 14562cfcf62SDimitry Andric TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); 14662cfcf62SDimitry Andric if (!MAI) 14762cfcf62SDimitry Andric return error("no asm info for target " + TripleName, Context); 14862cfcf62SDimitry Andric 14962cfcf62SDimitry Andric std::unique_ptr<MCSubtargetInfo> MSTI( 15062cfcf62SDimitry Andric TheTarget->createMCSubtargetInfo(TripleName, "", "")); 15162cfcf62SDimitry Andric if (!MSTI) 15262cfcf62SDimitry Andric return error("no subtarget info for target " + TripleName, Context); 15362cfcf62SDimitry Andric 154fe6060f1SDimitry Andric MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get()); 155fe6060f1SDimitry Andric std::unique_ptr<MCObjectFileInfo> MOFI( 156fe6060f1SDimitry Andric TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false)); 157fe6060f1SDimitry Andric MC.setObjectFileInfo(MOFI.get()); 158fe6060f1SDimitry Andric 15962cfcf62SDimitry Andric MCTargetOptions Options; 16062cfcf62SDimitry Andric auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options); 16162cfcf62SDimitry Andric if (!MAB) 16262cfcf62SDimitry Andric return error("no asm backend for target " + TripleName, Context); 16362cfcf62SDimitry Andric 16462cfcf62SDimitry Andric std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 16562cfcf62SDimitry Andric if (!MII) 16662cfcf62SDimitry Andric return error("no instr info info for target " + TripleName, Context); 16762cfcf62SDimitry Andric 168*81ad6265SDimitry Andric MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC); 16962cfcf62SDimitry Andric if (!MCE) 17062cfcf62SDimitry Andric return error("no code emitter for target " + TripleName, Context); 17162cfcf62SDimitry Andric 17262cfcf62SDimitry Andric // Create the output file. 17362cfcf62SDimitry Andric std::error_code EC; 17462cfcf62SDimitry Andric ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None); 17562cfcf62SDimitry Andric Optional<buffer_ostream> BOS; 17662cfcf62SDimitry Andric raw_pwrite_stream *OS; 17762cfcf62SDimitry Andric if (EC) 17862cfcf62SDimitry Andric return error(Twine(OutputFilename) + ": " + EC.message(), Context); 17962cfcf62SDimitry Andric if (OutFile.os().supportsSeeking()) { 18062cfcf62SDimitry Andric OS = &OutFile.os(); 18162cfcf62SDimitry Andric } else { 18262cfcf62SDimitry Andric BOS.emplace(OutFile.os()); 18362cfcf62SDimitry Andric OS = BOS.getPointer(); 18462cfcf62SDimitry Andric } 18562cfcf62SDimitry Andric 18662cfcf62SDimitry Andric std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( 187e8d8bef9SDimitry Andric *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB), 18862cfcf62SDimitry Andric MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, 18962cfcf62SDimitry Andric MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible, 19062cfcf62SDimitry Andric /*DWARFMustBeAtTheEnd*/ false)); 19162cfcf62SDimitry Andric if (!MS) 19262cfcf62SDimitry Andric return error("no object streamer for target " + TripleName, Context); 19362cfcf62SDimitry Andric 19462cfcf62SDimitry Andric if (auto Err = write(*MS, DWOFilenames)) { 19562cfcf62SDimitry Andric logAllUnhandledErrors(std::move(Err), WithColor::error()); 19662cfcf62SDimitry Andric return 1; 19762cfcf62SDimitry Andric } 19862cfcf62SDimitry Andric 199*81ad6265SDimitry Andric MS->finish(); 20062cfcf62SDimitry Andric OutFile.keep(); 20162cfcf62SDimitry Andric return 0; 20262cfcf62SDimitry Andric } 203