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//
34static const uint16_t bigEndian = 0x1200;
35static const uint16_t littleEndian = 0x0012;
36
37const 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
46const DYLDCache::ArchType DYLDCache::defaultArchitecture =
47	{ 0, 0, "dyld_v1 default", "unknown", littleEndian };
48
49
50//
51// Architecture matching and lookup
52//
53std::string DYLDCache::pathFor(const Architecture &arch)
54{
55	for (const ArchType *it = architectures; it->cpu; it++)
56		if (arch.matches(it->architecture()))
57			return it->path();
58	UnixError::throwMe(ENOEXEC);
59}
60
61const DYLDCache::ArchType *DYLDCache::matchArchitecture(const dyld_cache_header &header)
62{
63	for (const ArchType *arch = architectures; arch->cpu; arch++)
64		if (!strcmp(header.magic, arch->magic))
65			return arch;
66	if (!strncmp(header.magic, "dyld_v1 ", 8))
67		return &defaultArchitecture;
68	return NULL;
69}
70
71
72//
73// Construction and teardown
74//
75DYLDCache::DYLDCache(const std::string &path)
76{
77	this->open(path);
78	mLength = this->fileSize();
79	mBase = this->mmap(PROT_READ, mLength);
80	mHeader = at<dyld_cache_header>(0);
81
82	if ((mArch = matchArchitecture(*mHeader)) == NULL)
83		UnixError::throwMe(ENOEXEC);
84	mFlip = *((const uint8_t *)&mArch->order) != 0x12;
85
86	mSigStart = (size_t)flip(mHeader->codeSignatureOffset);
87	mSigLength = (size_t)flip(mHeader->codeSignatureSize);
88}
89
90
91DYLDCache::~DYLDCache()
92{
93	::munmap((void *)mBase, mLength);
94}
95
96
97//
98// Preflight a file for file type
99//
100bool DYLDCache::validate(UnixPlusPlus::FileDesc &fd)
101{
102	dyld_cache_header header;
103	return fd.read(&header, sizeof(header), 0) == sizeof(header)
104		&& matchArchitecture(header) != NULL;
105}
106
107
108//
109// Locate a mapping in the cache
110//
111DYLDCache::Mapping DYLDCache::mapping(unsigned ix) const
112{
113	assert(ix < this->mappingCount());
114	return Mapping(*this, flip(mHeader->mappingOffset) + ix * sizeof(shared_file_mapping_np));
115}
116
117
118//
119// Locate an image in the cache
120//
121DYLDCache::Image DYLDCache::image(unsigned ix) const
122{
123	assert(ix < this->imageCount());
124	return Image(*this, flip(mHeader->imagesOffset) + ix * sizeof(dyld_cache_image_info));
125}
126
127
128
129DYLDCache::Mapping DYLDCache::findMap(uint64_t address) const
130{
131	for (unsigned ix = 0; ix < mappingCount(); ix++) {
132		Mapping map = this->mapping(ix);
133		if (map.contains(address))
134			return map;
135	}
136	UnixError::throwMe(EINVAL);
137}
138
139uint64_t DYLDCache::mapAddress(uint64_t address) const
140{
141	Mapping map = this->findMap(address);
142	return (address - map.address()) + map.offset();
143}
144