generating executable file from .ll files - llvm

I'm writing a front-end for LLVM with java. My front-end produces .ll files. Then I use the following commands to convert these files to an executable file:
1. for each .ll file I use `'llvm-as file.ll'` to create a bitcode file
2. use `'llvm-ld -o executable my-bitcode-files -L/usr/lib/i386-linux-gnu -lstdc++'` to
generate an executable file.
Then when I run the executable file, I get the following error:
LLVM ERROR: Program used external function '_Znwm' which could not be resolved!
What should I do to resolve this issue?

You need to generate native executable, not the IR + wrapper. Try to add -native to llvm-ld cmdline.

Related

NASM and gdbgui [duplicate]

I'm working on a toy bootloader/kernel written in assembly and run on the qemu emulator. I can run qemu with -s -S option and debug with gdb using remote target, but I don't have any debug symbols loaded with gdb. How can I generate a symbol file from my assembly?
I'm using nasm to generate a binary image for qemu to run from my assembly file, but I haven't found anyway to include debug information in the image itself (I'm not sure if that even makes sense). I also found that gdb allows you to load an separate symbol file for debugging, so now my issue is how to generate a symbol file from my assembly code.
I've seen suggestions to use objcopy, but I believe that only works on elf files, not binary. I've tried getting nasm to generate an elf, but it keeps barfing because of my (necessary) org directive in the assembly file.
It would say try it like this:
use "-f elf -F dwarf -g" switches when assembling. This should produce elf file that contains debug symbols (and code and everything else).
Use objcopy to generate binary file.
Load binary file to your system.
Attach debugger, then tell it to load symbols from your .elf file (symbol-file yourfile.elf)
You need to solve why nasm can't generate .elf file with .org you have in there. I have no idea. GNA as is fine with this.
$ nasm -g -f elf64 -l 2.lst 2.asm
$ gcc -m64 -o 2.exe 2.o

Error in opening the compiled module of HDF5

I have a Fortran program with one of the files start with line use HDF5. The program was compiled and run on a particular cluster previously. Now, I am trying to compile the program in a different cluster, which has already hdf5 installed in a particular location.
I assume that the issue is that the compiler could not understand the location of the hdf5 installation directory. I tried specifying hdf5 location by exporting with the LD_LIBRARY_PATH also. Still it does not work. Can someone help me figure what I doing wrong?
The compiler flags also include -lhdf5_fortran and -lhdf5.
UPDATE: The error list on compilation is long. But, the beginning of it looks like this:
lbe_io_hdf5.F90(7): error #7002: Error in opening the compiled module file. Check INCLUDE paths. [HDF5]
use HDF5
------^
lbe_io_hdf5.F90(82): error #6683: A kind type parameter must be a compile-time constant. [HID_T]
integer(hid_t) :: file_id ! File identifier
It appears that I have been trying to locate the hdf5 header in wrong location. Using locate hdf5.h gave me the location of the header file and including the directory using -I solved the issue.
HDF5 comes with a compiler wrapper h5fc for Fortran. For a single program file:
h5fc -o my_program my_program.f90
For separate compilation and linking:
h5fc -c file1.f90
h5fc -c my_program.f90
h5fc -o my_program file1.o my_program.o
If you want to call the compiler directly, check the flags given by
h5fc -show
If there is no h5fc command, it means that you have no Fortran-enabled HDF5 install.

How to make llvm .bc file executable?

I have created a toy language that generates IR code and writes that code to a binary file with WriteBitcodeToFile (the C API). The result is a my-file.bc file.
In this file I have defined a main() function that takes no arguments and returns an int64 (should I change return type to byte maybe). How do I make this .bc file an executable. I'm running Linux.
Fredrik
You can generate an object file with llc and then use GCC to create an executable:
llc -filetype=obj my-file.bc
gcc my-file.o
./a.out
You can read more about llc on http://llvm.org/docs/CommandGuide/llc.html.
It is possible to execute a bc-file with lli command. However that doesn't create a stand alone executable product.
There's always the option of using llc to compile to assembly from which you can generate an executable.
http://llvm.org/docs/CommandGuide/llc.html

How to generate LLVM bitcode for a file using a compilation database?

I want to generate LLVM bitcode for a large number of C source files for which I have a compilation database . Is there way to invoke clang such that it reads the compilation database and uses the appropriate flags?
Background
For toy programs, the command to generate LLVM bitcode is simple:
clang -emit-llvm -c foo.c -o foo.bc
However, source files in large projects require lots of additional compilation flags, including -Is and -Ds and whatnot.
I want to write a script that iterates over a large number of source files and calls clang -emit-llvm ... on each to generate LLVM bitcode. The difficulty is that each clang -emit-llvm ... command has to have the flags specific to that source file. I have a compilation database for these source files, which perfectly captures the flags needed for each individual source file. Is there a way to make clang -emit-llvm ... aware of my compilation database?
One solution I've thought of is to parse the compilation database myself and find the appropriate entry for each source file, and modify the command entry to (a) include -emit-llvm and (b) change -o foo.o to -o foo.bc, and then run the command. This might work, but seems a bit hacky.
Instead of parsing the compilation database yourself, you could rely on the Python binding to do so. Judging from the test suite of the binding, you could do something like:
cdb = CompilationDatabase.fromDirectory(kInputsDir)
cmds = cdb.getAllCompileCommands()
and then slightly update the content of cmds.

How to generate gdb symbol file with nasm?

I'm working on a toy bootloader/kernel written in assembly and run on the qemu emulator. I can run qemu with -s -S option and debug with gdb using remote target, but I don't have any debug symbols loaded with gdb. How can I generate a symbol file from my assembly?
I'm using nasm to generate a binary image for qemu to run from my assembly file, but I haven't found anyway to include debug information in the image itself (I'm not sure if that even makes sense). I also found that gdb allows you to load an separate symbol file for debugging, so now my issue is how to generate a symbol file from my assembly code.
I've seen suggestions to use objcopy, but I believe that only works on elf files, not binary. I've tried getting nasm to generate an elf, but it keeps barfing because of my (necessary) org directive in the assembly file.
It would say try it like this:
use "-f elf -F dwarf -g" switches when assembling. This should produce elf file that contains debug symbols (and code and everything else).
Use objcopy to generate binary file.
Load binary file to your system.
Attach debugger, then tell it to load symbols from your .elf file (symbol-file yourfile.elf)
You need to solve why nasm can't generate .elf file with .org you have in there. I have no idea. GNA as is fine with this.
$ nasm -g -f elf64 -l 2.lst 2.asm
$ gcc -m64 -o 2.exe 2.o