Subject: automatic relocation problem? (WAS: problem with 3rd party lib)
From: Thomas Kowatsch <[email-masked]>
To: Cygwin List <[email-masked]>
Date: Thu, 16 Sep 1999 09:28:20 +0200

I tracked the problem a litlle bit more down. It seems to me that
it is a problem of automatic DLL relocation. If the 3rd party lib
described below id loaded some more libraries are also loaded
in sequence. I can see that when I use gdb. One or more of those
libraries are relocated to and address < 0x10000000. I used gdb's
stepi to track down the exact place where it fails and it is as
soon as the program branches to code of a library which is
loaded < 0x10000000. gdb complains "Cannot access memory at
address 0x2000000".

Below you can see my gdb session.

---------------------------

% gdb testit3
GNU gdb 4.17.1
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you
are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.

This GDB was configured as "i586-cygwin32"...
(gdb) b main
Breakpoint 1 at 0x4010ab: file ../testit3.c, line 19.
(gdb) r
Starting program: /tmp/sybtest/x/../testit3.exe
10000000:/c/Sybase/dll/debug/libsybdb.dll
warning: LDR: Automatic DLL Relocation in testit3.exe

warning: LDR: Dll libtcl.dll base 10000000 relocated due to collision with
C:\Sybase\dll\debug\libsybdb.dll

2590000:/c/Sybase/DLL/libtcl.dll
warning: LDR: Automatic DLL Relocation in testit3.exe

warning: LDR: Dll libintl.dll base 10000000 relocated due to collision with
C:\Sybase\dll\debug\libsybdb.dll

25b0000:/c/Sybase/DLL/libintl.dll

[failed reading symbols from DLL]
"/c/WINNT/system32/KERNEL32.dll": error reading line numbers

[failed reading symbols from DLL]
"/c/WINNT/system32/ADVAPI32.dll": error reading line numbers

77e70000:/c/WINNT/system32/USER32.dll
77ed0000:/c/WINNT/system32/GDI32.dll
77e10000:/c/WINNT/system32/RPCRT4.dll
779d0000:/c/WINNT/System32/MSVCRT40.dll
78000000:/c/WINNT/system32/MSVCRT.dll
780a0000:/c/WINNT/System32/MSVCIRT.dll
warning: LDR: Automatic DLL Relocation in testit3.exe

warning: LDR: Dll libcomn.dll base 10000000 relocated due to collision with
C:\Sybase\dll\debug\libsybdb.dll

25c0000:/c/Sybase/DLL/libcomn.dll
warning: LDR: Automatic DLL Relocation in testit3.exe

warning: LDR: Dll libcs.dll base 10000000 relocated due to collision with
C:\Sybase\dll\debug\libsybdb.dll

2610000:/c/Sybase/DLL/libcs.dll
61000000:/usr/local/cygnus/cygwin-b20/usr/bin/cygwin1.dll
5f810000:/c/WINNT/System32/rpcltc1.dll

Breakpoint 1, main () at ../testit3.c:19
19        dbwillconvert(SYBCHAR, SYBTEXT);
(gdb) n
21        if ((handle = LoadLibraryA("libxx.dll")) == NULL) {
(gdb)
613c0000:/tmp/sybtest/x/libxx.dll
warning: LDR: Automatic DLL Relocation in testit3.exe

warning: LDR: Dll libsybdb.dll base 10000000 relocated due to collision
with C:\Sybase\dll\debug\libsybdb.dll

libsybdb.dll (symbols previously loaded)
26        if ((func = (int(*)(void))GetProcAddress(handle, "Xx_Init")) ==
NULL) {
(gdb)
32        if ((*func)() == TCL_ERROR) {
(gdb) b Xx_Init
Breakpoint 2 at 0x613c1048: file xx.c, line 19.
(gdb) c
Continuing.
in Xx_Init()

Breakpoint 2, Xx_Init () at xx.c:19
19        ret = dbinit();
(gdb) stepi
0x613c1068 in dbinit@0 ()
(gdb)
0x1a0b93d0 in ?? ()
(gdb)
0x1a0b93d1 in ?? ()
(gdb)
0x1a0b93d3 in ?? ()
(gdb)
0x1a0b93d6 in ?? ()
(gdb)
0x1a0b93d7 in ?? ()
(gdb)
0x1a0b93d8 in ?? ()
(gdb)
0x1a0b93d9 in ?? ()
(gdb)
0x1a0b93e0 in ?? ()
(gdb)
0x1a0b93e6 in ?? ()
(gdb)
0x1a0b93e8 in ?? ()
(gdb)
0x1a0bc7a2 in ?? ()
(gdb) disassemble 0x1a0bc7a2 0x1a0bc7b0
Dump of assembler code from 0x1a0bc7a2 to 0x1a0bc7b0:
0x1a0bc7a2:     jmp    *0x1a0d9bf8
0x1a0bc7a8:     jmp    *0x1a0d9bf4
0x1a0bc7ae:     jmp    *0x1a0d9bf0
End of assembler dump.
(gdb) print/x *0x1a0d9bf8
$1 = 0x25c1ee0
(gdb) stepi
Cannot access memory at address 0x2000000.
(gdb) disassemble  0x25c1ee0  0x25c1f00
Dump of assembler code from 0x25c1ee0 to 0x25c1f00:
0x25c1ee0 <_size_of_stack_reserve__+6037216>:   mov    0x4(%esp,1),%eax
0x25c1ee4 <_size_of_stack_reserve__+6037220>:   push   %eax
0x25c1ee5 <_size_of_stack_reserve__+6037221>:   call   *0x2608234
0x25c1eeb <_size_of_stack_reserve__+6037227>:   add    $0x4,%esp
0x25c1eee <_size_of_stack_reserve__+6037230>:   ret    $0x4
0x25c1ef1 <_size_of_stack_reserve__+6037233>:   int3
0x25c1ef2 <_size_of_stack_reserve__+6037234>:   int3
0x25c1ef3 <_size_of_stack_reserve__+6037235>:   int3
0x25c1ef4 <_size_of_stack_reserve__+6037236>:   int3
0x25c1ef5 <_size_of_stack_reserve__+6037237>:   int3
0x25c1ef6 <_size_of_stack_reserve__+6037238>:   int3
0x25c1ef7 <_size_of_stack_reserve__+6037239>:   int3
0x25c1ef8 <_size_of_stack_reserve__+6037240>:   int3
0x25c1ef9 <_size_of_stack_reserve__+6037241>:   int3
0x25c1efa <_size_of_stack_reserve__+6037242>:   int3
0x25c1efb <_size_of_stack_reserve__+6037243>:   int3
0x25c1efc <_size_of_stack_reserve__+6037244>:   int3
0x25c1efd <_size_of_stack_reserve__+6037245>:   int3
0x25c1efe <_size_of_stack_reserve__+6037246>:   int3
0x25c1eff <_size_of_stack_reserve__+6037247>:   int3
End of assembler dump.
(gdb) quit

-----------------------------------

Thomas Kowatsch wrote:

> I have a 3rd party product which I have to use in my application.
> It is Sybase Open Client lib. I created the .a from .lib:
>
> ----------------------
>
> #!/bin/sh
> if [ $# -ne 2 ]; then
>   echo "usage: mkimplib <dotlibfile> <dll>"
>   exit 1
> fi
> if [ \! -r $1 ]; then
>   echo "cannot read $1"
>   exit 1
> fi
> if [ \! -r $2 ]; then
>   echo "cannot read $1"
>   exit 1
> fi
> prep=`expr "$1" : '\(...\).*'`
> libname=`expr "$1" : '\(.*\)\.lib'`
> if [ "$prep" != "lib" ]; then
>   libname="lib"$libname
> fi
> libfile=$1
> deffile=$libname.def
> echo "EXPORTS" > $deffile
> for s in `nm $libfile | grep '^........ T _' | sed 's/^........ . _//'`;
> do
>   case $s in
>     DllMain*) ;;
>     *)        echo $s ;;
>   esac
> done >> $deffile
> dlltool -k --add-stdcall-alias --def $deffile --dllname $2 --output-lib
> $libname.a
>
> ------------------------
>
> When I use the library in a program and link it to that it works
> ok. An example:
>
> -------------------------
>
> ----- testit.c
>
> #include <stdio.h>
> #include <errno.h>
>
> #define WIN32_LEAN_AND_MEAN
> #include <windows.h>
> #undef WIN32_LEAN_AND_MEAN
>
> #include <sybfront.h>
> #include <sybdb.h>
> #include <syberror.h>
>
> main()
> {
>   int ret;
>
>   if ((ret = dbinit()) == FAIL)
>     fprintf(stderr, "ERROR dbinit() failed\n");
>
>   printf("dbinit() returned %d\n", ret);
>
>   return 0;
> }
>
> -----
>
> % gcc -g -O2 -o testit.exe -I/c/Sybase/include testit.c -L/c/Sybase/lib
> -lsybdb
> % testit
> dbinit() returned 1
>
> ---------------------------
>
> The result is as expected. If I create a shared library which is doing
> the stuff in main() above it fails in the dbinit() func.
>
> ---------------------------
>
> ----- xx.c
>
> #include <stdio.h>
> #include <windows.h>
>
> int WINAPI DllEntryPoint(HANDLE h, DWORD reason, void *x)
> {
>   return TRUE;
> }
>
> #include <sybfront.h>
> #include <sybdb.h>
> #include <syberror.h>
>
> int Xx_Init()
> {
>   int ret;
>
>   printf("in Xx_Init()\n");
>
>   ret = dbinit();
>
>   printf("dbinit() returned %d\n", ret);
>
>   return ret;
> }
>
> ----- testit3.c
>
> #include <stdio.h>
> #include <errno.h>
>
> #define WIN32_LEAN_AND_MEAN
> #include <windows.h>
> #undef WIN32_LEAN_AND_MEAN
>
> #include <tcl.h>
> #include <sybfront.h>
> #include <sybdb.h>
> #include <syberror.h>
>
> main()
> {
>   HINSTANCE handle;
>   int (*func)(void);
>
>   if ((handle = LoadLibraryA("libxx.dll")) == NULL) {
>     fprintf(stderr, "ERROR loading lib\n");
>     return -1;
>   }
>
>   if ((func = (int(*)(void))GetProcAddress(handle, "Xx_Init")) == NULL)
> {
>     fprintf(stderr, "ERROR finding Xx_Init()\n");
>     return -1;
>   }
>
>   if ((*func)() == TCL_ERROR) {
>     fprintf(stderr, "ERROR calling dbinit()\n");
>     return -1;
>   }
>
>   printf("success\n");
>
>   return 0;
> }
>
> -----
>
> % gcc -g -O2 -I/c/Sybase/include -c xx.c
> % dllwrap --export-all --output-def libxx.def --entry _DllEntryPoint@12
> --implib libxx.a -o libxx.dll
> xx.o -L/c/Sybase/lib -lsybdb
> Warning: no export definition file provided
> dllwrap will create one, but may not be what you want
> % gcc -g -O2 -o testit3.exe -I/c/Sybase/include testit3.c -L. -lxx
> -L/c/Sybase/lib -lsybdb
> % testit3
> in Xx_Init()
>     0       0 [main] c:\TEMP\sybtest\x\testit3.exe 2592
> handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
>     0   15271 [main] testit3 2592 handle_exceptions: Dumping stack trace
> to testit3.exe.core
>
> ----------------------------
>
> Am I'm doing something obviously wrong? Please help.
> Regards
> Tommy

--
------------------------------------------------------------------------
Thomas Kowatsch                      | - Senior Software Engineer -
Switching Test Solutions AG          | mailto: [email-masked]
Wavetek Wandel & Goltermann Divison5 | web   : http://www.stest.com
Friesenbergstr. 75                   | Phone : +41 1 454-6731
CH-8055 Zuerich                      | FAX   : +41 1 454-6612

--
Want to unsubscribe from this list?
Send a message to [email-masked]