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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-04-15 05:06:56 +08:00
|
|
|
#include "ExecutableAtoms.h"
|
2014-06-13 01:12:28 +08:00
|
|
|
#include "MachONormalizedFile.h"
|
|
|
|
#include "lld/Core/File.h"
|
2015-01-22 06:54:56 +08:00
|
|
|
#include "lld/Core/Writer.h"
|
2014-06-13 01:12:28 +08:00
|
|
|
#include "lld/ReaderWriter/MachOLinkingContext.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.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"
|
2012-06-01 06:34:00 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-06-13 01:15:58 +08:00
|
|
|
#include <system_error>
|
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:
|
2015-04-11 05:40:59 +08:00
|
|
|
MachOWriter(const MachOLinkingContext &ctxt) : _ctx(ctxt) {}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Error writeFile(const lld::File &file, StringRef path) override {
|
2013-11-07 05:36:55 +08:00
|
|
|
// Construct empty normalized file from atoms.
|
2016-03-31 07:10:39 +08:00
|
|
|
llvm::Expected<std::unique_ptr<NormalizedFile>> nFile =
|
2015-04-11 05:40:59 +08:00
|
|
|
normalized::normalizedFromAtoms(file, _ctx);
|
2016-03-31 07:10:39 +08:00
|
|
|
if (auto ec = nFile.takeError())
|
2016-03-31 08:35:50 +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.
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.printAtoms()) {
|
|
|
|
std::unique_ptr<Writer> yamlWriter = createWriterYAML(_ctx);
|
2016-03-31 07:10:39 +08:00
|
|
|
if (auto ec = yamlWriter->writeFile(file, "-"))
|
2016-03-31 08:35:50 +08:00
|
|
|
return ec;
|
2014-05-15 05:32:21 +08:00
|
|
|
}
|
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
|
|
|
|
2015-04-07 04:43:35 +08:00
|
|
|
void createImplicitFiles(std::vector<std::unique_ptr<File>> &r) override {
|
2014-08-14 07:31:07 +08:00
|
|
|
// When building main executables, add _main as required entry point.
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputTypeHasEntry())
|
|
|
|
r.emplace_back(new CEntryFile(_ctx));
|
2014-08-14 07:31:07 +08:00
|
|
|
// If this can link with dylibs, need helper function (dyld_stub_binder).
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.needsStubsPass())
|
|
|
|
r.emplace_back(new StubHelperFile(_ctx));
|
2014-11-13 06:21:56 +08:00
|
|
|
// Final linked images can access a symbol for their mach_header.
|
2015-04-11 05:40:59 +08:00
|
|
|
if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT)
|
|
|
|
r.emplace_back(new MachHeaderAliasFile(_ctx));
|
2013-04-05 02:59:24 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
private:
|
2015-04-11 05:40:59 +08:00
|
|
|
const MachOLinkingContext &_ctx;
|
2013-11-07 05:36:55 +08:00
|
|
|
};
|
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
|