]>
git.saurik.com Git - apple/security.git/blob - libsecurity_utilities/lib/dyldcache.cpp
2 * Copyright (c) 2009 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
},
48 // Architecture matching and lookup
50 std::string
DYLDCache::pathFor(const Architecture
&arch
)
52 for (const ArchType
*it
= architectures
; it
->cpu
; it
++)
53 if (arch
.matches(it
->architecture()))
55 UnixError::throwMe(ENOEXEC
);
58 const DYLDCache::ArchType
*DYLDCache::matchArchitecture(const dyld_cache_header
&header
)
60 for (const ArchType
*arch
= architectures
; arch
->cpu
; arch
++)
61 if (!strcmp(header
.magic
, arch
->magic
))
68 // Construction and teardown
70 DYLDCache::DYLDCache(const std::string
&path
)
73 mLength
= this->fileSize();
74 mBase
= this->mmap(PROT_READ
, mLength
);
75 mHeader
= at
<dyld_cache_header
>(0);
77 if ((mArch
= matchArchitecture(*mHeader
)) == NULL
)
78 UnixError::throwMe(ENOEXEC
);
79 mFlip
= *((const uint8_t *)&mArch
->order
) != 0x12;
81 mSigStart
= flip(mHeader
->codeSignatureOffset
);
82 mSigLength
= flip(mHeader
->codeSignatureSize
);
86 DYLDCache::~DYLDCache()
88 ::munmap((void *)mBase
, mLength
);
93 // Preflight a file for file type
95 bool DYLDCache::validate(UnixPlusPlus::FileDesc
&fd
)
97 dyld_cache_header header
;
98 return fd
.read(&header
, sizeof(header
), 0) == sizeof(header
)
99 && matchArchitecture(header
) != NULL
;
104 // Locate a mapping in the cache
106 DYLDCache::Mapping
DYLDCache::mapping(unsigned ix
) const
108 assert(ix
< this->mappingCount());
109 return Mapping(*this, flip(mHeader
->mappingOffset
) + ix
* sizeof(shared_file_mapping_np
));
114 // Locate an image in the cache
116 DYLDCache::Image
DYLDCache::image(unsigned ix
) const
118 assert(ix
< this->imageCount());
119 return Image(*this, flip(mHeader
->imagesOffset
) + ix
* sizeof(dyld_cache_image_info
));
124 DYLDCache::Mapping
DYLDCache::findMap(uint64_t address
) const
126 for (unsigned ix
= 0; ix
< mappingCount(); ix
++) {
127 Mapping map
= this->mapping(ix
);
128 if (map
.contains(address
))
131 UnixError::throwMe(EINVAL
);
134 uint64_t DYLDCache::mapAddress(uint64_t address
) const
136 Mapping map
= this->findMap(address
);
137 return (address
- map
.address()) + map
.offset();