]> git.saurik.com Git - apple/libsecurity_codesigning.git/blob - lib/macho++.h
abf611865ee2e01d50b0de6ad8a1250ee568a6c4
[apple/libsecurity_codesigning.git] / lib / macho++.h
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 //
25 // macho++ - Mach-O object file helpers
26 //
27 #ifndef _H_MACHOPLUSPLUS
28 #define _H_MACHOPLUSPLUS
29
30 #include <mach-o/loader.h>
31 #include <mach-o/fat.h>
32 #include <mach-o/arch.h>
33 #include <security_utilities/globalizer.h>
34 #include <security_utilities/endian.h>
35 #include <security_utilities/unix++.h>
36 #include <security_utilities/cfutilities.h>
37
38 namespace Security {
39
40
41 //
42 // An architecture specification.
43 // Simply a pair or (cpu type, cpu subtype), really.
44 //
45 class Architecture : public std::pair<cpu_type_t, cpu_subtype_t> {
46 typedef std::pair<cpu_type_t, cpu_subtype_t> _Pair;
47 public:
48 Architecture() { }
49 explicit Architecture(cpu_type_t type, cpu_subtype_t sub = CPU_SUBTYPE_MULTIPLE)
50 : std::pair<cpu_type_t, cpu_subtype_t>(type, sub) { }
51 Architecture(const fat_arch &archInFile);
52
53 cpu_type_t cpuType() const { return this->first; }
54 cpu_subtype_t cpuSubtype() const { return this->second; }
55 const char *name() const;
56
57 static const cpu_type_t none = 0;
58 operator bool () const { return cpuType() != none; }
59 bool operator ! () const { return cpuType() == none; }
60
61 public:
62 friend bool operator == (const Architecture &a1, const Architecture &a2)
63 { return _Pair(a1) == _Pair(a2); }
64
65 friend bool operator < (const Architecture &a1, const Architecture &a2)
66 { return _Pair(a1) < _Pair(a2); }
67
68 bool matches(const Architecture &templ) const
69 { return first == templ.first && (second == templ.second || templ.second == 0 || templ.second == CPU_SUBTYPE_MULTIPLE); }
70
71 public:
72 static Architecture local();
73 };
74
75
76 //
77 // A Mach-O formatted file segment.
78 //
79 class MachO : public UnixPlusPlus::FileDesc {
80 public:
81 MachO(FileDesc fd, size_t offset = 0, size_t length = 0);
82 ~MachO();
83
84 size_t offset() const { return mOffset; }
85 size_t length() const { return mLength; }
86
87 template <class T>
88 T flip(T value) const
89 { return mFlip ? Security::flip(value) : value; }
90
91 bool isFlipped() const { return mFlip; }
92 bool is64() const { return m64; }
93
94 Architecture architecture() const;
95 uint32_t type() const;
96 uint32_t flags() const;
97
98 const load_command *loadCommands() const { return mCommands; }
99 const load_command *nextCommand(const load_command *command) const;
100
101 const segment_command *findSegment(const char *segname) const;
102 const section *findSection(const char *segname, const char *sectname) const;
103
104 const linkedit_data_command *findCodeSignature() const;
105
106 size_t signingOffset() const; // starting offset of CS section, or 0 if none
107 size_t signingLength() const; // length of CS section, or 0 if none
108 size_t signingExtent() const; // signingOffset, or file length if none
109
110 void seek(size_t offset); // relative to start of image
111 CFDataRef dataAt(size_t offset, size_t size);
112
113 private:
114 size_t mOffset; // starting file offset
115 size_t mLength; // Mach-O file length
116 bool m64; // is 64-bit
117 bool mFlip; // wrong byte order (flip all integers)
118 mach_header mHeader; // Mach-O header
119 load_command *mCommands; // load commands
120 load_command *mEndCommands; // end of load commands
121 };
122
123
124 //
125 // A Universal object represents a Mach-O binary image (whole) file.
126 // It can represent a true Universal (aka "Fat") file with multiple
127 // architectures; but it will also represent a single Mach-O ("thin")
128 // binary and make you believe it's a Universal with just one architecture.
129 //
130 class Universal : public UnixPlusPlus::FileDesc {
131 public:
132 Universal(FileDesc fd);
133 ~Universal();
134
135 // return a genuine MachO object for the given architecture
136 MachO *architecture() const; // native
137 MachO *architecture(const Architecture &arch) const; // given
138
139 // return (just) the starting offset of an architecture
140 size_t archOffset() const; // native
141 size_t archOffset(const Architecture &arch) const; // given
142
143 // return a set of architectures contained
144 typedef std::set<Architecture> Architectures;
145 void architectures(Architectures &archs);
146
147 bool isUniversal() const { return mArchList != NULL; }
148 Architecture bestNativeArch() const;
149
150 public:
151 static uint32_t typeOf(FileDesc fd);
152
153 private:
154 const fat_arch *findArch(const Architecture &arch) const;
155 MachO *findImage(const Architecture &arch) const;
156
157 private:
158 fat_arch *mArchList; // architectures (NULL if thin file)
159 unsigned mArchCount; // number of architectures (if fat)
160 Architecture mThinArch; // single architecture (if thin)
161 };
162
163
164 } // end namespace Security
165
166 #endif // !_H_MACHOPLUSPLUS