[LLVMdev] Primer with LLVM

Francisco Puentes fpuentes at udc.es
Fri Dec 31 05:30:00 PST 2004


Hi again, and thanks (Reid) for your fast response:

Yes, it works!!! Only changing the order of libraries in the Makefile.

Nowaday I have my software with the capability of compile assembly, bytecode
(from buffer and file) and link them with a set of libraries. It seems to
work perfectly (I don't generate code yet).

My real aim is to have a process (host) with execute several no-jit binaries
(guest) each one in his own thread (not forked!! each one with a main
function). Guest and host have interdependencies in both ways. This is a
part of my doctoral degree.

Before now, I was using TCC (Tiny C Compiler), but it have a big lack: it
isn't reentrant. It can only have one generated program at time. For this
reason I agree to use LLVM (two days ago) and rebuild all the project. 8-|

LLVM Makefile systen is great!! But I have no time to change nowadays all my
current Makefiles. Maybe later.

I have noticed that LLVM have memory laks; exactly a poor main with a simple
"return 0;" linked with the LLVM libraries report memory laks using
valgrind. Is it normal?

Now I have other problem: I have a Module and I need generate a iostream
(memory) with native x86 code (maybe elf/coff) to be executed later (into
the guest process space, without fork!!). I studied llc and lli, but they
don't help me much. Any idea? Are there any guy working in some like that?

A sample code will be greatfully :-)

-----Mensaje original-----
De: llvmdev-bounces at cs.uiuc.edu [mailto:llvmdev-bounces at cs.uiuc.edu] En
nombre de Reid Spencer
Enviado el: jueves, 30 de diciembre de 2004 21:47
Para: LLVM Developers Mailing List
Asunto: Re: [LLVMdev] Primer with LLVM

On Thu, 2004-12-30 at 11:14, Francisco Puentes wrote:
> Hi, everybody:
> 
Hi Francisco

> 
> I am a beginner with LLVM, in fact today was the first day that I use it.

Welcome!

> 
> I have several questions about LLVM:

If you haven't already, a good place to start is the Getting Started
Guide, at http://llvm.cs.uiuc.edu/docs/GettingStarted.html 

> Can I use LLVM to compile several files (bytecode), scripts (char*) and
link
> them with external libraries generating *only* one executable (all in
> memory)?

I'm not exactly sure what you're trying to do, but I believe you can do
what you want. The llvm-link tool will link together multiple bytecode
files. lli will generate code in memory (JIT compilation) and execute
it. Since the bulk of the implementation of these tools is in the LLVM
libraries, you can create your own process to do the same sorts of
things. 

I'm not quite sure what you mean by "scripts (char*)" but if you mean
LLVM assembly then this is possible to. The ParseAssemblyFile function
(include/llvm/Assembly/Parser.h) can be used to turn an LLVM assembly
into a Module* which is then suitable for linking via the linker
interface (include/llvm/Linker.h).

> 
> Can I invoke externals functions from a guest (LLVM generated) code which
> exist in the host code (the code that execute the first one)?

Yes. The lli interpreter/JIT compiler will resolve symbols within its
own executable or dynamically via a loadable plug-in (-load option).
Some platforms don't support this currently (Cygwin for example).

> 
> And a problem:
> 
> I made a little aplication with use the functions llvm::LinkModules and
> llvm::LinkLibraries that, I suppouse, exist in libLLVMLinker.a archive,

that is correct.

>  but
> gcc linker reports 'undefined' errors for this functions.

The only thing I can think of is dependencies that libLLVMLinker has.
You will probably want a linke line that looks something like: 

gcc -o myapp myapp.o -lLLVMLinker -lLLVMArchive -lLLVMBCReader \
      -lLLVMBCWriter -lLLVMCore -lLLVMSupport -lLLVMbzip2 -lLLVMSystem

To get examples of these library specifications, look at the llvm-ld and
gccld tools' Makefiles.

>  I don't use the
> makefile system from LLVM because is so complex to incorporate in this
point
> of my work that it isn't usefull at the moment.

That's unfortunate to hear. We've tried to make it as dead simple as
possible. The typical end-user Makefile for LLVM is about 3 or 4 lines
long.  What do you find confusing? Have you read the Makefile Guide?

>  I use the libraries directly
> in my own Makefile. I tried to explicitly insert **all** LLVM libraries
and
> it doesn't work :-(

Order of specification of the libraries on the link command is
important. There are numerous inter-dependencies amongst the libraries
and you need to understand them before this kind of approach will work.
Its unlikely that you would want *all* LLVM libraries in any tool. 

> 
> Does anybody have got a example of an aplication which uses these
functions
> to compile and execute in memory a multi-file application? (lli isn't
useful
> for me)

The closest thing we have is the HowToUseJIT example program. I believe
Duraid Madina is working on something similar. You might want to check
with him (duraid at octopus dot com dot au)

> 
> Thanks in advance and apologize so basic questions.

Glad to help. I hope you find LLVM useful.

Reid




More information about the llvm-dev mailing list