9

i've assumed that dumping a .bc file from a module was a trivial operation, but now, first time i have to actually do it from code, for the life of me i can't find one missing step in the process:

static void WriteModule ( const Module *  M, BitstreamWriter &  Stream )

http://llvm.org/docs/doxygen/html/BitcodeWriter_8cpp.html#a828cec7a8fed9d232556420efef7ae89

to write that module, first i need a BistreamWriter

BitstreamWriter::BitstreamWriter (SmallVectorImpl< char > &O)

http://llvm.org/docs/doxygen/html/classllvm_1_1BitstreamWriter.html

and for a BitstreamWriter i need a SmallVectorImpl. But, what next? Should i write the content of the SmallVectorImpl byte by byte on a file handler myself? is there a llvm api for this? do i need something else?

lurscher
  • 25,930
  • 29
  • 122
  • 185
  • 3
    [C api](http://llvm.org/docs/doxygen/html/BitWriter_8cpp_source.html) provides a simple way to do that. Use it directly or see how it works and do the same. – Piotr Praszmo Dec 16 '12 at 17:40

2 Answers2

15

The WriteModule function is static within lib/Bitcode/Writer/BitcodeWriter.cpp, which means it's not there for outside consumption (you can't even access it).

The same file has another function, however, called WriteBitcodeToFile, with this interface:

/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
void llvm::WriteBitcodeToFile(const Module *M, raw_ostream &Out);

I can't imagine a more convenient interface. The header file declaring it is ./include/llvm/Bitcode/ReaderWriter.h, by the way.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
0

I use following code :

std::error_code EC;
llvm::raw_fd_ostream OS("module", EC, llvm::sys::fs::F_None);
WriteBitcodeToFile(pBiFModule, OS);
OS.flush();

and then disassemble using llvm-dis.

voidMainReturn
  • 3,339
  • 6
  • 38
  • 66