MCCacheWriter.cpp revision 4928104ef3aad39e00d22ac81fd2aad6d41a25bf
1/*
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 "DebugHelper.h"
20#include "FileHandle.h"
21#include "Script.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
34#if USE_MCJIT
35namespace bcc {
36
37MCCacheWriter::~MCCacheWriter() {
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(mpPragmaListSection);
44  CHECK_AND_FREE(mpObjectSlotSection);
45  CHECK_AND_FREE(mpExportVarNameListSection);
46  CHECK_AND_FREE(mpExportFuncNameListSection);
47
48#undef CHECK_AND_FREE
49}
50
51bool MCCacheWriter::writeCacheFile(FileHandle *objFile, FileHandle *infoFile,
52                                 Script *S, uint32_t libRS_threadable) {
53  if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
54    return false;
55  }
56
57  mObjFile = objFile;
58  mInfoFile = infoFile;
59  mpOwner = S;
60
61  bool result = prepareHeader(libRS_threadable)
62             && prepareDependencyTable()
63             && preparePragmaList()
64             && prepareExportVarNameList()
65             && prepareExportFuncNameList()
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    LOGE("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, OBCC_MAGIC, 4);
91  memcpy(header->version, OBCC_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(OBCC_DependencyTable) +
110                     sizeof(OBCC_Dependency) * mDependencies.size();
111
112  OBCC_DependencyTable *tab = (OBCC_DependencyTable *)malloc(tableSize);
113
114  if (!tab) {
115    LOGE("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, pair<uint32_t, unsigned char const *> >::iterator
126       I = mDependencies.begin(), E = mDependencies.end(); I != E; ++I, ++i) {
127    OBCC_Dependency *dep = &tab->table[i];
128
129    dep->res_name_strp_index = addString(I->first.c_str(), I->first.size());
130    dep->res_type = I->second.first;
131    memcpy(dep->sha1, I->second.second, 20);
132  }
133
134  return true;
135}
136
137bool MCCacheWriter::preparePragmaList() {
138  size_t pragmaCount = mpOwner->getPragmaCount();
139
140  size_t listSize = sizeof(OBCC_PragmaList) +
141                    sizeof(OBCC_Pragma) * pragmaCount;
142
143  OBCC_PragmaList *list = (OBCC_PragmaList *)malloc(listSize);
144
145  if (!list) {
146    LOGE("Unable to allocate for pragma list\n");
147    return false;
148  }
149
150  mpPragmaListSection = list;
151  mpHeaderSection->pragma_list_size = listSize;
152
153  list->count = pragmaCount;
154
155  vector<char const *> keyList(pragmaCount);
156  vector<char const *> valueList(pragmaCount);
157  mpOwner->getPragmaList(pragmaCount, &*keyList.begin(), &*valueList.begin());
158
159  for (size_t i = 0; i < pragmaCount; ++i) {
160    char const *key = keyList[i];
161    char const *value = valueList[i];
162
163    size_t keyLen = strlen(key);
164    size_t valueLen = strlen(value);
165
166    OBCC_Pragma *pragma = &list->list[i];
167    pragma->key_strp_index = addString(key, keyLen);
168    pragma->value_strp_index = addString(value, valueLen);
169  }
170
171  return true;
172}
173
174bool MCCacheWriter::prepareStringPool() {
175  // Calculate string pool size
176  size_t size = sizeof(OBCC_StringPool) +
177                sizeof(OBCC_String) * mStringPool.size();
178
179  off_t strOffset = size;
180
181  for (size_t i = 0; i < mStringPool.size(); ++i) {
182    size += mStringPool[i].second + 1;
183  }
184
185  // Create string pool
186  OBCC_StringPool *pool = (OBCC_StringPool *)malloc(size);
187
188  if (!pool) {
189    LOGE("Unable to allocate string pool.\n");
190    return false;
191  }
192
193  mpStringPoolSection = pool;
194  mpHeaderSection->str_pool_size = size;
195
196  pool->count = mStringPool.size();
197
198  char *strPtr = reinterpret_cast<char *>(pool) + strOffset;
199
200  for (size_t i = 0; i < mStringPool.size(); ++i) {
201    OBCC_String *str = &pool->list[i];
202
203    str->length = mStringPool[i].second;
204    str->offset = strOffset;
205    memcpy(strPtr, mStringPool[i].first, str->length);
206
207    strPtr += str->length;
208    *strPtr++ = '\0';
209
210    strOffset += str->length + 1;
211  }
212
213  return true;
214}
215
216
217bool MCCacheWriter::prepareExportVarNameList() {
218  size_t varCount = mpOwner->getExportVarCount();
219  size_t listSize = sizeof(OBCC_String_Ptr) + sizeof(size_t) * varCount;
220
221  OBCC_String_Ptr *list = (OBCC_String_Ptr*)malloc(listSize);
222
223  if (!list) {
224    LOGE("Unable to allocate for export variable name list\n");
225    return false;
226  }
227
228  mpExportVarNameListSection = list;
229  mpHeaderSection->export_var_name_list_size = listSize;
230
231  list->count = static_cast<size_t>(varCount);
232
233  mpOwner->getExportVarNameList(varNameList);
234  for (size_t i = 0; i < varCount; ++i) {
235    list->strp_indexs[i] = addString(varNameList[i].c_str(), varNameList[i].length());
236  }
237  return true;
238}
239
240
241bool MCCacheWriter::prepareExportFuncNameList() {
242  size_t funcCount = mpOwner->getExportFuncCount();
243  size_t listSize = sizeof(OBCC_String_Ptr) + sizeof(size_t) * funcCount;
244
245  OBCC_String_Ptr *list = (OBCC_String_Ptr*)malloc(listSize);
246
247  if (!list) {
248    LOGE("Unable to allocate for export function name list\n");
249    return false;
250  }
251
252  mpExportFuncNameListSection = list;
253  mpHeaderSection->export_func_name_list_size = listSize;
254
255  list->count = static_cast<size_t>(funcCount);
256
257  mpOwner->getExportFuncNameList(funcNameList);
258  for (size_t i = 0; i < funcCount; ++i) {
259    list->strp_indexs[i] = addString(funcNameList[i].c_str(), funcNameList[i].length());
260  }
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(pragma_list);
307  OFFSET_INCREASE(func_table);
308  OFFSET_INCREASE(object_slot_list);
309  OFFSET_INCREASE(export_var_name_list);
310  OFFSET_INCREASE(export_func_name_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 {                                                                      \
321    if (mInfoFile->seek(OFFSET, SEEK_SET) == -1) {                          \
322      LOGE("Unable to seek to " #NAME " section for writing.\n");           \
323      return false;                                                         \
324    }                                                                       \
325                                                                            \
326    if (mInfoFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) !=      \
327        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(pragma_list, mpPragmaListSection);
344  WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection);
345
346  WRITE_SECTION_SIMPLE(export_var_name_list, mpExportVarNameListSection);
347  WRITE_SECTION_SIMPLE(export_func_name_list, mpExportFuncNameListSection);
348
349#undef WRITE_SECTION_SIMPLE
350#undef WRITE_SECTION
351
352  if (static_cast<size_t>(mObjFile->write(mpOwner->getELF(),
353                                          mpOwner->getELFSize()))
354      != mpOwner->getELFSize()) {
355      LOGE("Unable to write ELF to cache file.\n");
356      return false;
357  }
358
359  return true;
360}
361
362} // namespace bcc
363#endif
364