Main Function not Found in the Bitcode file generated by compiling haskell with LLVM - haskell

I want to run klee on the .bc file generated by compiling haskell file with ghc frontend and llvm backend.
I have following code in my haskell hello.hs file:
main = putStrLn "Hello World!"
I compile hello.hs with ghc using following command
ghc -fllvm -keep-llvm-files -force-recomp -hello.hs
which generate a hello.ll file along with other files. I then try to compile this .ll file into a .bc file.
llvm-as hello.ll -o hello.bc
The problem is when I try to run klee or try to run lli on the the .bc file I get following error
main function not found in module error
I am running ghc and llvm on docker. I have version 3.4 of llvm and version 7.6.3 of ghc.

Related

realgcc.exe: error: \build\Graham-Hutton-Haskell-Excersizes\Graham-Hutton-Haskell-Excersizes-tmp\Main.o: No such file or directory

I am trying to set up a simple project so I can learn haskell, well, learn it from the ground up to get a better understanding of the theoretical concepts underpinning it. I do however seem to get this error when trying to build it using cabal.
I was trying to build it and was expecting the default main file for cabal to work.
My file system
module Main where
main :: IO ()
main = putStrLn "Hello, Haskell!"
I am using ghc version 8.10.7
I am running the following commands:
cabal init
cabal build
and then I get the error on the build command/
The full error:
Build profile: -w ghc-9.2.5 -O1
In order, the following will be built (use -v for more details):
Graham-Hutton-Haskell-Excersizes-0.1.0.0 (exe:Graham-Hutton-Haskell-Excersizes) (first run)
Preprocessing executable 'Graham-Hutton-Haskell-Excersizes' for Graham-Hutton-Haskell-Excersizes-0.1.0.0..
Building executable 'Graham-Hutton-Haskell-Excersizes' for Graham-Hutton-Haskell-Excersizes-0.1.0.0..
Linking C:\Users\beren\source\repos\Graham Hutton Haskell Excersizes\dist-newstyle\build\x86_64-windows\ghc-9.2.5\Graham-Hutton-Haskell-Excersizes-0.1.0.0\x\Graham-Hutton-Haskell-Excersizes\build\Graham-Hutton-Haskell-Excersizes\Graham-Hutton-Haskell-Excersizes.exe ...
realgcc.exe: error: C:\Users\beren\source\repos\Graham Hutton Haskell Excersizes\dist-newstyle\build\x86_64-windows\ghc-9.2.5\Graham-Hutton-Haskell-Excersizes-0.1.0.0\x\Graham-Hutton-Haskell-Excersizes\build\Graham-Hutton-Haskell-Excersizes\Graham-Hutton-Haskell-Excersizes-tmp\Main.o: No such file or directory
gcc.exe' failed in phase Linker'. (Exit code: 1)
edit:
https://github.com/bacdekker/haskell-excersizes
Well, after checking/installing/updating gcc it ended up working after I made a new project in a different directory.

Text/Regex/TDFA/NewDFA/Engine.hs:13:33: parse error on input ‘#’

I have been trying to run a simple Haskell program using TDFA. The program is as follows:
import Control.Monad
import Data.Array
import qualified Data.Text as T
import Text.Regex
import Text.Regex.TDFA
import Text.Regex.Base
str = "abbbbaab" :: String
regex = "(a+)(b+)" :: String
main = do
if str (=~) :: regex then putStrLn "matched" else putStrLn "no matches"
when I try to run the above program using the command like: ghc test.hs -o test and then I am getting the following error (test.hs is the Haskell program which contains the above code):
Text/Regex/TDFA/NewDFA/Engine.hs:13:33: parse error on input ‘#’
Note that I have the latest version of TDFA installed on my pc. I did it by following ways:
ghc --make -o setup Setup.hs
Also, I did the following:
user#user-VirtualBox:~/regex-tdfa-master$ sudo cabal install regex-tdfa
[sudo] password for user:
Resolving dependencies...
All the requested packages are already installed:
regex-tdfa-1.2.2
Use --reinstall if you want to reinstall anyway.
I even tried it with the Makefile which I got from TDFA's github repository:
user#user-VirtualBox:~/regex-tdfa-master$ make
ghc -o setup --make ./Setup.hs
./setup clean
cleaning...
./setup configure --prefix=/Users/user/local/devel/trl --enable-library-profiling --user
Configuring regex-tdfa-1.2.2...
./setup build
Building regex-tdfa-1.2.2...
Preprocessing` library regex-tdfa-1.2.2...
[ 1 of 23] Compiling Text.Regex.TDFA.NewDFA.Uncons ( Text/Regex/TDFA/NewDFA/Uncons.hs, dist/build/Text/Regex/TDFA/NewDFA/Uncons.o )
...........(Skipped to make the things short here)..............................
[23 of 23] Compiling Text.Regex.TDFA ( Text/Regex/TDFA.hs, dist/build/Text/Regex/TDFA.o )
Text/Regex/TDFA.hs:60:8:
Could not find module ‘Text.Regex.Base’
Perhaps you haven't installed the profiling libraries for package ‘regex-base-0.93.2#regex_47KXx9dLqeO8MNJeizLKhP’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/ByteString.hs:24:8:
Could not find module ‘Text.Regex.Base.Impl’
Perhaps you haven't installed the profiling libraries for package ‘regex-base-0.93.2#regex_47KXx9dLqeO8MNJeizLKhP’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/CorePattern.hs:37:8:
Could not find module ‘Control.Monad.RWS’
Perhaps you haven't installed the profiling libraries for package ‘mtl-2.2.1#mtl_Aue4leSeVkpKLsfHIV51E8’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/ReadRegex.hs:13:8:
`Could not find module ‘Text.ParserCombinators.Parsec’`
Perhaps you haven't installed the profiling libraries for package ‘parsec-3.1.9#parse_EE5NO1mlYLh4J8mgDEshNv’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/String.hs:23:8:
Could not find module ‘Text.Regex.Base.RegexLike’`
Perhaps you haven't installed the profiling libraries for package ‘regex-base-0.93.2#regex_47KXx9dLqeO8MNJeizLKhP’?
Use -v to see a list of the files searched for.
Text/Regex/TDFA/TDFA.hs:10:8:`
Could not find module ‘Control.Monad.State’
Perhaps you haven't installed the profiling libraries for package ‘mtl-2.2.1#mtl_Aue4leSeVkpKLsfHIV51E8’?
Use -v to see a list of the files searched for.
Makefile:16: recipe for target 'build' failed
make: *** [build] Error 1
user#user-VirtualBox:~/regex-tdfa-master$
However, nothing worked. Hence, any help would be so appreciated...
Text/Regex/TDFA/NewDFA/Engine.hs:13:33: parse error on input ‘#’
On an initial note, this error points to a module of the regex-tdfa package, so you should have gotten it when building the package, rather than when building your test.hs, unless you were trying to build test.hs after putting it within the package source tree. In any case, line 13 of the mentioned file is:
import GHC.Prim(MutableByteArray#,RealWorld,Int#,sizeofMutableByteArray#,unsafeCoerce#)
The # names require the MagicHash GHC extension to be enabled. As there is no {-# LANGUAGE MagicHash #-} pragma at the beginning of the file, one should assume the extension is enabled through the .cabal file of the package, and that is indeed the case. That being so, your problem seems to be that you are attempting use the package modules straight from the source tree, without using an appropriate build tool such as Cabal. (Note that if you did successfully run cabal install regex-tdfa there should be no need of dealing with the source tree: ghc --make -o test test.hs should be enough.)
P.S.: There is an error in your test.hs. The last line should be...
if str =~ regex then putStrLn "matched" else putStrLn "no matches"
(I did manage to run it after this correction.)
It looks like you are compiling your program from within Regex-TDFA source code. I am able to reproduce your problem if I do cabal unpack regex-tdfa && cd regex-tdfa-1.2.2 && ghc --make test.hs.
The error happens because when GHC finds Regex-TDFA source files in the current directory, it just picks them and tries to compile, as if they were ordinary source files belonging to your project. However, Regex-TDFA cannot be built with simple ghc --make: in order to build it, you have to run cabal first. Cabal will read regex-tdfa.cabal file that contains the list of the necessary GHC extensions.
In particular, parse error happens because GHC extension MagicHash is missing (that's where the hash # comes from).
The "fix" is to move your test.hs to some other location: then GHC will use system package for Regex-TDFA.

How to use Text.Parsec in GHC-8.2.2 and Cabal-2.0.0.1

As I know Text.ParserCombinators.Parsec is replaced by Text.Parsec
Here, it is my environment
4.9.73-1-MANJARO
The Glorious Glasgow Haskell Compilation System, version 8.2.2
cabal-install version 2.0.0.1
compiled using version 2.0.1.0 of the Cabal library
The following source code is my main.hs
module Main where
import System.Environment
import Text.Parsec
main :: IO ()
main = do
args <- getArgs
putStrLn (readExpr (args !! 0))
and then I compile it
$ ghc -package parsec -o main main.hs
It occurs the following error messages
[1 of 1] Compiling Main ( main.hs, main.o )
main.hs:3:1: error:
Could not find module ‘Text.Parsec’
There are files missing in the ‘parsec-3.1.11’ package,
try running 'ghc-pkg check'.
Use -v to see a list of the files searched for.
|
3 | import Text.Parsec
| ^^^^^^^^^^^^^^^^^^
rm: cannot remove '*.hi': No such file or directory
./run.sh: line 11: ./TestProj: No such file or directory
I make sure I have installed parsec. So I want to ask any mistake I've done?
$ cabal install parsec
Resolving dependencies...
All the requested packages are already installed:
parsec-3.1.11
Use --reinstall if you want to reinstall anyway.
Manjaro might have inherited from Arch's issues with Haskell.
What is going on
Arch installs dynamic libraries, but ghc links statically by default. This is also what the error message "There are files missing in the ... package", indicating that the package exists but does not contain what ghc is looking for.
If I try compiling with the -v verbose flag on, the error message expands to:
ghc -v main.hs
(...)
Locations searched:
Text/Parsec.hs
Text/Parsec.lhs
Text/Parsec.hsig
Text/Parsec.lhsig
/usr/lib/ghc-8.2.2/site-local/parsec-3.1.11/Text/Parsec.hi
(...)
In particular, look into the last reported location, which may be different on your system; if my guess is correct, ghc is looking for a static interface file .hi as the message indicates but there is only a dynamic one .dyn_hi.
Fix
Compile with the -dynamic flag.
ghc -dynamic main.hs
And if that works read this to fix the setup:
https://wiki.archlinux.org/index.php/Haskell
You will have to choose between static and dynamic linking; I don't actually understand the trade-off here.

LLVM compilation not working with Haskell on Yosemite

I'm trying to use llvm with haskell and I'm getting the error "illegal hardware instruction"
Details:
I'm compiling a simple hello world program just to test it out:
Code:
module Main where
main :: IO ()
main = putStrLn "Hello World"
GHC version 7.10.1, llvm version 3.4.2 (older version because haskell doesn't work at all with 3.6.1).
Command to compile: ghc -O2 --make firstProgram.hs -fllvm -fforce-recomp
Exact error with command ./firstProgram
[1] 23665 illegal hardware instruction ./firstProgram
Thanks for any help!

How do I configure Cabal to understand a C FFI?

I am trying to create a Haskell library which uses a C FFI. I have 4 files:
MyLibrary.hs
c_dependency.h
c_dependency.c
c_dependency.o
test.hs
All on the same directory. test.hs loads MyLibrary and tests it:
-- test.hs
import qualified MyLibrary as Lib
main = Lib.test
I want to be able to compile test.hs as usual, without linking to c_dependency.o. That is, I want ghc test.hs; ./test to work. How can I init/configure cabal so it builds my library properly, taking care of linking c_dependency.o ?
A series of blog posts on Cabal, the FFI, and c2hs has what you're looking for in the second article, the "Compiling the library with Cabal" section.

Resources