blob: 2eaa8ce97a7ae55a89ccda607c70c71574acc512 [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() {
147 ssize_t funcCount = 0;
148
149 mpOwner->getFunctions(&funcCount, 0, NULL);
150
151 size_t tableSize = sizeof(OBCC_FuncTable) +
152 sizeof(OBCC_FuncInfo) * funcCount;
153
154 OBCC_FuncTable *tab = (OBCC_FuncTable *)malloc(tableSize);
155
156 if (!tab) {
157 LOGE("Unable to allocate for function table section.\n");
158 return false;
159 }
160
161 mpFuncTableSection = tab;
Shih-wei Liaof7cfc022011-01-07 06:39:53 -0800162 mpHeaderSection->func_table_size = tableSize;
Logana27a83f2011-01-07 10:25:48 +0800163
164 tab->count = static_cast<size_t>(funcCount);
165
Shih-wei Liaof7cfc022011-01-07 06:39:53 -0800166 // Get the function informations
Logana27a83f2011-01-07 10:25:48 +0800167 vector<char *> funcNameList(funcCount);
168 mpOwner->getFunctions(0, funcCount, &*funcNameList.begin());
169
170 for (int i = 0; i < funcCount; ++i) {
171 char *funcName = funcNameList[i];
172 size_t funcNameLen = strlen(funcName);
173
174 void *funcAddr = NULL;
175 ssize_t funcBinarySize = 0;
176 mpOwner->getFunctionBinary(funcName, &funcAddr, &funcBinarySize);
177
178 OBCC_FuncInfo *funcInfo = &tab->table[i];
179 funcInfo->name_strp_index = addString(funcName, funcNameLen);
180 funcInfo->cached_addr = funcAddr;
181 funcInfo->size = static_cast<size_t>(funcBinarySize);
182 }
183
184 return true;
185}
186
187
188bool CacheWriter::preparePragmaList() {
189 ssize_t stringCount;
190
191 mpOwner->getPragmas(&stringCount, 0, NULL);
192
193 size_t pragmaCount = static_cast<size_t>(stringCount) / 2;
194
195 size_t listSize = sizeof(OBCC_PragmaList) +
196 sizeof(OBCC_Pragma) * pragmaCount;
197
198 OBCC_PragmaList *list = (OBCC_PragmaList *)malloc(listSize);
199
200 if (!list) {
201 LOGE("Unable to allocate for pragma list\n");
202 return false;
203 }
204
205 mpPragmaListSection = list;
206 mpHeaderSection->pragma_list_size = listSize;
207
208 list->count = pragmaCount;
209
210 vector<char *> strings(stringCount);
211 mpOwner->getPragmas(&stringCount, stringCount, &*strings.begin());
212
213 for (size_t i = 0; i < pragmaCount; ++i) {
214 char *key = strings[2 * i];
215 size_t keyLen = strlen(key);
216
217 char *value = strings[2 * i + 1];
218 size_t valueLen = strlen(value);
219
220 OBCC_Pragma *pragma = &list->list[i];
221 pragma->key_strp_index = addString(key, keyLen);
222 pragma->value_strp_index = addString(value, valueLen);
223 }
224
225 return true;
226}
227
228
229bool CacheWriter::prepareRelocationTable() {
230 // TODO(logan): Implement relocation table cache write.
231 return false;
232}
233
234
235bool CacheWriter::prepareStringPool() {
236 // Calculate string pool size
237 size_t size = sizeof(OBCC_StringPool) +
238 sizeof(OBCC_String) * mStringPool.size();
239
240 off_t strOffset = size;
241
242 for (size_t i = 0; i < mStringPool.size(); ++i) {
243 size += mStringPool[i].second + 1;
244 }
245
246 // Create string pool
247 OBCC_StringPool *pool = (OBCC_StringPool *)malloc(size);
248
249 if (!pool) {
250 LOGE("Unable to allocate string pool.\n");
251 return false;
252 }
253
254 mpStringPoolSection = pool;
255 mpHeaderSection->str_pool_size = size;
256
Logana2e15af2011-01-07 11:46:08 +0800257 pool->count = mStringPool.size();
258
Logana27a83f2011-01-07 10:25:48 +0800259 char *strPtr = reinterpret_cast<char *>(pool) + strOffset;
260
261 for (size_t i = 0; i < mStringPool.size(); ++i) {
262 OBCC_String *str = &pool->list[i];
263
264 str->length = mStringPool[i].second;
265 str->offset = strOffset;
266 memcpy(strPtr, mStringPool[i].first, str->length);
267
268 strPtr += str->length;
269 *strPtr++ = '\0';
270
Logana2e15af2011-01-07 11:46:08 +0800271 strOffset += str->length + 1;
Logana27a83f2011-01-07 10:25:48 +0800272 }
273
274 return true;
275}
276
277
278bool CacheWriter::prepareExportVarList() {
279 ssize_t varCount;
280
281 mpOwner->getExportVars(&varCount, 0, NULL);
282
283 size_t listSize = sizeof(OBCC_ExportVarList) + sizeof(void *) * varCount;
284
285 OBCC_ExportVarList *list = (OBCC_ExportVarList *)malloc(listSize);
286
287 if (!list) {
288 LOGE("Unable to allocate for export variable list\n");
289 return false;
290 }
291
292 mpExportVarListSection = list;
293 mpHeaderSection->export_var_list_size = listSize;
294
295 list->count = static_cast<size_t>(varCount);
296
297 mpOwner->getExportVars(&varCount, varCount, list->cached_addr_list);
298 return true;
299}
300
301
302bool CacheWriter::prepareExportFuncList() {
303 ssize_t funcCount;
304
305 mpOwner->getExportFuncs(&funcCount, 0, NULL);
306
307 size_t listSize = sizeof(OBCC_ExportFuncList) + sizeof(void *) * funcCount;
308
309 OBCC_ExportFuncList *list = (OBCC_ExportFuncList *)malloc(listSize);
310
311 if (!list) {
312 LOGE("Unable to allocate for export function list\n");
313 return false;
314 }
315
316 mpExportFuncListSection = list;
317 mpHeaderSection->export_func_list_size = listSize;
318
319 list->count = static_cast<size_t>(funcCount);
320
321 mpOwner->getExportFuncs(&funcCount, funcCount, list->cached_addr_list);
322 return true;
323}
324
325
326bool CacheWriter::calcSectionOffset() {
327 size_t offset = sizeof(OBCC_Header);
328
329#define OFFSET_INCREASE(NAME) \
330 do { \
331 /* Align to a word */ \
332 size_t rem = offset % sizeof(int); \
333 if (rem > 0) { \
334 offset += sizeof(int) - rem; \
335 } \
336 \
337 /* Save the offset and increase it */ \
338 mpHeaderSection->NAME##_offset = offset; \
339 offset += mpHeaderSection->NAME##_size; \
340 } while (0)
341
342 OFFSET_INCREASE(str_pool);
343 OFFSET_INCREASE(depend_tab);
344 //OFFSET_INCREASE(reloc_tab);
345 OFFSET_INCREASE(export_var_list);
346 OFFSET_INCREASE(export_func_list);
347 OFFSET_INCREASE(pragma_list);
348 OFFSET_INCREASE(func_table);
349
350#undef OFFSET_INCREASE
351
352 // Context
353 long pagesize = sysconf(_SC_PAGESIZE);
354 size_t context_offset_rem = offset % pagesize;
355 if (context_offset_rem) {
356 offset += pagesize - context_offset_rem;
357 }
358
359 mpHeaderSection->context_offset = offset;
360 return true;
361}
362
363
364bool CacheWriter::calcContextChecksum() {
365 uint32_t sum = 0;
366 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpOwner->getContext());
367
368 for (size_t i = 0; i < BCC_CONTEXT_SIZE / sizeof(uint32_t); ++i) {
369 sum ^= *ptr++;
370 }
371
372 mpHeaderSection->context_parity_checksum = sum;
373 return true;
374}
375
376
377bool CacheWriter::writeAll() {
378#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \
379 do { \
380 if (mFile->seek(OFFSET, SEEK_SET) == -1) { \
381 LOGE("Unable to seek to " #NAME " section for writing.\n"); \
382 return false; \
383 } \
384 \
385 if (mFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
386 static_cast<ssize_t>(SIZE)) { \
387 LOGE("Unable to write " #NAME " section to cache file.\n"); \
388 return false; \
389 } \
390 } while (0)
391
392#define WRITE_SECTION_SIMPLE(NAME, SECTION) \
393 WRITE_SECTION(NAME, \
394 mpHeaderSection->NAME##_offset, \
395 mpHeaderSection->NAME##_size, \
396 SECTION)
397
398 WRITE_SECTION(header, 0, sizeof(OBCC_Header), mpHeaderSection);
399
400 WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection);
401 WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection);
402 //WRITE_SECTION_SIMPLE(reloc_tab, mpRelocationTableSection);
403 WRITE_SECTION_SIMPLE(export_var_list, mpExportVarListSection);
404 WRITE_SECTION_SIMPLE(export_func_list, mpExportFuncListSection);
405 WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection);
406 WRITE_SECTION_SIMPLE(func_table, mpFuncTableSection);
407
408 WRITE_SECTION(context, mpHeaderSection->context_offset, BCC_CONTEXT_SIZE,
409 mpOwner->getContext());
410
411#undef WRITE_SECTION_SIMPLE
412#undef WRITE_SECTION
413
414 return true;
415}
416
417
Logana27a83f2011-01-07 10:25:48 +0800418} // namespace bcc