blob: c2d0d049989755e26f468ac5b29f534f8c1e7187 [file] [log] [blame]
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -07001/*
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#include "MCCacheWriter.h"
18
19#include "ContextManager.h"
20#include "DebugHelper.h"
21#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
35#if USE_MCJIT
36namespace bcc {
37
38MCCacheWriter::~MCCacheWriter() {
39#define CHECK_AND_FREE(VAR) if (VAR) { free(VAR); }
40
41 CHECK_AND_FREE(mpHeaderSection);
42 CHECK_AND_FREE(mpStringPoolSection);
43 CHECK_AND_FREE(mpDependencyTableSection);
44 CHECK_AND_FREE(mpExportVarListSection);
45 CHECK_AND_FREE(mpExportFuncListSection);
46 CHECK_AND_FREE(mpPragmaListSection);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070047 CHECK_AND_FREE(mpObjectSlotSection);
48
49#undef CHECK_AND_FREE
50}
51
52bool MCCacheWriter::writeCacheFile(FileHandle *objFile, FileHandle *infoFile,
53 Script *S, uint32_t libRS_threadable) {
54 if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
55 return false;
56 }
57
58 mObjFile = objFile;
59 mInfoFile = infoFile;
60 mpOwner = S;
61
62 bool result = prepareHeader(libRS_threadable)
63 && prepareDependencyTable()
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070064 && preparePragmaList()
65 && prepareStringPool()
66 && prepareExportVarList()
67 && prepareExportFuncList()
68 && prepareObjectSlotList()
69 && calcSectionOffset()
70 && writeAll()
71 ;
72
73 return result;
74}
75
76
77bool MCCacheWriter::prepareHeader(uint32_t libRS_threadable) {
78 MCO_Header *header = (MCO_Header *)malloc(sizeof(MCO_Header));
79
80 if (!header) {
81 LOGE("Unable to allocate for header.\n");
82 return false;
83 }
84
85 mpHeaderSection = header;
86
87 // Initialize
88 memset(header, '\0', sizeof(MCO_Header));
89
90 // Magic word and version
91 memcpy(header->magic, OBCC_MAGIC, 4);
92 memcpy(header->version, OBCC_VERSION, 4);
Joseph Wen5de1adf2011-06-21 15:41:31 -070093 memcpy(header->libbcc_build_checksum, libbcc_build_checksum, 41);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070094
95 // Machine Integer Type
96 uint32_t number = 0x00000001;
97 header->endianness = (*reinterpret_cast<char *>(&number) == 1) ? 'e' : 'E';
98 header->sizeof_off_t = sizeof(off_t);
99 header->sizeof_size_t = sizeof(size_t);
100 header->sizeof_ptr_t = sizeof(void *);
101
102 header->root_base_addr = mpOwner->lookup("root");
103
104 LOGD("Lookup root() address [%p]", header->root_base_addr);
105
106 // libRS is threadable dirty hack
107 // TODO: This should be removed in the future
108 header->libRS_threadable = libRS_threadable;
109
110 return true;
111}
112
113
114bool MCCacheWriter::prepareDependencyTable() {
115 size_t tableSize = sizeof(OBCC_DependencyTable) +
116 sizeof(OBCC_Dependency) * mDependencies.size();
117
118 OBCC_DependencyTable *tab = (OBCC_DependencyTable *)malloc(tableSize);
119
120 if (!tab) {
121 LOGE("Unable to allocate for dependency table section.\n");
122 return false;
123 }
124
125 mpDependencyTableSection = tab;
126 mpHeaderSection->depend_tab_size = tableSize;
127
128 tab->count = mDependencies.size();
129
130 size_t i = 0;
131 for (map<string, pair<uint32_t, unsigned char const *> >::iterator
132 I = mDependencies.begin(), E = mDependencies.end(); I != E; ++I, ++i) {
133 OBCC_Dependency *dep = &tab->table[i];
134
135 dep->res_name_strp_index = addString(I->first.c_str(), I->first.size());
136 dep->res_type = I->second.first;
137 memcpy(dep->sha1, I->second.second, 20);
138 }
139
140 return true;
141}
142
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700143bool MCCacheWriter::preparePragmaList() {
144 size_t pragmaCount = mpOwner->getPragmaCount();
145
146 size_t listSize = sizeof(OBCC_PragmaList) +
147 sizeof(OBCC_Pragma) * pragmaCount;
148
149 OBCC_PragmaList *list = (OBCC_PragmaList *)malloc(listSize);
150
151 if (!list) {
152 LOGE("Unable to allocate for pragma list\n");
153 return false;
154 }
155
156 mpPragmaListSection = list;
157 mpHeaderSection->pragma_list_size = listSize;
158
159 list->count = pragmaCount;
160
161 vector<char const *> keyList(pragmaCount);
162 vector<char const *> valueList(pragmaCount);
163 mpOwner->getPragmaList(pragmaCount, &*keyList.begin(), &*valueList.begin());
164
165 for (size_t i = 0; i < pragmaCount; ++i) {
166 char const *key = keyList[i];
167 char const *value = valueList[i];
168
169 size_t keyLen = strlen(key);
170 size_t valueLen = strlen(value);
171
172 OBCC_Pragma *pragma = &list->list[i];
173 pragma->key_strp_index = addString(key, keyLen);
174 pragma->value_strp_index = addString(value, valueLen);
175 }
176
177 return true;
178}
179
180bool MCCacheWriter::prepareStringPool() {
181 // Calculate string pool size
182 size_t size = sizeof(OBCC_StringPool) +
183 sizeof(OBCC_String) * mStringPool.size();
184
185 off_t strOffset = size;
186
187 for (size_t i = 0; i < mStringPool.size(); ++i) {
188 size += mStringPool[i].second + 1;
189 }
190
191 // Create string pool
192 OBCC_StringPool *pool = (OBCC_StringPool *)malloc(size);
193
194 if (!pool) {
195 LOGE("Unable to allocate string pool.\n");
196 return false;
197 }
198
199 mpStringPoolSection = pool;
200 mpHeaderSection->str_pool_size = size;
201
202 pool->count = mStringPool.size();
203
204 char *strPtr = reinterpret_cast<char *>(pool) + strOffset;
205
206 for (size_t i = 0; i < mStringPool.size(); ++i) {
207 OBCC_String *str = &pool->list[i];
208
209 str->length = mStringPool[i].second;
210 str->offset = strOffset;
211 memcpy(strPtr, mStringPool[i].first, str->length);
212
213 strPtr += str->length;
214 *strPtr++ = '\0';
215
216 strOffset += str->length + 1;
217 }
218
219 return true;
220}
221
222
223bool MCCacheWriter::prepareExportVarList() {
224 size_t varCount = mpOwner->getExportVarCount();
225 size_t listSize = sizeof(OBCC_ExportVarList) + sizeof(void *) * varCount;
226
227 OBCC_ExportVarList *list = (OBCC_ExportVarList *)malloc(listSize);
228
229 if (!list) {
230 LOGE("Unable to allocate for export variable list\n");
231 return false;
232 }
233
234 mpExportVarListSection = list;
235 mpHeaderSection->export_var_list_size = listSize;
236
237 list->count = static_cast<size_t>(varCount);
238
239 mpOwner->getExportVarList(varCount, list->cached_addr_list);
240 return true;
241}
242
243
244bool MCCacheWriter::prepareExportFuncList() {
245 size_t funcCount = mpOwner->getExportFuncCount();
246 size_t listSize = sizeof(OBCC_ExportFuncList) + sizeof(void *) * funcCount;
247
248 OBCC_ExportFuncList *list = (OBCC_ExportFuncList *)malloc(listSize);
249
250 if (!list) {
251 LOGE("Unable to allocate for export function list\n");
252 return false;
253 }
254
255 mpExportFuncListSection = list;
256 mpHeaderSection->export_func_list_size = listSize;
257
258 list->count = static_cast<size_t>(funcCount);
259
260 mpOwner->getExportFuncList(funcCount, list->cached_addr_list);
261 return true;
262}
263
264
265bool MCCacheWriter::prepareObjectSlotList() {
266 size_t objectSlotCount = mpOwner->getObjectSlotCount();
267
268 size_t listSize = sizeof(OBCC_ObjectSlotList) +
269 sizeof(uint32_t) * objectSlotCount;
270
271 OBCC_ObjectSlotList *list = (OBCC_ObjectSlotList *)malloc(listSize);
272
273 if (!list) {
274 LOGE("Unable to allocate for object slot list\n");
275 return false;
276 }
277
278 mpObjectSlotSection = list;
279 mpHeaderSection->object_slot_list_size = listSize;
280
281 list->count = objectSlotCount;
282
283 mpOwner->getObjectSlotList(objectSlotCount, list->object_slot_list);
284 return true;
285}
286
287
288bool MCCacheWriter::calcSectionOffset() {
289 size_t offset = sizeof(MCO_Header);
290
291#define OFFSET_INCREASE(NAME) \
292 do { \
293 /* Align to a word */ \
294 size_t rem = offset % sizeof(int); \
295 if (rem > 0) { \
296 offset += sizeof(int) - rem; \
297 } \
298 \
299 /* Save the offset and increase it */ \
300 mpHeaderSection->NAME##_offset = offset; \
301 offset += mpHeaderSection->NAME##_size; \
302 } while (0)
303
304 OFFSET_INCREASE(str_pool);
305 OFFSET_INCREASE(depend_tab);
306 OFFSET_INCREASE(export_var_list);
307 OFFSET_INCREASE(export_func_list);
308 OFFSET_INCREASE(pragma_list);
309 OFFSET_INCREASE(func_table);
310 OFFSET_INCREASE(object_slot_list);
311
312#undef OFFSET_INCREASE
313
314 return true;
315}
316
317
318bool MCCacheWriter::writeAll() {
319#define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \
320 do { \
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700321 if (mInfoFile->seek(OFFSET, SEEK_SET) == -1) { \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700322 LOGE("Unable to seek to " #NAME " section for writing.\n"); \
323 return false; \
324 } \
325 \
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700326 if (mInfoFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700327 static_cast<ssize_t>(SIZE)) { \
328 LOGE("Unable to write " #NAME " section to cache file.\n"); \
329 return false; \
330 } \
331 } while (0)
332
333#define WRITE_SECTION_SIMPLE(NAME, SECTION) \
334 WRITE_SECTION(NAME, \
335 mpHeaderSection->NAME##_offset, \
336 mpHeaderSection->NAME##_size, \
337 SECTION)
338
339 WRITE_SECTION(header, 0, sizeof(MCO_Header), mpHeaderSection);
340
341 WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection);
342 WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection);
343 WRITE_SECTION_SIMPLE(export_var_list, mpExportVarListSection);
344 WRITE_SECTION_SIMPLE(export_func_list, mpExportFuncListSection);
345 WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700346 WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection);
347
348#undef WRITE_SECTION_SIMPLE
349#undef WRITE_SECTION
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700350
351 if (static_cast<size_t>(mObjFile->write(mpOwner->getELF(),
352 mpOwner->getELFSize()))
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700353 != mpOwner->getELFSize()) {
354 LOGE("Unable to write ELF to cache file.\n");
355 return false;
356 }
357
358 return true;
359}
360
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700361} // namespace bcc
362#endif