2 * Copyright (c) 2017 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
28 #include <mach-o/loader.h>
29 #include <mach-o/fat.h>
30 #include <uuid/uuid.h>
32 #include "Diagnostics.h"
34 #ifndef EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE
35 #define EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE 0x02
39 #ifndef PLATFORM_IOSSIMULATOR
40 #define PLATFORM_IOSSIMULATOR (7)
43 #ifndef PLATFORM_TVOSSIMULATOR
44 #define PLATFORM_TVOSSIMULATOR (8)
47 #ifndef PLATFORM_WATCHOSSIMULATOR
48 #define PLATFORM_WATCHOSSIMULATOR (9)
56 /// Returns true if (addLHS + addRHS) > b, or if the add overflowed
58 inline bool greaterThanAddOrOverflow(uint32_t addLHS
, uint32_t addRHS
, T b
) {
59 return (addLHS
> b
) || (addRHS
> (b
-addLHS
));
62 /// Returns true if (addLHS + addRHS) > b, or if the add overflowed
64 inline bool greaterThanAddOrOverflow(uint64_t addLHS
, uint64_t addRHS
, T b
) {
65 return (addLHS
> b
) || (addRHS
> (b
-addLHS
));
69 // Note, this should match PLATFORM_* values in <mach-o/loader.h>
72 macOS
= 1, // PLATFORM_MACOS
73 iOS
= 2, // PLATFORM_IOS
74 tvOS
= 3, // PLATFORM_TVOS
75 watchOS
= 4, // PLATFORM_WATCHOS
76 bridgeOS
= 5, // PLATFORM_BRIDGEOS
77 iOSMac
= 6, // PLATFORM_IOSMAC
78 iOS_simulator
= 7, // PLATFORM_IOSSIMULATOR
79 tvOS_simulator
= 8, // PLATFORM_TVOSSIMULATOR
80 watchOS_simulator
= 9 // PLATFORM_WATCHOSSIMULATOR
83 // A file read/mapped into memory
84 struct VIS_HIDDEN FatFile
: fat_header
86 static const FatFile
* isFatFile(const void* fileContent
);
87 void forEachSlice(Diagnostics
& diag
, uint64_t fileLen
, void (^callback
)(uint32_t sliceCpuType
, uint32_t sliceCpuSubType
, const void* sliceStart
, uint64_t sliceSize
, bool& stop
)) const;
88 bool isFatFileWithSlice(Diagnostics
& diag
, uint64_t fileLen
, const char* archName
, uint64_t& sliceOffset
, uint64_t& sliceLen
, bool& missingSlice
) const;
92 // A mach-o file read/mapped into memory
93 // Only info from mach_header or load commands is accessible (no LINKEDIT info)
94 struct VIS_HIDDEN MachOFile
: mach_header
96 static const char* archName(uint32_t cputype
, uint32_t cpusubtype
);
97 static const char* platformName(Platform platform
);
98 static uint32_t cpuTypeFromArchName(const char* archName
);
99 static uint32_t cpuSubtypeFromArchName(const char* archName
);
100 static void packedVersionToString(uint32_t packedVersion
, char versionString
[32]);
101 static const char* currentArchName();
102 static Platform
currentPlatform();
103 static uint64_t read_uleb128(Diagnostics
& diag
, const uint8_t*& p
, const uint8_t* end
);
104 static int64_t read_sleb128(Diagnostics
& diag
, const uint8_t*& p
, const uint8_t* end
);
107 bool hasMachOMagic() const;
108 bool isMachO(Diagnostics
& diag
, uint64_t fileSize
) const;
109 bool isDylib() const;
110 bool isBundle() const;
111 bool isMainExecutable() const;
112 bool isDynamicExecutable() const;
114 bool isArch(const char* archName
) const;
115 const char* archName() const;
117 uint32_t pointerSize() const;
118 bool uses16KPages() const;
119 bool supportsPlatform(Platform
) const;
120 bool isSimulatorBinary() const;
121 bool getUuid(uuid_t uuid
) const;
122 bool hasWeakDefs() const;
123 bool hasThreadLocalVariables() const;
124 bool getDylibInstallName(const char** installName
, uint32_t* compatVersion
, uint32_t* currentVersion
) const;
125 void forEachSupportedPlatform(void (^callback
)(Platform platform
, uint32_t minOS
, uint32_t sdk
)) const;
126 const char* installName() const; // returns nullptr is no install name
127 void forEachDependentDylib(void (^callback
)(const char* loadPath
, bool isWeak
, bool isReExport
, bool isUpward
, uint32_t compatVersion
, uint32_t curVersion
, bool& stop
)) const;
128 bool canBePlacedInDyldCache(const char* path
, void (^failureReason
)(const char*)) const;
129 bool canBeFairPlayEncrypted() const;
130 bool isFairPlayEncrypted(uint32_t& textOffset
, uint32_t& size
) const;
131 bool hasChainedFixups() const;
132 void forDyldEnv(void (^callback
)(const char* envVar
, bool& stop
)) const;
133 bool enforceCompatVersion() const;
141 uint64_t sizeOfSections
;
143 uint32_t protections
;
144 uint32_t textRelocs
: 1, // segment has text relocs (i386 only)
147 bool readable() const { return protections
& VM_PROT_READ
; }
148 bool writable() const { return protections
& VM_PROT_WRITE
; }
149 bool executable() const { return protections
& VM_PROT_EXECUTE
; }
157 const char* sectName
;
158 uint32_t sectFileOffset
;
160 uint32_t sectAlignP2
;
165 void forEachSegment(void (^callback
)(const SegmentInfo
& info
, bool& stop
)) const;
166 void forEachSection(void (^callback
)(const SectionInfo
& sectInfo
, bool malformedSectionRange
, bool& stop
)) const;
169 void forEachLoadCommand(Diagnostics
& diag
, void (^callback
)(const load_command
* cmd
, bool& stop
)) const;
171 const encryption_info_command
* findFairPlayEncryptionLoadCommand() const;
179 static const ArchInfo _s_archInfos
[];
185 uint32_t loadCommand
;
187 static const PlatformInfo _s_platformInfos
[];
193 #endif /* MachOFile_h */