From fefbd22814ce5ed3928cc38ad6be1bf7de013905 Mon Sep 17 00:00:00 2001 From: Pete Cooper Date: Wed, 30 Mar 2016 23:10:39 +0000 Subject: [PATCH] Convert lld file writing to llvm::Error. NFC. This converts the writeFile method, as well as some of the ones it calls in the normalized binary file writer and yaml writer. llvm-svn: 264961 --- lld/include/lld/Core/LinkingContext.h | 2 +- lld/include/lld/Core/Writer.h | 3 +- lld/lib/Core/LinkingContext.cpp | 2 +- lld/lib/Driver/DarwinLdDriver.cpp | 8 +++-- lld/lib/ReaderWriter/MachO/MachONormalizedFile.h | 4 +-- .../MachO/MachONormalizedFileBinaryWriter.cpp | 14 ++++---- .../MachO/MachONormalizedFileFromAtoms.cpp | 40 +++++++++++----------- lld/lib/ReaderWriter/MachO/WriterMachO.cpp | 11 +++--- lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp | 6 ++-- .../MachONormalizedFileBinaryReaderTests.cpp | 3 +- .../MachONormalizedFileBinaryWriterTests.cpp | 16 ++++----- 11 files changed, 57 insertions(+), 52 deletions(-) diff --git a/lld/include/lld/Core/LinkingContext.h b/lld/include/lld/Core/LinkingContext.h index cfbc239..7e4edaf 100644 --- a/lld/include/lld/Core/LinkingContext.h +++ b/lld/include/lld/Core/LinkingContext.h @@ -194,7 +194,7 @@ public: /// Calls through to the writeFile() method on the specified Writer. /// /// \param linkedFile This is the merged/linked graph of all input file Atoms. - virtual std::error_code writeFile(const File &linkedFile) const; + virtual llvm::Error writeFile(const File &linkedFile) const; /// Return the next ordinal and Increment it. virtual uint64_t getNextOrdinalAndIncrement() const { return _nextOrdinal++; } diff --git a/lld/include/lld/Core/Writer.h b/lld/include/lld/Core/Writer.h index 38db0ff..216f934 100644 --- a/lld/include/lld/Core/Writer.h +++ b/lld/include/lld/Core/Writer.h @@ -11,6 +11,7 @@ #define LLD_CORE_WRITER_H #include "lld/Core/LLVM.h" +#include "llvm/Support/Error.h" #include #include @@ -27,7 +28,7 @@ public: virtual ~Writer(); /// \brief Write a file from the supplied File object - virtual std::error_code writeFile(const File &linkedFile, StringRef path) = 0; + virtual llvm::Error writeFile(const File &linkedFile, StringRef path) = 0; /// \brief This method is called by Core Linking to give the Writer a chance /// to add file format specific "files" to set of files to be linked. This is diff --git a/lld/lib/Core/LinkingContext.cpp b/lld/lib/Core/LinkingContext.cpp index 1671c57..2732543 100644 --- a/lld/lib/Core/LinkingContext.cpp +++ b/lld/lib/Core/LinkingContext.cpp @@ -23,7 +23,7 @@ bool LinkingContext::validate(raw_ostream &diagnostics) { return validateImpl(diagnostics); } -std::error_code LinkingContext::writeFile(const File &linkedFile) const { +llvm::Error LinkingContext::writeFile(const File &linkedFile) const { return this->writer().writeFile(linkedFile, _outputPath); } diff --git a/lld/lib/Driver/DarwinLdDriver.cpp b/lld/lib/Driver/DarwinLdDriver.cpp index 41c7a8f..429840b 100644 --- a/lld/lib/Driver/DarwinLdDriver.cpp +++ b/lld/lib/Driver/DarwinLdDriver.cpp @@ -1199,9 +1199,11 @@ bool link(llvm::ArrayRef args, raw_ostream &diagnostics) { // Give linked atoms to Writer to generate output file. ScopedTask writeTask(getDefaultDomain(), "Write"); - if (std::error_code ec = ctx.writeFile(*merged)) { - diagnostics << "Failed to write file '" << ctx.outputPath() - << "': " << ec.message() << "\n"; + if (auto ec = ctx.writeFile(*merged)) { + // FIXME: This should be passed to logAllUnhandledErrors but it needs + // to be passed a Twine instead of a string. + diagnostics << "Failed to write file '" << ctx.outputPath() << "': "; + logAllUnhandledErrors(std::move(ec), diagnostics, std::string()); return false; } diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h b/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h index 7934a6c..98767e9 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFile.h @@ -294,7 +294,7 @@ readBinary(std::unique_ptr &mb, const MachOLinkingContext::Arch arch); /// Takes in-memory normalized view and writes a mach-o object file. -std::error_code writeBinary(const NormalizedFile &file, StringRef path); +llvm::Error writeBinary(const NormalizedFile &file, StringRef path); size_t headerAndLoadCommandsSize(const NormalizedFile &file); @@ -322,7 +322,7 @@ normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path, bool copyRefs); /// Takes atoms and generates a normalized macho-o view. -ErrorOr> +llvm::Expected> normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt); diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp index f85dbba..b8c7396 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp @@ -133,7 +133,7 @@ public: /// Writes the normalized file as a binary mach-o file to the specified /// path. This does not have a stream interface because the generated /// file may need the 'x' bit set. - std::error_code writeBinary(StringRef path); + llvm::Error writeBinary(StringRef path); private: uint32_t loadCommandsSize(uint32_t &count); @@ -1459,10 +1459,10 @@ void MachOFileLayout::writeLinkEditContent() { } } -std::error_code MachOFileLayout::writeBinary(StringRef path) { +llvm::Error MachOFileLayout::writeBinary(StringRef path) { // Check for pending error from constructor. if (_ec) - return _ec; + return llvm::errorCodeToError(_ec); // Create FileOutputBuffer with calculated size. unsigned flags = 0; if (_file.fileType != llvm::MachO::MH_OBJECT) @@ -1470,23 +1470,23 @@ std::error_code MachOFileLayout::writeBinary(StringRef path) { ErrorOr> fobOrErr = llvm::FileOutputBuffer::create(path, size(), flags); if (std::error_code ec = fobOrErr.getError()) - return ec; + return llvm::errorCodeToError(ec); std::unique_ptr &fob = *fobOrErr; // Write content. _buffer = fob->getBufferStart(); writeMachHeader(); std::error_code ec = writeLoadCommands(); if (ec) - return ec; + return llvm::errorCodeToError(ec); writeSectionContent(); writeLinkEditContent(); fob->commit(); - return std::error_code(); + return llvm::Error(); } /// Takes in-memory normalized view and writes a mach-o object file. -std::error_code writeBinary(const NormalizedFile &file, StringRef path) { +llvm::Error writeBinary(const NormalizedFile &file, StringRef path) { MachOFileLayout layout(file); return layout.writeBinary(path); } diff --git a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp index 5570382..bfd133e 100644 --- a/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp +++ b/lld/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp @@ -120,7 +120,7 @@ public: void copySectionInfo(NormalizedFile &file); void updateSectionInfo(NormalizedFile &file); void buildAtomToAddressMap(); - std::error_code addSymbols(const lld::File &atomFile, NormalizedFile &file); + llvm::Error addSymbols(const lld::File &atomFile, NormalizedFile &file); void addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file); void addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file); void addExportInfo(const lld::File &, NormalizedFile &file); @@ -164,9 +164,9 @@ private: uint8_t &segmentIndex, uint64_t &segmentStartAddr); const Atom *targetOfLazyPointer(const DefinedAtom *lpAtom); const Atom *targetOfStub(const DefinedAtom *stubAtom); - std::error_code getSymbolTableRegion(const DefinedAtom* atom, - bool &inGlobalsRegion, - SymbolScope &symbolScope); + llvm::Error getSymbolTableRegion(const DefinedAtom* atom, + bool &inGlobalsRegion, + SymbolScope &symbolScope); void appendSection(SectionInfo *si, NormalizedFile &file); uint32_t sectionIndexForAtom(const Atom *atom); @@ -820,55 +820,55 @@ bool Util::AtomSorter::operator()(const AtomAndIndex &left, return (left.atom->name().compare(right.atom->name()) < 0); } -std::error_code Util::getSymbolTableRegion(const DefinedAtom* atom, - bool &inGlobalsRegion, - SymbolScope &scope) { +llvm::Error Util::getSymbolTableRegion(const DefinedAtom* atom, + bool &inGlobalsRegion, + SymbolScope &scope) { bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT); switch (atom->scope()) { case Atom::scopeTranslationUnit: scope = 0; inGlobalsRegion = false; - return std::error_code(); + return llvm::Error(); case Atom::scopeLinkageUnit: if ((_ctx.exportMode() == MachOLinkingContext::ExportMode::whiteList) && _ctx.exportSymbolNamed(atom->name())) { - return make_dynamic_error_code(Twine("cannot export hidden symbol ") - + atom->name()); + return llvm::make_error( + Twine("cannot export hidden symbol ") + atom->name()); } if (rMode) { if (_ctx.keepPrivateExterns()) { // -keep_private_externs means keep in globals region as N_PEXT. scope = N_PEXT | N_EXT; inGlobalsRegion = true; - return std::error_code(); + return llvm::Error(); } } // scopeLinkageUnit symbols are no longer global once linked. scope = N_PEXT; inGlobalsRegion = false; - return std::error_code(); + return llvm::Error(); case Atom::scopeGlobal: if (_ctx.exportRestrictMode()) { if (_ctx.exportSymbolNamed(atom->name())) { scope = N_EXT; inGlobalsRegion = true; - return std::error_code(); + return llvm::Error(); } else { scope = N_PEXT; inGlobalsRegion = false; - return std::error_code(); + return llvm::Error(); } } else { scope = N_EXT; inGlobalsRegion = true; - return std::error_code(); + return llvm::Error(); } break; } } -std::error_code Util::addSymbols(const lld::File &atomFile, - NormalizedFile &file) { +llvm::Error Util::addSymbols(const lld::File &atomFile, + NormalizedFile &file) { bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT); // Mach-O symbol table has three regions: locals, globals, undefs. @@ -964,7 +964,7 @@ std::error_code Util::addSymbols(const lld::File &atomFile, file.undefinedSymbols.push_back(sym); } - return std::error_code(); + return llvm::Error(); } const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) { @@ -1350,7 +1350,7 @@ namespace mach_o { namespace normalized { /// Convert a set of Atoms into a normalized mach-o file. -ErrorOr> +llvm::Expected> normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &context) { // The util object buffers info until the normalized file can be made. @@ -1406,7 +1406,7 @@ normalizedFromAtoms(const lld::File &atomFile, util.updateSectionInfo(normFile); util.copySectionContent(normFile); if (auto ec = util.addSymbols(atomFile, normFile)) { - return ec; + return std::move(ec); } util.addIndirectSymbols(atomFile, normFile); util.addRebaseAndBindingInfo(atomFile, normFile); diff --git a/lld/lib/ReaderWriter/MachO/WriterMachO.cpp b/lld/lib/ReaderWriter/MachO/WriterMachO.cpp index cce0a17..972f987 100644 --- a/lld/lib/ReaderWriter/MachO/WriterMachO.cpp +++ b/lld/lib/ReaderWriter/MachO/WriterMachO.cpp @@ -28,17 +28,18 @@ class MachOWriter : public Writer { public: MachOWriter(const MachOLinkingContext &ctxt) : _ctx(ctxt) {} - std::error_code writeFile(const lld::File &file, StringRef path) override { + llvm::Error writeFile(const lld::File &file, StringRef path) override { // Construct empty normalized file from atoms. - ErrorOr> nFile = + llvm::Expected> nFile = normalized::normalizedFromAtoms(file, _ctx); - if (std::error_code ec = nFile.getError()) - return ec; + if (auto ec = nFile.takeError()) + return std::move(ec); // For testing, write out yaml form of normalized file. if (_ctx.printAtoms()) { std::unique_ptr yamlWriter = createWriterYAML(_ctx); - yamlWriter->writeFile(file, "-"); + if (auto ec = yamlWriter->writeFile(file, "-")) + return std::move(ec); } // Write normalized file as mach-o binary. diff --git a/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp b/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp index f98fa0e..a2f27f8 100644 --- a/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp +++ b/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp @@ -1280,12 +1280,12 @@ class Writer : public lld::Writer { public: Writer(const LinkingContext &context) : _ctx(context) {} - std::error_code writeFile(const lld::File &file, StringRef outPath) override { + llvm::Error writeFile(const lld::File &file, StringRef outPath) override { // Create stream to path. std::error_code ec; llvm::raw_fd_ostream out(outPath, ec, llvm::sys::fs::F_Text); if (ec) - return ec; + return llvm::errorCodeToError(ec); // Create yaml Output writer, using yaml options for context. YamlContext yamlContext; @@ -1297,7 +1297,7 @@ public: const lld::File *fileRef = &file; yout << fileRef; - return std::error_code(); + return llvm::Error(); } private: diff --git a/lld/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp b/lld/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp index 1a081d0..d29e0ba 100644 --- a/lld/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp +++ b/lld/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp @@ -735,5 +735,6 @@ TEST(BinaryReaderTest, hello_obj_ppc) { EXPECT_EQ(printfLabel.type, N_UNDF); EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT)); - writeBinary(*f, "/tmp/foo.o"); + auto ec = writeBinary(*f, "/tmp/foo.o"); + EXPECT_FALSE(ec); } diff --git a/lld/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp b/lld/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp index 87c5272..2aff441 100644 --- a/lld/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp +++ b/lld/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp @@ -148,8 +148,8 @@ TEST(BinaryWriterTest, obj_relocs_x86_64) { std::error_code ec = llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl); EXPECT_FALSE(ec); - ec = writeBinary(f, tmpFl); - EXPECT_FALSE(ec); + llvm::Error ec2 = writeBinary(f, tmpFl); + EXPECT_FALSE(ec2); } std::unique_ptr bufferOwner; @@ -260,8 +260,8 @@ TEST(BinaryWriterTest, obj_relocs_x86) { std::error_code ec = llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl); EXPECT_FALSE(ec); - ec = writeBinary(f, tmpFl); - EXPECT_FALSE(ec); + llvm::Error ec2 = writeBinary(f, tmpFl); + EXPECT_FALSE(ec2); } std::unique_ptr bufferOwner; std::unique_ptr f2; @@ -378,8 +378,8 @@ TEST(BinaryWriterTest, obj_relocs_armv7) { std::error_code ec = llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl); EXPECT_FALSE(ec); - ec = writeBinary(f, tmpFl); - EXPECT_FALSE(ec); + llvm::Error ec2 = writeBinary(f, tmpFl); + EXPECT_FALSE(ec2); } std::unique_ptr bufferOwner; std::unique_ptr f2; @@ -534,8 +534,8 @@ TEST(BinaryWriterTest, obj_relocs_ppc) { std::error_code ec = llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl); EXPECT_FALSE(ec); - ec = writeBinary(f, tmpFl); - EXPECT_FALSE(ec); + llvm::Error ec2 = writeBinary(f, tmpFl); + EXPECT_FALSE(ec2); } std::unique_ptr bufferOwner; std::unique_ptr f2; -- 2.7.4