/// 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++; }
#define LLD_CORE_WRITER_H
#include "lld/Core/LLVM.h"
+#include "llvm/Support/Error.h"
#include <memory>
#include <vector>
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
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);
}
// 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;
}
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);
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);
/// 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);
}
}
-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)
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);
}
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);
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);
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.
file.undefinedSymbols.push_back(sym);
}
- return std::error_code();
+ return llvm::Error();
}
const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
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.
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);
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.
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;
const lld::File *fileRef = &file;
yout << fileRef;
- return std::error_code();
+ return llvm::Error();
}
private:
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);
}
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::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;
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;
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;