blob: c667c198856ec49b883bbf3456a4e8b0c7cf3282 [file] [log] [blame]
Stephen Hines4a68b1c2012-05-03 12:28:14 -07001/*
2 * Copyright 2010-2012, 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 "MCCacheWriter.h"
18
19#include "DebugHelper.h"
20#include "OutputFile.h"
21#include "RSScript.h"
22
23#include <map>
24#include <string>
25#include <vector>
26#include <utility>
27
28#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
31
32using namespace std;
33
34namespace bcc {
35
36MCCacheWriter::~MCCacheWriter() {
37#define CHECK_AND_FREE(VAR) if (VAR) { free(VAR); }
38
39 CHECK_AND_FREE(mpHeaderSection);
40 CHECK_AND_FREE(mpStringPoolSection);
41 CHECK_AND_FREE(mpDependencyTableSection);
42 CHECK_AND_FREE(mpPragmaListSection);
43 CHECK_AND_FREE(mpObjectSlotSection);
44 CHECK_AND_FREE(mpExportVarNameListSection);
45 CHECK_AND_FREE(mpExportFuncNameListSection);
46
47#undef CHECK_AND_FREE
48}
49
50bool MCCacheWriter::writeCacheFile(OutputFile &objFile, OutputFile &infoFile,
51 RSScript *S, uint32_t libRS_threadable) {
52 if (objFile.hasError() || infoFile.hasError()) {
53 return false;
54 }
55
56 mObjFile = &objFile;
57 mInfoFile = &infoFile;
58 mpOwner = S;
59
60 bool result = prepareHeader(libRS_threadable)
61 && prepareDependencyTable()
62 && preparePragmaList()
63 && prepareExportVarNameList()
64 && prepareExportFuncNameList()
65 && prepareExportForEachNameList()
66 && prepareStringPool()
67 && prepareObjectSlotList()
68 && calcSectionOffset()
69 && writeAll()
70 ;
71
72 return result;
73}
74
75
76bool MCCacheWriter::prepareHeader(uint32_t libRS_threadable) {
77 MCO_Header *header = (MCO_Header *)malloc(sizeof(MCO_Header));
78
79 if (!header) {
80 ALOGE("Unable to allocate for header.\n");
81 return false;
82 }
83
84 mpHeaderSection = header;
85
86 // Initialize
87 memset(header, '\0', sizeof(MCO_Header));
88
89 // Magic word and version
90 memcpy(header->magic, MCO_MAGIC, 4);
91 memcpy(header->version, MCO_VERSION, 4);
92
93 // Machine Integer Type
94 uint32_t number = 0x00000001;
95 header->endianness = (*reinterpret_cast<char *>(&number) == 1) ? 'e' : 'E';
96 header->sizeof_off_t = sizeof(off_t);
97 header->sizeof_size_t = sizeof(size_t);
98 header->sizeof_ptr_t = sizeof(void *);
99
100 // libRS is threadable dirty hack
101 // TODO: This should be removed in the future
102 header->libRS_threadable = libRS_threadable;
103
104 return true;
105}
106
107
108bool MCCacheWriter::prepareDependencyTable() {
109 size_t tableSize = sizeof(MCO_DependencyTable) +
110 sizeof(MCO_Dependency) * mDependencies.size();
111
112 MCO_DependencyTable *tab = (MCO_DependencyTable *)malloc(tableSize);
113
114 if (!tab) {
115 ALOGE("Unable to allocate for dependency table section.\n");
116 return false;
117 }
118
119 mpDependencyTableSection = tab;
120 mpHeaderSection->depend_tab_size = tableSize;
121
122 tab->count = mDependencies.size();
123
124 size_t i = 0;
125 for (map<string, unsigned char const *>::iterator
126 I = mDependencies.begin(), E = mDependencies.end(); I != E; ++I, ++i) {
127 MCO_Dependency *dep = &tab->table[i];
128
129 dep->res_name_strp_index = addString(I->first.c_str(), I->first.size());
130 memcpy(dep->sha1, I->second, 20);
131 }
132
133 return true;
134}
135
136bool MCCacheWriter::preparePragmaList() {
137 size_t pragmaCount = mpOwner->getPragmaCount();
138
139 size_t listSize = sizeof(MCO_PragmaList) +
140 sizeof(MCO_Pragma) * pragmaCount;
141
142 MCO_PragmaList *list = (MCO_PragmaList *)malloc(listSize);
143
144 if (!list) {
145 ALOGE("Unable to allocate for pragma list\n");
146 return false;
147 }
148
149 mpPragmaListSection = list;
150 mpHeaderSection->pragma_list_size = listSize;
151
152 list->count = pragmaCount;
153
154 vector<char const *> keyList(pragmaCount);
155 vector<char const *> valueList(pragmaCount);
156 mpOwner->getPragmaList(pragmaCount, &*keyList.begin(), &*valueList.begin());
157
158 for (size_t i = 0; i < pragmaCount; ++i) {
159 char const *key = keyList[i];
160 char const *value = valueList[i];
161
162 size_t keyLen = strlen(key);
163 size_t valueLen = strlen(value);
164
165 MCO_Pragma *pragma = &list->list[i];
166 pragma->key_strp_index = addString(key, keyLen);
167 pragma->value_strp_index = addString(value, valueLen);
168 }
169
170 return true;
171}
172
173bool MCCacheWriter::prepareStringPool() {
174 // Calculate string pool size
175 size_t size = sizeof(MCO_StringPool) +
176 sizeof(MCO_String) * mStringPool.size();
177
178 off_t strOffset = size;
179
180 for (size_t i = 0; i < mStringPool.size(); ++i) {
181 size += mStringPool[i].second + 1;
182 }
183
184 // Create string pool
185 MCO_StringPool *pool = (MCO_StringPool *)malloc(size);
186
187 if (!pool) {
188 ALOGE("Unable to allocate string pool.\n");
189 return false;
190 }
191
192 mpStringPoolSection = pool;
193 mpHeaderSection->str_pool_size = size;
194
195 pool->count = mStringPool.size();
196
197 char *strPtr = reinterpret_cast<char *>(pool) + strOffset;
198
199 for (size_t i = 0; i < mStringPool.size(); ++i) {
200 MCO_String *str = &pool->list[i];
201
202 str->length = mStringPool[i].second;
203 str->offset = strOffset;
204 memcpy(strPtr, mStringPool[i].first, str->length);
205
206 strPtr += str->length;
207 *strPtr++ = '\0';
208
209 strOffset += str->length + 1;
210 }
211
212 return true;
213}
214
215
216bool MCCacheWriter::prepareExportVarNameList() {
217 size_t varCount = mpOwner->getExportVarCount();
218 size_t listSize = sizeof(MCO_String_Ptr) + sizeof(size_t) * varCount;
219
220 MCO_String_Ptr *list = (MCO_String_Ptr*)malloc(listSize);
221
222 if (!list) {
223 ALOGE("Unable to allocate for export variable name list\n");
224 return false;
225 }
226
227 mpExportVarNameListSection = list;
228 mpHeaderSection->export_var_name_list_size = listSize;
229
230 list->count = static_cast<size_t>(varCount);
231
232 mpOwner->getExportVarNameList(varNameList);
233 for (size_t i = 0; i < varCount; ++i) {
234 list->strp_indexs[i] = addString(varNameList[i].c_str(), varNameList[i].length());
235 }
236 return true;
237}
238
239
240bool MCCacheWriter::prepareExportFuncNameList() {
241 size_t funcCount = mpOwner->getExportFuncCount();
242 size_t listSize = sizeof(MCO_String_Ptr) + sizeof(size_t) * funcCount;
243
244 MCO_String_Ptr *list = (MCO_String_Ptr*)malloc(listSize);
245
246 if (!list) {
247 ALOGE("Unable to allocate for export function name list\n");
248 return false;
249 }
250
251 mpExportFuncNameListSection = list;
252 mpHeaderSection->export_func_name_list_size = listSize;
253
254 list->count = static_cast<size_t>(funcCount);
255
256 mpOwner->getExportFuncNameList(funcNameList);
257 for (size_t i = 0; i < funcCount; ++i) {
258 list->strp_indexs[i] = addString(funcNameList[i].c_str(), funcNameList[i].length());
259 }
260 return true;
261}
262
263
264bool MCCacheWriter::prepareExportForEachNameList() {
265 size_t forEachCount = mpOwner->getExportForEachCount();
266 size_t listSize = sizeof(MCO_String_Ptr) + sizeof(size_t) * forEachCount;
267
268 MCO_String_Ptr *list = (MCO_String_Ptr*)malloc(listSize);
269
270 if (!list) {
271 ALOGE("Unable to allocate for export forEach name list\n");
272 return false;
273 }
274
275 mpExportForEachNameListSection = list;
276 mpHeaderSection->export_foreach_name_list_size = listSize;
277
278 list->count = static_cast<size_t>(forEachCount);
279
280 mpOwner->getExportForEachNameList(forEachNameList);
281 for (size_t i = 0; i < forEachCount; ++i) {
282 list->strp_indexs[i] = addString(forEachNameList[i].c_str(), forEachNameList[i].length());
283 }
284 return true;
285}
286
287
288bool MCCacheWriter::prepareObjectSlotList() {
289 size_t objectSlotCount = mpOwner->getObjectSlotCount();
290
291 size_t listSize = sizeof(MCO_ObjectSlotList) +
292 sizeof(uint32_t) * objectSlotCount;
293
294 MCO_ObjectSlotList *list = (MCO_ObjectSlotList *)malloc(listSize);
295
296 if (!list) {
297 ALOGE("Unable to allocate for object slot list\n");
298 return false;
299 }
300
301 mpObjectSlotSection = list;
302 mpHeaderSection->object_slot_list_size = listSize;
303
304 list->count = objectSlotCount;
305
306 mpOwner->getObjectSlotList(objectSlotCount, list->object_slot_list);
307 return true;
308}
309
310
311bool MCCacheWriter::calcSectionOffset() {
312 size_t offset = sizeof(MCO_Header);
313
314#define OFFSET_INCREASE(NAME) \
315 do { \
316 /* Align to a word */ \
317 size_t rem = offset % sizeof(int); \
318 if (rem > 0) { \
319 offset += sizeof(int) - rem; \
320 } \
321 \
322 /* Save the offset and increase it */ \
323 mpHeaderSection->NAME##_offset = offset; \
324 offset += mpHeaderSection->NAME##_size; \
325 } while (0)
326
327 OFFSET_INCREASE(str_pool);
328 OFFSET_INCREASE(depend_tab);
329 OFFSET_INCREASE(pragma_list);
330 OFFSET_INCREASE(func_table);
331 OFFSET_INCREASE(object_slot_list);
332 OFFSET_INCREASE(export_var_name_list);
333 OFFSET_INCREASE(export_func_name_list);
334 OFFSET_INCREASE(export_foreach_name_list);
335
336#undef OFFSET_INCREASE
337
338 return true;
339}
340
341
342bool MCCacheWriter::writeAll() {
343#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \
344 do { \
345 if (mInfoFile->seek(OFFSET) == -1) { \
346 ALOGE("Unable to seek to " #NAME " section for writing.\n"); \
347 return false; \
348 } \
349 \
350 if (mInfoFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
351 static_cast<ssize_t>(SIZE)) { \
352 ALOGE("Unable to write " #NAME " section to cache file.\n"); \
353 return false; \
354 } \
355 } while (0)
356
357#define WRITE_SECTION_SIMPLE(NAME, SECTION) \
358 WRITE_SECTION(NAME, \
359 mpHeaderSection->NAME##_offset, \
360 mpHeaderSection->NAME##_size, \
361 SECTION)
362
363 WRITE_SECTION(header, 0, sizeof(MCO_Header), mpHeaderSection);
364
365 WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection);
366 WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection);
367 WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection);
368 WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection);
369
370 WRITE_SECTION_SIMPLE(export_var_name_list, mpExportVarNameListSection);
371 WRITE_SECTION_SIMPLE(export_func_name_list, mpExportFuncNameListSection);
372 WRITE_SECTION_SIMPLE(export_foreach_name_list, mpExportForEachNameListSection);
373
374#undef WRITE_SECTION_SIMPLE
375#undef WRITE_SECTION
376
377 if (static_cast<size_t>(mObjFile->write(mpOwner->getELF(),
378 mpOwner->getELFSize()))
379 != mpOwner->getELFSize()) {
380 ALOGE("Unable to write ELF to cache file.\n");
381 return false;
382 }
383
384 return true;
385}
386
387} // namespace bcc