blob: bf4fe47ef6143bb1371aa5b51d959038ad161c93 [file] [log] [blame]
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -07001/*
Stephen Hinescc366e52012-02-21 17:22:04 -08002 * Copyright 2010-2012, The Android Open Source Project
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -07003 *
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
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070019#include "DebugHelper.h"
20#include "FileHandle.h"
21#include "ScriptCached.h"
22#include "Runtime.h"
23
24#include <bcc/bcc_mccache.h>
25
26#include <llvm/ADT/OwningPtr.h>
27
28#include <errno.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31
32#include <utility>
33#include <vector>
34
35#include <new>
36
37#include <stdlib.h>
38#include <string.h>
39
40using namespace std;
41
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070042namespace bcc {
43
44MCCacheReader::~MCCacheReader() {
45 if (mpHeader) { free(mpHeader); }
46 if (mpCachedDependTable) { free(mpCachedDependTable); }
47 if (mpPragmaList) { free(mpPragmaList); }
Joseph Wenf36637f2011-07-06 18:27:12 -070048 if (mpVarNameList) { free(mpVarNameList); }
49 if (mpFuncNameList) { free(mpFuncNameList); }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070050}
51
Shih-wei Liao6d0804b2011-06-19 11:30:00 -070052ScriptCached *MCCacheReader::readCacheFile(FileHandle *objFile,
53 FileHandle *infoFile,
54 Script *S) {
Joseph Wen34c600a2011-07-25 17:59:17 -070055 bool result = checkCacheFile(objFile, infoFile, S)
56 && readPragmaList()
57 && readObjectSlotList()
58 && readObjFile()
59 && readVarNameList()
60 && readFuncNameList()
Stephen Hinescc366e52012-02-21 17:22:04 -080061 && readForEachNameList()
Joseph Wen34c600a2011-07-25 17:59:17 -070062 //&& relocate()
63 ;
64
65 return result ? mpResult.take() : NULL;
66}
67
68bool MCCacheReader::checkCacheFile(FileHandle *objFile,
69 FileHandle *infoFile,
70 Script *S) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070071 // Check file handle
72 if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
Joseph Wen34c600a2011-07-25 17:59:17 -070073 return false;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070074 }
75
76 mObjFile = objFile;
77 mInfoFile = infoFile;
78
79 // Allocate ScriptCached object
80 mpResult.reset(new (nothrow) ScriptCached(S));
81
82 if (!mpResult) {
Steve Block10c14122012-01-08 10:15:06 +000083 ALOGE("Unable to allocate ScriptCached object.\n");
Joseph Wen34c600a2011-07-25 17:59:17 -070084 return false;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070085 }
86
87 bool result = checkFileSize()
88 && readHeader()
89 && checkHeader()
90 && checkMachineIntType()
91 && checkSectionOffsetAndSize()
92 && readStringPool()
93 && checkStringPool()
94 && readDependencyTable()
95 && checkDependency()
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070096 ;
97
Joseph Wen34c600a2011-07-25 17:59:17 -070098 return result;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070099}
100
101
102bool MCCacheReader::checkFileSize() {
103 struct stat stfile;
104 if (fstat(mInfoFile->getFD(), &stfile) < 0) {
Steve Block10c14122012-01-08 10:15:06 +0000105 ALOGE("Unable to stat cache file.\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700106 return false;
107 }
108
109 mInfoFileSize = stfile.st_size;
110
111 if (mInfoFileSize < (off_t)sizeof(MCO_Header)) {
Steve Block10c14122012-01-08 10:15:06 +0000112 ALOGE("Cache file is too small to be correct.\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700113 return false;
114 }
115
116 return true;
117}
118
119
120bool MCCacheReader::readHeader() {
121 if (mInfoFile->seek(0, SEEK_SET) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000122 ALOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700123 return false;
124 }
125
126 mpHeader = (MCO_Header *)malloc(sizeof(MCO_Header));
127 if (!mpHeader) {
Steve Block10c14122012-01-08 10:15:06 +0000128 ALOGE("Unable to allocate for cache header.\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700129 return false;
130 }
131
132 if (mInfoFile->read(reinterpret_cast<char *>(mpHeader), sizeof(MCO_Header)) !=
133 (ssize_t)sizeof(MCO_Header)) {
Steve Block10c14122012-01-08 10:15:06 +0000134 ALOGE("Unable to read cache header.\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700135 return false;
136 }
137
138 // Dirty hack for libRS.
139 // TODO(all): This should be removed in the future.
140 if (mpHeader->libRS_threadable) {
141 mpResult->mLibRSThreadable = true;
142 }
143
144 return true;
145}
146
147
148bool MCCacheReader::checkHeader() {
Stephen Hines0e567862012-03-11 20:26:40 -0700149 if (memcmp(mpHeader->magic, MCO_MAGIC, 4) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000150 ALOGE("Bad magic word\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700151 return false;
152 }
153
Stephen Hines0e567862012-03-11 20:26:40 -0700154 if (memcmp(mpHeader->version, MCO_VERSION, 4) != 0) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700155 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
Steve Block440e0312012-01-04 20:06:16 +0000156 ALOGI("Cache file format version mismatch: now %s cached %s\n",
Stephen Hines0e567862012-03-11 20:26:40 -0700157 MCO_VERSION, mpHeader->version);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700158 return false;
159 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700160 return true;
161}
162
163
164bool MCCacheReader::checkMachineIntType() {
165 uint32_t number = 0x00000001;
166
167 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
168 if ((isLittleEndian && mpHeader->endianness != 'e') ||
169 (!isLittleEndian && mpHeader->endianness != 'E')) {
Steve Block10c14122012-01-08 10:15:06 +0000170 ALOGE("Machine endianness mismatch.\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700171 return false;
172 }
173
174 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
175 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
176 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
Steve Block10c14122012-01-08 10:15:06 +0000177 ALOGE("Machine integer size mismatch.\n");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700178 return false;
179 }
180
181 return true;
182}
183
184
185bool MCCacheReader::checkSectionOffsetAndSize() {
186#define CHECK_SECTION_OFFSET(NAME) \
187 do { \
188 off_t offset = mpHeader-> NAME##_offset; \
189 off_t size = (off_t)mpHeader-> NAME##_size; \
190 \
191 if (mInfoFileSize < offset || mInfoFileSize < offset + size) { \
Steve Block10c14122012-01-08 10:15:06 +0000192 ALOGE(#NAME " section overflow.\n"); \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700193 return false; \
194 } \
195 \
196 if (offset % sizeof(int) != 0) { \
Steve Block10c14122012-01-08 10:15:06 +0000197 ALOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int)); \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700198 return false; \
199 } \
200 \
201 if (size < static_cast<off_t>(sizeof(size_t))) { \
Steve Block10c14122012-01-08 10:15:06 +0000202 ALOGE(#NAME " size is too small to be correct.\n"); \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700203 return false; \
204 } \
205 } while (0)
206
207 CHECK_SECTION_OFFSET(str_pool);
208 CHECK_SECTION_OFFSET(depend_tab);
209 //CHECK_SECTION_OFFSET(reloc_tab);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700210 CHECK_SECTION_OFFSET(pragma_list);
211
212#undef CHECK_SECTION_OFFSET
213
214 return true;
215}
216
217
218#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
219 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
220 \
221 if (!NAME##_raw) { \
Steve Block10c14122012-01-08 10:15:06 +0000222 ALOGE("Unable to allocate for " #NAME "\n"); \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700223 return false; \
224 } \
225 \
226 /* We have to ensure that some one will deallocate NAME##_raw */ \
227 AUTO_MANAGED_HOLDER = NAME##_raw; \
228 \
229 if (mInfoFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
Steve Block10c14122012-01-08 10:15:06 +0000230 ALOGE("Unable to seek to " #NAME " section\n"); \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700231 return false; \
232 } \
233 \
234 if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw), \
235 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
236 { \
Steve Block10c14122012-01-08 10:15:06 +0000237 ALOGE("Unable to read " #NAME ".\n"); \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700238 return false; \
239 }
240
241
242bool MCCacheReader::readStringPool() {
Stephen Hines0e567862012-03-11 20:26:40 -0700243 CACHE_READER_READ_SECTION(MCO_StringPool,
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700244 mpResult->mpStringPoolRaw, str_pool);
245
246 char *str_base = reinterpret_cast<char *>(str_pool_raw);
247
248 vector<char const *> &pool = mpResult->mStringPool;
249 for (size_t i = 0; i < str_pool_raw->count; ++i) {
250 char *str = str_base + str_pool_raw->list[i].offset;
251 pool.push_back(str);
252 }
253
254 return true;
255}
256
257
258bool MCCacheReader::checkStringPool() {
Stephen Hines0e567862012-03-11 20:26:40 -0700259 MCO_StringPool *poolR = mpResult->mpStringPoolRaw;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700260 vector<char const *> &pool = mpResult->mStringPool;
261
262 // Ensure that every c-style string is ended with '\0'
263 for (size_t i = 0; i < poolR->count; ++i) {
264 if (pool[i][poolR->list[i].length] != '\0') {
Steve Block10c14122012-01-08 10:15:06 +0000265 ALOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700266 return false;
267 }
268 }
269
270 return true;
271}
272
273
274bool MCCacheReader::readDependencyTable() {
Stephen Hines0e567862012-03-11 20:26:40 -0700275 CACHE_READER_READ_SECTION(MCO_DependencyTable, mpCachedDependTable,
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700276 depend_tab);
277 return true;
278}
279
280
281bool MCCacheReader::checkDependency() {
282 if (mDependencies.size() != mpCachedDependTable->count) {
Steve Block10c14122012-01-08 10:15:06 +0000283 ALOGE("Dependencies count mismatch. (%lu vs %lu)\n",
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700284 (unsigned long)mDependencies.size(),
285 (unsigned long)mpCachedDependTable->count);
286 return false;
287 }
288
289 vector<char const *> &strPool = mpResult->mStringPool;
290 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
291
292 dep = mDependencies.begin();
293 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
294 string const &depName = dep->first;
295 uint32_t depType = dep->second.first;
296 unsigned char const *depSHA1 = dep->second.second;
297
Stephen Hines0e567862012-03-11 20:26:40 -0700298 MCO_Dependency *depCached =&mpCachedDependTable->table[i];
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700299 char const *depCachedName = strPool[depCached->res_name_strp_index];
300 uint32_t depCachedType = depCached->res_type;
301 unsigned char const *depCachedSHA1 = depCached->sha1;
302
303 if (depName != depCachedName) {
Steve Block10c14122012-01-08 10:15:06 +0000304 ALOGE("Cache dependency name mismatch:\n");
305 ALOGE(" given: %s\n", depName.c_str());
306 ALOGE(" cached: %s\n", depCachedName);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700307
308 return false;
309 }
310
311 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000312 ALOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700313
314#define PRINT_SHA1(PREFIX, X, POSTFIX) \
Steve Block10c14122012-01-08 10:15:06 +0000315 ALOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700316 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
317 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
318 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
319
320 PRINT_SHA1(" given: ", depSHA1, "\n");
321 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
322
323#undef PRINT_SHA1
324
325 return false;
326 }
327
328 if (depType != depCachedType) {
Steve Block10c14122012-01-08 10:15:06 +0000329 ALOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700330 return false;
331 }
332 }
333
334 return true;
335}
336
Joseph Wenf36637f2011-07-06 18:27:12 -0700337bool MCCacheReader::readVarNameList() {
Stephen Hines0e567862012-03-11 20:26:40 -0700338 CACHE_READER_READ_SECTION(MCO_String_Ptr, mpVarNameList, export_var_name_list);
Joseph Wenf36637f2011-07-06 18:27:12 -0700339 vector<char const *> const &strPool = mpResult->mStringPool;
340
Stephen Hines0e567862012-03-11 20:26:40 -0700341 mpResult->mpExportVars = (MCO_ExportVarList*)
Joseph Wen8eabcbf2011-07-07 12:37:48 -0700342 malloc(sizeof(size_t) +
343 sizeof(void*) * export_var_name_list_raw->count);
344 if (!mpResult->mpExportVars) {
Steve Block10c14122012-01-08 10:15:06 +0000345 ALOGE("Unable to allocate for mpExportVars\n");
Joseph Wen8eabcbf2011-07-07 12:37:48 -0700346 return false;
347 }
348 mpResult->mpExportVars->count = export_var_name_list_raw->count;
349
Joseph Wenf36637f2011-07-06 18:27:12 -0700350 for (size_t i = 0; i < export_var_name_list_raw->count; ++i) {
351 mpResult->mpExportVars->cached_addr_list[i] =
352 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_var_name_list_raw->strp_indexs[i]]);
Stephen Hines36999622012-03-11 19:15:51 -0700353#if DEBUG_MC_REFLECT
Steve Blockb20498e2011-12-20 16:24:20 +0000354 ALOGD("Get symbol address: %s -> %p",
Joseph Wenf36637f2011-07-06 18:27:12 -0700355 strPool[export_var_name_list_raw->strp_indexs[i]], mpResult->mpExportVars->cached_addr_list[i]);
356#endif
357 }
358 return true;
359}
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700360
Joseph Wenf36637f2011-07-06 18:27:12 -0700361bool MCCacheReader::readFuncNameList() {
Stephen Hines0e567862012-03-11 20:26:40 -0700362 CACHE_READER_READ_SECTION(MCO_String_Ptr, mpFuncNameList, export_func_name_list);
Joseph Wenf36637f2011-07-06 18:27:12 -0700363 vector<char const *> const &strPool = mpResult->mStringPool;
364
Stephen Hines0e567862012-03-11 20:26:40 -0700365 mpResult->mpExportFuncs = (MCO_ExportFuncList*)
Joseph Wen8eabcbf2011-07-07 12:37:48 -0700366 malloc(sizeof(size_t) +
367 sizeof(void*) * export_func_name_list_raw->count);
368 if (!mpResult->mpExportFuncs) {
Steve Block10c14122012-01-08 10:15:06 +0000369 ALOGE("Unable to allocate for mpExportFuncs\n");
Joseph Wen8eabcbf2011-07-07 12:37:48 -0700370 return false;
371 }
372 mpResult->mpExportFuncs->count = export_func_name_list_raw->count;
373
Joseph Wenf36637f2011-07-06 18:27:12 -0700374 for (size_t i = 0; i < export_func_name_list_raw->count; ++i) {
375 mpResult->mpExportFuncs->cached_addr_list[i] =
376 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_func_name_list_raw->strp_indexs[i]]);
Stephen Hines36999622012-03-11 19:15:51 -0700377#if DEBUG_MC_REFLECT
Steve Blockb20498e2011-12-20 16:24:20 +0000378 ALOGD("Get function address: %s -> %p",
Joseph Wenf36637f2011-07-06 18:27:12 -0700379 strPool[export_func_name_list_raw->strp_indexs[i]], mpResult->mpExportFuncs->cached_addr_list[i]);
380#endif
381 }
382 return true;
383}
384
Stephen Hinescc366e52012-02-21 17:22:04 -0800385bool MCCacheReader::readForEachNameList() {
Stephen Hines0e567862012-03-11 20:26:40 -0700386 CACHE_READER_READ_SECTION(MCO_String_Ptr, mpForEachNameList, export_foreach_name_list);
Stephen Hinescc366e52012-02-21 17:22:04 -0800387 vector<char const *> const &strPool = mpResult->mStringPool;
388
Stephen Hines0e567862012-03-11 20:26:40 -0700389 mpResult->mpExportForEach = (MCO_ExportForEachList*)
Stephen Hinescc366e52012-02-21 17:22:04 -0800390 malloc(sizeof(size_t) +
391 sizeof(void*) * export_foreach_name_list_raw->count);
392 if (!mpResult->mpExportForEach) {
393 ALOGE("Unable to allocate for mpExportForEach\n");
394 return false;
395 }
396 mpResult->mpExportForEach->count = export_foreach_name_list_raw->count;
397
398 for (size_t i = 0; i < export_foreach_name_list_raw->count; ++i) {
399 mpResult->mpExportForEach->cached_addr_list[i] =
400 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_foreach_name_list_raw->strp_indexs[i]]);
Stephen Hines36999622012-03-11 19:15:51 -0700401#if DEBUG_MC_REFLECT
Stephen Hinescc366e52012-02-21 17:22:04 -0800402 ALOGE("Get foreach function address: %s -> %p",
403 strPool[export_foreach_name_list_raw->strp_indexs[i]], mpResult->mpExportForEach->cached_addr_list[i]);
404#endif
405 }
406 return true;
407}
408
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700409bool MCCacheReader::readPragmaList() {
Stephen Hines0e567862012-03-11 20:26:40 -0700410 CACHE_READER_READ_SECTION(MCO_PragmaList, mpPragmaList, pragma_list);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700411
412 vector<char const *> const &strPool = mpResult->mStringPool;
413 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
414
415 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
Stephen Hines0e567862012-03-11 20:26:40 -0700416 MCO_Pragma *pragma = &pragma_list_raw->list[i];
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700417 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
418 strPool[pragma->value_strp_index]));
419 }
420
421 return true;
422}
423
424
425bool MCCacheReader::readObjectSlotList() {
Stephen Hines0e567862012-03-11 20:26:40 -0700426 CACHE_READER_READ_SECTION(MCO_ObjectSlotList,
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700427 mpResult->mpObjectSlotList, object_slot_list);
428 return true;
429}
430
431void *MCCacheReader::resolveSymbolAdapter(void *context, char const *name) {
432 MCCacheReader *self = reinterpret_cast<MCCacheReader *>(context);
433
434 if (void *Addr = FindRuntimeFunction(name)) {
435 return Addr;
436 }
437
438 if (self->mpSymbolLookupFn) {
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700439 if (void *Addr =
440 self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700441 return Addr;
442 }
443 }
444
Steve Block10c14122012-01-08 10:15:06 +0000445 ALOGE("Unable to resolve symbol: %s\n", name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700446 return NULL;
447}
448
449bool MCCacheReader::readObjFile() {
Daniel Malea094881f2011-12-14 17:39:16 -0500450 if (mpResult->mCachedELFExecutable.size() != 0) {
Stephen Hinese0918ac2012-03-01 23:28:09 -0800451 ALOGE("Attempted to read cached object into a non-empty script");
Daniel Malea094881f2011-12-14 17:39:16 -0500452 return false;
453 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700454 char readBuffer[1024];
455 int readSize;
456 while ((readSize = mObjFile->read(readBuffer, 1024)) > 0) {
Daniel Malea094881f2011-12-14 17:39:16 -0500457 mpResult->mCachedELFExecutable.append(readBuffer, readBuffer + readSize);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700458 }
459 if (readSize != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000460 ALOGE("Read file Error");
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700461 return false;
462 }
Stephen Hinese0918ac2012-03-01 23:28:09 -0800463 ALOGD("Read object file size %d", (int)mpResult->mCachedELFExecutable.size());
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700464 mpResult->mRSExecutable =
Daniel Malea094881f2011-12-14 17:39:16 -0500465 rsloaderCreateExec((unsigned char *)&*(mpResult->mCachedELFExecutable.begin()),
466 mpResult->mCachedELFExecutable.size(),
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700467 &resolveSymbolAdapter, this);
Daniel Malea094881f2011-12-14 17:39:16 -0500468
Daniel Malea76a57ea2012-03-21 12:46:39 -0400469 // Point ELF section headers to location of executable code, otherwise
470 // execution through GDB stops unexpectedly as GDB translates breakpoints
471 // in JITted code incorrectly (and complains about being unable to insert
472 // breakpoint at an invalid address)
473 rsloaderUpdateSectionHeaders(mpResult->mRSExecutable,
474 (unsigned char*) mpResult->mCachedELFExecutable.begin());
475
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700476 return true;
477}
478
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700479#undef CACHE_READER_READ_SECTION
480
481bool MCCacheReader::readRelocationTable() {
482 // TODO(logan): Not finished.
483 return true;
484}
485
486
487bool MCCacheReader::relocate() {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700488 return true;
489}
490
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700491} // namespace bcc