Murga-Projects Forums

Full Version: Alien is complicated
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm still trying to get my head around Alien...looks like something that could open some pretty huge doors. I got a funny error message at one point when fooling around:

Code:
gtkMsgBox.lua:11: bad argument #1 to 'tostring' (userdata expected, got userdata)


One of my systems has Gtk2 installed in /opt, so the alien.load function in the gtk test doesn't work since it uses a full path to the file. Removing "/usr/lib" (using just the name of the library) fixed it.

I've been trying to find out if alien can supply a list of functions available in a given library, but haven't had much luck so far. When making changes to the gtk test I eventually got something, but it doesn't look like gtk functions. I'm just poking at it for now.

Code:
local gtk,p,i=alien.load("libgtk-x11-2.0.so.0"),"pointer","int"
gtk_tab=getmetatable(gtk)
for i,v in pairs(gtk_tab) do
gtk_tab2=getmetatable(i) end
for i,v in pairs(gtk_tab2.__index) do print(i,v,type(v)) end

The result:

Code:
sub     function: 0x8262ef8     function
upper   function: 0x8262f28     function
len     function: 0x8262df0     function
gfind   function: 0x8262d80     function
rep     function: 0x8262e90     function
find    function: 0x8262cd8     function
match   function: 0x8262e58     function
char    function: 0x8260878     function
dump    function: 0x8262ca0     function
gmatch  function: 0x8262d80     function
reverse function: 0x8262ec0     function
byte    function: 0x8260840     function
format  function: 0x8262d10     function
gsub    function: 0x8262db8     function
lower   function: 0x8262e20     function

That looks like functions in string rather than gtk

I don't know what this supposed to do, but after loading en getting the metatable, it seems there are only functions in the metatable.
Then you take the metatable of all keys which is bound to give the string table, since they are all strings, its gtk2_tab["__index"] for instance.
So i'm not really that surprised.

Now the first error message is a funny one, reminds me a lot of windows Tongue
perhaps you pass it a wrong type of userdata?

greetz,

Jan-Pieter

mikshaw Wrote:
I'm still trying to get my head around Alien...looks like something that could open some pretty huge doors. I got a funny error message at one point when fooling around:

Code:
gtkMsgBox.lua:11: bad argument #1 to 'tostring' (userdata expected, got userdata)


It is not really uncommon that error messages are not very descriptive. Most likely you made an type error or used it wrong.

mikshaw Wrote:
One of my systems has Gtk2 installed in /opt, so the alien.load function in the gtk test doesn't work since it uses a full path to the file. Removing "/usr/lib" (using just the name of the library) fixed it.

Using full paths was never a good idea.

mikshaw Wrote:
I've been trying to find out if alien can supply a list of functions available in a given library, but haven't had much luck so far. When making changes to the gtk test I eventually got something, but it doesn't look like gtk functions. I'm just poking at it for now.

Code:
local gtk,p,i=alien.load("libgtk-x11-2.0.so.0"),"pointer","int"
gtk_tab=getmetatable(gtk)
for i,v in pairs(gtk_tab) do
gtk_tab2=getmetatable(i) end
for i,v in pairs(gtk_tab2.__index) do print(i,v,type(v)) end

This is not possible. So you can stop trying ;-)

I think you a wrong idea how the dynamic loader works. You could compare the binary format (ELF) with a small database, with multiple indexes. When you start a program the kernel creates a new address space and the loader maps the program into that address space (actually the program and the libraries are not loaded, they are only mapped. Only when a page of memory has to be accessed then it gets loaded from the disk. This is called demand paging. Also multiple programs and libraries that reference the same files on disk share the same physical memory pages. A system can therefore load hundreds or thousands of huge programs with a ton of dependencies. But since most of the programs have the same dependencies and not all functions are always called, the actual physical memory consumption could be pretty low) The loader/linker then parses the ELF headers for external symbols which have to be resolved. It also parses a section with a list of external libraries it has to check. The linker then checks the external libraries for the needed symbols and maps them (the libraries) also into the address space of the program. After the libraries have been mapped into the address space, the linker knows the position (memory address) of the symbols and can replace the stubs in the program with the actual address.

This is called relocation.

Of course only the symbols that are used get relocated. So a program that links against a library doesn't know about the other symbols. For that it has to parse the library headers.

In this case it is even a little bit more complicated. murgaLua isn't linked against libgtk.so. At startup time the linker/loader doesn't even know about the libgtk dependency. In this case alien uses a special functionality of the loader. The dlopen, dlsym functions of the loader. It allows a program to load a library (the loader just maps it into the address space) and then search for a symbol in that library (dlsym takes a string of the symbol name and returns just the memory address of that symbol). This is what alien uses when you use a C function from a library.

While gtk=alien.load("libgtk.so"); gtk_window_new=gtk.gtk_window_new would suggest that all function names are in a gtk table, this isn't the case. alien just uses the __index method in the metatable to fake such a table. It just uses the string of the entry name to look up the symbol with dlsym.

If you want to have a list of all the exported symbols from a library you can use the nm or readelf tools that come with the GNU binutils:

Code:
[nm -g -D /usr/lib/libgtk.so  | grep '^[0-9A-F]\{8\}\ T\ .*$'

or

Code:
readelf -s /usr/lib/libgtk.so  | grep -v UND


Or you can read the GTK documentation ;-)

Juergen

You're very right that I don't understand dynamic libraries. The truth is I don't understand binaries in general. All I know is there are binaries such as Lua and Bash that read what I write and hopefully execute the lines as useful commands =o)

Thank you for taking the time to explain a bit.

I started looking through the Xlib documentation yesterday...so far haven't done anything with it, but I'm hoping I'll gradually pick up a little useful knowledge here and there. I'm not a programmer, so like with Lua I'm taking the approach "what's this do?...poke".
Reference URL's