[llvm-dev] Parsing a bit code file

Buddhika Chamith Kahawitage Don via llvm-dev llvm-dev at lists.llvm.org
Fri Feb 23 16:16:33 PST 2018


I am trying to parse LLVM IR from a bit code file. I went through the
following steps.

hello.cpp

#include <iostream>
int main() {
  std::cout << "Hello world!" << "\n";
  return 0;}

dump.cpp

#include <llvm/IR/Module.h>#include <llvm/IRReader/IRReader.h>#include
<llvm/IR/LLVMContext.h>#include <llvm/Support/SourceMgr.h>
using namespace llvm;int main(){
  LLVMContext context;
  SMDiagnostic error;
  Module *m = parseIRFile("hello.bc", error, context).get();
  if(m)
  {
    m->dump();
  }

  return 0;}

*$ clang++ -S -O3 -emit-llvm hello.cpp -c -o hello.bc*

*$ clang++ -g -O3 dump.cpp `llvm-config --cxxflags --ldflags --system-libs
--libs all`-o dump*

*$ ./dump*

But I am getting a segmentation fault as given below. What may be causing
this? I am on llvm-6.0 rc2.

> * thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
>   * frame #0: 0x00007fff71902e3e libsystem_kernel.dylib`__pthread_kill + 10
>     frame #1: 0x00007fff71a41150 libsystem_pthread.dylib`pthread_kill + 333
>     frame #2: 0x00007fff7185f312 libsystem_c.dylib`abort + 127
>     frame #3: 0x00007fff71827368 libsystem_c.dylib`__assert_rtn + 320
>     frame #4: 0x0000000100477eb9 dump`llvm::isa_impl_cl<llvm::PointerType,
> llvm::Type const*>::doit(Val=0x0000000000000000) at Casting.h:106
>     frame #5: 0x0000000100477e28
> dump`llvm::isa_impl_wrap<llvm::PointerType, llvm::Type const*, llvm::Type
> const*>::doit(Val=0x00007ffeefbfee20) at Casting.h:133
>     frame #6: 0x0000000100477e02
> dump`llvm::isa_impl_wrap<llvm::PointerType, llvm::Type* const, llvm::Type
> const*>::doit(Val=0x00007ffeefbfee68) at Casting.h:123
>     frame #7: 0x0000000100477db5 dump`bool llvm::isa<llvm::PointerType,
> llvm::Type*>(Val=0x00007ffeefbfee68) at Casting.h:143
>     frame #8: 0x0000000100477d18 dump`llvm::cast_retty<llvm::PointerType,
> llvm::Type*>::ret_type llvm::cast<llvm::PointerType,
> llvm::Type>(Val=0x0000000000000000) at Casting.h:255
>     frame #9: 0x000000010047227d
> dump`llvm::GlobalValue::getType(this=0x00000001013ffff6) const at
> GlobalValue.h:265
>     frame #10: 0x00000001004bad34
> dump`llvm::TypeFinder::run(this=0x00007ffeefbff530, M=0x0000000101401e80,
> onlyNamed=false) at TypeFinder.cpp:38
>     frame #11: 0x00000001001a97c9 dump`(anonymous
> namespace)::TypePrinting::incorporateTypes(this=0x00007ffeefbff530,
> M=0x0000000101401e80) at AsmWriter.cpp:491
>     frame #12: 0x00000001001a964f dump`(anonymous
> namespace)::AssemblyWriter::AssemblyWriter(this=0x00007ffeefbff510,
> o=0x00007ffeefbff3f8, Mac=0x00007ffeefbff438, M=0x0000000101401e80,
> AAW=0x0000000000000000, IsForDebug=true, ShouldPreserveUseListOrder=false)
> at AsmWriter.cpp:2210
>     frame #13: 0x000000010019ce22 dump`(anonymous
> namespace)::AssemblyWriter::AssemblyWriter(this=0x00007ffeefbff510,
> o=0x00007ffeefbff3f8, Mac=0x00007ffeefbff438, M=0x0000000101401e80,
> AAW=0x0000000000000000, IsForDebug=true, ShouldPreserveUseListOrder=false)
> at AsmWriter.cpp:2207
>     frame #14: 0x000000010019e3e3
> dump`llvm::Module::print(this=0x0000000101401e80, ROS=0x00000001007d1a70,
> AAW=0x0000000000000000, ShouldPreserveUseListOrder=false, IsForDebug=true)
> const at AsmWriter.cpp:3415
>     frame #15: 0x00000001001a77e1
> dump`llvm::Module::dump(this=0x0000000101401e80) const at AsmWriter.cpp:3650
>     frame #16: 0x000000010000250e dump`main at dump.cpp:17 [opt]
>     frame #17: 0x00007fff717b3115 libdyld.dylib`start + 1


Thanks

Buddhika
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180223/de752a6b/attachment.html>


More information about the llvm-dev mailing list