AIX runtime linking

This is a discussion on AIX runtime linking within the aix forums in Operating Systems category; I'm porting an app to AIX 5.2 and it is set up I believe to do run time linking. I can't quite get the link options right on AIX to make it work. I've been reading all the linker documents I can find but no luck - I'd appreciate it if somebody could take a crack at it. Here is a simplified example Module Main is the initial program. It is going to dynamically load a shared library (dynload.so) whose static init routine is going to turn around an call back into main. Here is ...

Go Back   Database Forum > Operating Systems > aix

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 01-01-2007, 09:42 PM
Default AIX runtime linking

I'm porting an app to AIX 5.2 and it is set up I believe to do run time
linking. I can't quite get the link options right on AIX to make it
work. I've been reading all the linker documents I can find but no luck
- I'd appreciate it if somebody could take a crack at it.

Here is a simplified example

Module "Main" is the initial program. It is going to dynamically load a
shared library (dynload.so) whose static init routine is going to turn
around an call back into main. Here is the code for main.

#include
#include
#include

int main()
{
printf ("main.main - enter\n");
dlopen("./dynload.so", RTLD_NOW);
printf ("Main.main - exit\n");
return 1;
}

void callback ()
{
printf ("main.callback - enter\n");
}


Here is the code for dynload:

#include
#include

void callback();

static void __attribute__((constructor)) static_init(void)
{
printf ("dynload.static_init - enter\n");
callback();
printf ("dynload.static_init - exit\n");
}

Here is an example of how I've been trying to link them.

gcc -o main.o -c main.c
gcc -o main main.o -Xlinker -G -Xlinker -bexpall
gcc -o dynload.o -c dynload.c
gcc -o dynload.so dynload.o -Xlinker -G -Xlinker -bexpall


Here is the console outpout. You can see the the dynamic load worked OK
and the static init function had gotten control, but the callback
always fails.

root-at-pserver2 /tmp/tst $ /tmp/tst/main
main.main - enter
dynload.static_init - enter
Illegal instruction (core dumped)

I did and "dump -HTv dynload.so" and I think it looks right - but I'm
no expert in this.

***Loader Symbol Table Information***
[Index] Value Scn IMEX Sclass Type IMPid Name
[30] 0x00000000 undef IMP DS EXTref .. main
[31] 0x00000000 undef IMP DS EXTref ..
callback

Reply With Quote
  #2  
Old 01-01-2007, 10:19 PM
Default Re: AIX runtime linking

dam-at-us.ibm.com writes:

> gcc -o main main.o -Xlinker -G -Xlinker -bexpall


[I can't understand why people keep using non-portable, verbose and
error-prone -Xlinker when the same thing can be portably reduced to
'-Wl,-G,-bexpall']

Changing command line above to:

gcc -o main main.o -Wl,-bexpall,-brtllib

produces:

$ ./main
main.main - enter
dynload.static_init - enter
main.callback - enter
dynload.static_init - exit
Main.main - exit


Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Reply With Quote
  #3  
Old 01-02-2007, 01:25 AM
Default Re: AIX runtime linking

Thanks so much Paul - that did the trick. I didn't realize that
-Xlinker was such bad form - I won't use it anymore. Now my next
problem is that app tries to export a bunch of symbols with leading
underscores and it appears AIX filters those out. I can always add some
#defines to strip off the underscores but if you know of any easier way
(eg. a link option) let me know. Thanks again.


Paul Pluzhnikov wrote:
> dam-at-us.ibm.com writes:
>
> > gcc -o main main.o -Xlinker -G -Xlinker -bexpall

>
> [I can't understand why people keep using non-portable, verbose and
> error-prone -Xlinker when the same thing can be portably reduced to
> '-Wl,-G,-bexpall']
>
> Changing command line above to:
>
> gcc -o main main.o -Wl,-bexpall,-brtllib
>
> produces:
>
> $ ./main
> main.main - enter
> dynload.static_init - enter
> main.callback - enter
> dynload.static_init - exit
> Main.main - exit
>
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


Reply With Quote
  #4  
Old 01-02-2007, 11:23 AM
Default Re: AIX runtime linking

dam-at-us.ibm.com writes:

> Thanks so much


Please do not top-post.

> Now my next
> problem is that app tries to export a bunch of symbols with leading
> underscores and it appears AIX filters those out.
> I can always add some
> #defines to strip off the underscores but if you know of any easier way
> (eg. a link option) let me know.


The "official" way to export symbols is with an export list:

$ cat main.exp
#!main
_callback

$ cat main.c
void _callback ()
{
printf ("main.callback - enter\n");
}

$ gcc main.c -Wl,-bE:main.exp && dump -Tv a.out | grep callback
[17] 0x2000276c .data EXP DS Ldef [noIMid] _callback


Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Reply With Quote
  #5  
Old 01-02-2007, 11:43 AM
Default Re: AIX runtime linking

Yes - but this is a fairly large app and tying to manually maintain
export lists seems worse than going in and changing the names of the
relatively few offending symbols. Thanks again.

Paul Pluzhnikov wrote:
> dam-at-us.ibm.com writes:
>
> > Thanks so much

>
> Please do not top-post.
>
> > Now my next
> > problem is that app tries to export a bunch of symbols with leading
> > underscores and it appears AIX filters those out.
> > I can always add some
> > #defines to strip off the underscores but if you know of any easier way
> > (eg. a link option) let me know.

>
> The "official" way to export symbols is with an export list:
>
> $ cat main.exp
> #!main
> _callback
>
> $ cat main.c
> void _callback ()
> {
> printf ("main.callback - enter\n");
> }
>
> $ gcc main.c -Wl,-bE:main.exp && dump -Tv a.out | grep callback
> [17] 0x2000276c .data EXP DS Ldef [noIMid] _callback
>
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


Reply With Quote
  #6  
Old 01-02-2007, 11:48 AM
Default Re: AIX runtime linking

dam-at-us.ibm.com writes:

> Yes - but this is a fairly large app


A: Because it reverses the logical flow of conversation.
Q: Why is top posting frowned upon?

PLEASE do not top-post.

> and tying to manually maintain
> export lists seems worse than going in and changing the names of the
> relatively few offending symbols.


If there are few offending symbols, then maintaining a list of them
should not be a big deal.

Besides, you can use nm/sed/awk/perl etc. to generate needed lists
automatically.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Reply With Quote
  #7  
Old 01-02-2007, 12:39 PM
Default Re: AIX runtime linking

Paul Pluzhnikov wrote:
> dam-at-us.ibm.com writes:
>
>> gcc -o main main.o -Xlinker -G -Xlinker -bexpall

>
> Changing command line above to:
>
> gcc -o main main.o -Wl,-bexpall,-brtllib


Actually, the intended option should be
-Wl,-bexpall,-brtl

Reply With Quote
  #8  
Old 01-02-2007, 12:40 PM
Default Re: AIX runtime linking

Paul Pluzhnikov wrote:
>
> The "official" way to export symbols is with an export list:
>
> $ cat main.exp
> #!main
> _callback


Or you can use the (I believe still undocumented) -bexpfull option,
which will catch symbols with leading underscores.

Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 11:33 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Integrated by bbpixel2009 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.