]> git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/dyldcache.cpp
Security-55179.1.tar.gz
[apple/security.git] / libsecurity_utilities / lib / dyldcache.cpp
1 /*
2 * Copyright (c) 2009 Apple 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 // dyldcache - access layer to the DYLD Shared Library Cache file
26 //
27 #include "dyldcache.h"
28
29
30 //
31 // Table of supported architectures.
32 // The cache file header has no direct architecture information, so we need to deduce it like this.
33 //
34 static const uint16_t bigEndian = 0x1200;
35 static const uint16_t littleEndian = 0x0012;
36
37 const DYLDCache::ArchType DYLDCache::architectures[] = {
38 { CPU_TYPE_X86_64, CPU_SUBTYPE_MULTIPLE, "dyld_v1 x86_64", "x86_64", littleEndian },
39 { CPU_TYPE_X86, CPU_SUBTYPE_MULTIPLE, "dyld_v1 i386", "i386", littleEndian },
40 { CPU_TYPE_POWERPC, CPU_SUBTYPE_MULTIPLE, "dyld_v1 ppc", "rosetta", bigEndian },
41 { CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6, "dyld_v1 armv6", "armv6", littleEndian },
42 { CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V7, "dyld_v1 armv7", "armv7", littleEndian },
43 { 0 }
44 };
45
46
47 //
48 // Architecture matching and lookup
49 //
50 std::string DYLDCache::pathFor(const Architecture &arch)
51 {
52 for (const ArchType *it = architectures; it->cpu; it++)
53 if (arch.matches(it->architecture()))
54 return it->path();
55 UnixError::throwMe(ENOEXEC);
56 }
57
58 const DYLDCache::ArchType *DYLDCache::matchArchitecture(const dyld_cache_header &header)
59 {
60 for (const ArchType *arch = architectures; arch->cpu; arch++)
61 if (!strcmp(header.magic, arch->magic))
62 return arch;
63 return NULL;
64 }
65
66
67 //
68 // Construction and teardown
69 //
70 DYLDCache::DYLDCache(const std::string &path)
71 {
72 this->open(path);
73 mLength = this->fileSize();
74 mBase = this->mmap(PROT_READ, mLength);
75 mHeader = at<dyld_cache_header>(0);
76
77 if ((mArch = matchArchitecture(*mHeader)) == NULL)
78 UnixError::throwMe(ENOEXEC);
79 mFlip = *((const uint8_t *)&mArch->order) != 0x12;
80
81 mSigStart = flip(mHeader->codeSignatureOffset);
82 mSigLength = flip(mHeader->codeSignatureSize);
83 }
84
85
86 DYLDCache::~DYLDCache()
87 {
88 ::munmap((void *)mBase, mLength);
89 }
90
91
92 //
93 // Preflight a file for file type
94 //
95 bool DYLDCache::validate(UnixPlusPlus::FileDesc &fd)
96 {
97 dyld_cache_header header;
98 return fd.read(&header, sizeof(header), 0) == sizeof(header)
99 && matchArchitecture(header) != NULL;
100 }
101
102
103 //
104 // Locate a mapping in the cache
105 //
106 DYLDCache::Mapping DYLDCache::mapping(unsigned ix) const
107 {
108 assert(ix < this->mappingCount());
109 return Mapping(*this, flip(mHeader->mappingOffset) + ix * sizeof(shared_file_mapping_np));
110 }
111
112
113 //
114 // Locate an image in the cache
115 //
116 DYLDCache::Image DYLDCache::image(unsigned ix) const
117 {
118 assert(ix < this->imageCount());
119 return Image(*this, flip(mHeader->imagesOffset) + ix * sizeof(dyld_cache_image_info));
120 }
121
122
123
124 DYLDCache::Mapping DYLDCache::findMap(uint64_t address) const
125 {
126 for (unsigned ix = 0; ix < mappingCount(); ix++) {
127 Mapping map = this->mapping(ix);
128 if (map.contains(address))
129 return map;
130 }
131 UnixError::throwMe(EINVAL);
132 }
133
134 uint64_t DYLDCache::mapAddress(uint64_t address) const
135 {
136 Mapping map = this->findMap(address);
137 return (address - map.address()) + map.offset();
138 }