blob: 6d3a10422113391e6c36ddec466981a01b40a210 [file] [log] [blame]
Logana27a83f2011-01-07 10:25:48 +08001/*
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#define LOG_TAG "bcc"
18#include <cutils/log.h>
19
20#include "CacheWriter.h"
21
22#include "ContextManager.h"
23#include "FileHandle.h"
24#include "Script.h"
25
26#include <map>
27#include <string>
28#include <vector>
29#include <utility>
30
31#include <stdint.h>
32#include <stdlib.h>
33#include <string.h>
34
35using namespace std;
36
37namespace bcc {
38
Logan9d938942011-01-07 10:44:43 +080039CacheWriter::~CacheWriter() {
40#define CHECK_AND_FREE(VAR) if (VAR) { free(VAR); }
41
42 CHECK_AND_FREE(mpHeaderSection);
43 CHECK_AND_FREE(mpStringPoolSection);
44 CHECK_AND_FREE(mpDependencyTableSection);
45 //CHECK_AND_FREE(mpRelocationTableSection);
46 CHECK_AND_FREE(mpExportVarListSection);
47 CHECK_AND_FREE(mpExportFuncListSection);
48 CHECK_AND_FREE(mpPragmaListSection);
49 CHECK_AND_FREE(mpFuncTableSection);
50
51#undef CHECK_AND_FREE
52}
53
Logana27a83f2011-01-07 10:25:48 +080054bool CacheWriter::writeCacheFile(FileHandle *file, Script *S,
55 uint32_t libRS_threadable) {
56 if (!file || file->getFD() < 0) {
57 return false;
58 }
59
60 mFile = file;
61 mpOwner = S;
62
63 bool result = prepareHeader(libRS_threadable)
64 && prepareDependencyTable()
65 && prepareFuncTable()
66 && preparePragmaList()
67 //&& prepareRelocationTable()
68 && prepareStringPool()
69 && prepareExportVarList()
70 && prepareExportFuncList()
71 && calcSectionOffset()
72 && calcContextChecksum()
73 && writeAll()
74 ;
75
76 return result;
77}
78
79
80bool CacheWriter::prepareHeader(uint32_t libRS_threadable) {
81 OBCC_Header *header = (OBCC_Header *)malloc(sizeof(OBCC_Header));
82
83 if (!header) {
84 LOGE("Unable to allocate for header.\n");
85 return false;
86 }
87
88 mpHeaderSection = header;
89
90 // Initialize
91 memset(header, '\0', sizeof(OBCC_Header));
92
Shih-wei Liaof7cfc022011-01-07 06:39:53 -080093 // Magic word and version
Logana27a83f2011-01-07 10:25:48 +080094 memcpy(header->magic, OBCC_MAGIC, 4);
95 memcpy(header->version, OBCC_VERSION, 4);
Logane1323992011-01-12 04:47:13 +080096 memcpy(header->libbcc_build_time, libbcc_build_time, 24);
Logana27a83f2011-01-07 10:25:48 +080097
98 // Machine Integer Type
99 uint32_t number = 0x00000001;
100 header->endianness = (*reinterpret_cast<char *>(&number) == 1) ? 'e' : 'E';
101 header->sizeof_off_t = sizeof(off_t);
102 header->sizeof_size_t = sizeof(size_t);
103 header->sizeof_ptr_t = sizeof(void *);
104
105 // Context
106 header->context_cached_addr = mpOwner->getContext();
107
108 // libRS is threadable dirty hack
109 // TODO: This should be removed in the future
110 header->libRS_threadable = libRS_threadable;
111
112 return true;
113}
114
115
116bool CacheWriter::prepareDependencyTable() {
117 size_t tableSize = sizeof(OBCC_DependencyTable) +
118 sizeof(OBCC_Dependency) * mDependencies.size();
119
120 OBCC_DependencyTable *tab = (OBCC_DependencyTable *)malloc(tableSize);
Shih-wei Liaof7cfc022011-01-07 06:39:53 -0800121
Logana27a83f2011-01-07 10:25:48 +0800122 if (!tab) {
123 LOGE("Unable to allocate for dependency table section.\n");
124 return false;
125 }
126
127 mpDependencyTableSection = tab;
128 mpHeaderSection->depend_tab_size = tableSize;
129
130 tab->count = mDependencies.size();
131
132 size_t i = 0;
133 for (map<string, pair<uint32_t, unsigned char const *> >::iterator
134 I = mDependencies.begin(), E = mDependencies.end(); I != E; ++I, ++i) {
135 OBCC_Dependency *dep = &tab->table[i];
136
137 dep->res_name_strp_index = addString(I->first.c_str(), I->first.size());
138 dep->res_type = I->second.first;
139 memcpy(dep->sha1, I->second.second, 20);
140 }
141
142 return true;
143}
144
145
146bool CacheWriter::prepareFuncTable() {
Loganbe79ada2011-01-13 01:33:45 +0800147 size_t funcCount = mpOwner->getFuncCount();
Logana27a83f2011-01-07 10:25:48 +0800148
149 size_t tableSize = sizeof(OBCC_FuncTable) +
150 sizeof(OBCC_FuncInfo) * funcCount;
151
152 OBCC_FuncTable *tab = (OBCC_FuncTable *)malloc(tableSize);
153
154 if (!tab) {
155 LOGE("Unable to allocate for function table section.\n");
156 return false;
157 }
158
159 mpFuncTableSection = tab;
Shih-wei Liaof7cfc022011-01-07 06:39:53 -0800160 mpHeaderSection->func_table_size = tableSize;
Logana27a83f2011-01-07 10:25:48 +0800161
162 tab->count = static_cast<size_t>(funcCount);
163
Shih-wei Liaof7cfc022011-01-07 06:39:53 -0800164 // Get the function informations
Loganf340bf72011-01-14 17:51:40 +0800165 vector<FuncInfo> funcInfoList(funcCount);
166 mpOwner->getFuncInfoList(funcCount, &*funcInfoList.begin());
Logana27a83f2011-01-07 10:25:48 +0800167
Loganbe79ada2011-01-13 01:33:45 +0800168 for (size_t i = 0; i < funcCount; ++i) {
Loganf340bf72011-01-14 17:51:40 +0800169 FuncInfo *info = &funcInfoList[i];
170 OBCC_FuncInfo *outputInfo = &tab->table[i];
Logana27a83f2011-01-07 10:25:48 +0800171
Loganf340bf72011-01-14 17:51:40 +0800172 outputInfo->name_strp_index = addString(info->name, strlen(info->name));
173 outputInfo->cached_addr = info->addr;
174 outputInfo->size = info->size;
Logana27a83f2011-01-07 10:25:48 +0800175 }
176
177 return true;
178}
179
180
181bool CacheWriter::preparePragmaList() {
Loganbe79ada2011-01-13 01:33:45 +0800182 size_t pragmaCount = mpOwner->getPragmaCount();
Logana27a83f2011-01-07 10:25:48 +0800183
184 size_t listSize = sizeof(OBCC_PragmaList) +
185 sizeof(OBCC_Pragma) * pragmaCount;
186
187 OBCC_PragmaList *list = (OBCC_PragmaList *)malloc(listSize);
188
189 if (!list) {
190 LOGE("Unable to allocate for pragma list\n");
191 return false;
192 }
193
194 mpPragmaListSection = list;
195 mpHeaderSection->pragma_list_size = listSize;
196
197 list->count = pragmaCount;
198
Loganbe79ada2011-01-13 01:33:45 +0800199 vector<char const *> keyList(pragmaCount);
200 vector<char const *> valueList(pragmaCount);
201 mpOwner->getPragmaList(pragmaCount, &*keyList.begin(), &*valueList.begin());
Logana27a83f2011-01-07 10:25:48 +0800202
203 for (size_t i = 0; i < pragmaCount; ++i) {
Loganbe79ada2011-01-13 01:33:45 +0800204 char const *key = keyList[i];
205 char const *value = valueList[i];
Logana27a83f2011-01-07 10:25:48 +0800206
Loganbe79ada2011-01-13 01:33:45 +0800207 size_t keyLen = strlen(key);
Logana27a83f2011-01-07 10:25:48 +0800208 size_t valueLen = strlen(value);
209
210 OBCC_Pragma *pragma = &list->list[i];
211 pragma->key_strp_index = addString(key, keyLen);
212 pragma->value_strp_index = addString(value, valueLen);
213 }
214
215 return true;
216}
217
218
219bool CacheWriter::prepareRelocationTable() {
220 // TODO(logan): Implement relocation table cache write.
221 return false;
222}
223
224
225bool CacheWriter::prepareStringPool() {
226 // Calculate string pool size
227 size_t size = sizeof(OBCC_StringPool) +
228 sizeof(OBCC_String) * mStringPool.size();
229
230 off_t strOffset = size;
231
232 for (size_t i = 0; i < mStringPool.size(); ++i) {
233 size += mStringPool[i].second + 1;
234 }
235
236 // Create string pool
237 OBCC_StringPool *pool = (OBCC_StringPool *)malloc(size);
238
239 if (!pool) {
240 LOGE("Unable to allocate string pool.\n");
241 return false;
242 }
243
244 mpStringPoolSection = pool;
245 mpHeaderSection->str_pool_size = size;
246
Logana2e15af2011-01-07 11:46:08 +0800247 pool->count = mStringPool.size();
248
Logana27a83f2011-01-07 10:25:48 +0800249 char *strPtr = reinterpret_cast<char *>(pool) + strOffset;
250
251 for (size_t i = 0; i < mStringPool.size(); ++i) {
252 OBCC_String *str = &pool->list[i];
253
254 str->length = mStringPool[i].second;
255 str->offset = strOffset;
256 memcpy(strPtr, mStringPool[i].first, str->length);
257
258 strPtr += str->length;
259 *strPtr++ = '\0';
260
Logana2e15af2011-01-07 11:46:08 +0800261 strOffset += str->length + 1;
Logana27a83f2011-01-07 10:25:48 +0800262 }
263
264 return true;
265}
266
267
268bool CacheWriter::prepareExportVarList() {
Loganbe79ada2011-01-13 01:33:45 +0800269 size_t varCount = mpOwner->getExportVarCount();
Logana27a83f2011-01-07 10:25:48 +0800270 size_t listSize = sizeof(OBCC_ExportVarList) + sizeof(void *) * varCount;
271
272 OBCC_ExportVarList *list = (OBCC_ExportVarList *)malloc(listSize);
273
274 if (!list) {
275 LOGE("Unable to allocate for export variable list\n");
276 return false;
277 }
278
279 mpExportVarListSection = list;
280 mpHeaderSection->export_var_list_size = listSize;
281
282 list->count = static_cast<size_t>(varCount);
283
Loganbe79ada2011-01-13 01:33:45 +0800284 mpOwner->getExportVarList(varCount, list->cached_addr_list);
Logana27a83f2011-01-07 10:25:48 +0800285 return true;
286}
287
288
289bool CacheWriter::prepareExportFuncList() {
Loganbe79ada2011-01-13 01:33:45 +0800290 size_t funcCount = mpOwner->getExportFuncCount();
Logana27a83f2011-01-07 10:25:48 +0800291 size_t listSize = sizeof(OBCC_ExportFuncList) + sizeof(void *) * funcCount;
292
293 OBCC_ExportFuncList *list = (OBCC_ExportFuncList *)malloc(listSize);
294
295 if (!list) {
296 LOGE("Unable to allocate for export function list\n");
297 return false;
298 }
299
300 mpExportFuncListSection = list;
301 mpHeaderSection->export_func_list_size = listSize;
302
303 list->count = static_cast<size_t>(funcCount);
304
Loganbe79ada2011-01-13 01:33:45 +0800305 mpOwner->getExportFuncList(funcCount, list->cached_addr_list);
Logana27a83f2011-01-07 10:25:48 +0800306 return true;
307}
308
309
310bool CacheWriter::calcSectionOffset() {
311 size_t offset = sizeof(OBCC_Header);
312
313#define OFFSET_INCREASE(NAME) \
314 do { \
315 /* Align to a word */ \
316 size_t rem = offset % sizeof(int); \
317 if (rem > 0) { \
318 offset += sizeof(int) - rem; \
319 } \
320 \
321 /* Save the offset and increase it */ \
322 mpHeaderSection->NAME##_offset = offset; \
323 offset += mpHeaderSection->NAME##_size; \
324 } while (0)
325
326 OFFSET_INCREASE(str_pool);
327 OFFSET_INCREASE(depend_tab);
328 //OFFSET_INCREASE(reloc_tab);
329 OFFSET_INCREASE(export_var_list);
330 OFFSET_INCREASE(export_func_list);
331 OFFSET_INCREASE(pragma_list);
332 OFFSET_INCREASE(func_table);
333
334#undef OFFSET_INCREASE
335
336 // Context
337 long pagesize = sysconf(_SC_PAGESIZE);
338 size_t context_offset_rem = offset % pagesize;
339 if (context_offset_rem) {
340 offset += pagesize - context_offset_rem;
341 }
342
343 mpHeaderSection->context_offset = offset;
344 return true;
345}
346
347
348bool CacheWriter::calcContextChecksum() {
349 uint32_t sum = 0;
350 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpOwner->getContext());
351
352 for (size_t i = 0; i < BCC_CONTEXT_SIZE / sizeof(uint32_t); ++i) {
353 sum ^= *ptr++;
354 }
355
356 mpHeaderSection->context_parity_checksum = sum;
357 return true;
358}
359
360
361bool CacheWriter::writeAll() {
362#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \
363 do { \
364 if (mFile->seek(OFFSET, SEEK_SET) == -1) { \
365 LOGE("Unable to seek to " #NAME " section for writing.\n"); \
366 return false; \
367 } \
368 \
369 if (mFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
370 static_cast<ssize_t>(SIZE)) { \
371 LOGE("Unable to write " #NAME " section to cache file.\n"); \
372 return false; \
373 } \
374 } while (0)
375
376#define WRITE_SECTION_SIMPLE(NAME, SECTION) \
377 WRITE_SECTION(NAME, \
378 mpHeaderSection->NAME##_offset, \
379 mpHeaderSection->NAME##_size, \
380 SECTION)
381
382 WRITE_SECTION(header, 0, sizeof(OBCC_Header), mpHeaderSection);
383
384 WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection);
385 WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection);
386 //WRITE_SECTION_SIMPLE(reloc_tab, mpRelocationTableSection);
387 WRITE_SECTION_SIMPLE(export_var_list, mpExportVarListSection);
388 WRITE_SECTION_SIMPLE(export_func_list, mpExportFuncListSection);
389 WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection);
390 WRITE_SECTION_SIMPLE(func_table, mpFuncTableSection);
391
392 WRITE_SECTION(context, mpHeaderSection->context_offset, BCC_CONTEXT_SIZE,
393 mpOwner->getContext());
394
395#undef WRITE_SECTION_SIMPLE
396#undef WRITE_SECTION
397
398 return true;
399}
400
401
Logana27a83f2011-01-07 10:25:48 +0800402} // namespace bcc