Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 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 | #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 | |
| 35 | using namespace std; |
| 36 | |
| 37 | namespace bcc { |
| 38 | |
Logan | 9d93894 | 2011-01-07 10:44:43 +0800 | [diff] [blame] | 39 | CacheWriter::~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); |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 50 | CHECK_AND_FREE(mpObjectSlotSection); |
Logan | 9d93894 | 2011-01-07 10:44:43 +0800 | [diff] [blame] | 51 | |
| 52 | #undef CHECK_AND_FREE |
| 53 | } |
| 54 | |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 55 | bool CacheWriter::writeCacheFile(FileHandle *file, Script *S, |
| 56 | uint32_t libRS_threadable) { |
| 57 | if (!file || file->getFD() < 0) { |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | mFile = file; |
| 62 | mpOwner = S; |
| 63 | |
| 64 | bool result = prepareHeader(libRS_threadable) |
| 65 | && prepareDependencyTable() |
| 66 | && prepareFuncTable() |
| 67 | && preparePragmaList() |
| 68 | //&& prepareRelocationTable() |
| 69 | && prepareStringPool() |
| 70 | && prepareExportVarList() |
| 71 | && prepareExportFuncList() |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 72 | && prepareObjectSlotList() |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 73 | && calcSectionOffset() |
| 74 | && calcContextChecksum() |
| 75 | && writeAll() |
| 76 | ; |
| 77 | |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | |
| 82 | bool CacheWriter::prepareHeader(uint32_t libRS_threadable) { |
| 83 | OBCC_Header *header = (OBCC_Header *)malloc(sizeof(OBCC_Header)); |
| 84 | |
| 85 | if (!header) { |
| 86 | LOGE("Unable to allocate for header.\n"); |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | mpHeaderSection = header; |
| 91 | |
| 92 | // Initialize |
| 93 | memset(header, '\0', sizeof(OBCC_Header)); |
| 94 | |
Shih-wei Liao | f7cfc02 | 2011-01-07 06:39:53 -0800 | [diff] [blame] | 95 | // Magic word and version |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 96 | memcpy(header->magic, OBCC_MAGIC, 4); |
| 97 | memcpy(header->version, OBCC_VERSION, 4); |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 98 | memcpy(header->libbcc_build_time, libbcc_build_time, 24); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 99 | |
| 100 | // Machine Integer Type |
| 101 | uint32_t number = 0x00000001; |
| 102 | header->endianness = (*reinterpret_cast<char *>(&number) == 1) ? 'e' : 'E'; |
| 103 | header->sizeof_off_t = sizeof(off_t); |
| 104 | header->sizeof_size_t = sizeof(size_t); |
| 105 | header->sizeof_ptr_t = sizeof(void *); |
| 106 | |
| 107 | // Context |
| 108 | header->context_cached_addr = mpOwner->getContext(); |
| 109 | |
| 110 | // libRS is threadable dirty hack |
| 111 | // TODO: This should be removed in the future |
| 112 | header->libRS_threadable = libRS_threadable; |
| 113 | |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | bool CacheWriter::prepareDependencyTable() { |
| 119 | size_t tableSize = sizeof(OBCC_DependencyTable) + |
| 120 | sizeof(OBCC_Dependency) * mDependencies.size(); |
| 121 | |
| 122 | OBCC_DependencyTable *tab = (OBCC_DependencyTable *)malloc(tableSize); |
Shih-wei Liao | f7cfc02 | 2011-01-07 06:39:53 -0800 | [diff] [blame] | 123 | |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 124 | if (!tab) { |
| 125 | LOGE("Unable to allocate for dependency table section.\n"); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | mpDependencyTableSection = tab; |
| 130 | mpHeaderSection->depend_tab_size = tableSize; |
| 131 | |
| 132 | tab->count = mDependencies.size(); |
| 133 | |
| 134 | size_t i = 0; |
| 135 | for (map<string, pair<uint32_t, unsigned char const *> >::iterator |
| 136 | I = mDependencies.begin(), E = mDependencies.end(); I != E; ++I, ++i) { |
| 137 | OBCC_Dependency *dep = &tab->table[i]; |
| 138 | |
| 139 | dep->res_name_strp_index = addString(I->first.c_str(), I->first.size()); |
| 140 | dep->res_type = I->second.first; |
| 141 | memcpy(dep->sha1, I->second.second, 20); |
| 142 | } |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | bool CacheWriter::prepareFuncTable() { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 149 | size_t funcCount = mpOwner->getFuncCount(); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 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 Liao | f7cfc02 | 2011-01-07 06:39:53 -0800 | [diff] [blame] | 162 | mpHeaderSection->func_table_size = tableSize; |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 163 | |
| 164 | tab->count = static_cast<size_t>(funcCount); |
| 165 | |
Shih-wei Liao | f7cfc02 | 2011-01-07 06:39:53 -0800 | [diff] [blame] | 166 | // Get the function informations |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 167 | vector<FuncInfo> funcInfoList(funcCount); |
| 168 | mpOwner->getFuncInfoList(funcCount, &*funcInfoList.begin()); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 169 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 170 | for (size_t i = 0; i < funcCount; ++i) { |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 171 | FuncInfo *info = &funcInfoList[i]; |
| 172 | OBCC_FuncInfo *outputInfo = &tab->table[i]; |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 173 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 174 | outputInfo->name_strp_index = addString(info->name, strlen(info->name)); |
| 175 | outputInfo->cached_addr = info->addr; |
| 176 | outputInfo->size = info->size; |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | bool CacheWriter::preparePragmaList() { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 184 | size_t pragmaCount = mpOwner->getPragmaCount(); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 185 | |
| 186 | size_t listSize = sizeof(OBCC_PragmaList) + |
| 187 | sizeof(OBCC_Pragma) * pragmaCount; |
| 188 | |
| 189 | OBCC_PragmaList *list = (OBCC_PragmaList *)malloc(listSize); |
| 190 | |
| 191 | if (!list) { |
| 192 | LOGE("Unable to allocate for pragma list\n"); |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | mpPragmaListSection = list; |
| 197 | mpHeaderSection->pragma_list_size = listSize; |
| 198 | |
| 199 | list->count = pragmaCount; |
| 200 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 201 | vector<char const *> keyList(pragmaCount); |
| 202 | vector<char const *> valueList(pragmaCount); |
| 203 | mpOwner->getPragmaList(pragmaCount, &*keyList.begin(), &*valueList.begin()); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 204 | |
| 205 | for (size_t i = 0; i < pragmaCount; ++i) { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 206 | char const *key = keyList[i]; |
| 207 | char const *value = valueList[i]; |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 208 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 209 | size_t keyLen = strlen(key); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 210 | size_t valueLen = strlen(value); |
| 211 | |
| 212 | OBCC_Pragma *pragma = &list->list[i]; |
| 213 | pragma->key_strp_index = addString(key, keyLen); |
| 214 | pragma->value_strp_index = addString(value, valueLen); |
| 215 | } |
| 216 | |
| 217 | return true; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | bool CacheWriter::prepareRelocationTable() { |
| 222 | // TODO(logan): Implement relocation table cache write. |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | bool CacheWriter::prepareStringPool() { |
| 228 | // Calculate string pool size |
| 229 | size_t size = sizeof(OBCC_StringPool) + |
| 230 | sizeof(OBCC_String) * mStringPool.size(); |
| 231 | |
| 232 | off_t strOffset = size; |
| 233 | |
| 234 | for (size_t i = 0; i < mStringPool.size(); ++i) { |
| 235 | size += mStringPool[i].second + 1; |
| 236 | } |
| 237 | |
| 238 | // Create string pool |
| 239 | OBCC_StringPool *pool = (OBCC_StringPool *)malloc(size); |
| 240 | |
| 241 | if (!pool) { |
| 242 | LOGE("Unable to allocate string pool.\n"); |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | mpStringPoolSection = pool; |
| 247 | mpHeaderSection->str_pool_size = size; |
| 248 | |
Logan | a2e15af | 2011-01-07 11:46:08 +0800 | [diff] [blame] | 249 | pool->count = mStringPool.size(); |
| 250 | |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 251 | char *strPtr = reinterpret_cast<char *>(pool) + strOffset; |
| 252 | |
| 253 | for (size_t i = 0; i < mStringPool.size(); ++i) { |
| 254 | OBCC_String *str = &pool->list[i]; |
| 255 | |
| 256 | str->length = mStringPool[i].second; |
| 257 | str->offset = strOffset; |
| 258 | memcpy(strPtr, mStringPool[i].first, str->length); |
| 259 | |
| 260 | strPtr += str->length; |
| 261 | *strPtr++ = '\0'; |
| 262 | |
Logan | a2e15af | 2011-01-07 11:46:08 +0800 | [diff] [blame] | 263 | strOffset += str->length + 1; |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | return true; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | bool CacheWriter::prepareExportVarList() { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 271 | size_t varCount = mpOwner->getExportVarCount(); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 272 | size_t listSize = sizeof(OBCC_ExportVarList) + sizeof(void *) * varCount; |
| 273 | |
| 274 | OBCC_ExportVarList *list = (OBCC_ExportVarList *)malloc(listSize); |
| 275 | |
| 276 | if (!list) { |
| 277 | LOGE("Unable to allocate for export variable list\n"); |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | mpExportVarListSection = list; |
| 282 | mpHeaderSection->export_var_list_size = listSize; |
| 283 | |
| 284 | list->count = static_cast<size_t>(varCount); |
| 285 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 286 | mpOwner->getExportVarList(varCount, list->cached_addr_list); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 287 | return true; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | bool CacheWriter::prepareExportFuncList() { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 292 | size_t funcCount = mpOwner->getExportFuncCount(); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 293 | size_t listSize = sizeof(OBCC_ExportFuncList) + sizeof(void *) * funcCount; |
| 294 | |
| 295 | OBCC_ExportFuncList *list = (OBCC_ExportFuncList *)malloc(listSize); |
| 296 | |
| 297 | if (!list) { |
| 298 | LOGE("Unable to allocate for export function list\n"); |
| 299 | return false; |
| 300 | } |
| 301 | |
| 302 | mpExportFuncListSection = list; |
| 303 | mpHeaderSection->export_func_list_size = listSize; |
| 304 | |
| 305 | list->count = static_cast<size_t>(funcCount); |
| 306 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 307 | mpOwner->getExportFuncList(funcCount, list->cached_addr_list); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 308 | return true; |
| 309 | } |
| 310 | |
| 311 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 312 | bool CacheWriter::prepareObjectSlotList() { |
| 313 | size_t objectSlotCount = mpOwner->getObjectSlotCount(); |
| 314 | |
| 315 | size_t listSize = sizeof(OBCC_ObjectSlotList) + |
| 316 | sizeof(uint32_t) * objectSlotCount; |
| 317 | |
| 318 | OBCC_ObjectSlotList *list = (OBCC_ObjectSlotList *)malloc(listSize); |
| 319 | |
| 320 | if (!list) { |
| 321 | LOGE("Unable to allocate for object slot list\n"); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | mpObjectSlotSection = list; |
| 326 | mpHeaderSection->object_slot_list_size = listSize; |
| 327 | |
| 328 | list->count = objectSlotCount; |
| 329 | |
| 330 | mpOwner->getObjectSlotList(objectSlotCount, list->object_slot_list); |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 335 | bool CacheWriter::calcSectionOffset() { |
| 336 | size_t offset = sizeof(OBCC_Header); |
| 337 | |
| 338 | #define OFFSET_INCREASE(NAME) \ |
| 339 | do { \ |
| 340 | /* Align to a word */ \ |
| 341 | size_t rem = offset % sizeof(int); \ |
| 342 | if (rem > 0) { \ |
| 343 | offset += sizeof(int) - rem; \ |
| 344 | } \ |
| 345 | \ |
| 346 | /* Save the offset and increase it */ \ |
| 347 | mpHeaderSection->NAME##_offset = offset; \ |
| 348 | offset += mpHeaderSection->NAME##_size; \ |
| 349 | } while (0) |
| 350 | |
| 351 | OFFSET_INCREASE(str_pool); |
| 352 | OFFSET_INCREASE(depend_tab); |
| 353 | //OFFSET_INCREASE(reloc_tab); |
| 354 | OFFSET_INCREASE(export_var_list); |
| 355 | OFFSET_INCREASE(export_func_list); |
| 356 | OFFSET_INCREASE(pragma_list); |
| 357 | OFFSET_INCREASE(func_table); |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 358 | OFFSET_INCREASE(object_slot_list); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 359 | |
| 360 | #undef OFFSET_INCREASE |
| 361 | |
| 362 | // Context |
| 363 | long pagesize = sysconf(_SC_PAGESIZE); |
| 364 | size_t context_offset_rem = offset % pagesize; |
| 365 | if (context_offset_rem) { |
| 366 | offset += pagesize - context_offset_rem; |
| 367 | } |
| 368 | |
| 369 | mpHeaderSection->context_offset = offset; |
| 370 | return true; |
| 371 | } |
| 372 | |
| 373 | |
| 374 | bool CacheWriter::calcContextChecksum() { |
| 375 | uint32_t sum = 0; |
| 376 | uint32_t *ptr = reinterpret_cast<uint32_t *>(mpOwner->getContext()); |
| 377 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 378 | for (size_t i = 0; i < ContextManager::ContextSize / sizeof(uint32_t); ++i) { |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 379 | sum ^= *ptr++; |
| 380 | } |
| 381 | |
| 382 | mpHeaderSection->context_parity_checksum = sum; |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | bool CacheWriter::writeAll() { |
| 388 | #define WRITE_SECTION(NAME, OFFSET, SIZE, SECTION) \ |
| 389 | do { \ |
| 390 | if (mFile->seek(OFFSET, SEEK_SET) == -1) { \ |
| 391 | LOGE("Unable to seek to " #NAME " section for writing.\n"); \ |
| 392 | return false; \ |
| 393 | } \ |
| 394 | \ |
| 395 | if (mFile->write(reinterpret_cast<char *>(SECTION), (SIZE)) != \ |
| 396 | static_cast<ssize_t>(SIZE)) { \ |
| 397 | LOGE("Unable to write " #NAME " section to cache file.\n"); \ |
| 398 | return false; \ |
| 399 | } \ |
| 400 | } while (0) |
| 401 | |
| 402 | #define WRITE_SECTION_SIMPLE(NAME, SECTION) \ |
| 403 | WRITE_SECTION(NAME, \ |
| 404 | mpHeaderSection->NAME##_offset, \ |
| 405 | mpHeaderSection->NAME##_size, \ |
| 406 | SECTION) |
| 407 | |
| 408 | WRITE_SECTION(header, 0, sizeof(OBCC_Header), mpHeaderSection); |
| 409 | |
| 410 | WRITE_SECTION_SIMPLE(str_pool, mpStringPoolSection); |
| 411 | WRITE_SECTION_SIMPLE(depend_tab, mpDependencyTableSection); |
| 412 | //WRITE_SECTION_SIMPLE(reloc_tab, mpRelocationTableSection); |
| 413 | WRITE_SECTION_SIMPLE(export_var_list, mpExportVarListSection); |
| 414 | WRITE_SECTION_SIMPLE(export_func_list, mpExportFuncListSection); |
| 415 | WRITE_SECTION_SIMPLE(pragma_list, mpPragmaListSection); |
| 416 | WRITE_SECTION_SIMPLE(func_table, mpFuncTableSection); |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 417 | WRITE_SECTION_SIMPLE(object_slot_list, mpObjectSlotSection); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 418 | |
Logan | 1dc6314 | 2011-02-25 17:14:51 +0800 | [diff] [blame] | 419 | WRITE_SECTION(context, mpHeaderSection->context_offset, |
| 420 | ContextManager::ContextSize, |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 421 | mpOwner->getContext()); |
| 422 | |
| 423 | #undef WRITE_SECTION_SIMPLE |
| 424 | #undef WRITE_SECTION |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 430 | } // namespace bcc |