Need help again: ld and shared library problem ...

Started by James Ladd, June 02, 2006, 01:35:35 AM

Previous topic - Next topic

James Ladd

Please help ...

Just when I think I have made a step forward, there is one backwards....
:'(
Im trying to build a shared library and have it lused under linux.
Currently im getting the following warning:
ld: warning: type and size of dynamic symbol `_myfunc' are not defined

Can I ignore this warning ?

Can someone help me with an example in Gnu Assembler of a simple shared library
being successfully built and linked and the executable running ok with the lirbary ?

The files I have so far are attched, but the commands Im using to make and link
are:

as -g -o simple.o simple.s
as -g -o test.o test.s
ld --export-dynamic -e _DllMain -shared -o libsimple.so simple.o
ld -e _start -dynamic-linker /lib/ld-linux.so.2 -o test test.o -lsimple


In using linux in the above and cygwin. However on cygwin it complains about not
being able to find -lsimple.so

Please help ...

Rgs, James.



[attachment deleted by admin]

MichaelW

I don't know if this is related to your immediate problem, but a shared library apparently requires "position-independent code generation." For gcc you use the -fPIC or -fpic flags:

http://www.tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

For gas there is the .symver directive, but I suspect that there may be more  to it, like structural requirements, special labels, etc.

http://www.gnu.org/software/binutils/manual/gas-2.9.1/html_mono/as.html#SEC129

I did not download it, but there is apparently a Forth kernel implemented as a shared library, in a modified version of NASM, available with source here:

http://asm.sourceforge.net/resources.html

And there are several GAS projects, perhaps one that implements a shared library.

eschew obfuscation

James Ladd

MichaelW,

Thanks, I have read all those links but Ill check them again and keep at it.
I hope I am just doing something silly.

Anyone else have some ideas on solving the problem ?

Rgs,James.

arafel

Quote from: James Ladd on June 02, 2006, 01:35:35 AM
However on cygwin it complains about not
being able to find -lsimple.so

James,

You can't create linux executables (whenever its linked against static or shared libs) under cygwin. Cygwin`s ld produces pe-i386 objects. Hence the "-lsimple" error. ld tries to link against nonexistent libsimple.dll/libsimple.a.

Maybe recompiling binutils with support for elf targets will solve this....

James Ladd

arafel,

Im not concerning myself with the cygwin issues right now, just the linux issues.
I have linked a sharable library with the following options without error:

ld --export-dynamic -e _DllMain -soname=libsimple.so -Bdynamic -Bshareable -Bsymbolic -o libsimple.so simple.o

Now the problem I have is linking the main executable with this sharable library.
The command line im using is:

ld -o test test.o -L. -llibsimple.so

The error im getting is:

ld: cannot find -llibsimple.so

Any ideas? Anyone ?

I was expecting -L. to tell the linker to look in the current folder for the so.

Rgs, James.

Update
I also found this info, which im working through now. It tells you about ldconfig and
how libraries are found.
http://jrv.oddones.org/lib.html

Update
I think I have got the linking to work by changing the command line to the following:
ld -o test test.o -L. -lsimple

I havent verified that all is working yet as I dont have access to a debugger where im doing this.
However I dont suspect it does work as I can't execute the file in the current folder.
There is a 'test' in /usr/bin so I linked the file as 'foo' and tried to execute it with
./foo and I get a no such file or directory from 'ksh'
ksh: ./foo: No such file or directory

Ah, this jsut gets wierder.



arafel

ld automatically appends "lib" suffix and either ".so" or ".a" prefix to the name specified after -l switch. So if the library called libsimple.so it should be simply specified as -lsimple

Also by default ld will link against libc.so.1 which, afaik, is obsolete. --dynamic-linker /lib/ld-linux.so.2 should be specified instead.

Also exported symbols must have type definition. eg:
.global _myfunc
.type _myfunc,@function



No need to complicate things with ldconfig. Just copy the library into /lib or /usr/lib.


Try this:
as -g -o simple.o simple.s
as -g -o test.o test.s
ld -Bshareable -o libsimple.so simple.o
ld -Bdynamic --dynamic-linker /lib/ld-linux.so.2 -e _start -o test test.o -L. -lsimple
cp libsimple.so /usr/lib/libsimple.so
./test



James Ladd

#6
Arafel,

Thanks for the response. I'll try this at some point today and let you know how I go.
On the surface it looks spot on.

I'll need to fool around with LD_LIBRARY_PATH as I dont have permissions currently to
write to /usr/lib

Thanks again.

Rgs, James.

Update: I have tried the suggestion and its 'gold', thanks.
Im getting the following but this is easy to handle and its because I havent done the copy to
/usr/lib or played with LD_LIBRARY_PATH, but I can fix this.

./test: error while loading shared libraries: libsimple.so: cannot open shared o
bject file: No such file or directory


Thanks again.

James Ladd

#7
Dang    :(

It would seem that the Gnu Assembler ported to cygwin doesnt support the .type option.

.global _myfunc
.type _myfunc,@function


The version of Gnu Assembler is:

GNU assembler 2.16.91 20050610
Copyright 2005 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License.  This program has absolutely no warranty.
This assembler was configured for a target of `i686-pc-cygwin'.


I can work around it but its not as 'clean' as I would have liked.

added
Attached is the code that compiles and works for both Win32 and Linux.
Comments welcomed.

Does anyone else find this useful ?

[attachment deleted by admin]