Monday, July 2, 2012

32/64 bit shared library with 32/64 applications

mylib.c
========
#include <stdio.h>

int print_hello()
{
printf("Hello.\n");
return 0;
}

int print_world()
{
printf("World.\n");
return 0;
}

char *get_str()
{
return "Hello World";
}

$> xlc -c mylib.c -o mylib.o [-q64]
$> xlc -qmkshrobj -qexpfile=mylib.exp mylib.o [-q64]
$> cat mylib.exp [You can create this export file manually as well. By default all functions will be exported]
print_hello
print_world
get_str
$> xlc -qmkshrobj mylib.o [-q64]==> This will generate shr.o (mylib.o + mylib.exp)
$> [export OBJECT_MODE=64] ==> Though u compiled the code in 64 bit, ar (nm and strip etc) command does still work in 32 bit mode.
Hence, set the object mode to 64 bit, before calling the "ar" command.
$> ar -rv libfoo.a shr.o
This will generate the shared library (static linking) in AIX.

pgm.c
======
#include <stdio.h>

/* Note these are important to inform linker */
extern int print_hello();
extern int print_world();
extern char* get_str();

int main()
{
print_hello();
print_world();
printf("String from library: %s.\n", get_str());
}

$> xlc -o pgm pgm.c -L/home/harish/temp/ -lfoo [-q32|-q64]
$> ./pgm
Hello.
World.
String from library: Hello World.

NOTE: If you have compiled the application (pgm.c) with -q32 (as 32 bit),
then you would get error :
$> xlc -o pgm pgm.c -L/home/harish/temp/ -lfoo -q32
ld: 0711-317 ERROR: Undefined symbol: .print_hello
ld: 0711-317 ERROR: Undefined symbol: .print_world
ld: 0711-317 ERROR: Undefined symbol: .get_str
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.

[Use "-bnoquiet" option to find that ELF (XCOFF) format maintained by library
is different from what 32 bit application expects.]

$> xlc -o test test.c -L/perffs/ptx/harishse/sandbox/temp -lfoo -q32 -bnoquiet
(ld): halt 4
(ld): setfflag 4
(ld): savename test
(ld): filelist 6 1
(ld): i /lib/crt0.o
(ld): i test.o
(ld): lib /perffs/ptx/harishse/sandbox/temp/libfoo.a
(ld): lib /usr/vac/lib/libxlopt.a
(ld): lib /usr/vac/lib/libxl.a
(ld): lib /usr/lib/libc.a
LIBRARY: Shared object libc.a[shr.o]: 2874 symbols imported.
LIBRARY: Shared object libc.a[meth.o]: 2 symbols imported.
LIBRARY: Shared object libc.a[posix_aio.o]: 20 symbols imported.
LIBRARY: Shared object libc.a[aio.o]: 18 symbols imported.
LIBRARY: Shared object libc.a[pse.o]: 5 symbols imported.
LIBRARY: Shared object libc.a[dl.o]: 4 symbols imported.
LIBRARY: Shared object libc.a[pty.o]: 1 symbols imported.
FILELIST: Number of previously inserted files processed: 6
(ld): resolve
RESOLVE: 36 of 6085 symbols were kept.
(ld): addgl /usr/lib/glink.o
ADDGL: Glink code added for 6 symbols.
(ld): er full
ld: 0711-318 ERROR: Undefined symbols were found.
        The following symbols are in error:
 Symbol                    Inpndx  TY CL Source-File(Object-File) OR Import-File{Shared-object}
                              RLD: Address  Section  Rld-type Referencing Symbol
 ----------------------------------------------------------------------------------------------
 .print_hello              [26]    ER PR test.c(test.o)
                                   00000028 .text    R_RBR    [12]    .main
 .print_world              [28]    ER PR test.c(test.o)
                                   00000030 .text    R_RBR    [12]    .main
 .get_str                  [30]    ER PR test.c(test.o)
                                   00000038 .text    R_RBR    [12]    .main
ER: The return code is 8.