blob: 67def9b45e8ee7ebe0c520571e34e0a36975c047 [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
Logana27a83f2011-01-07 10:25:48 +080017#include "CacheWriter.h"
18
19#include "ContextManager.h"
Logan4dcd6792011-02-28 05:12:00 +080020#include "DebugHelper.h"
Logana27a83f2011-01-07 10:25:48 +080021#include "FileHandle.h"
22#include "Script.h"
23
24#include <map>
25#include <string>
26#include <vector>
27#include <utility>
28
29#include <stdint.h>
30#include <stdlib.h>
31#include <string.h>
32
33using namespace std;
34
35namespace bcc {
36
Logan9d938942011-01-07 10:44:43 +080037CacheWriter::~CacheWriter() {
38#define CHECK_AND_FREE(VAR) if (VAR) { free(VAR); }
39
40 CHECK_AND_FREE(mpHeaderSection);
41 CHECK_AND_FREE(mpStringPoolSection);
42 CHECK_AND_FREE(mpDependencyTableSection);
43 //CHECK_AND_FREE(mpRelocationTableSection);
44 CHECK_AND_FREE(mpExportVarListSection);
45 CHECK_AND_FREE(mpExportFuncListSection);
46 CHECK_AND_FREE(mpPragmaListSection);
47 CHECK_AND_FREE(mpFuncTableSection);
Stephen Hines071288a2011-01-27 14:38:26 -080048 CHECK_AND_FREE(mpObjectSlotSection);
Logan9d938942011-01-07 10:44:43 +080049
50#undef CHECK_AND_FREE
51}
52
Logana27a83f2011-01-07 10:25:48 +080053bool CacheWriter::writeCacheFile(FileHandle *file, Script *S,
54 uint32_t libRS_threadable) {
55 if (!file || file->getFD() < 0) {
56 return false;
57 }
58
59 mFile = file;
60 mpOwner = S;
61
62 bool result = prepareHeader(libRS_threadable)
63 && prepareDependencyTable()
64 && prepareFuncTable()
65 && preparePragmaList()
66 //&& prepareRelocationTable()
67 && prepareStringPool()
68 && prepareExportVarList()
69 && prepareExportFuncList()
Stephen Hines071288a2011-01-27 14:38:26 -080070 && prepareObjectSlotList()
Logana27a83f2011-01-07 10:25:48 +080071 && 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
Stephen Hines071288a2011-01-27 14:38:26 -0800310bool CacheWriter::prepareObjectSlotList() {
311 size_t objectSlotCount = mpOwner->getObjectSlotCount();
312
313 size_t listSize = sizeof(OBCC_ObjectSlotList) +
314 sizeof(uint32_t) * objectSlotCount;
315
316 OBCC_ObjectSlotList *list = (OBCC_ObjectSlotList *)malloc(listSize);
317
318 if (!list) {
319 LOGE("Unable to allocate for object slot list\n");
320 return false;
321 }
322
323 mpObjectSlotSection = list;
324 mpHeaderSection->object_slot_list_size = listSize;
325
326 list->count = objectSlotCount;
327
328 mpOwner->getObjectSlotList(objectSlotCount, list->object_slot_list);
329 return true;
330}
331
332
Logana27a83f2011-01-07 10:25:48 +0800333bool CacheWriter::calcSectionOffset() {
334 size_t offset = sizeof(OBCC_Header);
335
336#define OFFSET_INCREASE(NAME) \
337 do { \
338 /* Align to a word */ \
339 size_t rem = offset % sizeof(int); \
340 if (rem > 0) { \
341 offset += sizeof(int) - rem; \
342 } \
343 \
344 /* Save the offset and increase it */ \
345 mpHeaderSection->NAME##_offset = offset; \
346 offset += mpHeaderSection->NAME##_size; \
347 } while (0)
348
349 OFFSET_INCREASE(str_pool);
350 OFFSET_INCREASE(depend_tab);
351 //OFFSET_INCREASE(reloc_tab);
352 OFFSET_INCREASE(export_var_list);
353 OFFSET_INCREASE(export_func_list);
354 OFFSET_INCREASE(pragma_list);
355 OFFSET_INCREASE(func_table);
Stephen Hines071288a2011-01-27 14:38:26 -0800356 OFFSET_INCREASE(object_slot_list);
Logana27a83f2011-01-07 10:25:48 +0800357
358#undef OFFSET_INCREASE
359
360 // Context
361 long pagesize = sysconf(_SC_PAGESIZE);
362 size_t context_offset_rem = offset % pagesize;
363 if (context_offset_rem) {
364 offset += pagesize - context_offset_rem;
365 }
366
367 mpHeaderSection->context_offset = offset;
368 return true;
369}
370
371
372bool CacheWriter::calcContextChecksum() {
373 uint32_t sum = 0;
374 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpOwner->getContext());
375
Logan1dc63142011-02-25 17:14:51 +0800376 for (size_t i = 0; i < ContextManager::ContextSize / sizeof(uint32_t); ++i) {
Logana27a83f2011-01-07 10:25:48 +0800377 sum ^= *ptr++;
378 }
379
380 mpHeaderSection->context_parity_checksum = sum;
381 return true;
382}
383
384
385bool CacheWriter::writeAll() {
386#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \
387 do { \
388 if (mFile->seek(OFFSET, SEEK_SET) == -1) { \
389 LOGE("Unable to seek to " #NAME " section for writing.\n"); \
390 return false; \
391 } \
392 \
393 if (mFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
394 static_cast<ssize_t>(SIZE)) { \
395 LOGE("Unable to write " #NAME " section to cache file.\n"); \
396 return false; \
397 } \
398 } while (0)
399
400#define WRITE_SECTION_SIMPLE(NAME, SECTION) \
401 WRITE_SECTION(NAME, \
402 mpHeaderSection->NAME##_offset, \
403 mpHeaderSection->NAME##_size, \
404 SECTION)
405
406 WRITE_SECTION(header, 0, sizeof(OBCC_Header), mpHeaderSection);
407
408 WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection);
409 WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection);
410 //WRITE_SECTION_SIMPLE(reloc_tab, mpRelocationTableSection);
411 WRITE_SECTION_SIMPLE(export_var_list, mpExportVarListSection);
412 WRITE_SECTION_SIMPLE(export_func_list, mpExportFuncListSection);
413 WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection);
414 WRITE_SECTION_SIMPLE(func_table, mpFuncTableSection);
Stephen Hines071288a2011-01-27 14:38:26 -0800415 WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection);
Logana27a83f2011-01-07 10:25:48 +0800416
Logan1dc63142011-02-25 17:14:51 +0800417 WRITE_SECTION(context, mpHeaderSection->context_offset,
418 ContextManager::ContextSize,
Logana27a83f2011-01-07 10:25:48 +0800419 mpOwner->getContext());
420
421#undef WRITE_SECTION_SIMPLE
422#undef WRITE_SECTION
423
424 return true;
425}
426
427
Logana27a83f2011-01-07 10:25:48 +0800428} // namespace bcc