LIBPATH and Linking
This section discusses compiling and linking your AIX programs. The following command adds path_mr and path to your program’s libpath:
xlc -Lpath_mr -Lpath -o a.out foo.o
When malloc() dynamically loads tvheap_mr.a, it should find the library in path_mr. When tvheap_mr.a dynamically loads tvheap.a, it should find it in path.
The AIX linker supports relinking executables. This means that you can make an already complete application ready for the TotalView agent; for example:
cc a.out -Lpath_mr -Lpath -o a.out.new
Here's an example that does not link in the heap replacement library. Instead, it allows you to dynamically set MALLOCTYPE:
xlC -q32 -g \
-L/usr/local/tvheap_mr \
-L/home/totalview/interposition/lib prog.o -o prog
This next example shows how a program can be set up to access the TotalView agent by linking in the aix_malloctype.o module:
xlc -q32 -g \
-L/usr/local/tvheap_mr \
-L/home/totalview/interposition/lib prog.o \
/home/totalview/interposition/lib/aix_malloctype.o \
-o prog
You can check that the paths made it into the executable by running the dump command; for example:
% dump -Xany -Hv tx_memdebug_hello
tx_memdebug_hello:
***Loader Section***
Loader Header Information
VERSION# #SYMtableENT #RELOCent LENidSTR
0x00000001 0x0000001f 0x00000040 0x000000d3
#IMPfilID OFFidSTR LENstrTBL OFFstrTBL
0x00000005 0x00000608 0x00000080 0x000006db
***Import File Strings***
INDEX PATH BASE MEMBER
0 /.../lib:/usr/.../lib:/usr/lib:/lib
1 libc.a shr.o
2 libC.a shr.o
3 libpthreads.a shr_comm.o
4 libpthreads.a shr_xpg5.o
Index 0 in the Import File Strings section shows the search path the runtime loader uses when it dynamically loads a library. Some systems propagate the preload library environment to the processes they will run; others, do not. If they do not, you need to manually link them with the tvheap library.
In some circumstances, you might want to link your program instead of setting the MALLOCTYPE environment variable. If you set the MALLOCTYPE environment variable for your program and it uses fork()/exec() a program that is not linked with the agent, your program will terminate because it fails to find malloc().