Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 23457288a9
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 669 lines (570 sloc) 19.181 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
// Copyright 2011 Shinichiro Hamaji. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY Shinichiro Hamaji ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Shinichiro Hamaji OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.

// A Mach-O loader for linux.

#include <assert.h>
#include <dlfcn.h>
#include <err.h>
#include <errno.h>
#include <execinfo.h>
#include <fcntl.h>
#include <limits.h>
#include <memory>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <ucontext.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>

#include "env_flags.h"
#include "fat.h"
#include "log.h"
#include "mach-o.h"

using namespace std;

DEFINE_bool(TRACE_FUNCTIONS, false, "Show calling functions");
DEFINE_bool(PRINT_TIME, false, "Print time spent in this loader");

class MachO;

static map<string, string> g_rename;
static vector<const char*> g_bound_names;
static set<string> g_no_trampoline;
static map<uintptr_t, pair<string, uintptr_t> > g_exported_symbol_map;

// We only support x86-64 for now.
static const char* ARCH_NAME = "x86-64";

static char* g_darwin_executable_path;

static clock_t g_start_time;

static void initRename() {
#define RENAME(src, dst) g_rename.insert(make_pair(#src, #dst));
#define WRAP(src) RENAME(src, __darwin_ ## src)
#include "rename.tab"
#undef RENAME
#undef WRAP
}

static void initNoTrampoline() {
#define NO_TRAMPOLINE(name) g_no_trampoline.insert(#name);
#include "no_trampoline.tab"
#undef NO_TRAMPOLINE
}

static void undefinedFunction() {
  fprintf(stderr, "Undefined function called\n");
  abort();
}

static uint64_t alignMem(uint64_t p, uint64_t a) {
  a--;
  return (p + a) & ~a;
}

static void dumpInt(int bound_name_id) {
  if (bound_name_id < 0) {
    fprintf(stderr, "%d: negative bound function id\n", bound_name_id);
    return;
  }
  if (bound_name_id >= (int)g_bound_names.size()) {
    fprintf(stderr, "%d: bound function id overflow\n", bound_name_id);
    return;
  }
  if (!g_bound_names[bound_name_id]) {
    fprintf(stderr, "%d: unbound function id\n", bound_name_id);
    return;
  }
  printf("calling %s(%d)\n", g_bound_names[bound_name_id], bound_name_id);
  fflush(stdout);
}

template <bool is64>
struct BitsHelpers {
  typedef uint64_t intptr;
  typedef segment_command_64 mach_segment;

  static const vector<mach_segment*>& segments(const MachO& mach) {
    return mach.segments64();
  }
};

template <>
struct BitsHelpers<false> {
  typedef uint32_t intptr;
  typedef segment_command mach_segment;

  static const vector<mach_segment*>& segments(const MachO& mach) {
    return mach.segments();
  }
};

template <bool is64>
class MachOLoader {
  typedef BitsHelpers<is64> Helpers;
  typedef typename Helpers::intptr intptr;
  typedef typename Helpers::mach_segment Segment;
 public:
  MachOLoader() {
    if (FLAGS_TRACE_FUNCTIONS) {
      // Push all arguments into stack.

      // push %rax
      pushTrampolineCode(0x50);
      // push %rdi
      pushTrampolineCode(0x57);
      // push %rsi
      pushTrampolineCode(0x56);
      // push %rdx
      pushTrampolineCode(0x52);
      // push %rcx
      pushTrampolineCode(0x51);
      // push %r8
      pushTrampolineCode(0x5041);
      // push %r9
      pushTrampolineCode(0x5141);

      // push %xmm0..%xmm7
      for (int i = 0; i < 8; i++) {
        // sub $8, %rsp
        pushTrampolineCode(0x08ec8348);

        // movq %xmmN, (%rsp)
        pushTrampolineCode(0xd60f66);
        pushTrampolineCode(4 + i * 8);
        pushTrampolineCode(0x24);
      }

      // mov %r10, %rdi
      pushTrampolineCode(0xd7894c);

      // mov $func, %rdx
      pushTrampolineCode(0xba48);
      pushTrampolineCode64((unsigned long long)(void*)&dumpInt);

      // call *%rdx
      pushTrampolineCode(0xd2ff);

      // pop %xmm7..%xmm0
      for (int i = 7; i >= 0; i--) {
        // movq (%rsp), %xmmN
        pushTrampolineCode(0x7e0ff3);
        pushTrampolineCode(4 + i * 8);
        pushTrampolineCode(0x24);

        // add $8, %rsp
        pushTrampolineCode(0x08c48348);
      }

      // pop %r9
      pushTrampolineCode(0x5941);
      // pop %r8
      pushTrampolineCode(0x5841);
      // pop %rcx
      pushTrampolineCode(0x59);
      // pop %rdx
      pushTrampolineCode(0x5a);
      // pop %rsi
      pushTrampolineCode(0x5e);
      // pop %rdi
      pushTrampolineCode(0x5f);
      // pop %rax
      pushTrampolineCode(0x58);

      // ret
      pushTrampolineCode(0xc3);
    }
  }

  void loadSegments(const MachO& mach, intptr* slide, intptr* base) {
    *base = 0;
    --*base;

    const vector<Segment*>& segments = Helpers::segments(mach);
    for (size_t i = 0; i < segments.size(); i++) {
      Segment* seg = segments[i];
      const char* name = seg->segname;
      if (!strcmp(name, SEG_PAGEZERO) || !strcmp(name, SEG_LINKEDIT)) {
        continue;
      }

      LOG << seg->segname << ": "
          << "fileoff=" << seg->fileoff
          << ", vmaddr=" << seg->vmaddr << endl;

      int prot = 0;
      if (seg->initprot & VM_PROT_READ) {
        prot |= PROT_READ;
      }
      if (seg->initprot & VM_PROT_WRITE) {
        prot |= PROT_WRITE;
      }
      if (seg->initprot & VM_PROT_EXECUTE) {
        prot |= PROT_EXEC;
      }

      intptr filesize = alignMem(seg->filesize, 0x1000);
      intptr vmaddr = seg->vmaddr + *slide;
      if (vmaddr < last_addr_) {
        LOG << "will rebase: filename=" << mach.filename()
            << ", vmaddr=" << (void*)vmaddr
            << ", last_addr=" << (void*)last_addr_ << endl;
        assert(i == 0);
        vmaddr = last_addr_;
        *slide = vmaddr - seg->vmaddr;
      }
      *base = min(*base, vmaddr);

      intptr vmsize = seg->vmsize;
      LOG << "mmap(file) " << mach.filename() << ' ' << name
          << ": " << (void*)vmaddr << "-" << (void*)(vmaddr + filesize)
          << " offset=" << mach.offset() + seg->fileoff << endl;
      void* mapped = mmap((void*)vmaddr, filesize, prot,
                          MAP_PRIVATE | MAP_FIXED,
                          mach.fd(), mach.offset() + seg->fileoff);
      if (mapped == MAP_FAILED) {
        err(1, "%s mmap(file) failed", mach.filename().c_str());
      }

      if (vmsize != filesize) {
        assert(vmsize > filesize);
        LOG << "mmap(anon) " << mach.filename() << ' ' << name
            << ": " << (void*)(vmaddr + filesize) << "-"
            << (void*)(vmaddr + vmsize)
            << endl;
        void* mapped = mmap((void*)(vmaddr + filesize),
                            vmsize - filesize, prot,
                            MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
                            0, 0);
        if (mapped == MAP_FAILED) {
          err(1, "%s mmap(anon) failed", mach.filename().c_str());
        }
      }

      last_addr_ = max(last_addr_, (intptr)vmaddr + vmsize);
    }
  }

  void doRebase(const MachO& mach, intptr slide) {
    for (size_t i = 0; i < mach.rebases().size(); i++) {
      const MachO::Rebase& rebase = *mach.rebases()[i];
      switch (rebase.type) {
      case REBASE_TYPE_POINTER: {
        char** ptr = (char**)(rebase.vmaddr + slide);
        LOG << "rebase: " << i << ": " << (void*)rebase.vmaddr << ' '
            << (void*)*ptr << " => "
            << (void*)(*ptr + slide) << " @" << ptr << endl;
        *ptr += slide;
        break;
      }

      default:
        fprintf(stderr, "Unknown rebase type: %d\n", rebase.type);
        exit(1);
      }
    }
  }

  void loadInitFuncs(const MachO& mach, intptr slide) {
    for (size_t i = 0; i < mach.init_funcs().size(); i++) {
      init_funcs_.push_back(mach.init_funcs()[i] + slide);
    }
  }

  void loadDylibs(const MachO& mach) {
    for (size_t i = 0; i < mach.dylibs().size(); i++) {
      string dylib = mach.dylibs()[i];
      // For now, we assume a dylib is a system library if its path
      // starts with /
      // TODO(hamaji): Do something?
      if (dylib[0] == '/') {
        continue;
      }

      static const char executable_str[] = "@executable_path";
      static const size_t executable_str_len = strlen(executable_str);
      if (!strncmp(dylib.c_str(), executable_str, executable_str_len)) {
        string dir = g_darwin_executable_path;
        size_t found = dir.rfind('/');
        if (found == string::npos) {
          dir = ".";
        } else {
          dir = dir.substr(0, found);
        }
        dylib.replace(0, executable_str_len, dir);
      }

      auto_ptr<MachO> dylib_mach(readMachO(dylib.c_str(), ARCH_NAME));
      load(*dylib_mach);
    }
  }

  void doBind(const MachO& mach, intptr slide) {
    unsigned int common_code_size = (unsigned int)trampoline_.size();
    // Ensure that we won't change the address.
    trampoline_.reserve(common_code_size +
                        (1 + 6 + 5 + 10 + 3 + 2 + 1) * mach.binds().size());
    g_bound_names.resize(mach.binds().size());
    for (size_t i = 0; i < mach.binds().size(); i++) {
      MachO::Bind* bind = mach.binds()[i];
      if (bind->name[0] != '_') {
        LOG << bind->name << ": skipping" << endl;
        continue;
      }

      if (bind->type == BIND_TYPE_POINTER) {
        const char* name = bind->name + 1;

        map<string, string>::const_iterator found =
            g_rename.find(name);
        if (found != g_rename.end()) {
          LOG << "Applying renaming: " << name
              << " => " << found->second.c_str() << endl;
          name = found->second.c_str();
        }

        void** ptr = (void**)(bind->vmaddr + slide);
        void* sym = NULL;
        const map<string, MachO::Export>::const_iterator export_found =
            exports_.find(bind->name);
        if (export_found != exports_.end()) {
          sym = (void*)export_found->second.addr;
        }
        if (!sym) {
          sym = dlsym(RTLD_DEFAULT, name);
        }
        if (!sym) {
            ERR << name << ": undefined symbol" << endl;
            sym = (void*)&undefinedFunction;
        }

        LOG << "bind " << name << ": "
            << *ptr << " => " << sym << " @" << ptr << endl;

        if (FLAGS_TRACE_FUNCTIONS && !g_no_trampoline.count(name)) {
          LOG << "Generating trampoline for " << name << "..." << endl;

          *ptr = &trampoline_[0] + trampoline_.size();
          g_bound_names[i] = name;

          // push %rax ; to adjust alignment for sse
          pushTrampolineCode(0x50);

          // mov $i, %r10d
          pushTrampolineCode(0xba41);
          pushTrampolineCode32((unsigned int)i);

          // call &trampoline_[0]
          pushTrampolineCode(0xe8);
          pushTrampolineCode32((unsigned int)(-4-trampoline_.size()));

          // mov $sym, %r10
          pushTrampolineCode(0xba49);
          pushTrampolineCode64((unsigned long long)(void*)sym);
          // call *%r10
          pushTrampolineCode(0xd2ff41);

          // pop %r10
          pushTrampolineCode(0x5a41);

          // ret
          pushTrampolineCode(0xc3);
        } else {
          *ptr = sym;
        }
      } else {
        fprintf(stderr, "Unknown bind type: %d\n", bind->type);
        abort();
      }
    }
  }

  void loadExports(const MachO& mach, intptr slide, intptr base) {
    for (size_t i = 0; i < mach.exports().size(); i++) {
      MachO::Export exp = mach.exports()[i];
      exp.addr += base;
      if (!exports_.insert(make_pair(exp.name, exp)).second) {
        fprintf(stderr, "duplicated exported symbol: %s\n", exp.name.c_str());
      }
      g_exported_symbol_map.insert(
          make_pair(exp.addr,
                    make_pair(exp.name.substr(1), slide)));
    }
  }

  void load(const MachO& mach) {
    intptr slide = 0;
    intptr base = 0;

    loadSegments(mach, &slide, &base);

    doRebase(mach, slide);

    loadInitFuncs(mach, slide);

    loadDylibs(mach);

    doBind(mach, slide);

    loadExports(mach, slide, base);
  }

  void run(const MachO& mach, int argc, char** argv, char** envp) {
    load(mach);

    g_exported_symbol_map.insert(
        make_pair(last_addr_ + 1, make_pair("*** last addr ***", 0)));

    char* trampoline_start_addr =
        (char*)(((uintptr_t)&trampoline_[0]) & ~0xfff);
    uint64_t trampoline_size =
        alignMem(&trampoline_[0] + trampoline_.size() - trampoline_start_addr,
                 0x1000);
    mprotect(trampoline_start_addr, trampoline_size,
             PROT_READ | PROT_WRITE | PROT_EXEC);

    if (FLAGS_PRINT_TIME) {
      double elapsed = ((double)clock() - g_start_time) / CLOCKS_PER_SEC;
      printf("Elapsed time: %f sec\n", elapsed);
    }

    for (size_t i = 0; i < init_funcs_.size(); i++) {
      void** init_func = (void**)init_funcs_[i];
      LOG << "calling initializer function " << *init_func << endl;
      ((void(*)())*init_func)();
    }

    LOG << "booting from " << (void*)mach.entry() << "..." << endl;
    fflush(stdout);
    assert(argc > 0);
    boot(mach.entry(), argc, argv, envp);
    /*
int (*fp)(int, char**, char**) =
(int(*)(int, char**, char**))mach.entry();
int ret = fp(argc, argv, envp);
exit(ret);
*/
  }

 private:
  void boot(uint64_t entry, int argc, char** argv, char** envp);

  void pushTrampolineCode(unsigned int c) {
    while (c) {
      trampoline_.push_back(c & 255);
      c = c >> 8;
    }
  }

  void pushTrampolineCode64(unsigned long long c) {
    for (int i = 0; i < 8; i++) {
      trampoline_.push_back(c & 255);
      c = c >> 8;
    }
  }

  void pushTrampolineCode32(unsigned int c) {
    for (int i = 0; i < 4; i++) {
      trampoline_.push_back(c & 255);
      c = c >> 8;
    }
  }

  string trampoline_;
  intptr last_addr_;
  vector<uint64_t> init_funcs_;
  map<string, MachO::Export> exports_;
};

template <>
void MachOLoader<true>::boot(
    uint64_t entry, int argc, char** argv, char** envp) {
  __asm__ volatile(" mov %1, %%eax;\n"
                   " mov %2, %%rdx;\n"
                   " push $0;\n"
                   ".loop64:\n"
                   " sub $8, %%rdx;\n"
                   " push (%%rdx);\n"
                   " dec %%eax;\n"
                   " jnz .loop64;\n"
                   " mov %1, %%eax;\n"
                   " push %%rax;\n"
                   " jmp *%0;\n"
                   ::"r"(entry), "r"(argc), "r"(argv + argc), "r"(envp)
                   :"%rax", "%rdx");
  //fprintf(stderr, "done!\n");
}

template <>
void MachOLoader<false>::boot(
    uint64_t entry, int argc, char** argv, char** envp) {
  __asm__ volatile(""
                   ::"r"(entry), "r"(argc), "r"(argv), "r"(envp));
}

template <bool is64>
void runMachO(const MachO& mach, int argc, char** argv, char** envp) {
  MachOLoader<is64> loader;
  loader.run(mach, argc, argv, envp);
}

#if 0
static int getBacktrace(void** trace, int max_depth) {
    typedef struct frame {
        struct frame *bp;
        void *ret;
    } frame;

    int depth;
    frame* bp = (frame*)__builtin_frame_address(0);
    for (depth = 0; bp && depth < max_depth; depth++) {
        trace[depth] = bp->ret;
        bp = bp->bp;
    }
    return depth;
}
#endif

static char g_dumped_stack_frame_buf[4096];

static const char* dumpExportedSymbol(void* p) {
  uintptr_t addr = reinterpret_cast<uintptr_t>(p);
  map<uintptr_t, pair<string, uintptr_t> >::const_iterator found =
    g_exported_symbol_map.lower_bound(addr);
  if (found == g_exported_symbol_map.begin() ||
      found == g_exported_symbol_map.end()) {
    return NULL;
  }

  --found;
  const char* name = found->second.first.c_str();
  uintptr_t func_offset = addr - found->first;
  uintptr_t file_offset = (uintptr_t)p - found->second.second;
  snprintf(g_dumped_stack_frame_buf, 4095, "%s(+%lx) [%p(%lx)]",
           name, func_offset, p, file_offset);
  return g_dumped_stack_frame_buf;
}

/* signal handler for fatal errors */
static void handleSignal(int signum, siginfo_t* siginfo, void* vuc) {
  ucontext_t *uc = (ucontext_t*)vuc;
  void* pc = (void*)uc->uc_mcontext.gregs[REG_RIP];

  fprintf(stderr, "%s(%d) %d (@%p) PC: %p\n\n",
          strsignal(signum), signum, siginfo->si_code, siginfo->si_addr, pc);

  void* trace[100];
  int len = backtrace(trace, 99);
  //int len = getBacktrace(trace, 99);
  char** syms = backtrace_symbols(trace, len);
  for (int i = len - 1; i > 0; i--) {
    if (syms[i] && syms[i][0] != '[') {
      fprintf(stderr, "%s\n", syms[i]);
    } else {
      const char* s = dumpExportedSymbol(trace[i]);
      if (s) {
        fprintf(stderr, "%s\n", s);
      } else {
        fprintf(stderr, "%p\n", trace[i]);
      }
    }
  }
}

/* Generate a stack backtrace when a CPU exception occurs. */
static void initSignalHandler() {
  struct sigaction sigact;
  sigact.sa_flags = SA_SIGINFO | SA_RESETHAND;
  sigact.sa_sigaction = handleSignal;
  sigemptyset(&sigact.sa_mask);
  sigaction(SIGFPE, &sigact, NULL);
  sigaction(SIGILL, &sigact, NULL);
  sigaction(SIGSEGV, &sigact, NULL);
  sigaction(SIGBUS, &sigact, NULL);
  sigaction(SIGABRT, &sigact, NULL);
}

int main(int argc, char* argv[], char* envp[]) {
  g_start_time = clock();
  initSignalHandler();
  initRename();
  initNoTrampoline();

  if (!dlopen("libmac.so", RTLD_NOW | RTLD_GLOBAL)) {
    if (!dlopen("libmac/libmac.so", RTLD_NOW | RTLD_GLOBAL)) {
      fprintf(stderr, "libmac not found\n");
      exit(1);
    }
  }

  char* loader_path = (char*)dlsym(RTLD_DEFAULT, "__loader_path");
  realpath(argv[0], loader_path);

  argc--;
  argv++;
  for (;;) {
    if (argc == 0) {
      fprintf(stderr, "An argument required.\n");
      exit(1);
    }

    const char* arg = argv[0];
    if (arg[0] != '-') {
      break;
    }

    // TODO(hamaji): Do something for switches.

    argc--;
    argv++;
  }

  g_darwin_executable_path =
      (char*)dlsym(RTLD_DEFAULT, "__darwin_executable_path");
  realpath(argv[0], g_darwin_executable_path);

  auto_ptr<MachO> mach(readMachO(argv[0], ARCH_NAME));
  if (mach->is64()) {
    runMachO<true>(*mach, argc, argv, envp);
  } else {
    runMachO<false>(*mach, argc, argv, envp);
  }
}
Something went wrong with that request. Please try again.