Re: [PATCH] Fix up msymbol type of dll trampoline to mst_solib_trampoline

asmwarrior
2013-07-05T08:44:36Z

This is my testing on the patch, it works fine.
It looks like the issue only happens when __declspec(dllimport) is NOT used when building the exe file.

Here is the source code:
add.c:
--------------------------------------------------------------
//this file will generate a dll

__declspec(dllexport) int __cdecl Add_C(int a, int b)
{
return (a + b);
}

__declspec(dllexport) int __stdcall Add_S(int a, int b)
{
return (a + b);
}
--------------------------------------------------------------
main.c:
--------------------------------------------------------------
//calling the dll

#include <stdlib.h>
#include <stdio.h>

#if USE_DLLIMPORT
__declspec(dllimport) int __cdecl Add_C(int a, int b);
__declspec(dllimport) int __stdcall Add_S(int a, int b);
#else
extern int __cdecl Add_C(int a, int b);
extern int __stdcall Add_S(int a, int b);
#endif // USE_DLLIMPORT

int main(int argc, char** argv)
{

int a = Add_C(1, 2);
printf("%d\n", a);
a = Add_S(3,4);
printf("%d\n", a);

return 0;
}
--------------------------------------------------------------
I have two GDB, the old GDB named gdb.exe does not have your patch,
the new gdbnew.exe has your patch applied.
See the log below (I'm using mingw gcc 4.6.3)

--------------------------------------------------------------
E:\code\cb\test_code\learndll>gcc -g -c -o add.o add.c

E:\code\cb\test_code\learndll>gcc -o add.dll add.o -shared -Wl,--out-implib,liba
dd.a
Creating library file: libadd.a

E:\code\cb\test_code\learndll>gcc -g -c -o main.o main.c

E:\code\cb\test_code\learndll>gcc -o main.exe main.o -L. -ladd

E:\code\cb\test_code\learndll>gdb main.exe
GNU gdb (GDB) 7.6.50.20130607-cvs
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from E:\code\cb\test_code\learndll\main.exe...done.
(gdb) b main.c:18
Breakpoint 1 at 0x4016f6: file main.c, line 18.
(gdb) r
Starting program: E:\code\cb\test_code\learndll\main.exe
[New Thread 2596.0x9d0]

Breakpoint 1, main (argc=1, argv=0x3e3f80) at main.c:18
18 int a = Add_C(1, 2);
(gdb) b Add_C
Breakpoint 2 at 0x40175c (2 locations)
(gdb) info symbol 0x40175c
Add_C in section .text of E:\code\cb\test_code\learndll\main.exe
(gdb) b Add_S
Function "Add_S" not defined.
Make breakpoint pending on future shared library load? (y or [n]) n
(gdb) c
Continuing.

Breakpoint 2, 0x0040175c in Add_C ()
(gdb) c
Continuing.

Breakpoint 2, Add_C (a=1, b=2) at add.c:5
5 return (a + b);
(gdb) l
1 //this file will generate a dll
2
3 __declspec(dllexport) int __cdecl Add_C(int a, int b)
4 {
5 return (a + b);
6 }
7
8 __declspec(dllexport) int __stdcall Add_S(int a, int b)
9 {
10 return (a + b);
(gdb) c
Continuing.
3
7
[Inferior 1 (process 2596) exited normally]
(gdb) q
--------------------------------------------------------------

The main.exe above does not use the __declspec(dllimport), so you will see
(gdb) b Add_C
Breakpoint 2 at 0x40175c (2 locations)

Now, with the new gdb, it works quite well.
--------------------------------------------------------------
E:\code\cb\test_code\learndll>gdbnew main.exe
GNU gdb (GDB) 7.6.50.20130705-cvs
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from E:\code\cb\test_code\learndll\main.exe...done.
(gdb) b main.c:18
Breakpoint 1 at 0x4016f6: file main.c, line 18.
(gdb) r
Starting program: E:\code\cb\test_code\learndll\main.exe
[New Thread 2496.0xcac]

Breakpoint 1, main (argc=1, argv=0x3e3f80) at main.c:18
18 int a = Add_C(1, 2);
(gdb) b Add_C
Breakpoint 2 at 0x70f41703: file add.c, line 5.
(gdb) c
Continuing.

Breakpoint 2, Add_C (a=1, b=2) at add.c:5
5 return (a + b);
(gdb) l
1 //this file will generate a dll
2
3 __declspec(dllexport) int __cdecl Add_C(int a, int b)
4 {
5 return (a + b);
6 }
7
8 __declspec(dllexport) int __stdcall Add_S(int a, int b)
9 {
10 return (a + b);
(gdb) c
Continuing.
3
7
[Inferior 1 (process 2496) exited normally]
(gdb) q
--------------------------------------------------------------

Now, I build the main.exe with __declspec(dllimport)
--------------------------------------------------------------

E:\code\cb\test_code\learndll>gcc -g -c -o main.o main.c -DUSE_DLLIMPORT

E:\code\cb\test_code\learndll>gcc -o main.exe main.o -L. -ladd

E:\code\cb\test_code\learndll>gdb main.exe
GNU gdb (GDB) 7.6.50.20130607-cvs
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from E:\code\cb\test_code\learndll\main.exe...done.
(gdb) b main.c:18
Breakpoint 1 at 0x4016f6: file main.c, line 18.
(gdb) r
Starting program: E:\code\cb\test_code\learndll\main.exe
[New Thread 3916.0x5bc]

Breakpoint 1, main (argc=1, argv=0x3e3f80) at main.c:18
18 int a = Add_C(1, 2);
(gdb) b Add_C
Breakpoint 2 at 0x70f41703: file add.c, line 5.
(gdb) c
Continuing.

Breakpoint 2, Add_C (a=1, b=2) at add.c:5
5 return (a + b);
(gdb) c
Continuing.
3
7
[Inferior 1 (process 3916) exited normally]
(gdb) q
--------------------------------------------------------------
Both the gdb.exe and gdbnew.exe has the same result. (only one location when using "b Add_C")

BTW:
1, It looks like I can't set a breakpoint on a function with is __stdcall calling convention
When I type:
(gdb) b Add_S
Function "Add_S" not defined.
In fact, the symbol name about "Add_S" function is "Add_S@8", (This can be seen from the disassembler)
I don't know how to set such breakpoint by function names.

2, I see there are symbol names for Add_C: one is named "Add_C" and the other is "_imp__Add_C",
so it has "_imp__" before "Add_C". (One underline prefix and double underlines postfix).

Yuanhui Zhang