| 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/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/LLVMDriver.h" |
| 31 | #include "llvm/Support/MemoryBuffer.h" |
| 32 | #include "llvm/Support/TargetSelect.h" |
| 33 | #include "llvm/Support/ToolOutputFile.h" |
| 34 | #include <optional> |
| 35 | |
| 36 | using namespace llvm; |
| 37 | using namespace llvm::object; |
| 38 | |
| 39 | static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags; |
| 40 | |
| 41 | // Command-line option boilerplate. |
| 42 | namespace { |
| 43 | enum ID { |
| 44 | OPT_INVALID = 0, // This is not an option ID. |
| 45 | #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__), |
| 46 | #include "Opts.inc" |
| 47 | #undef OPTION |
| 48 | }; |
| 49 | |
| 50 | #define OPTTABLE_STR_TABLE_CODE |
| 51 | #include "Opts.inc" |
| 52 | #undef OPTTABLE_STR_TABLE_CODE |
| 53 | |
| 54 | #define OPTTABLE_PREFIXES_TABLE_CODE |
| 55 | #include "Opts.inc" |
| 56 | #undef OPTTABLE_PREFIXES_TABLE_CODE |
| 57 | |
| 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 |
| 63 | }; |
| 64 | |
| 65 | class DwpOptTable : public opt::GenericOptTable { |
| 66 | public: |
| 67 | DwpOptTable() |
| 68 | : GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {} |
| 69 | }; |
| 70 | } // end anonymous namespace |
| 71 | |
| 72 | // Options |
| 73 | static std::vector<std::string> ExecFilenames; |
| 74 | static std::string OutputFilename; |
| 75 | static std::string ContinueOption; |
| 76 | |
| 77 | static Expected<SmallVector<std::string, 16>> |
| 78 | getDWOFilenames(StringRef ExecFilename) { |
| 79 | auto ErrOrObj = object::ObjectFile::createObjectFile(ObjectPath: ExecFilename); |
| 80 | if (!ErrOrObj) |
| 81 | return ErrOrObj.takeError(); |
| 82 | |
| 83 | const ObjectFile &Obj = *ErrOrObj.get().getBinary(); |
| 84 | std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj); |
| 85 | |
| 86 | SmallVector<std::string, 16> DWOPaths; |
| 87 | for (const auto &CU : DWARFCtx->compile_units()) { |
| 88 | const DWARFDie &Die = CU->getUnitDIE(); |
| 89 | std::string DWOName = dwarf::toString( |
| 90 | V: Die.find(Attrs: {dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), Default: "" ); |
| 91 | if (DWOName.empty()) |
| 92 | continue; |
| 93 | std::string DWOCompDir = |
| 94 | dwarf::toString(V: Die.find(Attr: dwarf::DW_AT_comp_dir), Default: "" ); |
| 95 | if (!DWOCompDir.empty()) { |
| 96 | SmallString<16> DWOPath(DWOName); |
| 97 | sys::path::make_absolute(current_directory: DWOCompDir, path&: DWOPath); |
| 98 | if (!sys::fs::exists(Path: DWOPath) && sys::fs::exists(Path: DWOName)) |
| 99 | DWOPaths.push_back(Elt: std::move(DWOName)); |
| 100 | else |
| 101 | DWOPaths.emplace_back(Args: DWOPath.data(), Args: DWOPath.size()); |
| 102 | } else { |
| 103 | DWOPaths.push_back(Elt: std::move(DWOName)); |
| 104 | } |
| 105 | } |
| 106 | return std::move(DWOPaths); |
| 107 | } |
| 108 | |
| 109 | static int error(const Twine &Error, const Twine &Context) { |
| 110 | errs() << Twine("while processing " ) + Context + ":\n" ; |
| 111 | errs() << Twine("error: " ) + Error + "\n" ; |
| 112 | return 1; |
| 113 | } |
| 114 | |
| 115 | static Expected<Triple> readTargetTriple(StringRef FileName) { |
| 116 | auto ErrOrObj = object::ObjectFile::createObjectFile(ObjectPath: FileName); |
| 117 | if (!ErrOrObj) |
| 118 | return ErrOrObj.takeError(); |
| 119 | |
| 120 | return ErrOrObj->getBinary()->makeTriple(); |
| 121 | } |
| 122 | |
| 123 | int llvm_dwp_main(int argc, char **argv, const llvm::ToolContext &) { |
| 124 | DwpOptTable Tbl; |
| 125 | llvm::BumpPtrAllocator A; |
| 126 | llvm::StringSaver Saver{A}; |
| 127 | OnCuIndexOverflow OverflowOptValue = OnCuIndexOverflow::HardStop; |
| 128 | Dwarf64StrOffsetsPromotion Dwarf64StrOffsetsValue = |
| 129 | Dwarf64StrOffsetsPromotion::Disabled; |
| 130 | |
| 131 | opt::InputArgList Args = |
| 132 | Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) { |
| 133 | llvm::errs() << Msg << '\n'; |
| 134 | std::exit(status: 1); |
| 135 | }); |
| 136 | |
| 137 | if (Args.hasArg(Ids: OPT_help)) { |
| 138 | Tbl.printHelp(OS&: llvm::outs(), Usage: "llvm-dwp [options] <input files>" , |
| 139 | Title: "merge split dwarf (.dwo) files" ); |
| 140 | std::exit(status: 0); |
| 141 | } |
| 142 | |
| 143 | if (Args.hasArg(Ids: OPT_version)) { |
| 144 | llvm::cl::PrintVersionMessage(); |
| 145 | std::exit(status: 0); |
| 146 | } |
| 147 | |
| 148 | OutputFilename = Args.getLastArgValue(Id: OPT_outputFileName, Default: "" ); |
| 149 | if (Arg *Arg = Args.getLastArg(Ids: OPT_continueOnCuIndexOverflow, |
| 150 | Ids: OPT_continueOnCuIndexOverflow_EQ)) { |
| 151 | if (Arg->getOption().matches(ID: OPT_continueOnCuIndexOverflow)) { |
| 152 | OverflowOptValue = OnCuIndexOverflow::Continue; |
| 153 | } else { |
| 154 | ContinueOption = Arg->getValue(); |
| 155 | if (ContinueOption == "soft-stop" ) { |
| 156 | OverflowOptValue = OnCuIndexOverflow::SoftStop; |
| 157 | } else if (ContinueOption == "continue" ) { |
| 158 | OverflowOptValue = OnCuIndexOverflow::Continue; |
| 159 | } else { |
| 160 | llvm::errs() << "invalid value for --continue-on-cu-index-overflow" |
| 161 | << ContinueOption << '\n'; |
| 162 | exit(status: 1); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | if (Arg *Arg = Args.getLastArg(Ids: OPT_dwarf64StringOffsets, |
| 168 | Ids: OPT_dwarf64StringOffsets_EQ)) { |
| 169 | if (Arg->getOption().matches(ID: OPT_dwarf64StringOffsets)) { |
| 170 | Dwarf64StrOffsetsValue = Dwarf64StrOffsetsPromotion::Enabled; |
| 171 | } else { |
| 172 | std::string OptValue = Arg->getValue(); |
| 173 | if (OptValue == "disabled" ) { |
| 174 | Dwarf64StrOffsetsValue = Dwarf64StrOffsetsPromotion::Disabled; |
| 175 | } else if (OptValue == "enabled" ) { |
| 176 | Dwarf64StrOffsetsValue = Dwarf64StrOffsetsPromotion::Enabled; |
| 177 | } else if (OptValue == "always" ) { |
| 178 | Dwarf64StrOffsetsValue = Dwarf64StrOffsetsPromotion::Always; |
| 179 | } else { |
| 180 | llvm::errs() |
| 181 | << "invalid value for --dwarf64-str-offsets-promotion. Valid " |
| 182 | "values are one of: \"enabled\", \"disabled\" or \"always\".\n" ; |
| 183 | exit(status: 1); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | for (const llvm::opt::Arg *A : Args.filtered(Ids: OPT_execFileNames)) |
| 189 | ExecFilenames.emplace_back(args: A->getValue()); |
| 190 | |
| 191 | std::vector<std::string> DWOFilenames; |
| 192 | for (const llvm::opt::Arg *A : Args.filtered(Ids: OPT_INPUT)) |
| 193 | DWOFilenames.emplace_back(args: A->getValue()); |
| 194 | |
| 195 | llvm::InitializeAllTargetInfos(); |
| 196 | llvm::InitializeAllTargetMCs(); |
| 197 | llvm::InitializeAllTargets(); |
| 198 | llvm::InitializeAllAsmPrinters(); |
| 199 | |
| 200 | for (const auto &ExecFilename : ExecFilenames) { |
| 201 | auto DWOs = getDWOFilenames(ExecFilename); |
| 202 | if (!DWOs) { |
| 203 | logAllUnhandledErrors( |
| 204 | E: handleErrors(E: DWOs.takeError(), |
| 205 | Hs: [&](std::unique_ptr<ECError> EC) -> Error { |
| 206 | return createFileError(F: ExecFilename, |
| 207 | E: Error(std::move(EC))); |
| 208 | }), |
| 209 | OS&: WithColor::error()); |
| 210 | return 1; |
| 211 | } |
| 212 | DWOFilenames.insert(position: DWOFilenames.end(), |
| 213 | first: std::make_move_iterator(i: DWOs->begin()), |
| 214 | last: std::make_move_iterator(i: DWOs->end())); |
| 215 | } |
| 216 | |
| 217 | if (DWOFilenames.empty()) { |
| 218 | WithColor::defaultWarningHandler(Warning: make_error<DWPError>( |
| 219 | Args: "executable file does not contain any references to dwo files" )); |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | std::string ErrorStr; |
| 224 | StringRef Context = "dwarf streamer init" ; |
| 225 | |
| 226 | auto ErrOrTriple = readTargetTriple(FileName: DWOFilenames.front()); |
| 227 | if (!ErrOrTriple) { |
| 228 | logAllUnhandledErrors( |
| 229 | E: handleErrors(E: ErrOrTriple.takeError(), |
| 230 | Hs: [&](std::unique_ptr<ECError> EC) -> Error { |
| 231 | return createFileError(F: DWOFilenames.front(), |
| 232 | E: Error(std::move(EC))); |
| 233 | }), |
| 234 | OS&: WithColor::error()); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | // Get the target. |
| 239 | const Target *TheTarget = |
| 240 | TargetRegistry::lookupTarget(ArchName: "" , TheTriple&: *ErrOrTriple, Error&: ErrorStr); |
| 241 | if (!TheTarget) |
| 242 | return error(Error: ErrorStr, Context); |
| 243 | std::string TripleName = ErrOrTriple->getTriple(); |
| 244 | Triple TheTriple(TripleName); |
| 245 | |
| 246 | // Create all the MC Objects. |
| 247 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT: TheTriple)); |
| 248 | if (!MRI) |
| 249 | return error(Error: Twine("no register info for target " ) + TripleName, Context); |
| 250 | |
| 251 | MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags(); |
| 252 | std::unique_ptr<MCAsmInfo> MAI( |
| 253 | TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions)); |
| 254 | if (!MAI) |
| 255 | return error(Error: "no asm info for target " + TripleName, Context); |
| 256 | |
| 257 | std::unique_ptr<MCSubtargetInfo> MSTI( |
| 258 | TheTarget->createMCSubtargetInfo(TheTriple, CPU: "" , Features: "" )); |
| 259 | if (!MSTI) |
| 260 | return error(Error: "no subtarget info for target " + TripleName, Context); |
| 261 | |
| 262 | MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get()); |
| 263 | std::unique_ptr<MCObjectFileInfo> MOFI( |
| 264 | TheTarget->createMCObjectFileInfo(Ctx&: MC, /*PIC=*/false)); |
| 265 | MC.setObjectFileInfo(MOFI.get()); |
| 266 | |
| 267 | MCTargetOptions Options; |
| 268 | auto MAB = TheTarget->createMCAsmBackend(STI: *MSTI, MRI: *MRI, Options); |
| 269 | if (!MAB) |
| 270 | return error(Error: "no asm backend for target " + TripleName, Context); |
| 271 | |
| 272 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 273 | if (!MII) |
| 274 | return error(Error: "no instr info info for target " + TripleName, Context); |
| 275 | |
| 276 | MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(II: *MII, Ctx&: MC); |
| 277 | if (!MCE) |
| 278 | return error(Error: "no code emitter for target " + TripleName, Context); |
| 279 | |
| 280 | // Create the output file. |
| 281 | std::error_code EC; |
| 282 | ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None); |
| 283 | std::optional<buffer_ostream> BOS; |
| 284 | raw_pwrite_stream *OS; |
| 285 | if (EC) |
| 286 | return error(Error: Twine(OutputFilename) + ": " + EC.message(), Context); |
| 287 | if (OutFile.os().supportsSeeking()) { |
| 288 | OS = &OutFile.os(); |
| 289 | } else { |
| 290 | BOS.emplace(args&: OutFile.os()); |
| 291 | OS = &*BOS; |
| 292 | } |
| 293 | |
| 294 | std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( |
| 295 | T: *ErrOrTriple, Ctx&: MC, TAB: std::unique_ptr<MCAsmBackend>(MAB), |
| 296 | OW: MAB->createObjectWriter(OS&: *OS), Emitter: std::unique_ptr<MCCodeEmitter>(MCE), |
| 297 | STI: *MSTI)); |
| 298 | if (!MS) |
| 299 | return error(Error: "no object streamer for target " + TripleName, Context); |
| 300 | |
| 301 | if (auto Err = |
| 302 | write(Out&: *MS, Inputs: DWOFilenames, OverflowOptValue, StrOffsetsOptValue: Dwarf64StrOffsetsValue)) { |
| 303 | logAllUnhandledErrors(E: std::move(Err), OS&: WithColor::error()); |
| 304 | return 1; |
| 305 | } |
| 306 | |
| 307 | MS->finish(); |
| 308 | OutFile.keep(); |
| 309 | return 0; |
| 310 | } |
| 311 | |