blob: adf81933c3faf7fa449f2b53d8f3f16ee92d69fb [file] [log] [blame]
Logan1f028c02010-11-27 01:02: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
Logan1f028c02010-11-27 01:02:48 +080020#if defined(__arm__)
21# define DEFAULT_ARM_CODEGEN
22# define PROVIDE_ARM_CODEGEN
23#elif defined(__i386__)
24# define DEFAULT_X86_CODEGEN
25# define PROVIDE_X86_CODEGEN
26#elif defined(__x86_64__)
27# define DEFAULT_X64_CODEGEN
28# define PROVIDE_X64_CODEGEN
29#endif
30
31#if defined(FORCE_ARM_CODEGEN)
32# define DEFAULT_ARM_CODEGEN
33# undef DEFAULT_X86_CODEGEN
34# undef DEFAULT_X64_CODEGEN
35# define PROVIDE_ARM_CODEGEN
36# undef PROVIDE_X86_CODEGEN
37# undef PROVIDE_X64_CODEGEN
38#elif defined(FORCE_X86_CODEGEN)
39# undef DEFAULT_ARM_CODEGEN
40# define DEFAULT_X86_CODEGEN
41# undef DEFAULT_X64_CODEGEN
42# undef PROVIDE_ARM_CODEGEN
43# define PROVIDE_X86_CODEGEN
44# undef PROVIDE_X64_CODEGEN
45#elif defined(FORCE_X64_CODEGEN)
46# undef DEFAULT_ARM_CODEGEN
47# undef DEFAULT_X86_CODEGEN
48# define DEFAULT_X64_CODEGEN
49# undef PROVIDE_ARM_CODEGEN
50# undef PROVIDE_X86_CODEGEN
51# define PROVIDE_X64_CODEGEN
52#endif
53
54#if defined(DEFAULT_ARM_CODEGEN)
Logandf23afa2010-11-27 11:04:54 +080055# define TARGET_TRIPLE_STRING "armv7-none-linux-gnueabi"
Logan1f028c02010-11-27 01:02:48 +080056#elif defined(DEFAULT_X86_CODEGEN)
Logandf23afa2010-11-27 11:04:54 +080057# define TARGET_TRIPLE_STRING "i686-unknown-linux"
Logan1f028c02010-11-27 01:02:48 +080058#elif defined(DEFAULT_X64_CODEGEN)
Logandf23afa2010-11-27 11:04:54 +080059# define TARGET_TRIPLE_STRING "x86_64-unknown-linux"
Logan1f028c02010-11-27 01:02:48 +080060#endif
61
62#if (defined(__VFP_FP__) && !defined(__SOFTFP__))
63# define ARM_USE_VFP
64#endif
65
Logandf23afa2010-11-27 11:04:54 +080066#include "bcc_compiler.h"
Logan1f028c02010-11-27 01:02:48 +080067
Logandf23afa2010-11-27 11:04:54 +080068#include "llvm/ADT/StringRef.h"
Logan1f028c02010-11-27 01:02:48 +080069
Logandf23afa2010-11-27 11:04:54 +080070#include "llvm/Analysis/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080071
Logan1f028c02010-11-27 01:02:48 +080072#include "llvm/Bitcode/ReaderWriter.h"
73
Logan1f028c02010-11-27 01:02:48 +080074#include "llvm/CodeGen/Passes.h"
Logan1f028c02010-11-27 01:02:48 +080075#include "llvm/CodeGen/RegAllocRegistry.h"
76#include "llvm/CodeGen/SchedulerRegistry.h"
Logan1f028c02010-11-27 01:02:48 +080077
Logandf23afa2010-11-27 11:04:54 +080078#include "llvm/Transforms/IPO.h"
79#include "llvm/Transforms/Scalar.h"
80
81#include "llvm/Target/SubtargetFeature.h"
82#include "llvm/Target/TargetData.h"
83#include "llvm/Target/TargetMachine.h"
84#include "llvm/Target/TargetOptions.h"
85#include "llvm/Target/TargetRegistry.h"
86#include "llvm/Target/TargetSelect.h"
87
88#include "llvm/Support/ErrorHandling.h"
89#include "llvm/Support/MemoryBuffer.h"
90
91#include "llvm/GlobalValue.h"
92#include "llvm/Linker.h"
93#include "llvm/LLVMContext.h"
94#include "llvm/Metadata.h"
95#include "llvm/Module.h"
96#include "llvm/PassManager.h"
97#include "llvm/Value.h"
98
99#include <errno.h>
100#include <sys/file.h>
101#include <sys/mman.h>
102#include <sys/stat.h>
103#include <sys/types.h>
104#include <unistd.h>
105
106#include <string>
107#include <vector>
Logan1f028c02010-11-27 01:02:48 +0800108
109
Logandf23afa2010-11-27 11:04:54 +0800110namespace {
Logan1f028c02010-11-27 01:02:48 +0800111
112#define TEMP_FAILURE_RETRY1(exp) ({ \
113 typeof (exp) _rc; \
114 do { \
115 _rc = (exp); \
116 } while (_rc == -1 && errno == EINTR); \
117 _rc; })
118
119
Logandf23afa2010-11-27 11:04:54 +0800120int sysWriteFully(int fd, const void* buf, size_t count, const char* logMsg) {
Logan1f028c02010-11-27 01:02:48 +0800121 while (count != 0) {
122 ssize_t actual = TEMP_FAILURE_RETRY1(write(fd, buf, count));
123 if (actual < 0) {
124 int err = errno;
125 LOGE("%s: write failed: %s\n", logMsg, strerror(err));
126 return err;
127 } else if (actual != (ssize_t) count) {
128 LOGD("%s: partial write (will retry): (%d of %zd)\n",
129 logMsg, (int) actual, count);
130 buf = (const void*) (((const uint8_t*) buf) + actual);
131 }
132 count -= actual;
133 }
134
135 return 0;
136}
137
Logandf23afa2010-11-27 11:04:54 +0800138} // namespace anonymous
139
Logan1f028c02010-11-27 01:02:48 +0800140
141namespace bcc {
142
143//////////////////////////////////////////////////////////////////////////////
144// BCC Compiler Static Variables
145//////////////////////////////////////////////////////////////////////////////
146
147bool Compiler::GlobalInitialized = false;
148
149bool Compiler::BccMmapImgAddrTaken[BCC_MMAP_IMG_COUNT];
150
151// Code generation optimization level for the compiler
152llvm::CodeGenOpt::Level Compiler::CodeGenOptLevel;
153
154std::string Compiler::Triple;
155
156std::string Compiler::CPU;
157
158std::vector<std::string> Compiler::Features;
159
160// The named of metadata node that pragma resides (should be synced with
161// slang.cpp)
162const llvm::StringRef Compiler::PragmaMetadataName = "#pragma";
163
164// The named of metadata node that export variable name resides (should be
165// synced with slang_rs_metadata.h)
166const llvm::StringRef Compiler::ExportVarMetadataName = "#rs_export_var";
167
168// The named of metadata node that export function name resides (should be
169// synced with slang_rs_metadata.h)
170const llvm::StringRef Compiler::ExportFuncMetadataName = "#rs_export_func";
171
172
173//////////////////////////////////////////////////////////////////////////////
174// Compiler
175//////////////////////////////////////////////////////////////////////////////
176
177void Compiler::GlobalInitialization() {
178 if (GlobalInitialized)
179 return;
180
181 // if (!llvm::llvm_is_multithreaded())
182 // llvm::llvm_start_multithreaded();
183
184 // Set Triple, CPU and Features here
185 Triple = TARGET_TRIPLE_STRING;
186
187 // TODO(sliao): NEON for JIT
188 // Features.push_back("+neon");
189 // Features.push_back("+vmlx");
190 // Features.push_back("+neonfp");
191 Features.push_back("+vfp3");
192 Features.push_back("+d16");
193
194#if defined(DEFAULT_ARM_CODEGEN) || defined(PROVIDE_ARM_CODEGEN)
195 LLVMInitializeARMTargetInfo();
196 LLVMInitializeARMTarget();
197#if defined(USE_DISASSEMBLER)
198 LLVMInitializeARMDisassembler();
199 LLVMInitializeARMAsmPrinter();
200#endif
201#endif
202
203#if defined(DEFAULT_X86_CODEGEN) || defined(PROVIDE_X86_CODEGEN)
204 LLVMInitializeX86TargetInfo();
205 LLVMInitializeX86Target();
206#if defined(USE_DISASSEMBLER)
207 LLVMInitializeX86Disassembler();
208 LLVMInitializeX86AsmPrinter();
209#endif
210#endif
211
212#if defined(DEFAULT_X64_CODEGEN) || defined(PROVIDE_X64_CODEGEN)
213 LLVMInitializeX86TargetInfo();
214 LLVMInitializeX86Target();
215#if defined(USE_DISASSEMBLER)
216 LLVMInitializeX86Disassembler();
217 LLVMInitializeX86AsmPrinter();
218#endif
219#endif
220
221 // -O0: llvm::CodeGenOpt::None
222 // -O1: llvm::CodeGenOpt::Less
223 // -O2: llvm::CodeGenOpt::Default
224 // -O3: llvm::CodeGenOpt::Aggressive
225 CodeGenOptLevel = llvm::CodeGenOpt::None;
226
227 // Below are the global settings to LLVM
228
229 // Disable frame pointer elimination optimization
230 llvm::NoFramePointerElim = false;
231
232 // Use hardfloat ABI
233 //
234 // TODO(all): Need to detect the CPU capability and decide whether to use
235 // softfp. To use softfp, change following 2 lines to
236 //
237 // llvm::FloatABIType = llvm::FloatABI::Soft;
238 // llvm::UseSoftFloat = true;
239 //
240 llvm::FloatABIType = llvm::FloatABI::Soft;
241 llvm::UseSoftFloat = false;
242
243 // BCC needs all unknown symbols resolved at JIT/compilation time.
244 // So we don't need any dynamic relocation model.
245 llvm::TargetMachine::setRelocationModel(llvm::Reloc::Static);
246
247#if defined(DEFAULT_X64_CODEGEN)
248 // Data address in X86_64 architecture may reside in a far-away place
249 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Medium);
250#else
251 // This is set for the linker (specify how large of the virtual addresses
252 // we can access for all unknown symbols.)
253 llvm::TargetMachine::setCodeModel(llvm::CodeModel::Small);
254#endif
255
256 // Register the scheduler
257 llvm::RegisterScheduler::setDefault(llvm::createDefaultScheduler);
258
259 // Register allocation policy:
260 // createFastRegisterAllocator: fast but bad quality
261 // createLinearScanRegisterAllocator: not so fast but good quality
262 llvm::RegisterRegAlloc::setDefault
263 ((CodeGenOptLevel == llvm::CodeGenOpt::None) ?
264 llvm::createFastRegisterAllocator :
265 llvm::createLinearScanRegisterAllocator);
266
267 GlobalInitialized = true;
268}
269
270
271void Compiler::LLVMErrorHandler(void *UserData, const std::string &Message) {
272 std::string *Error = static_cast<std::string*>(UserData);
273 Error->assign(Message);
274 LOGE("%s", Message.c_str());
275 exit(1);
276}
277
278
279CodeMemoryManager *Compiler::createCodeMemoryManager() {
280 mCodeMemMgr.reset(new CodeMemoryManager());
281 return mCodeMemMgr.get();
282}
283
284
285CodeEmitter *Compiler::createCodeEmitter() {
286 mCodeEmitter.reset(new CodeEmitter(mCodeMemMgr.take()));
287 return mCodeEmitter.get();
288}
289
290
291Compiler::Compiler()
292 : mUseCache(false),
293 mCacheNew(false),
294 mCacheFd(-1),
295 mCacheMapAddr(NULL),
296 mCacheHdr(NULL),
297 mCacheSize(0),
298 mCacheDiff(0),
299 mCodeDataAddr(NULL),
300 mpSymbolLookupFn(NULL),
301 mpSymbolLookupContext(NULL),
302 mContext(NULL),
303 mModule(NULL),
304 mHasLinked(false) /* Turn off linker */ {
305 llvm::remove_fatal_error_handler();
306 llvm::install_fatal_error_handler(LLVMErrorHandler, &mError);
307 mContext = new llvm::LLVMContext();
308 return;
309}
310
311
312int Compiler::readBC(const char *bitcode,
313 size_t bitcodeSize,
314 const BCCchar *resName) {
315 GlobalInitialization();
316
317 if (resName) {
318 // Turn on mUseCache mode iff
319 // 1. Has resName
320 // and, assuming USE_RELOCATE is false:
321 // 2. Later running code doesn't violate the following condition:
322 // mCodeDataAddr (set in loadCacheFile()) ==
323 // mCacheHdr->cachedCodeDataAddr
324 //
325 // BTW, this condition is achievable only when in the earlier
326 // cache-generating run,
327 // mpCodeMem == BccCodeAddr - MaxCodeSize - MaxGlobalVarSize,
328 // which means the mmap'ed is in the reserved area,
329 //
330 // Note: Upon violation, mUseCache will be set back to false.
331 mUseCache = true;
332
333 mCacheFd = openCacheFile(resName, true /* createIfMissing */);
334 if (mCacheFd >= 0 && !mCacheNew) { // Just use cache file
335 return -mCacheFd;
336 }
337 }
338
339 llvm::OwningPtr<llvm::MemoryBuffer> MEM;
340
341 if (bitcode == NULL || bitcodeSize <= 0)
342 return 0;
343
344 // Package input to object MemoryBuffer
345 MEM.reset(llvm::MemoryBuffer::getMemBuffer(
346 llvm::StringRef(bitcode, bitcodeSize)));
347
348 if (MEM.get() == NULL) {
349 setError("Error reading input program bitcode into memory");
350 return hasError();
351 }
352
353 // Read the input Bitcode as a Module
354 mModule = llvm::ParseBitcodeFile(MEM.get(), *mContext, &mError);
355 MEM.reset();
356 return hasError();
357}
358
359
360int Compiler::linkBC(const char *bitcode, size_t bitcodeSize) {
361 llvm::OwningPtr<llvm::MemoryBuffer> MEM;
362
363 if (bitcode == NULL || bitcodeSize <= 0)
364 return 0;
365
366 if (mModule == NULL) {
367 setError("No module presents for linking");
368 return hasError();
369 }
370
371 MEM.reset(llvm::MemoryBuffer::getMemBuffer(
372 llvm::StringRef(bitcode, bitcodeSize)));
373
374 if (MEM.get() == NULL) {
375 setError("Error reading input library bitcode into memory");
376 return hasError();
377 }
378
379 llvm::OwningPtr<llvm::Module> Lib(llvm::ParseBitcodeFile(MEM.get(),
380 *mContext,
381 &mError));
382 if (Lib.get() == NULL)
383 return hasError();
384
385 if (llvm::Linker::LinkModules(mModule, Lib.take(), &mError))
386 return hasError();
387
388 // Everything for linking should be settled down here with no error occurs
389 mHasLinked = true;
390 return hasError();
391}
392
393
394// interface for bccLoadBinary()
395int Compiler::loadCacheFile() {
396 // Check File Descriptor
397 if (mCacheFd < 0) {
398 LOGE("loading cache from invalid mCacheFd = %d\n", (int)mCacheFd);
399 goto giveup;
400 }
401
402 // Check File Size
403 struct stat statCacheFd;
404 if (fstat(mCacheFd, &statCacheFd) < 0) {
405 LOGE("unable to stat mCacheFd = %d\n", (int)mCacheFd);
406 goto giveup;
407 }
408
409 mCacheSize = statCacheFd.st_size;
410
411 if (mCacheSize < sizeof(oBCCHeader) ||
412 mCacheSize <= MaxCodeSize + MaxGlobalVarSize) {
413 LOGE("mCacheFd %d is too small to be correct\n", (int)mCacheFd);
414 goto giveup;
415 }
416
417 if (lseek(mCacheFd, 0, SEEK_SET) != 0) {
418 LOGE("Unable to seek to 0: %s\n", strerror(errno));
419 goto giveup;
420 }
421
422 // Part 1. Deal with the non-codedata section first
423 {
424 // Read cached file and perform quick integrity check
425
426 off_t heuristicCodeOffset = mCacheSize - MaxCodeSize - MaxGlobalVarSize;
427 LOGW("TODO(sliao)@loadCacheFile: mCacheSize=%x, heuristicCodeOffset=%llx",
428 (unsigned int)mCacheSize,
429 (unsigned long long int)heuristicCodeOffset);
430
431 mCacheMapAddr = (char *)malloc(heuristicCodeOffset);
432 if (!mCacheMapAddr) {
433 flock(mCacheFd, LOCK_UN);
434 LOGE("allocation failed.\n");
435 goto bail;
436 }
437
438 size_t nread = TEMP_FAILURE_RETRY1(read(mCacheFd, mCacheMapAddr,
439 heuristicCodeOffset));
440 if (nread != (size_t)heuristicCodeOffset) {
441 LOGE("read(mCacheFd) failed\n");
442 goto bail;
443 }
444
445 mCacheHdr = reinterpret_cast<oBCCHeader *>(mCacheMapAddr);
446 // Sanity check
447 if (mCacheHdr->codeOffset != (uint32_t)heuristicCodeOffset) {
448 LOGE("assertion failed: heuristic code offset is not correct.\n");
449 goto bail;
450 }
451 LOGW("TODO(sliao): mCacheHdr->cachedCodeDataAddr=%x", mCacheHdr->cachedCodeDataAddr);
452 LOGW("mCacheHdr->rootAddr=%x", mCacheHdr->rootAddr);
453 LOGW("mCacheHdr->initAddr=%x", mCacheHdr->initAddr);
454 LOGW("mCacheHdr->codeOffset=%x", mCacheHdr->codeOffset);
455 LOGW("mCacheHdr->codeSize=%x", mCacheHdr->codeSize);
456
457 // Verify the Cache File
458 if (memcmp(mCacheHdr->magic, OBCC_MAGIC, 4) != 0) {
459 LOGE("bad magic word\n");
460 goto bail;
461 }
462
463 if (memcmp(mCacheHdr->magicVersion, OBCC_MAGIC_VERS, 4) != 0) {
464 LOGE("bad oBCC version 0x%08x\n",
465 *reinterpret_cast<uint32_t *>(mCacheHdr->magicVersion));
466 goto bail;
467 }
468
469 if (mCacheSize < mCacheHdr->relocOffset +
470 mCacheHdr->relocCount * sizeof(oBCCRelocEntry)) {
471 LOGE("relocate table overflow\n");
472 goto bail;
473 }
474
475 if (mCacheSize < mCacheHdr->exportVarsOffset +
476 mCacheHdr->exportVarsCount * sizeof(uint32_t)) {
477 LOGE("export variables table overflow\n");
478 goto bail;
479 }
480
481 if (mCacheSize < mCacheHdr->exportFuncsOffset +
482 mCacheHdr->exportFuncsCount * sizeof(uint32_t)) {
483 LOGE("export functions table overflow\n");
484 goto bail;
485 }
486
487 if (mCacheSize < mCacheHdr->exportPragmasOffset +
488 mCacheHdr->exportPragmasCount * sizeof(uint32_t)) {
489 LOGE("export pragmas table overflow\n");
490 goto bail;
491 }
492
493 if (mCacheSize < mCacheHdr->codeOffset + mCacheHdr->codeSize) {
494 LOGE("code cache overflow\n");
495 goto bail;
496 }
497
498 if (mCacheSize < mCacheHdr->dataOffset + mCacheHdr->dataSize) {
499 LOGE("data (global variable) cache overflow\n");
500 goto bail;
501 }
502
503 long pagesize = sysconf(_SC_PAGESIZE);
504 if (mCacheHdr->codeOffset % pagesize != 0) {
505 LOGE("code offset must aligned to pagesize\n");
506 goto bail;
507 }
508 }
509
510 // Part 2. Deal with the codedata section
511 {
512 long pagesize = sysconf(_SC_PAGESIZE);
513
514 if (mCacheHdr->cachedCodeDataAddr % pagesize == 0) {
515 void *addr = reinterpret_cast<char *>(mCacheHdr->cachedCodeDataAddr);
516
517 // Try to mmap at cached address directly.
518 mCodeDataAddr = (char *) mmap(addr,
519 BCC_MMAP_IMG_SIZE,
520 PROT_READ | PROT_EXEC | PROT_WRITE,
521 MAP_PRIVATE | MAP_FIXED,
522 mCacheFd,
523 mCacheHdr->codeOffset);
524
525 if (mCodeDataAddr && mCodeDataAddr != MAP_FAILED) {
526 // Cheers! Mapped at the cached address successfully.
527
528 // Update the BccMmapImgAddrTaken table (if required)
529 if (mCacheHdr->cachedCodeDataAddr >= BCC_MMAP_IMG_BEGIN) {
530 size_t offset = mCacheHdr->cachedCodeDataAddr - BCC_MMAP_IMG_BEGIN;
531
532 if ((offset % BCC_MMAP_IMG_SIZE) == 0 &&
533 (offset / BCC_MMAP_IMG_SIZE) < BCC_MMAP_IMG_COUNT) {
534 Compiler::BccMmapImgAddrTaken[offset / BCC_MMAP_IMG_SIZE] = true;
535 }
536 }
537
538#if 1
539 // Check the checksum of code and data
540 {
541 uint32_t sum = mCacheHdr->checksum;
542 uint32_t *ptr = (uint32_t *)mCodeDataAddr;
543
544 for (size_t i = 0; i < BCC_MMAP_IMG_SIZE / sizeof(uint32_t); ++i) {
545 sum ^= *ptr++;
546 }
547
548 if (sum != 0) {
549 LOGE("Checksum check failed\n");
550 goto bail;
551 }
552
553 LOGE("Passed checksum even parity verification.\n");
554 }
555#endif
556
557 flock(mCacheFd, LOCK_UN);
558 return 0; // loadCacheFile succeed!
559 }
560 }
561 }
562
563#if !USE_RELOCATE
564 // Note: Since this build does not support relocation, we have no
565 // choose but give up to load the cached file, and recompile the
566 // code.
567
568 flock(mCacheFd, LOCK_UN);
569 goto bail;
570#else
571
572 // Note: Currently, relocation code is not working. Give up now.
573 flock(mCacheFd, LOCK_UN);
574 goto bail;
575
576 // TODO(logan): Following code is not working. Don't use them.
577 // And rewrite them asap.
578#if 0
579 {
580 // Try to allocate at arbitary address. And perform relocation.
581 mCacheMapAddr = (char *) mmap(0,
582 mCacheSize,
583 PROT_READ | PROT_EXEC | PROT_WRITE,
584 MAP_PRIVATE,
585 mCacheFd,
586 0);
587
588 if (mCacheMapAddr == MAP_FAILED) {
589 LOGE("unable to mmap .oBBC cache: %s\n", strerror(errno));
590 flock(mCacheFd, LOCK_UN);
591 goto giveup;
592 }
593
594 flock(mCacheFd, LOCK_UN);
595 mCodeDataAddr = mCacheMapAddr + mCacheHdr->codeOffset;
596
597 // Relocate
598 mCacheDiff = mCodeDataAddr -
599 reinterpret_cast<char *>(mCacheHdr->cachedCodeDataAddr);
600
601 if (mCacheDiff) { // To relocate
602 if (mCacheHdr->rootAddr) {
603 mCacheHdr->rootAddr += mCacheDiff;
604 }
605
606 if (mCacheHdr->initAddr) {
607 mCacheHdr->initAddr += mCacheDiff;
608 }
609
610 oBCCRelocEntry *cachedRelocTable =
611 reinterpret_cast<oBCCRelocEntry *>(mCacheMapAddr +
612 mCacheHdr->relocOffset);
613
614 std::vector<llvm::MachineRelocation> relocations;
615
616 // Read in the relocs
617 for (size_t i = 0; i < mCacheHdr->relocCount; i++) {
618 oBCCRelocEntry *entry = &cachedRelocTable[i];
619
620 llvm::MachineRelocation reloc =
621 llvm::MachineRelocation::getGV((uintptr_t)entry->relocOffset,
622 (unsigned)entry->relocType, 0, 0);
623
624 reloc.setResultPointer(
625 reinterpret_cast<char *>(entry->cachedResultAddr) + mCacheDiff);
626
627 relocations.push_back(reloc);
628 }
629
630 // Rewrite machine code using llvm::TargetJITInfo relocate
631 {
632 llvm::TargetMachine *TM = NULL;
633 const llvm::Target *Target;
634 std::string FeaturesStr;
635
636 // Create TargetMachine
637 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
638 if (hasError())
639 goto bail;
640
641 if (!CPU.empty() || !Features.empty()) {
642 llvm::SubtargetFeatures F;
643 F.setCPU(CPU);
644 for (std::vector<std::string>::const_iterator I = Features.begin(),
645 E = Features.end(); I != E; I++)
646 F.AddFeature(*I);
647 FeaturesStr = F.getString();
648 }
649
650 TM = Target->createTargetMachine(Triple, FeaturesStr);
651 if (TM == NULL) {
652 setError("Failed to create target machine implementation for the"
653 " specified triple '" + Triple + "'");
654 goto bail;
655 }
656
657 TM->getJITInfo()->relocate(mCodeDataAddr,
658 &relocations[0], relocations.size(),
659 (unsigned char *)mCodeDataAddr+MaxCodeSize);
660
661 if (mCodeEmitter.get()) {
662 mCodeEmitter->Disassemble(llvm::StringRef("cache"),
663 reinterpret_cast<uint8_t*>(mCodeDataAddr),
664 2 * 1024 /*MaxCodeSize*/,
665 false);
666 }
667
668 delete TM;
669 }
670 } // End of if (mCacheDiff)
671
672 return 0; // Success!
673 }
674#endif
675#endif
676
677bail:
678 if (mCacheMapAddr) {
679 free(mCacheMapAddr);
680 }
681
682 if (mCodeDataAddr && mCodeDataAddr != MAP_FAILED) {
683 if (munmap(mCodeDataAddr, BCC_MMAP_IMG_SIZE) != 0) {
684 LOGE("munmap failed: %s\n", strerror(errno));
685 }
686 }
687
688 mCacheMapAddr = NULL;
689 mCacheHdr = NULL;
690 mCodeDataAddr = NULL;
691
692giveup:
693 return 1;
694}
695
696// interace for bccCompileBC()
697int Compiler::compile() {
698 llvm::TargetData *TD = NULL;
699
700 llvm::TargetMachine *TM = NULL;
701 const llvm::Target *Target;
702 std::string FeaturesStr;
703
704 llvm::FunctionPassManager *CodeGenPasses = NULL;
705
706 const llvm::NamedMDNode *PragmaMetadata;
707 const llvm::NamedMDNode *ExportVarMetadata;
708 const llvm::NamedMDNode *ExportFuncMetadata;
709
710 if (mModule == NULL) // No module was loaded
711 return 0;
712
713 // Create TargetMachine
714 Target = llvm::TargetRegistry::lookupTarget(Triple, mError);
715 if (hasError())
716 goto on_bcc_compile_error;
717
718 if (!CPU.empty() || !Features.empty()) {
719 llvm::SubtargetFeatures F;
720 F.setCPU(CPU);
721 for (std::vector<std::string>::const_iterator I = Features.begin(),
722 E = Features.end();
723 I != E;
724 I++)
725 F.AddFeature(*I);
726 FeaturesStr = F.getString();
727 }
728
729 TM = Target->createTargetMachine(Triple, FeaturesStr);
730 if (TM == NULL) {
731 setError("Failed to create target machine implementation for the"
732 " specified triple '" + Triple + "'");
733 goto on_bcc_compile_error;
734 }
735
736 // Create memory manager for creation of code emitter later.
737 if (!mCodeMemMgr.get() && !createCodeMemoryManager()) {
738 setError("Failed to startup memory management for further compilation");
739 goto on_bcc_compile_error;
740 }
741 mCodeDataAddr = (char *) (mCodeMemMgr.get()->getCodeMemBase());
742
743 // Create code emitter
744 if (!mCodeEmitter.get()) {
745 if (!createCodeEmitter()) {
746 setError("Failed to create machine code emitter to complete"
747 " the compilation");
748 goto on_bcc_compile_error;
749 }
750 } else {
751 // Reuse the code emitter
752 mCodeEmitter->reset();
753 }
754
755 mCodeEmitter->setTargetMachine(*TM);
756 mCodeEmitter->registerSymbolCallback(mpSymbolLookupFn,
757 mpSymbolLookupContext);
758
759 // Get target data from Module
760 TD = new llvm::TargetData(mModule);
761
762 // Load named metadata
763 ExportVarMetadata = mModule->getNamedMetadata(ExportVarMetadataName);
764 ExportFuncMetadata = mModule->getNamedMetadata(ExportFuncMetadataName);
765 PragmaMetadata = mModule->getNamedMetadata(PragmaMetadataName);
766
767 // Create LTO passes and run them on the mModule
768 if (mHasLinked) {
769 llvm::TimePassesIsEnabled = true; // TODO(all)
770 llvm::PassManager LTOPasses;
771 LTOPasses.add(new llvm::TargetData(*TD));
772
773 std::vector<const char*> ExportSymbols;
774
775 // A workaround for getting export variable and function name. Will refine
776 // it soon.
777 if (ExportVarMetadata) {
778 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
779 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
780 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
781 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
782 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
783 llvm::StringRef ExportVarName =
784 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
785 ExportSymbols.push_back(ExportVarName.data());
786 }
787 }
788 }
789 }
790
791 if (ExportFuncMetadata) {
792 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
793 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
794 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
795 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
796 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
797 llvm::StringRef ExportFuncName =
798 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
799 ExportSymbols.push_back(ExportFuncName.data());
800 }
801 }
802 }
803 }
804 // root() and init() are born to be exported
805 ExportSymbols.push_back("root");
806 ExportSymbols.push_back("init");
807
808 // We now create passes list performing LTO. These are copied from
809 // (including comments) llvm::createStandardLTOPasses().
810
811 // Internalize all other symbols not listed in ExportSymbols
812 LTOPasses.add(llvm::createInternalizePass(ExportSymbols));
813
814 // Propagate constants at call sites into the functions they call. This
815 // opens opportunities for globalopt (and inlining) by substituting
816 // function pointers passed as arguments to direct uses of functions.
817 LTOPasses.add(llvm::createIPSCCPPass());
818
819 // Now that we internalized some globals, see if we can hack on them!
820 LTOPasses.add(llvm::createGlobalOptimizerPass());
821
822 // Linking modules together can lead to duplicated global constants, only
823 // keep one copy of each constant...
824 LTOPasses.add(llvm::createConstantMergePass());
825
826 // Remove unused arguments from functions...
827 LTOPasses.add(llvm::createDeadArgEliminationPass());
828
829 // Reduce the code after globalopt and ipsccp. Both can open up
830 // significant simplification opportunities, and both can propagate
831 // functions through function pointers. When this happens, we often have
832 // to resolve varargs calls, etc, so let instcombine do this.
833 LTOPasses.add(llvm::createInstructionCombiningPass());
834
835 // Inline small functions
836 LTOPasses.add(llvm::createFunctionInliningPass());
837
838 // Remove dead EH info.
839 LTOPasses.add(llvm::createPruneEHPass());
840
841 // Internalize the globals again after inlining
842 LTOPasses.add(llvm::createGlobalOptimizerPass());
843
844 // Remove dead functions.
845 LTOPasses.add(llvm::createGlobalDCEPass());
846
847 // If we didn't decide to inline a function, check to see if we can
848 // transform it to pass arguments by value instead of by reference.
849 LTOPasses.add(llvm::createArgumentPromotionPass());
850
851 // The IPO passes may leave cruft around. Clean up after them.
852 LTOPasses.add(llvm::createInstructionCombiningPass());
853 LTOPasses.add(llvm::createJumpThreadingPass());
854
855 // Break up allocas
856 LTOPasses.add(llvm::createScalarReplAggregatesPass());
857
858 // Run a few AA driven optimizations here and now, to cleanup the code.
859 LTOPasses.add(llvm::createFunctionAttrsPass()); // Add nocapture.
860 LTOPasses.add(llvm::createGlobalsModRefPass()); // IP alias analysis.
861
862 // Hoist loop invariants.
863 LTOPasses.add(llvm::createLICMPass());
864
865 // Remove redundancies.
866 LTOPasses.add(llvm::createGVNPass());
867
868 // Remove dead memcpys.
869 LTOPasses.add(llvm::createMemCpyOptPass());
870
871 // Nuke dead stores.
872 LTOPasses.add(llvm::createDeadStoreEliminationPass());
873
874 // Cleanup and simplify the code after the scalar optimizations.
875 LTOPasses.add(llvm::createInstructionCombiningPass());
876
877 LTOPasses.add(llvm::createJumpThreadingPass());
878
879 // Delete basic blocks, which optimization passes may have killed.
880 LTOPasses.add(llvm::createCFGSimplificationPass());
881
882 // Now that we have optimized the program, discard unreachable functions.
883 LTOPasses.add(llvm::createGlobalDCEPass());
884
885 LTOPasses.run(*mModule);
886 }
887
888 // Create code-gen pass to run the code emitter
889 CodeGenPasses = new llvm::FunctionPassManager(mModule);
890 CodeGenPasses->add(TD); // Will take the ownership of TD
891
892 if (TM->addPassesToEmitMachineCode(*CodeGenPasses,
893 *mCodeEmitter,
894 CodeGenOptLevel)) {
895 setError("The machine code emission is not supported by BCC on target '"
896 + Triple + "'");
897 goto on_bcc_compile_error;
898 }
899
900 // Run the pass (the code emitter) on every non-declaration function in the
901 // module
902 CodeGenPasses->doInitialization();
903 for (llvm::Module::iterator I = mModule->begin(), E = mModule->end();
904 I != E; I++) {
905 if (!I->isDeclaration()) {
906 CodeGenPasses->run(*I);
907 }
908 }
909
910 CodeGenPasses->doFinalization();
911
912 // Copy the global address mapping from code emitter and remapping
913 if (ExportVarMetadata) {
914 for (int i = 0, e = ExportVarMetadata->getNumOperands(); i != e; i++) {
915 llvm::MDNode *ExportVar = ExportVarMetadata->getOperand(i);
916 if (ExportVar != NULL && ExportVar->getNumOperands() > 1) {
917 llvm::Value *ExportVarNameMDS = ExportVar->getOperand(0);
918 if (ExportVarNameMDS->getValueID() == llvm::Value::MDStringVal) {
919 llvm::StringRef ExportVarName =
920 static_cast<llvm::MDString*>(ExportVarNameMDS)->getString();
921
922 CodeEmitter::global_addresses_const_iterator I, E;
923 for (I = mCodeEmitter->global_address_begin(),
924 E = mCodeEmitter->global_address_end();
925 I != E; I++) {
926 if (I->first->getValueID() != llvm::Value::GlobalVariableVal)
927 continue;
928 if (ExportVarName == I->first->getName()) {
929 mExportVars.push_back(I->second);
930 break;
931 }
932 }
933 if (I != mCodeEmitter->global_address_end())
934 continue; // found
935 }
936 }
937 // if reaching here, we know the global variable record in metadata is
938 // not found. So we make an empty slot
939 mExportVars.push_back(NULL);
940 }
941 assert((mExportVars.size() == ExportVarMetadata->getNumOperands()) &&
942 "Number of slots doesn't match the number of export variables!");
943 }
944
945 if (ExportFuncMetadata) {
946 for (int i = 0, e = ExportFuncMetadata->getNumOperands(); i != e; i++) {
947 llvm::MDNode *ExportFunc = ExportFuncMetadata->getOperand(i);
948 if (ExportFunc != NULL && ExportFunc->getNumOperands() > 0) {
949 llvm::Value *ExportFuncNameMDS = ExportFunc->getOperand(0);
950 if (ExportFuncNameMDS->getValueID() == llvm::Value::MDStringVal) {
951 llvm::StringRef ExportFuncName =
952 static_cast<llvm::MDString*>(ExportFuncNameMDS)->getString();
953 mExportFuncs.push_back(mCodeEmitter->lookup(ExportFuncName));
954 }
955 }
956 }
957 }
958
959 // Tell code emitter now can release the memory using during the JIT since
960 // we have done the code emission
961 mCodeEmitter->releaseUnnecessary();
962
963 // Finally, read pragma information from the metadata node of the @Module if
964 // any.
965 if (PragmaMetadata)
966 for (int i = 0, e = PragmaMetadata->getNumOperands(); i != e; i++) {
967 llvm::MDNode *Pragma = PragmaMetadata->getOperand(i);
968 if (Pragma != NULL &&
969 Pragma->getNumOperands() == 2 /* should have exactly 2 operands */) {
970 llvm::Value *PragmaNameMDS = Pragma->getOperand(0);
971 llvm::Value *PragmaValueMDS = Pragma->getOperand(1);
972
973 if ((PragmaNameMDS->getValueID() == llvm::Value::MDStringVal) &&
974 (PragmaValueMDS->getValueID() == llvm::Value::MDStringVal)) {
975 llvm::StringRef PragmaName =
976 static_cast<llvm::MDString*>(PragmaNameMDS)->getString();
977 llvm::StringRef PragmaValue =
978 static_cast<llvm::MDString*>(PragmaValueMDS)->getString();
979
980 mPragmas.push_back(
981 std::make_pair(std::string(PragmaName.data(),
982 PragmaName.size()),
983 std::string(PragmaValue.data(),
984 PragmaValue.size())));
985 }
986 }
987 }
988
989on_bcc_compile_error:
990 // LOGE("on_bcc_compiler_error");
991 if (CodeGenPasses) {
992 delete CodeGenPasses;
993 } else if (TD) {
994 delete TD;
995 }
996 if (TM)
997 delete TM;
998
999 if (mError.empty()) {
1000 if (mUseCache && mCacheFd >= 0 && mCacheNew) {
1001 genCacheFile();
1002 flock(mCacheFd, LOCK_UN);
1003 }
1004
1005 return false;
1006 }
1007
1008 // LOGE(getErrorMessage());
1009 return true;
1010}
1011
1012
1013// interface for bccGetScriptLabel()
1014void *Compiler::lookup(const char *name) {
1015 void *addr = NULL;
1016 if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
1017 if (!strcmp(name, "root")) {
1018 addr = reinterpret_cast<void *>(mCacheHdr->rootAddr);
1019 } else if (!strcmp(name, "init")) {
1020 addr = reinterpret_cast<void *>(mCacheHdr->initAddr);
1021 }
1022 return addr;
1023 }
1024
1025 if (mCodeEmitter.get())
1026 // Find function pointer
1027 addr = mCodeEmitter->lookup(name);
1028 return addr;
1029}
1030
1031
1032// Interface for bccGetExportVars()
1033void Compiler::getExportVars(BCCsizei *actualVarCount,
1034 BCCsizei maxVarCount,
1035 BCCvoid **vars) {
1036 int varCount;
1037
1038 if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
1039 varCount = static_cast<int>(mCacheHdr->exportVarsCount);
1040 if (actualVarCount)
1041 *actualVarCount = varCount;
1042 if (varCount > maxVarCount)
1043 varCount = maxVarCount;
1044 if (vars) {
1045 uint32_t *cachedVars = (uint32_t *)(mCacheMapAddr +
1046 mCacheHdr->exportVarsOffset);
1047
1048 for (int i = 0; i < varCount; i++) {
1049 *vars++ = (BCCvoid *)(reinterpret_cast<char *>(*cachedVars) +
1050 mCacheDiff);
1051 cachedVars++;
1052 }
1053 }
1054 return;
1055 }
1056
1057 varCount = mExportVars.size();
1058 if (actualVarCount)
1059 *actualVarCount = varCount;
1060 if (varCount > maxVarCount)
1061 varCount = maxVarCount;
1062 if (vars) {
1063 for (ExportVarList::const_iterator I = mExportVars.begin(),
1064 E = mExportVars.end();
1065 I != E;
1066 I++) {
1067 *vars++ = *I;
1068 }
1069 }
1070
1071 return;
1072}
1073
1074
1075// Interface for bccGetExportFuncs()
1076void Compiler::getExportFuncs(BCCsizei *actualFuncCount,
1077 BCCsizei maxFuncCount,
1078 BCCvoid **funcs) {
1079 int funcCount;
1080
1081 if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
1082 funcCount = static_cast<int>(mCacheHdr->exportFuncsCount);
1083 if (actualFuncCount)
1084 *actualFuncCount = funcCount;
1085 if (funcCount > maxFuncCount)
1086 funcCount = maxFuncCount;
1087 if (funcs) {
1088 uint32_t *cachedFuncs = (uint32_t *)(mCacheMapAddr +
1089 mCacheHdr->exportFuncsOffset);
1090
1091 for (int i = 0; i < funcCount; i++) {
1092 *funcs++ = (BCCvoid *)(reinterpret_cast<char *>(*cachedFuncs) +
1093 mCacheDiff);
1094 cachedFuncs++;
1095 }
1096 }
1097 return;
1098 }
1099
1100 funcCount = mExportFuncs.size();
1101 if (actualFuncCount)
1102 *actualFuncCount = funcCount;
1103 if (funcCount > maxFuncCount)
1104 funcCount = maxFuncCount;
1105 if (funcs) {
1106 for (ExportFuncList::const_iterator I = mExportFuncs.begin(),
1107 E = mExportFuncs.end();
1108 I != E;
1109 I++) {
1110 *funcs++ = *I;
1111 }
1112 }
1113
1114 return;
1115}
1116
1117
1118// Interface for bccGetPragmas()
1119void Compiler::getPragmas(BCCsizei *actualStringCount,
1120 BCCsizei maxStringCount,
1121 BCCchar **strings) {
1122 int stringCount;
1123 if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
1124 if (actualStringCount)
1125 *actualStringCount = 0; // XXX
1126 return;
1127 }
1128
1129 stringCount = mPragmas.size() * 2;
1130
1131 if (actualStringCount)
1132 *actualStringCount = stringCount;
1133 if (stringCount > maxStringCount)
1134 stringCount = maxStringCount;
1135 if (strings) {
1136 for (PragmaList::const_iterator it = mPragmas.begin();
1137 stringCount > 0;
1138 stringCount -= 2, it++) {
1139 *strings++ = const_cast<BCCchar*>(it->first.c_str());
1140 *strings++ = const_cast<BCCchar*>(it->second.c_str());
1141 }
1142 }
1143
1144 return;
1145}
1146
1147
1148// Interface for bccGetFunctions()
1149void Compiler::getFunctions(BCCsizei *actualFunctionCount,
1150 BCCsizei maxFunctionCount,
1151 BCCchar **functions) {
1152 if (mCodeEmitter.get())
1153 mCodeEmitter->getFunctionNames(actualFunctionCount,
1154 maxFunctionCount,
1155 functions);
1156 else
1157 *actualFunctionCount = 0;
1158
1159 return;
1160}
1161
1162
1163// Interface for bccGetFunctionBinary()
1164void Compiler::getFunctionBinary(BCCchar *function,
1165 BCCvoid **base,
1166 BCCsizei *length) {
1167 if (mCodeEmitter.get()) {
1168 mCodeEmitter->getFunctionBinary(function, base, length);
1169 } else {
1170 *base = NULL;
1171 *length = 0;
1172 }
1173 return;
1174}
1175
1176
1177Compiler::~Compiler() {
1178 if (!mCodeMemMgr.get()) {
1179 // mCodeDataAddr and mCacheMapAddr are from loadCacheFile and not
1180 // managed by CodeMemoryManager.
1181
1182 if (mCodeDataAddr != 0 && mCodeDataAddr != MAP_FAILED) {
1183 if (munmap(mCodeDataAddr, BCC_MMAP_IMG_SIZE) < 0) {
1184 LOGE("munmap failed while releasing mCodeDataAddr\n");
1185 }
1186 }
1187
1188 if (mCacheMapAddr) {
1189 free(mCacheMapAddr);
1190 }
1191
1192 mCodeDataAddr = 0;
1193 mCacheMapAddr = 0;
1194 }
1195
1196 delete mModule;
1197 // llvm::llvm_shutdown();
1198 delete mContext;
1199 return;
1200}
1201
1202
1203// Design of caching EXE:
1204// ======================
1205// 1. Each process will have virtual address available starting at 0x7e00000.
1206// E.g., Books and Youtube all have its own 0x7e00000. Next, we should
1207// minimize the chance of needing to do relocation INSIDE an app too.
1208//
1209// 2. Each process will have ONE class static variable called BccCodeAddr.
1210// I.e., even though the Compiler class will have multiple Compiler objects,
1211// e.g, one object for carousel.rs and the other for pageturn.rs,
1212// both Compiler objects will share 1 static variable called BccCodeAddr.
1213//
1214// Key observation: Every app (process) initiates, say 3, scripts (which
1215// correspond to 3 Compiler objects) in the same order, usually.
1216//
1217// So, we should mmap to, e.g., 0x7e00000, 0x7e40000, 0x7e80000 for the 3
1218// scripts, respectively. Each time, BccCodeAddr should be updated after
1219// JITTing a script. BTW, in ~Compiler(), BccCodeAddr should NOT be
1220// decremented back by CodeDataSize. I.e., for 3 scripts: A, B, C,
1221// even if it's A -> B -> ~B -> C -> ~C -> B -> C ... no relocation will
1222// ever be needed.)
1223//
1224// If we are lucky, then we don't need relocation ever, since next time the
1225// application gets run, the 3 scripts are likely created in the SAME order.
1226//
1227//
1228// End-to-end algorithm on when to caching and when to JIT:
1229// ========================================================
1230// Prologue:
1231// ---------
1232// Assertion: bccReadBC() is always called and is before bccCompileBC(),
1233// bccLoadBinary(), ...
1234//
1235// Key variable definitions: Normally,
1236// Compiler::BccCodeAddr: non-zero if (USE_CACHE)
1237// | (Stricter, because currently relocation doesn't work. So mUseCache only
1238// | when BccCodeAddr is nonzero.)
1239// V
1240// mUseCache: In addition to (USE_CACHE), resName is non-zero
1241// Note: mUseCache will be set to false later on whenever we find that caching
1242// won't work. E.g., when mCodeDataAddr != mCacheHdr->cachedCodeDataAddr.
1243// This is because currently relocation doesn't work.
1244// | (Stricter, initially)
1245// V
1246// mCacheFd: In addition, >= 0 if openCacheFile() returns >= 0
1247// | (Stricter)
1248// V
1249// mCacheNew: In addition, mCacheFd's size is 0, so need to call genCacheFile()
1250// at the end of compile()
1251//
1252//
1253// Main algorithm:
1254// ---------------
1255// #if !USE_RELOCATE
1256// Case 1. ReadBC() doesn't detect a cache file:
1257// compile(), which calls genCacheFile() at the end.
1258// Note: mCacheNew will guard the invocation of genCacheFile()
1259// Case 2. ReadBC() find a cache file
1260// loadCacheFile(). But if loadCacheFile() failed, should go to Case 1.
1261// #endif
1262
1263// Note: loadCacheFile() and genCacheFile() go hand in hand
1264void Compiler::genCacheFile() {
1265 if (lseek(mCacheFd, 0, SEEK_SET) != 0) {
1266 LOGE("Unable to seek to 0: %s\n", strerror(errno));
1267 return;
1268 }
1269
1270 bool codeOffsetNeedPadding = false;
1271
1272 uint32_t offset = sizeof(oBCCHeader);
1273
1274 // BCC Cache File Header
1275 oBCCHeader *hdr = (oBCCHeader *)malloc(sizeof(oBCCHeader));
1276
1277 if (!hdr) {
1278 LOGE("Unable to allocate oBCCHeader.\n");
1279 return;
1280 }
1281
1282 // Magic Words
1283 memcpy(hdr->magic, OBCC_MAGIC, 4);
1284 memcpy(hdr->magicVersion, OBCC_MAGIC_VERS, 4);
1285
1286 // Timestamp
1287 hdr->sourceWhen = 0; // TODO(all)
1288 hdr->rslibWhen = 0; // TODO(all)
1289 hdr->libRSWhen = 0; // TODO(all)
1290 hdr->libbccWhen = 0; // TODO(all)
1291
1292 // Current Memory Address (Saved for Recalculation)
1293 hdr->cachedCodeDataAddr = reinterpret_cast<uint32_t>(mCodeDataAddr);
1294 hdr->rootAddr = reinterpret_cast<uint32_t>(lookup("root"));
1295 hdr->initAddr = reinterpret_cast<uint32_t>(lookup("init"));
1296
1297 // Relocation Table Offset and Entry Count
1298 hdr->relocOffset = sizeof(oBCCHeader);
1299 hdr->relocCount = mCodeEmitter->getCachingRelocations().size();
1300
1301 offset += hdr->relocCount * (sizeof(oBCCRelocEntry));
1302
1303 // Export Variable Table Offset and Entry Count
1304 hdr->exportVarsOffset = offset;
1305 hdr->exportVarsCount = mExportVars.size();
1306
1307 offset += hdr->exportVarsCount * sizeof(uint32_t);
1308
1309 // Export Function Table Offset and Entry Count
1310 hdr->exportFuncsOffset = offset;
1311 hdr->exportFuncsCount = mExportFuncs.size();
1312
1313 offset += hdr->exportFuncsCount * sizeof(uint32_t);
1314
1315 // Export Pragmas Table Offset and Entry Count
1316 hdr->exportPragmasOffset = offset;
1317 hdr->exportPragmasCount = 0; // TODO(all): mPragmas.size();
1318
1319 offset += hdr->exportPragmasCount * sizeof(uint32_t);
1320
1321 // Code Offset and Size
1322
1323 { // Always pad to the page boundary for now
1324 long pagesize = sysconf(_SC_PAGESIZE);
1325
1326 if (offset % pagesize > 0) {
1327 codeOffsetNeedPadding = true;
1328 offset += pagesize - (offset % pagesize);
1329 }
1330 }
1331
1332 hdr->codeOffset = offset;
1333 hdr->codeSize = MaxCodeSize;
1334
1335 offset += hdr->codeSize;
1336
1337 // Data (Global Variable) Offset and Size
1338 hdr->dataOffset = offset;
1339 hdr->dataSize = MaxGlobalVarSize;
1340
1341 offset += hdr->dataSize;
1342
1343 // Checksum
1344#if 1
1345 {
1346 // Note: This is an simple checksum implementation that are using xor
1347 // to calculate even parity (for code and data only).
1348
1349 uint32_t sum = 0;
1350 uint32_t *ptr = (uint32_t *)mCodeDataAddr;
1351
1352 for (size_t i = 0; i < BCC_MMAP_IMG_SIZE / sizeof(uint32_t); ++i) {
1353 sum ^= *ptr++;
1354 }
1355
1356 hdr->checksum = sum;
1357 }
1358#else
1359 hdr->checksum = 0; // Set Field checksum. TODO(all)
1360#endif
1361
1362 // Write Header
1363 sysWriteFully(mCacheFd, reinterpret_cast<char const *>(hdr),
1364 sizeof(oBCCHeader), "Write oBCC header");
1365
1366 // Write Relocation Entry Table
1367 {
1368 size_t allocSize = hdr->relocCount * sizeof(oBCCRelocEntry);
1369
1370 oBCCRelocEntry const*records = &mCodeEmitter->getCachingRelocations()[0];
1371
1372 sysWriteFully(mCacheFd, reinterpret_cast<char const *>(records),
1373 allocSize, "Write Relocation Entries");
1374 }
1375
1376 // Write Export Variables Table
1377 {
1378 uint32_t *record, *ptr;
1379
1380 record = (uint32_t *)calloc(hdr->exportVarsCount, sizeof(uint32_t));
1381 ptr = record;
1382
1383 if (!record) {
1384 goto bail;
1385 }
1386
1387 for (ExportVarList::const_iterator I = mExportVars.begin(),
1388 E = mExportVars.end(); I != E; I++) {
1389 *ptr++ = reinterpret_cast<uint32_t>(*I);
1390 }
1391
1392 sysWriteFully(mCacheFd, reinterpret_cast<char const *>(record),
1393 hdr->exportVarsCount * sizeof(uint32_t),
1394 "Write ExportVars");
1395
1396 free(record);
1397 }
1398
1399 // Write Export Functions Table
1400 {
1401 uint32_t *record, *ptr;
1402
1403 record = (uint32_t *)calloc(hdr->exportFuncsCount, sizeof(uint32_t));
1404 ptr = record;
1405
1406 if (!record) {
1407 goto bail;
1408 }
1409
1410 for (ExportFuncList::const_iterator I = mExportFuncs.begin(),
1411 E = mExportFuncs.end(); I != E; I++) {
1412 *ptr++ = reinterpret_cast<uint32_t>(*I);
1413 }
1414
1415 sysWriteFully(mCacheFd, reinterpret_cast<char const *>(record),
1416 hdr->exportFuncsCount * sizeof(uint32_t),
1417 "Write ExportFuncs");
1418
1419 free(record);
1420 }
1421
1422
1423 // TODO(all): Write Export Pragmas Table
1424#if 0
1425#else
1426 // Note: As long as we have comment out export pragmas table code,
1427 // we have to seek the position to correct offset.
1428
1429 lseek(mCacheFd, hdr->codeOffset, SEEK_SET);
1430#endif
1431
1432 if (codeOffsetNeedPadding) {
1433 // requires additional padding
1434 lseek(mCacheFd, hdr->codeOffset, SEEK_SET);
1435 }
1436
1437 // Write Generated Code and Global Variable
1438 sysWriteFully(mCacheFd, mCodeDataAddr, MaxCodeSize + MaxGlobalVarSize,
1439 "Write code and global variable");
1440
1441 goto close_return;
1442
1443bail:
1444 if (ftruncate(mCacheFd, 0) != 0) {
1445 LOGW("Warning: unable to truncate cache file: %s\n", strerror(errno));
1446 }
1447
1448close_return:
1449 free(hdr);
1450 close(mCacheFd);
1451 mCacheFd = -1;
1452}
1453
1454
1455// OpenCacheFile() returns fd of the cache file.
1456// Input:
1457// BCCchar *resName: Used to genCacheFileName()
1458// bool createIfMissing: If false, turn off caching
1459// Output:
1460// returns fd: If -1: Failed
1461// mCacheNew: If true, the returned fd is new. Otherwise, the fd is the
1462// cache file's file descriptor
1463// Note: openCacheFile() will check the cache file's validity,
1464// such as Magic number, sourceWhen... dependencies.
1465int Compiler::openCacheFile(const BCCchar *resName, bool createIfMissing) {
1466 int fd, cc;
1467 struct stat fdStat, fileStat;
1468 bool readOnly = false;
1469
1470 char *cacheFileName = genCacheFileName(resName, ".oBCC");
1471
1472 mCacheNew = false;
1473
1474retry:
1475 /*
1476 * Try to open the cache file. If we've been asked to,
1477 * create it if it doesn't exist.
1478 */
1479 fd = createIfMissing ? open(cacheFileName, O_CREAT|O_RDWR, 0644) : -1;
1480 if (fd < 0) {
1481 fd = open(cacheFileName, O_RDONLY, 0);
1482 if (fd < 0) {
1483 if (createIfMissing) {
1484 LOGW("Can't open bcc-cache '%s': %s\n",
1485 cacheFileName, strerror(errno));
1486 mUseCache = false;
1487 }
1488 return fd;
1489 }
1490 readOnly = true;
1491 }
1492
1493 /*
1494 * Grab an exclusive lock on the cache file. If somebody else is
1495 * working on it, we'll block here until they complete.
1496 */
1497 LOGV("bcc: locking cache file %s (fd=%d, boot=%d)\n",
1498 cacheFileName, fd);
1499
1500 cc = flock(fd, LOCK_EX | LOCK_NB);
1501 if (cc != 0) {
1502 LOGD("bcc: sleeping on flock(%s)\n", cacheFileName);
1503 cc = flock(fd, LOCK_EX);
1504 }
1505
1506 if (cc != 0) {
1507 LOGE("Can't lock bcc cache '%s': %d\n", cacheFileName, cc);
1508 close(fd);
1509 return -1;
1510 }
1511 LOGV("bcc: locked cache file\n");
1512
1513 /*
1514 * Check to see if the fd we opened and locked matches the file in
1515 * the filesystem. If they don't, then somebody else unlinked ours
1516 * and created a new file, and we need to use that one instead. (If
1517 * we caught them between the unlink and the create, we'll get an
1518 * ENOENT from the file stat.)
1519 */
1520 cc = fstat(fd, &fdStat);
1521 if (cc != 0) {
1522 LOGE("Can't stat open file '%s'\n", cacheFileName);
1523 LOGV("bcc: unlocking cache file %s\n", cacheFileName);
1524 goto close_fail;
1525 }
1526 cc = stat(cacheFileName, &fileStat);
1527 if (cc != 0 ||
1528 fdStat.st_dev != fileStat.st_dev || fdStat.st_ino != fileStat.st_ino) {
1529 LOGD("bcc: our open cache file is stale; sleeping and retrying\n");
1530 LOGV("bcc: unlocking cache file %s\n", cacheFileName);
1531 flock(fd, LOCK_UN);
1532 close(fd);
1533 usleep(250 * 1000); // if something is hosed, don't peg machine
1534 goto retry;
1535 }
1536
1537 /*
1538 * We have the correct file open and locked. If the file size is zero,
1539 * then it was just created by us, and we want to fill in some fields
1540 * in the "bcc" header and set "mCacheNew". Otherwise, we want to
1541 * verify that the fields in the header match our expectations, and
1542 * reset the file if they don't.
1543 */
1544 if (fdStat.st_size == 0) {
1545 if (readOnly) { // The device is readOnly --> close_fail
1546 LOGW("bcc: file has zero length and isn't writable\n");
1547 goto close_fail;
1548 }
1549 /*cc = createEmptyHeader(fd);
1550 if (cc != 0)
1551 goto close_fail;
1552 */
1553 mCacheNew = true;
1554 LOGV("bcc: successfully initialized new cache file\n");
1555 } else {
1556 // Calculate sourceWhen
1557 // XXX
1558 uint32_t sourceWhen = 0;
1559 uint32_t rslibWhen = 0;
1560 uint32_t libRSWhen = 0;
1561 uint32_t libbccWhen = 0;
1562 if (!checkHeaderAndDependencies(fd,
1563 sourceWhen,
1564 rslibWhen,
1565 libRSWhen,
1566 libbccWhen)) {
1567 // If checkHeaderAndDependencies returns 0: FAILED
1568 // Will truncate the file and retry to createIfMissing the file
1569
1570 if (readOnly) { // Shouldn't be readonly.
1571 /*
1572 * We could unlink and rewrite the file if we own it or
1573 * the "sticky" bit isn't set on the directory. However,
1574 * we're not able to truncate it, which spoils things. So,
1575 * give up now.
1576 */
1577 if (createIfMissing) {
1578 LOGW("Cached file %s is stale and not writable\n",
1579 cacheFileName);
1580 }
1581 goto close_fail;
1582 }
1583
1584 /*
1585 * If we truncate the existing file before unlinking it, any
1586 * process that has it mapped will fail when it tries to touch
1587 * the pages? Probably OK because we use MAP_PRIVATE.
1588 */
1589 LOGD("oBCC file is stale or bad; removing and retrying (%s)\n",
1590 cacheFileName);
1591 if (ftruncate(fd, 0) != 0) {
1592 LOGW("Warning: unable to truncate cache file '%s': %s\n",
1593 cacheFileName, strerror(errno));
1594 /* keep going */
1595 }
1596 if (unlink(cacheFileName) != 0) {
1597 LOGW("Warning: unable to remove cache file '%s': %d %s\n",
1598 cacheFileName, errno, strerror(errno));
1599 /* keep going; permission failure should probably be fatal */
1600 }
1601 LOGV("bcc: unlocking cache file %s\n", cacheFileName);
1602 flock(fd, LOCK_UN);
1603 close(fd);
1604 goto retry;
1605 } else {
1606 // Got cacheFile! Good to go.
1607 LOGV("Good cache file\n");
1608 }
1609 }
1610
1611 assert(fd >= 0);
1612 return fd;
1613
1614close_fail:
1615 flock(fd, LOCK_UN);
1616 close(fd);
1617 return -1;
1618} // End of openCacheFile()
1619
1620char *Compiler::genCacheFileName(const char *fileName,
1621 const char *subFileName) {
1622 char nameBuf[512];
1623 static const char kCachePath[] = "bcc-cache";
1624 char absoluteFile[sizeof(nameBuf)];
1625 const size_t kBufLen = sizeof(nameBuf) - 1;
1626 const char *dataRoot;
1627 char *cp;
1628
1629 // Get the absolute path of the raw/***.bc file.
1630 absoluteFile[0] = '\0';
1631 if (fileName[0] != '/') {
1632 /*
1633 * Generate the absolute path. This doesn't do everything it
1634 * should, e.g. if filename is "./out/whatever" it doesn't crunch
1635 * the leading "./" out, but it'll do.
1636 */
1637 if (getcwd(absoluteFile, kBufLen) == NULL) {
1638 LOGE("Can't get CWD while opening raw/***.bc file\n");
1639 return NULL;
1640 }
1641 // TODO(srhines): strncat() is a bit dangerous
1642 strncat(absoluteFile, "/", kBufLen);
1643 }
1644 strncat(absoluteFile, fileName, kBufLen);
1645
1646 if (subFileName != NULL) {
1647 strncat(absoluteFile, "/", kBufLen);
1648 strncat(absoluteFile, subFileName, kBufLen);
1649 }
1650
1651 /* Turn the path into a flat filename by replacing
1652 * any slashes after the first one with '@' characters.
1653 */
1654 cp = absoluteFile + 1;
1655 while (*cp != '\0') {
1656 if (*cp == '/') {
1657 *cp = '@';
1658 }
1659 cp++;
1660 }
1661
1662 /* Build the name of the cache directory.
1663 */
1664 dataRoot = getenv("ANDROID_DATA");
1665 if (dataRoot == NULL)
1666 dataRoot = "/data";
1667 snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kCachePath);
1668
1669 /* Tack on the file name for the actual cache file path.
1670 */
1671 strncat(nameBuf, absoluteFile, kBufLen);
1672
1673 LOGV("Cache file for '%s' '%s' is '%s'\n", fileName, subFileName, nameBuf);
1674 return strdup(nameBuf);
1675}
1676
1677/*
1678 * Read the oBCC header, verify it, then read the dependent section
1679 * and verify that data as well.
1680 *
1681 * On successful return, the file will be seeked immediately past the
1682 * oBCC header.
1683 */
1684bool Compiler::checkHeaderAndDependencies(int fd,
1685 uint32_t sourceWhen,
1686 uint32_t rslibWhen,
1687 uint32_t libRSWhen,
1688 uint32_t libbccWhen) {
1689 ssize_t actual;
1690 oBCCHeader optHdr;
1691 uint32_t val;
1692 uint8_t const *magic, *magicVer;
1693
1694 /*
1695 * Start at the start. The "bcc" header, when present, will always be
1696 * the first thing in the file.
1697 */
1698 if (lseek(fd, 0, SEEK_SET) != 0) {
1699 LOGE("bcc: failed to seek to start of file: %s\n", strerror(errno));
1700 goto bail;
1701 }
1702
1703 /*
1704 * Read and do trivial verification on the bcc header. The header is
1705 * always in host byte order.
1706 */
1707 actual = read(fd, &optHdr, sizeof(optHdr));
1708 if (actual < 0) {
1709 LOGE("bcc: failed reading bcc header: %s\n", strerror(errno));
1710 goto bail;
1711 } else if (actual != sizeof(optHdr)) {
1712 LOGE("bcc: failed reading bcc header (got %d of %zd)\n",
1713 (int) actual, sizeof(optHdr));
1714 goto bail;
1715 }
1716
1717 magic = optHdr.magic;
1718 if (memcmp(magic, OBCC_MAGIC, 4) != 0) {
1719 /* not an oBCC file, or previous attempt was interrupted */
1720 LOGD("bcc: incorrect opt magic number (0x%02x %02x %02x %02x)\n",
1721 magic[0], magic[1], magic[2], magic[3]);
1722 goto bail;
1723 }
1724
1725 magicVer = optHdr.magicVersion;
1726 if (memcmp(magic+4, OBCC_MAGIC_VERS, 4) != 0) {
1727 LOGW("bcc: stale oBCC version (0x%02x %02x %02x %02x)\n",
1728 magicVer[0], magicVer[1], magicVer[2], magicVer[3]);
1729 goto bail;
1730 }
1731
1732 /*
1733 * Do the header flags match up with what we want?
1734 *
1735 * This is useful because it allows us to automatically regenerate
1736 * a file when settings change (e.g. verification is now mandatory),
1737 * but can cause difficulties if the thing we depend upon
1738 * were handled differently than the current options specify.
1739 *
1740 * So, for now, we essentially ignore "expectVerify" and "expectOpt"
1741 * by limiting the match mask.
1742 *
1743 * The only thing we really can't handle is incorrect byte-ordering.
1744 */
1745
1746 val = optHdr.sourceWhen;
1747 if (val && (val != sourceWhen)) {
1748 LOGI("bcc: source file mod time mismatch (%08x vs %08x)\n",
1749 val, sourceWhen);
1750 goto bail;
1751 }
1752 val = optHdr.rslibWhen;
1753 if (val && (val != rslibWhen)) {
1754 LOGI("bcc: rslib file mod time mismatch (%08x vs %08x)\n",
1755 val, rslibWhen);
1756 goto bail;
1757 }
1758 val = optHdr.libRSWhen;
1759 if (val && (val != libRSWhen)) {
1760 LOGI("bcc: libRS file mod time mismatch (%08x vs %08x)\n",
1761 val, libRSWhen);
1762 goto bail;
1763 }
1764 val = optHdr.libbccWhen;
1765 if (val && (val != libbccWhen)) {
1766 LOGI("bcc: libbcc file mod time mismatch (%08x vs %08x)\n",
1767 val, libbccWhen);
1768 goto bail;
1769 }
1770
1771 return true;
1772
1773bail:
1774 return false;
1775}
1776
1777} // namespace bcc