diff --git a/llvm/test/tools/llvm-dwp/WebAssembly/lit.local.cfg b/llvm/test/tools/llvm-dwp/WebAssembly/lit.local.cfg new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-dwp/WebAssembly/lit.local.cfg @@ -0,0 +1,4 @@ +if not 'WebAssembly' in config.root.targets: + config.unsupported = True + +config.suffixes = ['.s'] diff --git a/llvm/test/tools/llvm-dwp/WebAssembly/simple_dwo.s b/llvm/test/tools/llvm-dwp/WebAssembly/simple_dwo.s new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-dwp/WebAssembly/simple_dwo.s @@ -0,0 +1,11 @@ +# RUN: llvm-mc %s -filetype obj -triple wasm32-unknown-unknown -o %t.o \ +# RUN: -split-dwarf-file=%t.dwo -dwarf-version=5 +# RUN: llvm-dwp %t.dwo -o %t.dwp +# RUN: llvm-dwarfdump -v %t.dwp | FileCheck %s + +# This test checks whether llvm-dwp is able to emit object files of the same +# triple as its inputs. + +# CHECK: file format WASM + +# Empty file, we just care about the file type. diff --git a/llvm/tools/llvm-dwp/llvm-dwp.cpp b/llvm/tools/llvm-dwp/llvm-dwp.cpp --- a/llvm/tools/llvm-dwp/llvm-dwp.cpp +++ b/llvm/tools/llvm-dwp/llvm-dwp.cpp @@ -696,6 +696,14 @@ return 1; } +static Expected readTargetTriple(StringRef FileName) { + auto ErrOrObj = object::ObjectFile::createObjectFile(FileName); + if (!ErrOrObj) + return ErrOrObj.takeError(); + + return ErrOrObj->getBinary()->makeTriple(); +} + int main(int argc, char **argv) { InitLLVM X(argc, argv); @@ -706,17 +714,36 @@ llvm::InitializeAllTargets(); llvm::InitializeAllAsmPrinters(); + std::vector DWOFilenames = InputFiles; + for (const auto &ExecFilename : ExecFilenames) { + auto DWOs = getDWOFilenames(ExecFilename); + if (!DWOs) { + logAllUnhandledErrors(DWOs.takeError(), WithColor::error()); + return 1; + } + DWOFilenames.insert(DWOFilenames.end(), + std::make_move_iterator(DWOs->begin()), + std::make_move_iterator(DWOs->end())); + } + + if (DWOFilenames.empty()) + return 0; + std::string ErrorStr; StringRef Context = "dwarf streamer init"; - Triple TheTriple("x86_64-linux-gnu"); + auto ErrOrTriple = readTargetTriple(DWOFilenames.front()); + if (!ErrOrTriple) { + logAllUnhandledErrors(ErrOrTriple.takeError(), WithColor::error()); + return 1; + } // Get the target. const Target *TheTarget = - TargetRegistry::lookupTarget("", TheTriple, ErrorStr); + TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr); if (!TheTarget) return error(ErrorStr, Context); - std::string TripleName = TheTriple.getTriple(); + std::string TripleName = ErrOrTriple->getTriple(); // Create all the MC Objects. std::unique_ptr MRI(TheTarget->createMCRegInfo(TripleName)); @@ -731,7 +758,7 @@ MCObjectFileInfo MOFI; MCContext MC(MAI.get(), MRI.get(), &MOFI); - MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, MC); + MOFI.InitMCObjectFileInfo(*ErrOrTriple, /*PIC*/ false, MC); std::unique_ptr MSTI( TheTarget->createMCSubtargetInfo(TripleName, "", "")); @@ -766,25 +793,13 @@ } std::unique_ptr MS(TheTarget->createMCObjectStreamer( - TheTriple, MC, std::unique_ptr(MAB), + *ErrOrTriple, MC, std::unique_ptr(MAB), MAB->createObjectWriter(*OS), std::unique_ptr(MCE), *MSTI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible, /*DWARFMustBeAtTheEnd*/ false)); if (!MS) return error("no object streamer for target " + TripleName, Context); - std::vector DWOFilenames = InputFiles; - for (const auto &ExecFilename : ExecFilenames) { - auto DWOs = getDWOFilenames(ExecFilename); - if (!DWOs) { - logAllUnhandledErrors(DWOs.takeError(), WithColor::error()); - return 1; - } - DWOFilenames.insert(DWOFilenames.end(), - std::make_move_iterator(DWOs->begin()), - std::make_move_iterator(DWOs->end())); - } - if (auto Err = write(*MS, DWOFilenames)) { logAllUnhandledErrors(std::move(Err), WithColor::error()); return 1;