1 //===- lib/ReaderWriter/MachO/WriterMachO.cpp -----------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ExecutableAtoms.h"
11 #include "MachONormalizedFile.h"
12 #include "lld/Core/File.h"
13 #include "lld/Core/Writer.h"
14 #include "lld/ReaderWriter/MachOLinkingContext.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileOutputBuffer.h"
18 #include "llvm/Support/MachO.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <system_error>
22 using lld::mach_o::normalized::NormalizedFile;
27 class MachOWriter : public Writer {
29 MachOWriter(const MachOLinkingContext &ctxt) : _ctx(ctxt) {}
31 llvm::Error writeFile(const lld::File &file, StringRef path) override {
32 // Construct empty normalized file from atoms.
33 llvm::Expected<std::unique_ptr<NormalizedFile>> nFile =
34 normalized::normalizedFromAtoms(file, _ctx);
35 if (auto ec = nFile.takeError())
38 // For testing, write out yaml form of normalized file.
39 if (_ctx.printAtoms()) {
40 std::unique_ptr<Writer> yamlWriter = createWriterYAML(_ctx);
41 if (auto ec = yamlWriter->writeFile(file, "-"))
45 // Write normalized file as mach-o binary.
46 return writeBinary(*nFile->get(), path);
49 void createImplicitFiles(std::vector<std::unique_ptr<File>> &r) override {
50 // When building main executables, add _main as required entry point.
51 if (_ctx.outputTypeHasEntry())
52 r.emplace_back(new CEntryFile(_ctx));
53 // If this can link with dylibs, need helper function (dyld_stub_binder).
54 if (_ctx.needsStubsPass())
55 r.emplace_back(new StubHelperFile(_ctx));
56 // Final linked images can access a symbol for their mach_header.
57 if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT)
58 r.emplace_back(new MachHeaderAliasFile(_ctx));
61 const MachOLinkingContext &_ctx;
67 std::unique_ptr<Writer> createWriterMachO(const MachOLinkingContext &context) {
68 return std::unique_ptr<Writer>(new lld::mach_o::MachOWriter(context));