]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/ReaderWriter/MachO/WriterMachO.cpp
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / lib / ReaderWriter / MachO / WriterMachO.cpp
1 //===- lib/ReaderWriter/MachO/WriterMachO.cpp -----------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ExecutableAtoms.hpp"
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>
21
22 using lld::mach_o::normalized::NormalizedFile;
23
24 namespace lld {
25 namespace mach_o {
26
27 class MachOWriter : public Writer {
28 public:
29   MachOWriter(const MachOLinkingContext &ctxt) : _context(ctxt) { }
30
31   std::error_code writeFile(const lld::File &file, StringRef path) override {
32     // Construct empty normalized file from atoms.
33     ErrorOr<std::unique_ptr<NormalizedFile>> nFile =
34                                 normalized::normalizedFromAtoms(file, _context);
35     if (std::error_code ec = nFile.getError())
36       return ec;
37
38     // For testing, write out yaml form of normalized file.
39     if (_context.printAtoms()) {
40       std::unique_ptr<Writer> yamlWriter = createWriterYAML(_context);
41       yamlWriter->writeFile(file, "-");
42     }
43
44     // Write normalized file as mach-o binary.
45     return writeBinary(*nFile->get(), path);
46   }
47
48   bool createImplicitFiles(std::vector<std::unique_ptr<File> > &r) override {
49     // When building main executables, add _main as required entry point.
50     if (_context.outputTypeHasEntry())
51       r.emplace_back(new CEntryFile(_context));
52     // If this can link with dylibs, need helper function (dyld_stub_binder).
53     if (_context.needsStubsPass())
54       r.emplace_back(new StubHelperFile(_context));
55     // Final linked images can access a symbol for their mach_header.
56     if (_context.outputMachOType() != llvm::MachO::MH_OBJECT)
57       r.emplace_back(new MachHeaderAliasFile(_context));
58
59     return true;
60   }
61 private:
62    const MachOLinkingContext  &_context;
63  };
64
65
66 } // namespace mach_o
67
68 std::unique_ptr<Writer> createWriterMachO(const MachOLinkingContext &context) {
69   return std::unique_ptr<Writer>(new lld::mach_o::MachOWriter(context));
70 }
71
72 } // namespace lld