Why is my program crashing before it starts?

2019-05-22 13:57发布

问题:

The program was compiled by g++ with the -g flag, -static-libgcc, and -static-libstdc++. No optimization flags were included. For some reason though, I cant get into main. Why?

$ 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 shows that it cant open SYSNTFY.DLL and cant find IEFRAME.DLL. This, however, is not new and should not be the problem.

(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.

This illustrates that the entry point is indeed __tmainCRTStartup, but that gdb doesnt seem to get there.

The answer was like commented: a library was messing everything up. In order to figure it out I unlinked every library one by one until it managed to get to main.

回答1:

I would suspect that you have a static or global variable initialization that's throwing a SIGSEGV or other error... all static and global vars are initialized before main is executed.

Also... I see you are running MinGW -- do you have your paths set up correctly to the MinGW bin directories? When I build MinGW apps (via Eclipse) I have a launcher app and the app that both get built. I have to use the launcher unless I have the MinGW bin directories on my Windows path.

Reading further, it's worthy of note that DLLs are loaded before mainCRTStartup is called. For a Windoze app, you would normally break on _DllMain to deal with that. I'm not sure how/where that gets handled in MinGW?



回答2:

I had this same problem when using Cygwin and I found a poor solution: giving executable permission to the shared library:

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

and, also, adding the option -m0755 to my $(INSTALL) command.