1 //===- lib/ReaderWriter/MachO/ObjCPass.cpp -------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
12 #include "ArchHandler.h"
14 #include "MachOPasses.h"
15 #include "lld/Core/DefinedAtom.h"
16 #include "lld/Core/File.h"
17 #include "lld/Core/LLVM.h"
18 #include "lld/Core/Reference.h"
19 #include "lld/Core/Simple.h"
20 #include "lld/ReaderWriter/MachOLinkingContext.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
28 /// ObjC Image Info Atom created by the ObjC pass.
30 class ObjCImageInfoAtom : public SimpleDefinedAtom {
32 ObjCImageInfoAtom(const File &file,
33 MachOLinkingContext::ObjCConstraint objCConstraint,
34 uint32_t swiftVersion)
35 : SimpleDefinedAtom(file) {
37 Data.info.version = 0;
39 switch (objCConstraint) {
40 case MachOLinkingContext::objc_unknown:
41 llvm_unreachable("Shouldn't run the objc pass without a constraint");
42 case MachOLinkingContext::objc_supports_gc:
43 case MachOLinkingContext::objc_gc_only:
44 llvm_unreachable("GC is not supported");
45 case MachOLinkingContext::objc_retainReleaseForSimulator:
46 // The retain/release for simulator flag is already the correct
47 // encoded value for the data so just set it here.
48 Data.info.flags = (uint32_t)objCConstraint;
50 case MachOLinkingContext::objc_retainRelease:
51 // We don't need to encode this flag, so just leave the flags as 0.
56 Data.info.flags |= (swiftVersion << 8);
59 ~ObjCImageInfoAtom() override = default;
61 ContentType contentType() const override {
62 return DefinedAtom::typeObjCImageInfo;
65 Alignment alignment() const override {
69 uint64_t size() const override {
73 ContentPermissions permissions() const override {
74 return DefinedAtom::permR__;
77 ArrayRef<uint8_t> rawContent() const override {
78 return llvm::makeArrayRef(Data.bytes, size());
83 struct objc_image_info {
94 class ObjCPass : public Pass {
96 ObjCPass(const MachOLinkingContext &context)
98 _file(*_ctx.make_file<MachOFile>("<mach-o objc pass>")) {
99 _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
102 llvm::Error perform(SimpleFile &mergedFile) override {
103 // Add the image info.
104 mergedFile.addAtom(*getImageInfo());
106 return llvm::Error::success();
111 const DefinedAtom* getImageInfo() {
112 return new (_file.allocator()) ObjCImageInfoAtom(_file,
113 _ctx.objcConstraint(),
114 _ctx.swiftVersion());
117 const MachOLinkingContext &_ctx;
123 void addObjCPass(PassManager &pm, const MachOLinkingContext &ctx) {
124 pm.add(llvm::make_unique<ObjCPass>(ctx));
127 } // end namespace mach_o
128 } // end namespace lld