Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/lib/ReaderWriter/MachO/WriterMachO.cpp
Line
Count
Source (jump to first uncovered line)
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.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/BinaryFormat/MachO.h"
16
#include "llvm/Support/Debug.h"
17
#include "llvm/Support/ErrorHandling.h"
18
#include "llvm/Support/FileOutputBuffer.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
182
  MachOWriter(const MachOLinkingContext &ctxt) : _ctx(ctxt) {}
30
31
167
  llvm::Error writeFile(const lld::File &file, StringRef path) override {
32
167
    // Construct empty normalized file from atoms.
33
167
    llvm::Expected<std::unique_ptr<NormalizedFile>> nFile =
34
167
        normalized::normalizedFromAtoms(file, _ctx);
35
167
    if (auto ec = nFile.takeError())
36
1
      return ec;
37
166
38
166
    // For testing, write out yaml form of normalized file.
39
166
    
if (166
_ctx.printAtoms()166
) {
40
60
      std::unique_ptr<Writer> yamlWriter = createWriterYAML(_ctx);
41
60
      if (auto ec = yamlWriter->writeFile(file, "-"))
42
0
        return ec;
43
166
    }
44
166
45
166
    // Write normalized file as mach-o binary.
46
166
    return writeBinary(*nFile->get(), path);
47
166
  }
48
49
182
  void createImplicitFiles(std::vector<std::unique_ptr<File>> &r) override {
50
182
    // When building main executables, add _main as required entry point.
51
182
    if (_ctx.outputTypeHasEntry())
52
54
      r.emplace_back(new CEntryFile(_ctx));
53
182
    // If this can link with dylibs, need helper function (dyld_stub_binder).
54
182
    if (_ctx.needsStubsPass())
55
101
      r.emplace_back(new StubHelperFile(_ctx));
56
182
    // Final linked images can access a symbol for their mach_header.
57
182
    if (_ctx.outputMachOType() != llvm::MachO::MH_OBJECT)
58
101
      r.emplace_back(new MachHeaderAliasFile(_ctx));
59
182
  }
60
private:
61
  const MachOLinkingContext &_ctx;
62
 };
63
64
65
} // namespace mach_o
66
67
182
std::unique_ptr<Writer> createWriterMachO(const MachOLinkingContext &context) {
68
182
  return std::unique_ptr<Writer>(new lld::mach_o::MachOWriter(context));
69
182
}
70
71
} // namespace lld