Example #1
0
/*
 * This is the true entry point for untrusted code.
 * See nacl_startup.h for the layout at the argument pointer.
 */
void _start(uint32_t *info) {
  void (*fini)(void) = nacl_startup_fini(info);
  int argc = nacl_startup_argc(info);
  char **argv = nacl_startup_argv(info);
  char **envp = nacl_startup_envp(info);
  Elf32_auxv_t *auxv = nacl_startup_auxv(info);

  environ = envp;

  /*
   * Record the approximate address from which the stack grows
   * (usually downwards) so that libpthread can report it.  Taking the
   * address of any stack-allocated variable will work here.
   */
  __nacl_initial_thread_stack_end = &info;

  __libnacl_irt_init(auxv);

  /*
   * If we were started by a dynamic linker, then it passed its finalizer
   * function here.  For static linking, this is always NULL.
   */
  if (fini != NULL)
    atexit(fini);

  atexit(&__libc_fini_array);

  __pthread_initialize();

  __libc_init_array();

  int (*main_ptr)(int argc, char **argv, char **envp) = &__nacl_main;
  if (main_ptr == NULL)
    main_ptr = &main;

  exit(main_ptr(argc, argv, envp));

  /*NOTREACHED*/
  __builtin_trap();
}
static void chainload(const char *program, const char *interp_prefix,
                      int argc, char **argv, int envc, char **envp) {
  if (nacl_interface_query(NACL_IRT_RESOURCE_OPEN_v0_1, &resource_open,
                           sizeof(resource_open)) != sizeof(resource_open))
    resource_open.open_resource = NULL;
  if (nacl_interface_query(NACL_IRT_CODE_DATA_ALLOC_v0_1, &code_data_alloc,
                           sizeof(code_data_alloc)) !=
      sizeof(code_data_alloc)) {
    fprintf(stderr, "Failed to find necessary IRT interface %s!\n",
            NACL_IRT_CODE_DATA_ALLOC_v0_1);
    exit(1);
  }

  const size_t pagesize = NACL_MAP_PAGESIZE;
  const TYPE_nacl_irt_query irt_query = __nacl_irt_query;

  /*
   * Populate our own info array on the stack.  We do not assume that the
   * argv and envp arrays are in their usual layout, since the caller could
   * be passing different values.
   */
  uint32_t info[NACL_STARTUP_ARGV + argc + 1 + envc + 1 + (kAuxvCount * 2)];
  info[NACL_STARTUP_FINI] = 0;
  info[NACL_STARTUP_ENVC] = envc;
  info[NACL_STARTUP_ARGC] = argc;
  memcpy(nacl_startup_argv(info), argv, (argc + 1) * sizeof(argv[0]));
  memcpy(nacl_startup_envp(info), envp, (envc + 1) * sizeof(envp[0]));
  Elf32_auxv_t *auxv = nacl_startup_auxv(info);

  /*
   * Populate the auxiliary vector with the values dynamic linkers expect.
   */
  auxv[kPhdr].a_type = AT_PHDR;
  auxv[kPhent].a_type = AT_PHENT;
  auxv[kPhent].a_un.a_val = sizeof(Elf32_Phdr);
  auxv[kPhnum].a_type = AT_PHNUM;
  auxv[kBase].a_type = AT_BASE;
  auxv[kEntry].a_type = AT_ENTRY;
  auxv[kSysinfo].a_type = AT_SYSINFO;
  auxv[kSysinfo].a_un.a_val = (uint32_t) irt_query;
  auxv[kNull].a_type = AT_NULL;
  auxv[kNull].a_un.a_val = 0;

  /*
   * Load the program and point the auxv elements at its phdrs and entry.
   */
  const char *interp = NULL;
  uint32_t entry = load_elf_file(program,
                                 pagesize,
                                 &auxv[kBase].a_un.a_val,
                                 &auxv[kPhdr].a_un.a_val,
                                 &auxv[kPhnum].a_un.a_val,
                                 &interp);
  auxv[kEntry].a_un.a_val = entry;

  if (auxv[kPhdr].a_un.a_val == 0)
    auxv[kPhdr].a_type = AT_IGNORE;

  DEBUG_PRINTF("XXX loaded %s, entry %#x, interp %s\n", program, entry, interp);

  if (interp != NULL) {
    /*
     * There was a PT_INTERP, so we have a dynamic linker to load.
     */

    char interp_buf[PATH_MAX];
    if (interp_prefix != NULL) {
      /*
       * Apply the command-line-specified prefix to the embedded file name.
       */
      snprintf(interp_buf, sizeof(interp_buf), "%s%s", interp_prefix, interp);
      interp = interp_buf;
    }

    entry = load_elf_file(interp, pagesize, NULL, NULL, NULL, NULL);

    DEBUG_PRINTF("XXX loaded PT_INTERP %s, entry %#x\n", interp, entry);
  }

  for (uint32_t *p = info; p < (uint32_t *) &auxv[kNull + 1]; ++p)
    DEBUG_PRINTF("XXX info[%d] = %#x\n", p - info, *p);

  /*
   * Off to the races!
   * The application's entry point should not return.  Crash if it does.
   */
  DEBUG_PRINTF("XXX user entry point: %#x(%#x)\n", entry, (uintptr_t) info);
  (*(void (*)(uint32_t[])) entry)(info);
  while (1)
    __builtin_trap();
}