2016-03-01 22:29:00 +00:00
|
|
|
//===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-03-01 22:29:00 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF
|
|
|
|
// package files).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2021-07-22 14:11:49 -07:00
|
|
|
#include "llvm/DWP/DWP.h"
|
|
|
|
#include "llvm/DWP/DWPError.h"
|
|
|
|
#include "llvm/DWP/DWPStringPool.h"
|
2017-10-11 01:57:21 +00:00
|
|
|
#include "llvm/MC/MCAsmBackend.h"
|
2015-12-01 00:48:39 +00:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2017-10-11 23:34:47 +00:00
|
|
|
#include "llvm/MC/MCCodeEmitter.h"
|
2015-12-01 00:48:39 +00:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
2018-05-18 18:26:45 +00:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2022-02-08 15:32:21 +01:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2020-03-04 00:47:43 +01:00
|
|
|
#include "llvm/MC/MCTargetOptionsCommandFlags.h"
|
2021-10-08 10:48:15 -07:00
|
|
|
#include "llvm/MC/TargetRegistry.h"
|
2023-07-12 18:49:58 +00:00
|
|
|
#include "llvm/Option/ArgList.h"
|
|
|
|
#include "llvm/Option/Option.h"
|
2020-06-23 12:23:29 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2015-12-01 00:48:39 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2023-07-31 23:05:36 +00:00
|
|
|
#include "llvm/Support/LLVMDriver.h"
|
2022-02-10 18:39:06 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2015-12-05 03:06:30 +00:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2019-02-12 20:00:51 +00:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2022-11-26 18:49:27 -08:00
|
|
|
#include <optional>
|
2015-12-01 00:48:39 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
2015-12-01 18:07:07 +00:00
|
|
|
using namespace llvm::object;
|
2015-12-01 00:48:39 +00:00
|
|
|
|
2020-03-04 00:47:43 +01:00
|
|
|
static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags;
|
|
|
|
|
2023-07-12 18:49:58 +00:00
|
|
|
// Command-line option boilerplate.
|
|
|
|
namespace {
|
|
|
|
enum ID {
|
|
|
|
OPT_INVALID = 0, // This is not an option ID.
|
2023-08-04 11:19:09 -07:00
|
|
|
#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
|
2023-07-12 18:49:58 +00:00
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2024-12-11 15:44:44 -08:00
|
|
|
#define OPTTABLE_STR_TABLE_CODE
|
2023-07-12 18:49:58 +00:00
|
|
|
#include "Opts.inc"
|
2024-12-11 15:44:44 -08:00
|
|
|
#undef OPTTABLE_STR_TABLE_CODE
|
|
|
|
|
|
|
|
#define OPTTABLE_PREFIXES_TABLE_CODE
|
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTTABLE_PREFIXES_TABLE_CODE
|
2023-07-12 18:49:58 +00:00
|
|
|
|
Reapply "[Option] Add "Visibility" field and clone the OptTable APIs to use it"
This reverts commit 4e3b89483a6922d3f48670bb1c50a37f342918c6, with
fixes for places I'd missed updating in lld and lldb. I've also
renamed OptionVisibility::Default to "DefaultVis" to avoid ambiguity
since the undecorated name has to be available anywhere Options.inc is
included.
Original message follows:
This splits OptTable's "Flags" field into "Flags" and "Visibility",
updates the places where we instantiate Option tables, and adds
variants of the OptTable APIs that use Visibility mask instead of
Include/Exclude flags.
We need to do this to clean up a bunch of complexity in the clang
driver's option handling - there's a whole slew of flags like
CoreOption, NoDriverOption, and FlangOnlyOption there today to try to
handle all of the permutations of flags that the various drivers need,
but it really doesn't scale well, as can be seen by things like the
somewhat recently introduced CLDXCOption.
Instead, we'll provide an additive model for visibility that's
separate from the other flags. For things like "HelpHidden", which is
used as a "subtractive" modifier for option visibility, we leave that
in "Flags" and handle it as a special case.
Note that we don't actually update the users of the Include/Exclude
APIs here or change the flags that exist in clang at all - that will
come in a follow up that refactors clang's Options.td to use the
increased flexibility this change allows.
Differential Revision: https://reviews.llvm.org/D157149
2023-08-14 14:07:04 -07:00
|
|
|
using namespace llvm::opt;
|
2023-07-12 18:49:58 +00:00
|
|
|
static constexpr opt::OptTable::Info InfoTable[] = {
|
2023-08-04 11:19:09 -07:00
|
|
|
#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
|
2023-07-12 18:49:58 +00:00
|
|
|
#include "Opts.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
|
|
|
class DwpOptTable : public opt::GenericOptTable {
|
|
|
|
public:
|
2024-12-11 15:44:44 -08:00
|
|
|
DwpOptTable()
|
|
|
|
: GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {}
|
2023-07-12 18:49:58 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
// Options
|
|
|
|
static std::vector<std::string> ExecFilenames;
|
|
|
|
static std::string OutputFilename;
|
2023-12-02 04:01:22 +08:00
|
|
|
static std::string ContinueOption;
|
2023-06-01 15:53:50 +08:00
|
|
|
|
2017-09-02 08:19:01 +00:00
|
|
|
static Expected<SmallVector<std::string, 16>>
|
|
|
|
getDWOFilenames(StringRef ExecFilename) {
|
|
|
|
auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename);
|
|
|
|
if (!ErrOrObj)
|
|
|
|
return ErrOrObj.takeError();
|
|
|
|
|
|
|
|
const ObjectFile &Obj = *ErrOrObj.get().getBinary();
|
|
|
|
std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj);
|
|
|
|
|
|
|
|
SmallVector<std::string, 16> DWOPaths;
|
|
|
|
for (const auto &CU : DWARFCtx->compile_units()) {
|
|
|
|
const DWARFDie &Die = CU->getUnitDIE();
|
|
|
|
std::string DWOName = dwarf::toString(
|
|
|
|
Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), "");
|
|
|
|
if (DWOName.empty())
|
|
|
|
continue;
|
|
|
|
std::string DWOCompDir =
|
|
|
|
dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
|
|
|
|
if (!DWOCompDir.empty()) {
|
2024-06-13 11:30:01 +02:00
|
|
|
SmallString<16> DWOPath(DWOName);
|
2021-02-16 13:35:32 -08:00
|
|
|
sys::fs::make_absolute(DWOCompDir, DWOPath);
|
2022-09-10 08:05:20 +08:00
|
|
|
if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
|
|
|
|
DWOPaths.push_back(std::move(DWOName));
|
|
|
|
else
|
|
|
|
DWOPaths.emplace_back(DWOPath.data(), DWOPath.size());
|
2017-09-02 08:19:01 +00:00
|
|
|
} else {
|
|
|
|
DWOPaths.push_back(std::move(DWOName));
|
|
|
|
}
|
|
|
|
}
|
2020-02-10 07:06:45 -08:00
|
|
|
return std::move(DWOPaths);
|
2017-09-02 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 23:37:44 +00:00
|
|
|
static int error(const Twine &Error, const Twine &Context) {
|
|
|
|
errs() << Twine("while processing ") + Context + ":\n";
|
|
|
|
errs() << Twine("error: ") + Error + "\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-01-25 11:05:13 +01:00
|
|
|
static Expected<Triple> readTargetTriple(StringRef FileName) {
|
|
|
|
auto ErrOrObj = object::ObjectFile::createObjectFile(FileName);
|
|
|
|
if (!ErrOrObj)
|
|
|
|
return ErrOrObj.takeError();
|
|
|
|
|
|
|
|
return ErrOrObj->getBinary()->makeTriple();
|
|
|
|
}
|
|
|
|
|
2023-07-31 23:05:36 +00:00
|
|
|
int llvm_dwp_main(int argc, char **argv, const llvm::ToolContext &) {
|
2023-07-12 18:49:58 +00:00
|
|
|
DwpOptTable Tbl;
|
|
|
|
llvm::BumpPtrAllocator A;
|
|
|
|
llvm::StringSaver Saver{A};
|
2023-12-02 04:01:22 +08:00
|
|
|
OnCuIndexOverflow OverflowOptValue = OnCuIndexOverflow::HardStop;
|
2023-07-12 18:49:58 +00:00
|
|
|
opt::InputArgList Args =
|
|
|
|
Tbl.parseArgs(argc, argv, OPT_UNKNOWN, Saver, [&](StringRef Msg) {
|
|
|
|
llvm::errs() << Msg << '\n';
|
|
|
|
std::exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (Args.hasArg(OPT_help)) {
|
|
|
|
Tbl.printHelp(llvm::outs(), "llvm-dwp [options] <input files>",
|
|
|
|
"merge split dwarf (.dwo) files");
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Args.hasArg(OPT_version)) {
|
|
|
|
llvm::cl::PrintVersionMessage();
|
|
|
|
std::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputFilename = Args.getLastArgValue(OPT_outputFileName, "");
|
2023-12-18 12:47:24 -08:00
|
|
|
if (Arg *Arg = Args.getLastArg(OPT_continueOnCuIndexOverflow,
|
|
|
|
OPT_continueOnCuIndexOverflow_EQ)) {
|
|
|
|
if (Arg->getOption().matches(OPT_continueOnCuIndexOverflow)) {
|
2023-12-02 04:01:22 +08:00
|
|
|
OverflowOptValue = OnCuIndexOverflow::Continue;
|
2023-12-18 12:47:24 -08:00
|
|
|
} else {
|
|
|
|
ContinueOption = Arg->getValue();
|
|
|
|
if (ContinueOption == "soft-stop") {
|
|
|
|
OverflowOptValue = OnCuIndexOverflow::SoftStop;
|
|
|
|
} else if (ContinueOption == "continue") {
|
|
|
|
OverflowOptValue = OnCuIndexOverflow::Continue;
|
|
|
|
} else {
|
|
|
|
llvm::errs() << "invalid value for --continue-on-cu-index-overflow"
|
|
|
|
<< ContinueOption << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
2023-12-02 04:01:22 +08:00
|
|
|
}
|
|
|
|
}
|
2023-07-12 18:49:58 +00:00
|
|
|
|
|
|
|
for (const llvm::opt::Arg *A : Args.filtered(OPT_execFileNames))
|
|
|
|
ExecFilenames.emplace_back(A->getValue());
|
|
|
|
|
|
|
|
std::vector<std::string> DWOFilenames;
|
|
|
|
for (const llvm::opt::Arg *A : Args.filtered(OPT_INPUT))
|
|
|
|
DWOFilenames.emplace_back(A->getValue());
|
2015-12-01 00:48:39 +00:00
|
|
|
|
|
|
|
llvm::InitializeAllTargetInfos();
|
|
|
|
llvm::InitializeAllTargetMCs();
|
|
|
|
llvm::InitializeAllTargets();
|
|
|
|
llvm::InitializeAllAsmPrinters();
|
|
|
|
|
2021-01-25 11:05:13 +01:00
|
|
|
for (const auto &ExecFilename : ExecFilenames) {
|
|
|
|
auto DWOs = getDWOFilenames(ExecFilename);
|
|
|
|
if (!DWOs) {
|
2022-09-14 11:32:41 +08:00
|
|
|
logAllUnhandledErrors(
|
|
|
|
handleErrors(DWOs.takeError(),
|
|
|
|
[&](std::unique_ptr<ECError> EC) -> Error {
|
|
|
|
return createFileError(ExecFilename,
|
|
|
|
Error(std::move(EC)));
|
|
|
|
}),
|
|
|
|
WithColor::error());
|
2021-01-25 11:05:13 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
DWOFilenames.insert(DWOFilenames.end(),
|
|
|
|
std::make_move_iterator(DWOs->begin()),
|
|
|
|
std::make_move_iterator(DWOs->end()));
|
|
|
|
}
|
|
|
|
|
2024-06-28 01:47:34 +08:00
|
|
|
if (DWOFilenames.empty()) {
|
|
|
|
WithColor::defaultWarningHandler(make_error<DWPError>(
|
|
|
|
"executable file does not contain any references to dwo files"));
|
2021-01-25 11:05:13 +01:00
|
|
|
return 0;
|
2024-06-28 01:47:34 +08:00
|
|
|
}
|
2021-01-25 11:05:13 +01:00
|
|
|
|
2015-12-01 00:48:39 +00:00
|
|
|
std::string ErrorStr;
|
|
|
|
StringRef Context = "dwarf streamer init";
|
|
|
|
|
2021-01-25 11:05:13 +01:00
|
|
|
auto ErrOrTriple = readTargetTriple(DWOFilenames.front());
|
|
|
|
if (!ErrOrTriple) {
|
2022-09-14 11:32:41 +08:00
|
|
|
logAllUnhandledErrors(
|
|
|
|
handleErrors(ErrOrTriple.takeError(),
|
|
|
|
[&](std::unique_ptr<ECError> EC) -> Error {
|
|
|
|
return createFileError(DWOFilenames.front(),
|
|
|
|
Error(std::move(EC)));
|
|
|
|
}),
|
|
|
|
WithColor::error());
|
2021-01-25 11:05:13 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2015-12-01 00:48:39 +00:00
|
|
|
|
|
|
|
// Get the target.
|
|
|
|
const Target *TheTarget =
|
2021-01-25 11:05:13 +01:00
|
|
|
TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr);
|
2015-12-01 00:48:39 +00:00
|
|
|
if (!TheTarget)
|
|
|
|
return error(ErrorStr, Context);
|
2021-01-25 11:05:13 +01:00
|
|
|
std::string TripleName = ErrOrTriple->getTriple();
|
2015-12-01 00:48:39 +00:00
|
|
|
|
|
|
|
// Create all the MC Objects.
|
|
|
|
std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
|
|
|
|
if (!MRI)
|
|
|
|
return error(Twine("no register info for target ") + TripleName, Context);
|
|
|
|
|
2020-03-04 00:47:43 +01:00
|
|
|
MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags();
|
2019-10-23 12:24:35 +02:00
|
|
|
std::unique_ptr<MCAsmInfo> MAI(
|
|
|
|
TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions));
|
2015-12-01 00:48:39 +00:00
|
|
|
if (!MAI)
|
|
|
|
return error("no asm info for target " + TripleName, Context);
|
|
|
|
|
2018-01-03 08:53:05 +00:00
|
|
|
std::unique_ptr<MCSubtargetInfo> MSTI(
|
|
|
|
TheTarget->createMCSubtargetInfo(TripleName, "", ""));
|
|
|
|
if (!MSTI)
|
|
|
|
return error("no subtarget info for target " + TripleName, Context);
|
|
|
|
|
2021-05-23 14:15:23 -07:00
|
|
|
MCContext MC(*ErrOrTriple, MAI.get(), MRI.get(), MSTI.get());
|
|
|
|
std::unique_ptr<MCObjectFileInfo> MOFI(
|
|
|
|
TheTarget->createMCObjectFileInfo(MC, /*PIC=*/false));
|
|
|
|
MC.setObjectFileInfo(MOFI.get());
|
2021-05-05 10:03:02 -07:00
|
|
|
|
2016-07-25 17:18:28 +00:00
|
|
|
MCTargetOptions Options;
|
2018-01-03 08:53:05 +00:00
|
|
|
auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options);
|
2015-12-01 00:48:39 +00:00
|
|
|
if (!MAB)
|
|
|
|
return error("no asm backend for target " + TripleName, Context);
|
|
|
|
|
|
|
|
std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
|
|
|
|
if (!MII)
|
|
|
|
return error("no instr info info for target " + TripleName, Context);
|
|
|
|
|
2022-02-16 13:09:59 +08:00
|
|
|
MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, MC);
|
2015-12-01 00:48:39 +00:00
|
|
|
if (!MCE)
|
|
|
|
return error("no code emitter for target " + TripleName, Context);
|
|
|
|
|
|
|
|
// Create the output file.
|
|
|
|
std::error_code EC;
|
2019-08-05 05:43:48 +00:00
|
|
|
ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None);
|
2022-11-26 18:49:27 -08:00
|
|
|
std::optional<buffer_ostream> BOS;
|
2018-09-06 20:26:54 +00:00
|
|
|
raw_pwrite_stream *OS;
|
2015-12-01 00:48:39 +00:00
|
|
|
if (EC)
|
|
|
|
return error(Twine(OutputFilename) + ": " + EC.message(), Context);
|
2019-02-12 20:00:51 +00:00
|
|
|
if (OutFile.os().supportsSeeking()) {
|
|
|
|
OS = &OutFile.os();
|
2018-09-06 20:26:54 +00:00
|
|
|
} else {
|
2019-02-12 20:00:51 +00:00
|
|
|
BOS.emplace(OutFile.os());
|
2022-11-21 19:03:40 -08:00
|
|
|
OS = &*BOS;
|
2018-09-06 20:26:54 +00:00
|
|
|
}
|
2015-12-01 00:48:39 +00:00
|
|
|
|
|
|
|
std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
|
2021-01-25 11:05:13 +01:00
|
|
|
*ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB),
|
2024-07-20 21:27:36 -07:00
|
|
|
MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE),
|
|
|
|
*MSTI));
|
2015-12-01 00:48:39 +00:00
|
|
|
if (!MS)
|
|
|
|
return error("no object streamer for target " + TripleName, Context);
|
|
|
|
|
2023-12-02 04:01:22 +08:00
|
|
|
if (auto Err = write(*MS, DWOFilenames, OverflowOptValue)) {
|
2019-02-12 20:37:33 +00:00
|
|
|
logAllUnhandledErrors(std::move(Err), WithColor::error());
|
2016-05-12 19:59:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-03-01 21:24:04 +00:00
|
|
|
|
2022-06-07 00:31:02 -07:00
|
|
|
MS->finish();
|
2019-02-12 20:00:51 +00:00
|
|
|
OutFile.keep();
|
|
|
|
return 0;
|
2015-12-01 00:48:34 +00:00
|
|
|
}
|