blob: b017ffdf9fdad80f17b114599e7a91210362f502 [file] [log] [blame]
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -07001/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "MCCacheReader.h"
18
19#include "ContextManager.h"
20#include "DebugHelper.h"
21#include "FileHandle.h"
22#include "ScriptCached.h"
23#include "Runtime.h"
24
25#include <bcc/bcc_mccache.h>
26
27#include <llvm/ADT/OwningPtr.h>
28
29#include <errno.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32
33#include <utility>
34#include <vector>
35
36#include <new>
37
38#include <stdlib.h>
39#include <string.h>
40
41using namespace std;
42
43
44#if USE_MCJIT
45namespace bcc {
46
47MCCacheReader::~MCCacheReader() {
48 if (mpHeader) { free(mpHeader); }
49 if (mpCachedDependTable) { free(mpCachedDependTable); }
50 if (mpPragmaList) { free(mpPragmaList); }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070051}
52
Shih-wei Liao6d0804b2011-06-19 11:30:00 -070053ScriptCached *MCCacheReader::readCacheFile(FileHandle *objFile,
54 FileHandle *infoFile,
55 Script *S) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070056 // Check file handle
57 if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
58 return false;
59 }
60
61 mObjFile = objFile;
62 mInfoFile = infoFile;
63
64 // Allocate ScriptCached object
65 mpResult.reset(new (nothrow) ScriptCached(S));
66
67 if (!mpResult) {
68 LOGE("Unable to allocate ScriptCached object.\n");
69 return NULL;
70 }
71
72 bool result = checkFileSize()
73 && readHeader()
74 && checkHeader()
75 && checkMachineIntType()
76 && checkSectionOffsetAndSize()
77 && readStringPool()
78 && checkStringPool()
79 && readDependencyTable()
80 && checkDependency()
81 && readExportVarList()
82 && readExportFuncList()
83 && readPragmaList()
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070084 && readObjectSlotList()
85 && readObjFile()
86 && relocate()
87 ;
88
89 return result ? mpResult.take() : NULL;
90}
91
92
93bool MCCacheReader::checkFileSize() {
94 struct stat stfile;
95 if (fstat(mInfoFile->getFD(), &stfile) < 0) {
96 LOGE("Unable to stat cache file.\n");
97 return false;
98 }
99
100 mInfoFileSize = stfile.st_size;
101
102 if (mInfoFileSize < (off_t)sizeof(MCO_Header)) {
103 LOGE("Cache file is too small to be correct.\n");
104 return false;
105 }
106
107 return true;
108}
109
110
111bool MCCacheReader::readHeader() {
112 if (mInfoFile->seek(0, SEEK_SET) != 0) {
113 LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
114 return false;
115 }
116
117 mpHeader = (MCO_Header *)malloc(sizeof(MCO_Header));
118 if (!mpHeader) {
119 LOGE("Unable to allocate for cache header.\n");
120 return false;
121 }
122
123 if (mInfoFile->read(reinterpret_cast<char *>(mpHeader), sizeof(MCO_Header)) !=
124 (ssize_t)sizeof(MCO_Header)) {
125 LOGE("Unable to read cache header.\n");
126 return false;
127 }
128
129 // Dirty hack for libRS.
130 // TODO(all): This should be removed in the future.
131 if (mpHeader->libRS_threadable) {
132 mpResult->mLibRSThreadable = true;
133 }
134
135 return true;
136}
137
138
139bool MCCacheReader::checkHeader() {
140 if (memcmp(mpHeader->magic, OBCC_MAGIC, 4) != 0) {
141 LOGE("Bad magic word\n");
142 return false;
143 }
144
145 if (memcmp(mpHeader->version, OBCC_VERSION, 4) != 0) {
146 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
147 LOGI("Cache file format version mismatch: now %s cached %s\n",
148 OBCC_VERSION, mpHeader->version);
149 return false;
150 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700151 return true;
152}
153
154
155bool MCCacheReader::checkMachineIntType() {
156 uint32_t number = 0x00000001;
157
158 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
159 if ((isLittleEndian && mpHeader->endianness != 'e') ||
160 (!isLittleEndian && mpHeader->endianness != 'E')) {
161 LOGE("Machine endianness mismatch.\n");
162 return false;
163 }
164
165 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
166 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
167 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
168 LOGE("Machine integer size mismatch.\n");
169 return false;
170 }
171
172 return true;
173}
174
175
176bool MCCacheReader::checkSectionOffsetAndSize() {
177#define CHECK_SECTION_OFFSET(NAME) \
178 do { \
179 off_t offset = mpHeader-> NAME##_offset; \
180 off_t size = (off_t)mpHeader-> NAME##_size; \
181 \
182 if (mInfoFileSize < offset || mInfoFileSize < offset + size) { \
183 LOGE(#NAME " section overflow.\n"); \
184 return false; \
185 } \
186 \
187 if (offset % sizeof(int) != 0) { \
188 LOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int)); \
189 return false; \
190 } \
191 \
192 if (size < static_cast<off_t>(sizeof(size_t))) { \
193 LOGE(#NAME " size is too small to be correct.\n"); \
194 return false; \
195 } \
196 } while (0)
197
198 CHECK_SECTION_OFFSET(str_pool);
199 CHECK_SECTION_OFFSET(depend_tab);
200 //CHECK_SECTION_OFFSET(reloc_tab);
201 CHECK_SECTION_OFFSET(export_var_list);
202 CHECK_SECTION_OFFSET(export_func_list);
203 CHECK_SECTION_OFFSET(pragma_list);
204
205#undef CHECK_SECTION_OFFSET
206
207 return true;
208}
209
210
211#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
212 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
213 \
214 if (!NAME##_raw) { \
215 LOGE("Unable to allocate for " #NAME "\n"); \
216 return false; \
217 } \
218 \
219 /* We have to ensure that some one will deallocate NAME##_raw */ \
220 AUTO_MANAGED_HOLDER = NAME##_raw; \
221 \
222 if (mInfoFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
223 LOGE("Unable to seek to " #NAME " section\n"); \
224 return false; \
225 } \
226 \
227 if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw), \
228 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
229 { \
230 LOGE("Unable to read " #NAME ".\n"); \
231 return false; \
232 }
233
234
235bool MCCacheReader::readStringPool() {
236 CACHE_READER_READ_SECTION(OBCC_StringPool,
237 mpResult->mpStringPoolRaw, str_pool);
238
239 char *str_base = reinterpret_cast<char *>(str_pool_raw);
240
241 vector<char const *> &pool = mpResult->mStringPool;
242 for (size_t i = 0; i < str_pool_raw->count; ++i) {
243 char *str = str_base + str_pool_raw->list[i].offset;
244 pool.push_back(str);
245 }
246
247 return true;
248}
249
250
251bool MCCacheReader::checkStringPool() {
252 OBCC_StringPool *poolR = mpResult->mpStringPoolRaw;
253 vector<char const *> &pool = mpResult->mStringPool;
254
255 // Ensure that every c-style string is ended with '\0'
256 for (size_t i = 0; i < poolR->count; ++i) {
257 if (pool[i][poolR->list[i].length] != '\0') {
258 LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
259 return false;
260 }
261 }
262
263 return true;
264}
265
266
267bool MCCacheReader::readDependencyTable() {
268 CACHE_READER_READ_SECTION(OBCC_DependencyTable, mpCachedDependTable,
269 depend_tab);
270 return true;
271}
272
273
274bool MCCacheReader::checkDependency() {
275 if (mDependencies.size() != mpCachedDependTable->count) {
276 LOGE("Dependencies count mismatch. (%lu vs %lu)\n",
277 (unsigned long)mDependencies.size(),
278 (unsigned long)mpCachedDependTable->count);
279 return false;
280 }
281
282 vector<char const *> &strPool = mpResult->mStringPool;
283 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
284
285 dep = mDependencies.begin();
286 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
287 string const &depName = dep->first;
288 uint32_t depType = dep->second.first;
289 unsigned char const *depSHA1 = dep->second.second;
290
291 OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
292 char const *depCachedName = strPool[depCached->res_name_strp_index];
293 uint32_t depCachedType = depCached->res_type;
294 unsigned char const *depCachedSHA1 = depCached->sha1;
295
296 if (depName != depCachedName) {
297 LOGE("Cache dependency name mismatch:\n");
298 LOGE(" given: %s\n", depName.c_str());
299 LOGE(" cached: %s\n", depCachedName);
300
301 return false;
302 }
303
304 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
305 LOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
306
307#define PRINT_SHA1(PREFIX, X, POSTFIX) \
308 LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
309 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
310 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
311 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
312
313 PRINT_SHA1(" given: ", depSHA1, "\n");
314 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
315
316#undef PRINT_SHA1
317
318 return false;
319 }
320
321 if (depType != depCachedType) {
322 LOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
323 return false;
324 }
325 }
326
327 return true;
328}
329
330bool MCCacheReader::readExportVarList() {
331 CACHE_READER_READ_SECTION(OBCC_ExportVarList,
332 mpResult->mpExportVars, export_var_list);
333 return true;
334}
335
336
337bool MCCacheReader::readExportFuncList() {
338 CACHE_READER_READ_SECTION(OBCC_ExportFuncList,
339 mpResult->mpExportFuncs, export_func_list);
340 return true;
341}
342
343
344bool MCCacheReader::readPragmaList() {
345 CACHE_READER_READ_SECTION(OBCC_PragmaList, mpPragmaList, pragma_list);
346
347 vector<char const *> const &strPool = mpResult->mStringPool;
348 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
349
350 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
351 OBCC_Pragma *pragma = &pragma_list_raw->list[i];
352 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
353 strPool[pragma->value_strp_index]));
354 }
355
356 return true;
357}
358
359
360bool MCCacheReader::readObjectSlotList() {
361 CACHE_READER_READ_SECTION(OBCC_ObjectSlotList,
362 mpResult->mpObjectSlotList, object_slot_list);
363 return true;
364}
365
366void *MCCacheReader::resolveSymbolAdapter(void *context, char const *name) {
367 MCCacheReader *self = reinterpret_cast<MCCacheReader *>(context);
368
369 if (void *Addr = FindRuntimeFunction(name)) {
370 return Addr;
371 }
372
373 if (self->mpSymbolLookupFn) {
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700374 if (void *Addr =
375 self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700376 return Addr;
377 }
378 }
379
380 LOGE("Unable to resolve symbol: %s\n", name);
381 return NULL;
382}
383
384bool MCCacheReader::readObjFile() {
385 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
386 char readBuffer[1024];
387 int readSize;
388 while ((readSize = mObjFile->read(readBuffer, 1024)) > 0) {
389 mEmittedELFExecutable.append(readBuffer, readBuffer + readSize);
390 }
391 if (readSize != 0) {
392 LOGE("Read file Error");
393 return false;
394 }
Ying Wanga61d5012011-07-01 15:54:00 -0700395 LOGD("Read object file size %d", (int)mEmittedELFExecutable.size());
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700396 mpResult->mRSExecutable =
397 rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(),
398 mEmittedELFExecutable.size(),
399 &resolveSymbolAdapter, this);
400 return true;
401}
402
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700403#undef CACHE_READER_READ_SECTION
404
405bool MCCacheReader::readRelocationTable() {
406 // TODO(logan): Not finished.
407 return true;
408}
409
410
411bool MCCacheReader::relocate() {
412 void *rootPtr = rsloaderGetSymbolAddress(mpResult->mRSExecutable, "root");
413 int mRootOffset = reinterpret_cast<char *>(rootPtr) -
414 reinterpret_cast<char *>(mpHeader->root_base_addr);
415 for (size_t i = 0; i < mpResult->getExportVarCount(); ++i) {
Joseph Wen4e199de2011-06-23 17:48:24 -0700416 // Variable is optimized out by libbcc. Don't relocate.
417 if (mpResult->mpExportVars->cached_addr_list[i] == 0x00) continue;
418
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700419 mpResult->mpExportVars->cached_addr_list[i] =
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700420 reinterpret_cast<void *>(
421 reinterpret_cast<char *>(mpResult->mpExportVars->cached_addr_list[i])
422 + mRootOffset);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700423 }
424 for (size_t i = 0; i < mpResult->getExportFuncCount(); ++i) {
425 mpResult->mpExportFuncs->cached_addr_list[i] =
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700426 reinterpret_cast<void *>(
427 reinterpret_cast<char *>(mpResult->mpExportFuncs->cached_addr_list[i])
428 + mRootOffset);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700429 }
430 return true;
431}
432
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700433} // namespace bcc
434#endif