Operating System - HP-UX
1841417 Members
3478 Online
110181 Solutions
New Discussion

Re: Creating a shared object

 
SOLVED
Go to solution
devshlom
Regular Advisor

Creating a shared object

Hi,
I have to create a shared object (so) on HP-UX.
I already have a Makefile that does it for all the other unix platforms (aix, linux, solaris)
1. what should to make it work on HP?
2. can one of you give me a working and small example for a Makefile that creates a shared object?

tx,
shlom
29 REPLIES 29
Fabien GUTIERREZ
Frequent Advisor

Re: Creating a shared object

on pa-risc shared library are designed as .sl files and on integrity servers as .so files
you can refer to this external link to find examples of compile and linking options

http://www.franz.com/support/documentation/current/doc/foreign-functions.htm#ff-on-hp-1
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

On HP-UX these are called shared libraries.
Here is the Linker Online Help:
http://docs.hp.com/en/11782/LinkerOnliinehelp0709/creatingandusinglibraries.htm

Creating shlibs is pretty simple:
libfoo.so: foo.o
$(CC) -b -o $@ foo.o

foo.o: foo.c
$(CC) -c foo.c

If you are on PA, change to ".sl" and add +z to the compile line.
devshlom
Regular Advisor

Re: Creating a shared object

hi dennis,
tx again for your answers.

Sorry for the beginner questions - I still new to this platforms...
1. how do I know if I'm working on PA? when I run uname -a I get: "HP-UX hpux B.11.23 U 9000/800 3697136004 unlimited-user license"

2. When I add +Z to the compile command I get: "gcc: +Z: No such file or directory"

3. In your example:
- you put "-b" what this option stands for?
- should I used also "-shared"?

As you see, I'm a little confused here - if you make it simple for me I will be grateful.

my gcc is: (gcc -v)
Using built-in specs.
Target: hppa2.0w-hp-hpux11.23
Configured with: ../gcc/configure
Thread model: posix
gcc version 4.2.2


tx!!!
shlom
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>1. how do I know if I'm working on PA? when I run uname -a I get: "HP-UX hpux B.11.23 U 9000/800

This is PA. Otherwise it says ia64 for Integrity.

>2. When I add +Z to the compile command I get: "gcc: +Z: No such file or directory"
>my gcc is:

Ah, you are using a foreign devil compiler.
You need -fpic.

>- you put "-b" what this option stands for?
>- should I used also "-shared"?

-b is the linker and HP's compiler option to create shlibs.
Yes, replace by -shared for gcc.
devshlom
Regular Advisor

Re: Creating a shared object

so,
after we both know what I'm working on,pls see the reason I got confused
when I use the following:
"$(CC) -shared -fpic -o $@ $(OBJ) ...", I get this weird msg:
/usr/ccs/bin/ld: CODE_ONE_SYM fixup to non-code subspace in file /home/s/import/so/TestSo/TestSo/debug/HPUX-Risc/test.o - shared library must be position independent. Use +z or +Z to recompile.
collect2: ld returned 1 exit status

so? how can I prevent this?

tx
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>ld: CODE_ONE_SYM fixup to non-code subspace in file test.o - shared library must be position independent. Use +z or +Z to recompile.
>how can I prevent this?

Was test.o compiled with -fpic?
devshlom
Regular Advisor

Re: Creating a shared object

tx dennis,


yes - the object did compile with the -fpic!!!
1. I succeeded create the sample so (the small one) - but the big one still doesn't work (even with the -fPIC)

2. another question:
after knowing how to generate a shared object, what is the standard for its extension? should I use ".sl" instead of ".so"

tx
Dennis Handly
Acclaimed Contributor
Solution

Re: Creating a shared object

>yes - the object did compile with the -fpic!!

That's not what the linker says. So either you didn't or gcc is broken.
If you want to attach a copy of test.o, I can look at it.

>1. but the big one still doesn't work

The above linker error? Or something else?

>2. what is the standard for its extension? should I use ".sl" instead of ".so"

On PA you use .sl. On IPF you use .sl.
devshlom
Regular Advisor

Re: Creating a shared object

Hi again,

1. After I added -fpic to both compilation and linkage the test shared object was created!!!

2. The big program that I mentioned before is my real shared object (not the tester)that I have to create - here is the error I get:
/usr/ccs/bin/ld: Invalid loader fixup in text space needed in output file for symbol "CClassName::~CClassName()" in input file "/home/s/lib_somelib.a(Someobject.o)"
collect2: ld returned 1 exit status
this is the first time I get such error - any idea? is it something to do with the extenal lib that is linked with the shared object? any idea?

3. when I run file on the tester shared object I get the following result:
"libtest.so:PA-RISC2.0 shared library -not stripped" - I think it's ok - what do say?

4. according to what you said before, I will have to change the extesion to "sl" - don't I?


tx a lot!
shlom
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>ld: Invalid loader fixup in text space needed in output file for symbol "CClassName::~CClassName()" in Someobject.o

This the same missing PIC option or g++ is broken.

>is it something to do with the external lib that is linked with the shared object?

Probably not.

>libtest.so: PA-RISC2.0 shared library
> I think it's ok

Yes, this is fine

>4. I will have to change the extension to "sl" - don't I?

You don't "have" to but it is a good idea.
Otherwise ld won't find it with -ltest without extra work.
devshlom
Regular Advisor

Re: Creating a shared object

hi,

about the PIC/pic:
-------------------
is there any difference between "-fPIC" to "-fpic"?
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>is there any difference between "-fPIC" to "-fpic"?

There is a difference between +z and +Z, it is possible that gcc has the same?

Basically +Z is needed for load modules that have lots of symbols. You only need it if the linker runs out of short addressing space for the linkage table.
devshlom
Regular Advisor

Re: Creating a shared object

Hi,

As I mentioned before I succeeded in compiling and linking my simple tester and create the sl.
But when I tried to link it with another lib (archive) I failed unless I add "-fpic" also to this lib compilation.
1. Is this ok?!
2. Does it mean that all the libraries that should be linked with the shared object must be compiled also with "-fpic"?
3. Another fact I not sure of:
when I used GCC_PATH=`gcc -print-file-name=libstdc++.a" I still fail. BUT when GCC_PATH=-lstdc++ it works!

tx in advance!
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>But when I tried to link it with another lib (archive) I failed unless I add "-fpic" also to this lib compilation.
>2. Does it mean that all the libraries that should be linked with the shared lib must be compiled also with "-fpic"?

Yes, ALL objects in a shlib must be compiled as PIC.

>3. Another fact I not sure of: when I used GCC_PATH=`gcc -print-file-name=libstdc++.a" I still fail. BUT when GCC_PATH=-lstdc++ it works!

No clue, What was GCC_PATH set too? Go with what works.
devshlom
Regular Advisor

Re: Creating a shared object

tx again.

so, for using this shared object:

I have object libmytest.sl
I need to create some exe that uses this shared lib.

1. should I put this on /usr/lib/ or there is another standard in HP?

2. should I set LD_LIBRARY_PATH to this location - or there are some equivaent environment variable for the linker to search for shared objects?

devshlom
Regular Advisor

Re: Creating a shared object

and also important:

the link line:

gcc -o tester -ltestmyso -L$(BUILD_DIR) $(OBJ)-lpthread

is this ok??

tx.
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>1. should I put this on /usr/lib/ or there is another standard in HP?

That's the PA HP-UX default for HP's products.
There is no reason you can't put your stuff there. But you could also put your shlib with your executable or some parallel lib/ directory. It may depend on how many executables use that shlib and if they are all in the same directory structure.

>2. should I set LD_LIBRARY_PATH to this location - or there are some equivalent environment variable for the linker to search for shared libs?

For PA32, you need to use SHLIB_PATH for dld to find it. Or you can hardcode the path at link time with -Wl,+b,path.

>the link line: gcc -ltestmyso -L$(BUILD_DIR) $(OBJ)-lpthread
>is this ok??

You need a space between "$(OBJ)" and -lpthread.
And -L should come before its -llibs.
devshlom
Regular Advisor

Re: Creating a shared object

tx again.

finally my sample works well also with external libs!!! so this is a good progress.

now.. back to the real project:
I need to find a way (if exists) to make my so link with no errors with minimal changes => instead of changing all the libs Makefiles (that my slib needs) to use "-fpic"
=> can I put some linker flag in my sl makefile that makes it link well?! (if I don't have a choice, I will so this change only for HPUX but this is the last option)

tx
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>I need to find a way to make my so link with no errors with minimal changes => instead of changing all the libs Makefiles (that my slib needs) to use "-fpic"
>=> can I put some linker flag in my sl makefile that makes it link well?

This is a compile option, not link.
If you were using HP's compiler you could just export CCOPTS="+z" and then do the make.

I don't know if gcc has something similar?
But well designed makefiles allow you to add options on the command lines or have centralized make include files where you can change this all in one place.
devshlom
Regular Advisor

Re: Creating a shared object

Hi,

I just want to be sure of something that was mentioned before:
is "+z" is the the equivalent for "-fPIC" for HPUX compiler?!
=> the "+z" will compiled the code position independent?


tx
devshlom
Regular Advisor

Re: Creating a shared object

and another question:

For all the other platform, I managed to create the shared lib without using the "-fpic" - only for HP-UX this was the only thing that fix the "invalis fix loader..." link error I got.

Do you have any clue why it worked for me for all the other unix platforms (with PIC) and here I'm required?? (is it something I missing here - or just a different and acceptable behavior?!)

tx,
shlom
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>is "+z" is the the equivalent for "-fPIC" for HP-UX compiler?! the "+z" will compiled the code position independent?

I'm not sure which is +z and which is +Z. Try "-fpic" first.

>I managed to create the shared lib without using the "-fpic" - only for HP-UX this was ...
>Do you have any clue why it worked for me for all the other unix platforms (with PIC) and here I'm required??

Not sure. On IPF, +z/+Z is obsolete and the default is PIC. But for gcc is may not be.

>just a different and acceptable behavior?

Naturally. :-)
Non-PIC code is faster and better for executables.
devshlom
Regular Advisor

Re: Creating a shared object

I had a mistake on the second question - here is the correct one:
I'm trying to understand how I managed to create shared lib successfully WITHOUT "-fpic" in all the other UNIX platforms.
Actually I need to know:
1.Why only the HP-UX enforce me using the PIC?
2. For my knoladge, is there any way to create a shared lib in HPUX without compiling position independent objects at all?!

tx
Dennis Handly
Acclaimed Contributor

Re: Creating a shared object

>1.Why only the HP-UX enforce me using the PIC?

The IPF HP-UX default is PIC, probably because of this. But I believe IPF Linux also defaults to non-PIC.
The HP-UX PA32 decision was made over 23+ years when there weren't shlibs on HP-UX.

>2. is there any way to create a shared lib in HP-UX without compiling position independent objects at all?

Of course not. PIC is required by shlibs. The whole purpose of a shlib is to map it in memory at any location and be used by many executables, both which needs PIC.