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
This commit is contained in:
Pete Cooper 2016-03-30 23:10:39 +00:00
parent 3fb963389e
commit fefbd22814
11 changed files with 57 additions and 52 deletions

View File

@ -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++; }

View File

@ -11,6 +11,7 @@
#define LLD_CORE_WRITER_H
#include "lld/Core/LLVM.h"
#include "llvm/Support/Error.h"
#include <memory>
#include <vector>
@ -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

View File

@ -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);
}

View File

@ -1199,9 +1199,11 @@ bool link(llvm::ArrayRef<const char *> 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;
}

View File

@ -294,7 +294,7 @@ readBinary(std::unique_ptr<MemoryBuffer> &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<std::unique_ptr<NormalizedFile>>
llvm::Expected<std::unique_ptr<NormalizedFile>>
normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt);

View File

@ -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<std::unique_ptr<llvm::FileOutputBuffer>> fobOrErr =
llvm::FileOutputBuffer::create(path, size(), flags);
if (std::error_code ec = fobOrErr.getError())
return ec;
return llvm::errorCodeToError(ec);
std::unique_ptr<llvm::FileOutputBuffer> &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);
}

View File

@ -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<GenericError>(
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<std::unique_ptr<NormalizedFile>>
llvm::Expected<std::unique_ptr<NormalizedFile>>
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);

View File

@ -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<std::unique_ptr<NormalizedFile>> nFile =
llvm::Expected<std::unique_ptr<NormalizedFile>> 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<Writer> yamlWriter = createWriterYAML(_ctx);
yamlWriter->writeFile(file, "-");
if (auto ec = yamlWriter->writeFile(file, "-"))
return std::move(ec);
}
// Write normalized file as mach-o binary.

View File

@ -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:

View File

@ -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);
}

View File

@ -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<MemoryBuffer> 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<MemoryBuffer> bufferOwner;
std::unique_ptr<NormalizedFile> 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<MemoryBuffer> bufferOwner;
std::unique_ptr<NormalizedFile> 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<MemoryBuffer> bufferOwner;
std::unique_ptr<NormalizedFile> f2;