summaryrefslogtreecommitdiffstats
path: root/llvm/tools/lli/ChildTarget/ChildTarget.cpp
blob: 1a6cd4211d08d15e4a9ca8bf30222dc467a462f8 (plain)
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
#include "llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h"
#include "llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/Process.h"
#include <sstream>

#include "../RemoteJITUtils.h"

using namespace llvm;
using namespace llvm::orc;
using namespace llvm::sys;

#ifdef __x86_64__
typedef OrcX86_64 HostOrcArch;
#else
typedef OrcGenericArchitecture HostOrcArch;
#endif

ExitOnError ExitOnErr;

int main(int argc, char *argv[]) {

  if (argc != 3) {
    errs() << "Usage: " << argv[0] << " <input fd> <output fd>\n";
    return 1;
  }

  ExitOnErr.setBanner(std::string(argv[0]) + ":");

  int InFD;
  int OutFD;
  {
    std::istringstream InFDStream(argv[1]), OutFDStream(argv[2]);
    InFDStream >> InFD;
    OutFDStream >> OutFD;
  }

  if (sys::DynamicLibrary::LoadLibraryPermanently(nullptr)) {
    errs() << "Error loading program symbols.\n";
    return 1;
  }

  auto SymbolLookup = [](const std::string &Name) {
    return RTDyldMemoryManager::getSymbolAddressInProcess(Name);
  };

  auto RegisterEHFrames = [](uint8_t *Addr, uint32_t Size) {
    RTDyldMemoryManager::registerEHFramesInProcess(Addr, Size);
  };

  auto DeregisterEHFrames = [](uint8_t *Addr, uint32_t Size) {
    RTDyldMemoryManager::deregisterEHFramesInProcess(Addr, Size);
  };

  FDRPCChannel Channel(InFD, OutFD);
  typedef remote::OrcRemoteTargetServer<FDRPCChannel, HostOrcArch> JITServer;
  JITServer Server(Channel, SymbolLookup, RegisterEHFrames, DeregisterEHFrames);

  while (1) {
    uint32_t RawId;
    ExitOnErr(Server.startReceivingFunction(Channel, RawId));
    auto Id = static_cast<JITServer::JITFuncId>(RawId);
    switch (Id) {
    case JITServer::TerminateSessionId:
      ExitOnErr(Server.handleTerminateSession());
      return 0;
    default:
      ExitOnErr(Server.handleKnownFunction(Id));
      break;
    }
  }

  close(InFD);
  close(OutFD);

  return 0;
}
OpenPOWER on IntegriCloud