2012-06-01 06:34:00 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/WriterMachO.cpp -----------------------------===//
|
2012-04-07 09:31:00 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-23 09:18:43 +08:00
|
|
|
#include "lld/ReaderWriter/Writer.h"
|
2014-06-13 01:12:28 +08:00
|
|
|
#include "ExecutableAtoms.hpp"
|
|
|
|
#include "MachONormalizedFile.h"
|
|
|
|
#include "lld/Core/File.h"
|
|
|
|
#include "lld/ReaderWriter/MachOLinkingContext.h"
|
2012-04-07 09:31:00 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2012-06-01 06:34:00 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-09-15 00:11:34 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/MachO.h"
|
2012-06-01 06:34:00 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/system_error.h"
|
2012-04-07 09:31:00 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
using lld::mach_o::normalized::NormalizedFile;
|
|
|
|
|
2012-04-07 09:31:00 +08:00
|
|
|
namespace lld {
|
2012-06-01 06:34:00 +08:00
|
|
|
namespace mach_o {
|
2012-04-07 09:31:00 +08:00
|
|
|
|
2012-06-01 06:34:00 +08:00
|
|
|
class MachOWriter : public Writer {
|
2012-04-07 09:31:00 +08:00
|
|
|
public:
|
2013-11-07 05:36:55 +08:00
|
|
|
MachOWriter(const MachOLinkingContext &ctxt) : _context(ctxt) { }
|
|
|
|
|
2014-06-12 22:53:47 +08:00
|
|
|
std::error_code writeFile(const lld::File &file, StringRef path) override {
|
2013-11-07 05:36:55 +08:00
|
|
|
// Construct empty normalized file from atoms.
|
2014-01-27 11:09:26 +08:00
|
|
|
ErrorOr<std::unique_ptr<NormalizedFile>> nFile =
|
2013-11-07 05:36:55 +08:00
|
|
|
normalized::normalizedFromAtoms(file, _context);
|
2014-06-12 22:53:47 +08:00
|
|
|
if (std::error_code ec = nFile.getError())
|
2014-01-09 06:00:09 +08:00
|
|
|
return ec;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-05-15 05:32:21 +08:00
|
|
|
// For testing, write out yaml form of normalized file.
|
|
|
|
if (_context.printAtoms()) {
|
|
|
|
std::unique_ptr<Writer> yamlWriter = createWriterYAML(_context);
|
|
|
|
yamlWriter->writeFile(file, "-");
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
// Write normalized file as mach-o binary.
|
|
|
|
return writeBinary(*nFile->get(), path);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2014-03-07 05:14:04 +08:00
|
|
|
bool createImplicitFiles(std::vector<std::unique_ptr<File> > &r) override {
|
2013-11-07 05:36:55 +08:00
|
|
|
if (_context.outputFileType() == llvm::MachO::MH_EXECUTE) {
|
|
|
|
// When building main executables, add _main as required entry point.
|
|
|
|
r.emplace_back(new CRuntimeFile(_context));
|
2012-04-07 09:31:00 +08:00
|
|
|
}
|
2013-08-07 06:31:59 +08:00
|
|
|
return true;
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
private:
|
|
|
|
const MachOLinkingContext &_context;
|
|
|
|
};
|
2012-04-07 09:31:00 +08:00
|
|
|
|
|
|
|
|
2012-06-01 06:34:00 +08:00
|
|
|
} // namespace mach_o
|
|
|
|
|
2013-08-07 06:31:59 +08:00
|
|
|
std::unique_ptr<Writer> createWriterMachO(const MachOLinkingContext &context) {
|
|
|
|
return std::unique_ptr<Writer>(new lld::mach_o::MachOWriter(context));
|
2012-06-01 06:34:00 +08:00
|
|
|
}
|
|
|
|
|
2012-04-07 09:31:00 +08:00
|
|
|
} // namespace lld
|