For a binary with embedded bitcode (i.e., compiled with -fembed-bitcode
). How can i just extract that section such that I can use that bitcode file like any other bitcode file e.g, run opt
or llvm-dis
As a testcase i have this hello-world program:
// hello.cpp
#include<iostream>
int main() {
std::cout << "hello world";
return 0;
}
compiled with: clang++ -O2 test.cpp -o test.o -fembed-bitcode -c
the objdump
shows there is a __bitcode section in there:
objdump -h test.o
test.o: file format Mach-O 64-bit x86-64
Sections:
Idx Name Size Address Type
0 __text 000002eb 0000000000000000 TEXT
1 __gcc_except_tab 00000068 00000000000002ec DATA
2 __cstring 0000000c 0000000000000354 DATA
3 __bitcode 00002bc0 0000000000000360 DATA
4 __cmdline 00000046 0000000000002f20 DATA
5 __compact_unwind 00000060 0000000000002f68 DATA
6 __eh_frame 000000d0 0000000000002fc8 DATA
Now as per https://github.com/llvm/llvm-project/blob/master/llvm/test/tools/llvm-objcopy/MachO/dump-section.test, I'm trying to extract the __bitcode section in the following way:
# Take1
./bin/llvm-objcopy --dump-section=DATA,__bitcode=a.bc test.o
./bin/llvm-objcopy: error: 'test.o': section 'DATA,__bitcode' not found
# Take2
./bin/llvm-objcopy --dump-section=__bitcode=a.bc test.o
./bin/llvm-objcopy: error: 'test.o': section '__bitcode' not found
# Take3
./bin/llvm-objcopy --dump-section __DATA,__bitcode=a.bc test.o
./bin/llvm-objcopy: error: 'test.o': section '__DATA,__bitcode' not found
# Take4
./bin/llvm-objcopy --dump-section=__DATA,__bitcode=a.bc test.o
./bin/llvm-objcopy: error: 'test.o': section '__DATA,__bitcode' not found
What am I missing here?
Actually the __bitcode
is in LLVM
section. so the following commands worked.
llvm-objcopy --dump-section=__LLVM,__bitcode=a.bc test.o
segedit test.o -extract __LLVM __bitcode a.bc
Seems like a bug in objdump
, it doesn't recognize the LLVM section.