aboutsummaryrefslogtreecommitdiff
path: root/lib/ReaderWriter/MachO/ObjCPass.cpp
blob: 4712d8ca969cd700ed334811e5806efbb55b831a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//===- lib/ReaderWriter/MachO/ObjCPass.cpp -------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//

#include "ArchHandler.h"
#include "File.h"
#include "MachOPasses.h"
#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
#include "lld/Core/LLVM.h"
#include "lld/Core/Reference.h"
#include "lld/Core/Simple.h"
#include "lld/ReaderWriter/MachOLinkingContext.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"

namespace lld {
namespace mach_o {

///
/// ObjC Image Info Atom created by the ObjC pass.
///
class ObjCImageInfoAtom : public SimpleDefinedAtom {
public:
  ObjCImageInfoAtom(const File &file,
                    MachOLinkingContext::ObjCConstraint objCConstraint,
                    uint32_t swiftVersion)
      : SimpleDefinedAtom(file) {

    Data.info.version = 0;

    switch (objCConstraint) {
    case MachOLinkingContext::objc_unknown:
      llvm_unreachable("Shouldn't run the objc pass without a constraint");
    case MachOLinkingContext::objc_supports_gc:
    case MachOLinkingContext::objc_gc_only:
      llvm_unreachable("GC is not supported");
    case MachOLinkingContext::objc_retainReleaseForSimulator:
      // The retain/release for simulator flag is already the correct
      // encoded value for the data so just set it here.
      Data.info.flags = (uint32_t)objCConstraint;
      break;
    case MachOLinkingContext::objc_retainRelease:
      // We don't need to encode this flag, so just leave the flags as 0.
      Data.info.flags = 0;
      break;
    }

    Data.info.flags |= (swiftVersion << 8);
  }

  ~ObjCImageInfoAtom() override = default;

  ContentType contentType() const override {
    return DefinedAtom::typeObjCImageInfo;
  }

  Alignment alignment() const override {
    return 4;
  }

  uint64_t size() const override {
    return 8;
  }

  ContentPermissions permissions() const override {
    return DefinedAtom::permR__;
  }

  ArrayRef<uint8_t> rawContent() const override {
    return llvm::makeArrayRef(Data.bytes, size());
  }

private:

  struct objc_image_info  {
    uint32_t	version;
    uint32_t	flags;
  };

  union {
    objc_image_info info;
    uint8_t bytes[8];
  } Data;
};

class ObjCPass : public Pass {
public:
  ObjCPass(const MachOLinkingContext &context)
      : _ctx(context),
        _file(*_ctx.make_file<MachOFile>("<mach-o objc pass>")) {
    _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
  }

  llvm::Error perform(SimpleFile &mergedFile) override {
    // Add the image info.
    mergedFile.addAtom(*getImageInfo());

    return llvm::Error::success();
  }

private:

  const DefinedAtom* getImageInfo() {
    return new (_file.allocator()) ObjCImageInfoAtom(_file,
                                                     _ctx.objcConstraint(),
                                                     _ctx.swiftVersion());
  }

  const MachOLinkingContext   &_ctx;
  MachOFile                   &_file;
};



void addObjCPass(PassManager &pm, const MachOLinkingContext &ctx) {
  pm.add(llvm::make_unique<ObjCPass>(ctx));
}

} // end namespace mach_o
} // end namespace lld