<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix">You're going to have to read the C++
      headers for a lot of things to make sense; the C headers don't
      really have much documentation.<br>
      <br>
      In this case:<br>
      <br>
        /// getFunctionAddress - Return the address of the specified
      function.<br>
        /// This may involve code generation.<br>
        virtual uint64_t getFunctionAddress(const std::string &Name)
      {<br>
          // Default implementation for the interpreter.  MCJIT will
      override this.<br>
          // Interpreter clients should use getPointerToFunction
      instead.<br>
          return 0;<br>
        }<br>
      <br>
      I would suggest using MCJIT (LLVMCreateMCJITCompilerForModule).<br>
      <br>
      -Eli<br>
      <br>
      On 3/8/2017 5:25 PM, Toshiyasu Morita wrote:<br>
    </div>
    <blockquote
cite="mid:CA+f7Snd3MTneq4s7j-V2=HM5cULHg2LryOV4GYkk1R1abEqwdw@mail.gmail.com"
      type="cite">
      <div dir="ltr">
        <div>
          <div>Oops, missed initializing some stuff. Added:<br>
            <br>
                    LLVMLinkInMCJIT();<br>
                    LLVMInitializeNativeTarget();<br>
                    LLVMInitializeNativeAsmPrinter();<br>
                    LLVMInitializeNativeAsmParser();<br>
            <br>
          </div>
          Now it crashes in LLVMGetFunctionAddress().<br>
          <br>
        </div>
        Hmm.<br>
        <br>
      </div>
      <div class="gmail_extra"><br>
        <div class="gmail_quote">On Wed, Mar 8, 2017 at 5:14 PM,
          Toshiyasu Morita <span dir="ltr"><<a
              moz-do-not-send="true" href="mailto:toshi@tensyr.com"
              target="_blank">toshi@tensyr.com</a>></span> wrote:<br>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div dir="ltr">
              <div>
                <div>
                  <div>
                    <div>Made it a bit further. Here's the current code:<br>
                      <br>
                      void llvm_load_IR_library(char *path)<br>
                      {<br>
                              char *error;<br>
                              LLVMExecutionEngineRef engine;<br>
                              object0_t* (*func)(void), *output;<br>
                              LLVMContextRef global_context;<br>
                              LLVMMemoryBufferRef module;<br>
                              LLVMModuleRef ir_lib_module;<br>
                              bool flag;<br>
                      <br>
                              printf("loading IR library from path:
                      %s\n", path);<br>
                      <br>
                              LLVMCreateMemoryBufferWithCont<wbr>entsOfFile(path,
                      &module, &error);<br>
                      <br>
                              global_context = LLVMGetGlobalContext();<br>
                      <br>
                              flag = LLVMGetBitcodeModuleInContext2<wbr>(global_context,
                      module, &ir_lib_module);<br>
                      <br>
                              printf("<wbr>LLVMGetBitcodeModuleInContext2<wbr>()
                      returned %d\n", flag);<br>
                              <br>
                              LLVMVerifyModule(ir_lib_<wbr>module,
                      LLVMAbortProcessAction, &error);<br>
                              LLVMDumpModule(ir_lib_module);<br>
                      <br>
                              if (<wbr>LLVMCreateExecutionEngineForMo<wbr>dule(&engine,
                      ir_lib_module, &error) != 0) {<br>
                                      fprintf(stderr, "failed to create
                      execution engine\n");<br>
                                      abort();<br>
                              }<br>
                      <br>
                              // Call the function<br>
                              func = (object0_t * (*)(void))<wbr>LLVMGetFunctionAddress(engine,
                      "test");<br>
                              output = (*func)();<br>
                      }<br>
                      <br>
                    </div>
                    when this code is executed, i see:<br>
                    <br>
                    ...<br>
                    LLVMGetBitcodeModuleInContext2<wbr>() returned 0<br>
                    ; ModuleID = '/home/toshi/tensyr/debug_<wbr>build/test.bc'<br>
                    source_filename = "test.c"<br>
                    target datalayout = "e-m:e-i64:64-f80:128-n8:16:<wbr>32:64-S128"<br>
                    target triple = "x86_64-unknown-linux-gnu"<br>
                    <br>
                    @str = private unnamed_addr constant [16 x i8]
                    c"test successful\00"<br>
                    <br>
                    ; Materializable<br>
                    ; Function Attrs: nounwind uwtable<br>
                    define void @test() local_unnamed_addr #0 {}<br>
                    <br>
                    ; Function Attrs: nounwind<br>
                    declare i32 @puts(i8* nocapture readonly) #1<br>
                    <br>
                    attributes #0 = { nounwind uwtable
                    "disable-tail-calls"="false"
                    "less-precise-fpmad"="false"
                    "no-frame-pointer-elim"="<wbr>false"
                    "no-infs-fp-math"="false" "no-jump-tables"="false"
                    "no-nans-fp-math"="false"
                    "no-signed-zeros-fp-math"="<wbr>false"
                    "stack-protector-buffer-size"=<wbr>"8"
                    "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,<wbr>+sse,+sse2,+x87"
                    "unsafe-fp-math"="false" "use-soft-float"="false" }<br>
                    attributes #1 = { nounwind }<br>
                    <br>
                    !llvm.ident = !{!0}<br>
                    <br>
                    !0 = !{!"clang version 3.9.1
                    (tags/RELEASE_391/final)"}<br>
                    failed to create execution engine<br>
                    Aborted (core dumped)<br>
                    <br>
                  </div>
                  Not sure why LLVMCreateExecutionEngineForMo<wbr>dule
                  is failing.<br>
                </div>
                Any help appreciated.<br>
                <br>
              </div>
              Toshi<br>
              <br>
            </div>
            <div class="HOEnZb">
              <div class="h5">
                <div class="gmail_extra"><br>
                  <div class="gmail_quote">On Wed, Mar 8, 2017 at 3:56
                    PM, Toshiyasu Morita <span dir="ltr"><<a
                        moz-do-not-send="true"
                        href="mailto:toshi@tensyr.com" target="_blank">toshi@tensyr.com</a>></span>
                    wrote:<br>
                    <blockquote class="gmail_quote" style="margin:0 0 0
                      .8ex;border-left:1px #ccc solid;padding-left:1ex">
                      <div dir="ltr"><span><span
                            class="m_4582426381616258799m_-9171418793383708215gmail-im">
                            <blockquote type="cite">
                              <div dir="ltr">
                                <div>Or do you mean I need to load the
                                  module into memory before calling
                                  LLVMGetBitcodeModuleInContext2<wbr>?<br>
                                </div>
                              </div>
                            </blockquote>
                            <br>
                          </span> > Yes, you need to load the module
                          into memory first.<br>
                          > LLVMCreateMemoryBufferWithCont<wbr>entsOfFile
                          will do that for you.</span>
                        <div
                          class="m_4582426381616258799m_-9171418793383708215gmail-yj6qo
m_4582426381616258799m_-9171418793383708215gmail-ajU">
                          <div
                            id="m_4582426381616258799m_-9171418793383708215gmail-:z5"
class="m_4582426381616258799m_-9171418793383708215gmail-ajR"><img
                              moz-do-not-send="true"
                              class="m_4582426381616258799m_-9171418793383708215gmail-ajT"
src="https://ssl.gstatic.com/ui/v1/icons/mail/images/cleardot.gif"><br>
                          </div>
                          <div
                            id="m_4582426381616258799m_-9171418793383708215gmail-:z5"
class="m_4582426381616258799m_-9171418793383708215gmail-ajR">Thanks!<br>
                            <br>
                          </div>
                        </div>
                      </div>
                      <div class="m_4582426381616258799HOEnZb">
                        <div class="m_4582426381616258799h5">
                          <div class="gmail_extra"><br>
                            <div class="gmail_quote">On Wed, Mar 8, 2017
                              at 3:48 PM, Friedman, Eli <span dir="ltr"><<a
                                  moz-do-not-send="true"
                                  href="mailto:efriedma@codeaurora.org"
                                  target="_blank">efriedma@codeaurora.org</a>></span>
                              wrote:<br>
                              <blockquote class="gmail_quote"
                                style="margin:0 0 0 .8ex;border-left:1px
                                #ccc solid;padding-left:1ex">
                                <div bgcolor="#FFFFFF" text="#000000"><span>
                                    <div
class="m_4582426381616258799m_-9171418793383708215m_3832703634050351426moz-cite-prefix">On
                                      3/8/2017 3:44 PM, Toshiyasu Morita
                                      wrote:<br>
                                    </div>
                                    <blockquote type="cite">
                                      <div dir="ltr">
                                        <div>
                                          <div>
                                            <div>
                                              <div><span
class="m_4582426381616258799m_-9171418793383708215m_3832703634050351426gmail-im">
                                                  <blockquote
                                                    class="gmail_quote"
                                                    style="margin:0px
                                                    0px 0px
                                                    0.8ex;border-left:1px
                                                    solid
                                                    rgb(204,204,204);padding-left:1ex"><br>
                                                            module_path
                                                    =
                                                    LLVMCreateMemoryBufferWithMemo<wbr>ryRange(path,
                                                    strlen(path),
                                                    "path", 1);<br>
                                                  </blockquote>
                                                  <br>
                                                </span>
                                                LLVMCreateMemoryBufferWithCont<wbr>entsOfFile
                                                takes a path.<br>
                                              </div>
                                              <br>
                                            </div>
                                            Erm...no...the code is
                                            calling
                                            LLVMCreateMemoryBufferWithMemo<wbr>ryRange,
                                            not
                                            LLVMCreateMemoryBufferWithCont<wbr>entsOfFile...<br>
                                            <br>
                                          </div>
                                          Or do you mean I need to load
                                          the module into memory before
                                          calling
                                          LLVMGetBitcodeModuleInContext2<wbr>?<br>
                                        </div>
                                      </div>
                                    </blockquote>
                                    <br>
                                  </span> Yes, you need to load the
                                  module into memory first. 
                                  LLVMCreateMemoryBufferWithCont<wbr>entsOfFile
                                  will do that for you.<span><br>
                                    <br>
                                    -Eli<br>
                                    <pre class="m_4582426381616258799m_-9171418793383708215m_3832703634050351426moz-signature" cols="72">-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
                                  </span></div>
                              </blockquote>
                            </div>
                            <br>
                          </div>
                        </div>
                      </div>
                    </blockquote>
                  </div>
                  <br>
                </div>
              </div>
            </div>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
    <p><br>
    </p>
    <pre class="moz-signature" cols="72">-- 
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project</pre>
  </body>
</html>