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 "MachONormalizedFileBinaryUtils.h"
15 #include "MachOPasses.h"
16 #include "lld/Common/LLVM.h"
17 #include "lld/Core/DefinedAtom.h"
18 #include "lld/Core/File.h"
19 #include "lld/Core/Reference.h"
20 #include "lld/Core/Simple.h"
21 #include "lld/ReaderWriter/MachOLinkingContext.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/STLExtras.h"
29 /// ObjC Image Info Atom created by the ObjC pass.
31 class ObjCImageInfoAtom : public SimpleDefinedAtom {
33 ObjCImageInfoAtom(const File &file, bool isBig,
34 MachOLinkingContext::ObjCConstraint objCConstraint,
35 uint32_t swiftVersion)
36 : SimpleDefinedAtom(file) {
38 Data.info.version = 0;
40 switch (objCConstraint) {
41 case MachOLinkingContext::objc_unknown:
42 llvm_unreachable("Shouldn't run the objc pass without a constraint");
43 case MachOLinkingContext::objc_supports_gc:
44 case MachOLinkingContext::objc_gc_only:
45 llvm_unreachable("GC is not supported");
46 case MachOLinkingContext::objc_retainReleaseForSimulator:
47 // The retain/release for simulator flag is already the correct
48 // encoded value for the data so just set it here.
49 Data.info.flags = (uint32_t)objCConstraint;
51 case MachOLinkingContext::objc_retainRelease:
52 // We don't need to encode this flag, so just leave the flags as 0.
57 Data.info.flags |= (swiftVersion << 8);
59 normalized::write32(Data.bytes + 4, Data.info.flags, isBig);
62 ~ObjCImageInfoAtom() override = default;
64 ContentType contentType() const override {
65 return DefinedAtom::typeObjCImageInfo;
68 Alignment alignment() const override {
72 uint64_t size() const override {
76 ContentPermissions permissions() const override {
77 return DefinedAtom::permR__;
80 ArrayRef<uint8_t> rawContent() const override {
81 return llvm::makeArrayRef(Data.bytes, size());
86 struct objc_image_info {
97 class ObjCPass : public Pass {
99 ObjCPass(const MachOLinkingContext &context)
101 _file(*_ctx.make_file<MachOFile>("<mach-o objc pass>")) {
102 _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
105 llvm::Error perform(SimpleFile &mergedFile) override {
106 // Add the image info.
107 mergedFile.addAtom(*getImageInfo());
109 return llvm::Error::success();
114 const DefinedAtom* getImageInfo() {
115 bool IsBig = MachOLinkingContext::isBigEndian(_ctx.arch());
116 return new (_file.allocator()) ObjCImageInfoAtom(_file, IsBig,
117 _ctx.objcConstraint(),
118 _ctx.swiftVersion());
121 const MachOLinkingContext &_ctx;
127 void addObjCPass(PassManager &pm, const MachOLinkingContext &ctx) {
128 pm.add(llvm::make_unique<ObjCPass>(ctx));
131 } // end namespace mach_o
132 } // end namespace lld