Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2005-08

Re: Exporting from Elf okay?

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

L505

[moved to devel]

|
| With regards to gnu linux:
| How about the Elf format?
| Is it possible to export code from an elf? (i.e. export functions) |

Okay, I researched the topic. Of course, yes, you can export functions from an executable on linux. But freepascal doesn't seem to like it just yet. It must be done supposedly with the -E option to the linker (ld) or -export-dynamic (gcc is -rdynamic)

This of course, was after modifying the compiler to parse the exports keyword properly
[psub.pas]

else if islibrary or
(target_info.system in [system_i386_linux,system {<<<inserted this}<SNIP>]) then read_exports

So after getting the parsing of the exports keyword set up to work, I tried passing the -E option in freepascal compiler (the -k option as also seen in the lazarus linker options tab)

After compiling a program, with an export it didn't seem to work properly. It compiles okay, but when I run it two writeln('test')'s appear instead of one, and the program quits without an error.

So now the next thing to figure out, is why exports from an executable on linux won't work
even when you pass the -E or export-dynamic option to LD.

I'm guessing the symbols aren't being placed in the object files correctly for the linker to use, or they aren't being placed you guys have any tips let me know.. I'll be looking into the compiler sources more, probably the sections that write the object files now.

Reply by email

Hans-Peter Diettrich
to L505
L505 wrote 7 lines
| With regards to gnu linux: | How about the Elf format? | Is it possible to export code from an elf? (i.e. export functions) | Okay, I researched the topic. Of course, yes, you can export functions from an executable on linux. But freepascal doesn't seem to like it just yet.

How do you intend to use the exported symbols of an stand-alone executable?
As a shared library, or how else?

Perhaps Linux and Windows executable files are not too different. On Windows the file format (MZ) is the same for DLLs and EXE files, the main difference is the invocation through the WinMain or LibMain entry points. On Linux the shared libraries may have a similar implementation, so that the exported symbols can be used only with shared library modules.

DoDi

Reply by email

L505
to Hans-Peter Diettrich

| > Okay, I researched the topic. Of course, yes, you can export functions from an executable
| > on linux. But freepascal doesn't seem to like it just yet. |
| How do you intend to use the exported symbols of an stand-alone | executable?
| As a shared library, or how else?

A shared library can call functions from the executable that it is running from. Two way communication is very important to me.

| Perhaps Linux and Windows executable files are not too different. On

They aren't really, is what I found, after reseaching them.

| Windows the file format (MZ) is the same for DLLs and EXE files, the | main difference is the invocation through the WinMain or LibMain entry | points. On Linux the shared libraries may have a similar implementation, | so that the exported symbols can be used only with shared library | modules.

Functions can be used in executables in linux by calling the -export-dynamic or -E with the linker (LD), but it isn't working yet with freepascal. Works with GCC (also called -rdynamic).

In windows, you can either get the handle of the exe using getmodule, and then getprocaddress(exehandle, 'myproc')or you can just call statically procedure myproc; external 'myprogram.exe'; (instead of mylibrary.dll ).

I asked some GCC guys "can an elf executable export functions?" and they sort of laughed at me. They do it all the time, and they wondered why I was asking such an obvious question. But I honestly think a lot of people don't know you can do this.

Without being able to export functions form an executable, I find it's sort of like having a client without a server. Why have a client without a server? To me, it is essential for the executable to export functions.. (this is where the true power can be squeezed out of plug-in systems - Lazarus RB Edition relies on this and would never work without it...unless I used sockets, interprocess communication, or something similar.)

Lars

Reply by email

Peter Vreman
to L505
you wrote 48 lines
| > Okay, I researched the topic. Of course, yes, you can export functions from an executable | > on linux. But freepascal doesn't seem to like it just yet. | | How do you intend to use the exported symbols of an stand-alone | executable? | As a shared library, or how else? A shared library can call functions from the executable that it is running from. Two way communication is very important to me. | Perhaps Linux and Windows executable files are not too different. On They aren't really, is what I found, after reseaching them. | Windows the file format (MZ) is the same for DLLs and EXE files, the | main difference is the invocation through the WinMain or LibMain entry | points. On Linux the shared libraries may have a similar implementation, | so that the exported symbols can be used only with shared library | modules. Functions can be used in executables in linux by calling the -export-dynamic or -E with the linker (LD), but it isn't working yet with freepascal. Works with GCC (also called -rdynamic). In windows, you can either get the handle of the exe using getmodule, and then getprocaddress(exehandle, 'myproc')or you can just call statically procedure myproc; external 'myprogram.exe'; (instead of mylibrary.dll ). I asked some GCC guys "can an elf executable export functions?" and they sort of laughed at me. They do it all the time, and they wondered why I was asking such an obvious question. But I honestly think a lot of people don't know you can do this. Without being able to export functions form an executable, I find it's sort of like having a client without a server. Why have a client without a server? To me, it is essential for the executable to export functions.. (this is where the true power can be squeezed out of plug-in systems - Lazarus RB Edition relies on this and would never work without it...unless I used sockets, interprocess communication, or something similar.)

If gcc can do it also fpc can be made to do it. But for me it has no priority to add support for this to fpc. Ofcourse patches are welcome.

Peter

Reply by email

L505
to Peter Vreman

I have found a large PDF file to study which contains a lot of information about Elf and object file format. Hope to get my feet wet. Have also downloaded the Linker sources to see what exactly it does differently when it receives the -E option.

If gcc can do it also fpc can be made to do it. But for me it has no priority to add support for this to fpc. Ofcourse patches are welcome.

Will work on one. I think it is an important feature maybe in plug-in situations specifically. But a lot of programs have plug in systems these days (and maybe not as well thought out ones, who don't utilize this trick). Not sure what else people use this feature for other than plug-in style situations - might ask the GCC crowd if I'm brave enough.

Reply by email

L505
Show quoted text 6 lines
> > I implementing exporting from programs in svn trunk. Did someone test? are you trying to implement it now?

nevermind, I see SVN log on August 13

testing and looking now

Reply by email

L505
to Florian Klaempfl: I implementing exporting from programs in svn trunk. Did someone test?
I implementing exporting from programs in svn trunk. Did someone test?

Okay, I envy you Florian. But we all do.

From the minimal tests I have done - it works just as I had explained in the original mail :)

The big test will be when I compile the big ugly and beautiful Lazarus RB beast on linux.

I have to familiarize myself with the syntax. You used "public" keyword you after the functions. That is required you think, in order for this to work on linux? And the {$linklib } directive is needed in all cases? Or just the way you chose to try it?

From looking at your SVN mods, it appears part of the tricks in getting this to work was to make

sure certain things weren't stripped from the library? (which could have taken me months to just halfway figure out)

Regards
Lars

Reply by email