为什么我的程序在启动之前会崩溃?

时间:2013-04-09 19:40:22

标签: c++ c windows gdb mingw

该程序由g ++编译,带有-g标志,-static-libgcc和-static-libstdc ++。没有包含优化标志。出于某种原因,我不能进入主要的。为什么呢?

$ nm -C test.exe | grep main
006c05b0 T __getmainargs
006b0ad0 T __main
0088d0e8 B __mingw_winmain_hInstance
0088d0e4 B __mingw_winmain_lpCmdLine
0088d0ec B __mingw_winmain_nShowCmd
006ce518 D __native_dllmain_reason
00401180 t __tmainCRTStartup
0088edc8 I _imp____getmainargs
007491c0 r jisx0213_to_ucs_main
00405f0c T main
00401570 T mainCRTStartup
00884010 b mainret
004a3371 T sqlite3_backup_remaining
0078ada0 r uhc_1_2charset_main
0078c440 r uhc_1_2uni_main_page81
007899a0 r uhc_2_2charset_main
0078db00 r uhc_2_2uni_main_pagea1

$ gdb test.exe
GNU gdb (pcx32) 7.3.50.20111127-cvs
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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 "i686-w64-mingw32".
For bug reporting instructions, please see:
...
Reading symbols from c:\test.exe
...done.
(gdb) break main
Breakpoint 1 at 0x405f15: file test.cpp, line 1054.
(gdb) break mainCRTStartup
Breakpoint 2 at 0x401570
(gdb) break __tmainCRTStartup
Breakpoint 3 at 0x40118c
(gdb) break __main
Breakpoint 4 at 0x6b0ad0
(gdb) break __getmainargs
Breakpoint 5 at 0x6c05b0
(gdb) run
Starting program: c:\test.exe
[New Thread 5832.0xc0c]
During startup program exited with code 0xc0000022.
(gdb)

P.S。 dependency walker显示它无法打开SYSNTFY.DLL并且无法找到IEFRAME.DLL。然而,这不是新的,不应该是问题。

(gdb) info files
Symbols from "c:\test.exe".
Local exec file:
        `c:\test.exe',
        file type pei-i386.
        Entry point: 0x401570
        0x00401000 - 0x006c14c4 is .text
        0x006c2000 - 0x006ce5d0 is .data
        0x006cf000 - 0x0080c3e0 is .rdata
        0x0080d000 - 0x00883c58 is .eh_frame
        0x00884000 - 0x0088d178 is .bss
        0x0088e000 - 0x00891d40 is .idata
        0x00892000 - 0x00892038 is .CRT
        0x00893000 - 0x00893020 is .tls
(gdb) break *0x401570
Note: breakpoint 2 also set at pc 0x401570.
Breakpoint 6 at 0x401570
(gdb) run
Starting program: c:\test.exe
[New Thread 5332.0x28b0]
During startup program exited with code 0xc0000022.

这说明入口点确实是__tmainCRTStartup,但是gdb似乎没有到达那里。

答案就像评论一样:图书馆弄乱了一切。为了解决这个问题,我逐个取消了每个库的链接,直到它成功进入main。

2 个答案:

答案 0 :(得分:6)

我怀疑你有一个静态或全局变量初始化,抛出一个SIGSEGV或其他错误......所有静态和全局变量都在main执行之前被初始化。

另外......我看到你正在运行MinGW - 你是否正确设置了你的路径到MinGW bin目录?当我构建MinGW应用程序(通过Eclipse)时,我有一个启动器应用程序和应用程序都可以构建。除非我的Windows路径上有MinGW bin目录,否则我必须使用启动器。

进一步阅读,值得注意的是DLL在调用mainCRTStartup之前加载。对于Windoze应用程序,您通常会在_DllMain上进行处理。我不确定在MinGW中如何/在哪里处理?

答案 1 :(得分:1)

使用Cygwin时遇到了同样的问题,我找到了一个差的解决方案:给共享库提供了可执行权限:

$ chmod a+x lib<name>.so.<ver>

并且还将选项-m0755添加到我的$(INSTALL)命令。