]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/dyldcache.cpp
2 * Copyright (c) 2009,2011-2012 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@
25 // dyldcache - access layer to the DYLD Shared Library Cache file
27 #include "dyldcache.h"
31 // Table of supported architectures.
32 // The cache file header has no direct architecture information, so we need to deduce it like this.
34 static const uint16_t bigEndian
= 0x1200;
35 static const uint16_t littleEndian
= 0x0012;
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
},
46 const DYLDCache::ArchType
DYLDCache::defaultArchitecture
=
47 { 0, 0, "dyld_v1 default", "unknown", littleEndian
};
51 // Architecture matching and lookup
53 std::string
DYLDCache::pathFor(const Architecture
&arch
)
55 for (const ArchType
*it
= architectures
; it
->cpu
; it
++)
56 if (arch
.matches(it
->architecture()))
58 UnixError::throwMe(ENOEXEC
);
61 const DYLDCache::ArchType
*DYLDCache::matchArchitecture(const dyld_cache_header
&header
)
63 for (const ArchType
*arch
= architectures
; arch
->cpu
; arch
++)
64 if (!strcmp(header
.magic
, arch
->magic
))
66 if (!strncmp(header
.magic
, "dyld_v1 ", 8))
67 return &defaultArchitecture
;
73 // Construction and teardown
75 DYLDCache::DYLDCache(const std::string
&path
)
78 mLength
= this->fileSize();
79 mBase
= this->mmap(PROT_READ
, mLength
);
80 mHeader
= at
<dyld_cache_header
>(0);
82 if ((mArch
= matchArchitecture(*mHeader
)) == NULL
)
83 UnixError::throwMe(ENOEXEC
);
84 mFlip
= *((const uint8_t *)&mArch
->order
) != 0x12;
86 mSigStart
= (size_t)flip(mHeader
->codeSignatureOffset
);
87 mSigLength
= (size_t)flip(mHeader
->codeSignatureSize
);
88 size_t sigEnd
= mSigStart
+ mSigLength
;
89 if (mSigStart
> sigEnd
|| sigEnd
> mLength
)
90 UnixError::throwMe(ENOEXEC
);
94 DYLDCache::~DYLDCache()
96 ::munmap((void *)mBase
, mLength
);
101 // Preflight a file for file type
103 bool DYLDCache::validate(UnixPlusPlus::FileDesc
&fd
)
105 dyld_cache_header header
;
106 return fd
.read(&header
, sizeof(header
), 0) == sizeof(header
)
107 && matchArchitecture(header
) != NULL
;
112 // Locate a mapping in the cache
114 DYLDCache::Mapping
DYLDCache::mapping(unsigned ix
) const
116 assert(ix
< this->mappingCount());
117 return Mapping(*this, flip(mHeader
->mappingOffset
) + ix
* sizeof(shared_file_mapping_np
));
122 // Locate an image in the cache
124 DYLDCache::Image
DYLDCache::image(unsigned ix
) const
126 assert(ix
< this->imageCount());
127 return Image(*this, flip(mHeader
->imagesOffset
) + ix
* sizeof(dyld_cache_image_info
));
132 DYLDCache::Mapping
DYLDCache::findMap(uint64_t address
) const
134 for (unsigned ix
= 0; ix
< mappingCount(); ix
++) {
135 Mapping map
= this->mapping(ix
);
136 if (map
.contains(address
))
139 UnixError::throwMe(EINVAL
);
142 uint64_t DYLDCache::mapAddress(uint64_t address
) const
144 Mapping map
= this->findMap(address
);
145 return (address
- map
.address()) + map
.offset();