static BCCScriptRef loadScript() {
  if (!inFile) {
    fprintf(stderr, "input file required\n");
    return NULL;
  }

  struct stat statInFile;
  if (stat(inFile, &statInFile) < 0) {
    fprintf(stderr, "Unable to stat input file: %s\n", strerror(errno));
    return NULL;
  }

  if (!S_ISREG(statInFile.st_mode)) {
    fprintf(stderr, "Input file should be a regular file.\n");
    return NULL;
  }

  FILE *in = fopen(inFile, "r");
  if (!in) {
    fprintf(stderr, "Could not open input file %s\n", inFile);
    return NULL;
  }

  size_t bitcodeSize = statInFile.st_size;

  std::vector<char> bitcode(bitcodeSize + 1, '\0');
  size_t nread = fread(&*bitcode.begin(), 1, bitcodeSize, in);

  if (nread != bitcodeSize)
      fprintf(stderr, "Could not read all of file %s\n", inFile);

  BCCScriptRef script = bccCreateScript();

  if (bccReadBC(script, "file", &*bitcode.begin(), bitcodeSize, 0) != 0) {
    fprintf(stderr, "bcc: FAILS to read bitcode");
    bccDisposeScript(script);
    return NULL;
  }

  bccRegisterSymbolCallback(script, lookupSymbol, NULL);

  if (bccPrepareExecutable(script, ".", "cache", 0) != 0) {
    fprintf(stderr, "bcc: FAILS to prepare executable.\n");
    bccDisposeScript(script);
    return NULL;
  }

  return script;
}
Esempio n. 2
0
extern "C" JNIEXPORT jboolean JNICALL Java_com_android_photoeditor_filters_ImageUtils_init(
    JNIEnv *env, jobject obj, jbyteArray scriptRef, jint length)
{
#if !defined(__GDK__) && !defined(__NOGDK__)
    void *new_func_ptr[JNI_max];
    int i, all_func_found = 1;

    BCCScriptRef script_ref = bccCreateScript();
    jbyte* script_ptr = (jbyte *)env->GetPrimitiveArrayCritical(scriptRef, (jboolean *)0);
    LOGI("BCC Script Len: %d", length);
    if (bccReadBC(script_ref, "libjni_photoeditor_portable.bc", (const char*)script_ptr, length, 0)) {
        LOGE("Error! Cannot bccReadBc");
        return JNI_FALSE;
    }
    if (script_ptr) {
        env->ReleasePrimitiveArrayCritical(scriptRef, script_ptr, 0);
    }
#if 0
    if (bccLinkFile(script_ref, "/system/lib/libclcore.bc", 0)) {
        LOGE("Error! Cannot bccLinkBC");
        return JNI_FALSE;
    }
#endif
    bccRegisterSymbolCallback(script_ref, lookupSymbol, NULL);

#ifdef OLD_BCC
    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/photoeditorLLVM.oBCC", 0)) {
        LOGE("Error! Cannot bccPrepareExecutable");
        return JNI_FALSE;
    }
#else
    if (bccPrepareExecutable(script_ref, "/data/data/com.android.photoeditor/", "photoeditorLLVM", 0)) {
        LOGE("Error! Cannot bccPrepareExecutable");
        return JNI_FALSE;
    }
#endif // OLD_BCC

    for(i=0; i<JNI_max; i++) {
        new_func_ptr[i] = bccGetFuncAddr(script_ref, JNIFunc[i].func_name);
        if (new_func_ptr[i] == NULL) {
            LOGE("Error! Cannot find %s()\n", JNIFunc[i].func_name);
            all_func_found = 0;
            //return JNI_FALSE;
        } else
            LOGI("Found %s() @ 0x%x", JNIFunc[i].func_name, (unsigned)new_func_ptr[i]);
    }

    //bccDisposeScript(script_ref);

    if (all_func_found)
    {
        LOGI("Use LLVM version");
        for(i=0; i<JNI_max; i++)
            JNIFunc[i].func_ptr = new_func_ptr[i];
    }

    return JNI_TRUE;
#else

    return JNI_FALSE;

#endif // !__GDK__ && !__NOGDK__
}
Esempio n. 3
0
static BCCScriptRef loadScript() {
  if (!InFile) {
    fprintf(stderr, "input file required.\n");
    return NULL;
  }

  BCCScriptRef script = bccCreateScript();

  if (bccReadFile(script, InFile, /* flags */BCC_SKIP_DEP_SHA1) != 0) {
    fprintf(stderr, "bcc: FAILS to read bitcode.");
    bccDisposeScript(script);
    return NULL;
  }

  char *output = NULL;

  if (OutFile != NULL) {
    // Copy the outFile since we're going to modify it
    size_t outFileLen = strlen(OutFile);
    output = new char [outFileLen + 1];
    strncpy(output, OutFile, outFileLen);
  } else {
    if (OutType == OT_Executable) {
      output = new char [(sizeof(DEFAULT_OUTPUT_FILENAME) - 1) + 1];
      strncpy(output, DEFAULT_OUTPUT_FILENAME,
                  sizeof(DEFAULT_OUTPUT_FILENAME) - 1);
    } else {
      size_t inFileLen = strlen(InFile);
      output = new char [inFileLen + 3 /* ensure there's room for .so */ + 1];
      strncpy(output, InFile, inFileLen);

      char *fileExtension = strrchr(output, '.');
      if (fileExtension == NULL) {
        // append suffix
        fileExtension = output + inFileLen;
        *fileExtension = '.';
      }

      fileExtension++;  // skip '.'
      if (OutType == OT_Relocatable) {
        *fileExtension++ = 'o';
      } else /* must be OT_SharedObject */{
        *fileExtension++ = 's';
        *fileExtension++ = 'o';
      }
      *fileExtension++ = '\0';
    }
  }

  int bccResult = 0;
  const char *errMsg = NULL;
  switch (OutType) {
    case OT_Executable: {
      bccResult = 1;
      errMsg = "generation of executable is unsupported currently.";
      break;
    }
    case OT_Relocatable: {
      bccResult = bccPrepareRelocatable(script, output, OutRelocModel, 0);
      errMsg = "failed to generate relocatable.";
      break;
    }
    case OT_SharedObject: {
      if (IntermediateOutFile != NULL) {
        bccResult =
            bccPrepareRelocatable(script, IntermediateOutFile, bccRelocPIC, 0);
        errMsg = "failed to generate intermediate relocatable.";
      }

      if (bccResult == 0) {
        bccResult =
            bccPrepareSharedObject(script, IntermediateOutFile, output, 0);
        errMsg = "failed to generate shared library.";
      }
      break;
    }
  }

  delete [] output;

  if (bccResult == 0) {
    return script;
  } else {
    fprintf(stderr, "bcc: %s\n", errMsg);
    bccDisposeScript(script);
    return NULL;
  }
}