Author Topic: No stdlib, custom startup code - GDB doesn't work  (Read 6300 times)

MrMadguy

  • Guest
No stdlib, custom startup code - GDB doesn't work
« on: March 30, 2023, 02:23:34 pm »
I want to develop program with both Windows and DOS HX support and therefore my program should:
1) Support i386
2) Have minimal size
So I decided to disable stdlib and use my custom startup code instead.
Code: asm
.intel_syntax noprefix
.globl _start
#if __x86_64__
    .globl Main
    .globl ExitProcess
#else
    .globl _Main
    .globl _ExitProcess@4
    Main=_Main
    ExitProcess=_ExitProcess@4
#endif
.text
_start:
    call Main
    #if __x86_64__
        mov ecx, eax
    #else
        push eax
    #endif
    call ExitProcess
Please note, that I had to rename main to Main to avoid "__main is undefined" error.

But I have problem now. GDB doesn't work. I set breakpoint, press Debug button and breakpoint isn't triggered. What can be wrong?

MrMadguy

  • Guest
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #1 on: March 30, 2023, 05:09:33 pm »
Ehhh. Problem is most likely caused by "No source file" and it affects normal projects too. I don't know how to solve it, as path to source files doesn't contain spaces or non-ASCII characters.

Only place, where breakpoints work - startup code. It's very strange, because startup file is placed in exactly the same directory, as main project file. But even in this case GDB doesn't work properly - program execution is stopped, but breakpoint isn't triggered in code editor. If I attempt to resume execution - program just crashes.
« Last Edit: March 30, 2023, 05:18:37 pm by MrMadguy »

MrMadguy

  • Guest
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #2 on: March 30, 2023, 06:57:50 pm »
Both problems are fixed. Problem with breakpoints not triggering is fixed via adding -g to linker settings. I'm not sure, why default Debug config didn't have it. May be because I deleted it, when I was experimenting with compiler/linker settings. Second problem with application crashing when debugging is fixed via adding separate 32bit debugging profile for 32bit applications.

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #3 on: April 12, 2023, 04:51:45 pm »
I have Code::Blocks 20.03 64bit IDE. Both 32bit and 64bit versions of MinGW gcc are installed. 32bit and 64bit build targets are configured to use corresponding compilers and corresponding versions of GDB (i.e. two debugger profiles are created). When I debug 64bit target, every attempt to use Disassembly window causes gdborig.exe crash, while everything works properly for 32bit target. What can be reason for this?

And can anybody tell me, if there is some compiler/linker directive to remove unused (i.e. no exported symbols) .edata section from exe file? I already use -ffunction-sections -fdata-sections for compiler and -Wl,--gc-sections for linker and it doesn't help.
Is it healthy for project not to have regular stable releases?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #4 on: April 12, 2023, 09:54:06 pm »
some version of gdb have a bug where they crash in assembly window. This is a known bug in gdb and should be fixed in the last versions (i think version 8.1 had the bug, but i am not sure)

Quote
And can anybody tell me, if there is some compiler/linker directive to remove unused (i.e. no exported symbols) .edata section from exe file
a forum about compiler would be the better place to ask

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #5 on: April 13, 2023, 08:15:46 am »
some version of gdb have a bug where they crash in assembly window. This is a known bug in gdb and should be fixed in the last versions (i think version 8.1 had the bug, but i am not sure)
It's default version, provided in Code::Blocks 20.03 distribution. May be it would be good idea to change it? Is it safe to download newer version here or some specific version is compatible with Code::Blocks?
Is it healthy for project not to have regular stable releases?

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2750
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #6 on: April 13, 2023, 06:27:47 pm »
I was successful using gdb.exe version 8.1 with mingw 7.
Also successful using gdb version 10 with mingw 8.1 .
All of this was used on windows 10.

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #7 on: April 13, 2023, 07:47:13 pm »
I was successful using gdb.exe version 8.1 with mingw 7.
Also successful using gdb version 10 with mingw 8.1 .
All of this was used on windows 10.
I'm new to this software. Where it's safe to download it? Troyans are reported by VirusTotal very often in 32bit versions of MinGW. Is it safe to download here?
Is it healthy for project not to have regular stable releases?

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2750
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #8 on: April 13, 2023, 09:43:45 pm »
Sorry, my experience with gdb is 64 bit.

But my guess is that msys2 has a 32bit gdb that you can try and it's pretty safe. Just ask VirusTotal (https://www.virustotal.com/gui/home/upload) to check it out.

Though ChatGPT references running it from the terminal, you can also run it within codeblocks. Just add the location of msys2 gdb.exe into the path before running CodeBlocks.

Or run CodeBlocks from a .cmd file with the first item in the path as the location of Msys2 gdb.
« Last Edit: April 13, 2023, 10:02:01 pm by Pecan »

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #9 on: April 16, 2023, 03:32:35 pm »
I personally like this solution, because it's distributed as complete toolkit, that looks very similar to one, that is distributed with Code::Blocks itself. I guess, I can trust it, because it's referenced by GCC's official site.

But I still have two problems:
1) Omitting unused export section in exe file
2) Omitting unneeded import ordinal hints

I guess, ld.exe is responsible for this task. I've studied it's command line options and couldn't find any way to achieve my goal. So, I guess, I'll have to deal with it. #2 problem can possibly be fixed via using different import libraries.
Is it healthy for project not to have regular stable releases?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #10 on: April 16, 2023, 09:02:22 pm »
MinGW64 official site https://www.mingw-w64.org/downloads/ I saw no mention by the GCC Official Site.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #11 on: April 22, 2023, 08:38:37 am »
What disappoints me - is how harder development is due to paranoid anit-viruses. After just several days of development my 3kb code, that does nothing, is already treated as some scary Trojan:Win32/Qakbot.FA!MTB. Is any program, that uses LoadLibrary and GetProcAddress automatically treated as virus? Objdump doesn't show anything suspicious. Only my code, that should be there.
« Last Edit: April 22, 2023, 12:18:45 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?

Offline nenin

  • Almost regular
  • **
  • Posts: 202
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #12 on: April 23, 2023, 10:57:31 am »
I'm new to this software. Where it's safe to download it?
1. https://github.com/niXman/mingw-builds-binaries - it might  be considered like "original/official". It is continuation of the SourceForge  https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/
2. https://winlibs.com/ - a lot of versions, but "old" gdb were  built without python support.   
Separated gdb, best I found: https://github.com/ssbssa/gdb/
Just in case: you can use 64b CB with 64b gdb with 32b code.
Also "heavy likely" that you should use sjlj version, if you want to build for exotic platform.

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #13 on: April 23, 2023, 11:13:23 am »
Just in case: you can use 64b CB with 64b gdb with 32b code.
I'm not sure about newer versions, but for old version I had to install 32bit gdb because breakpoints weren't working.
Is it healthy for project not to have regular stable releases?

Offline nenin

  • Almost regular
  • **
  • Posts: 202
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #14 on: April 23, 2023, 02:17:22 pm »
I'm not sure about newer versions, but for old version I had to install 32bit gdb because breakpoints weren't working.
GDB64 start to support 32b code on Win since 11 (or10?) version. 

Offline Mr.Madguy

  • Multiple posting newcomer
  • *
  • Posts: 12
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #15 on: April 24, 2023, 08:46:57 pm »
If somebody is interested, for now problem with anti-virus is fixed via comparing my C++ code with similar ASM program, that doesn't have such problems. Looks like anti-viruses hate GetModuleHandle("KERNEL32.DLL"). It's better to use LoadLibrary/FreeLibrary instead.

Another question - why it's necessary to install 32bit gcc in order to compile for 32bit target (i.e. -m32)? Why there is no cross-compile option? According to error messages 64bit compiler can't find 32bit libraries when using -m32. So may be there is some way to configure paths instead of using 32bit compiler?
Is it healthy for project not to have regular stable releases?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #16 on: April 25, 2023, 12:57:57 am »
If somebody is interested, for now problem with anti-virus is fixed via comparing my C++ code with similar ASM program, that doesn't have such problems. Looks like anti-viruses hate GetModuleHandle("KERNEL32.DLL"). It's better to use LoadLibrary/FreeLibrary instead.

Another question - why it's necessary to install 32bit gcc in order to compile for 32bit target (i.e. -m32)? Why there is no cross-compile option? According to error messages 64bit compiler can't find 32bit libraries when using -m32. So may be there is some way to configure paths instead of using 32bit compiler?

For "-m32" to work for complex programs; the Compiler Builder needs to build the support libraries for 32 bit. Most of the 64 bit Compilers that support "-m32" do not build very many 32 bit libraries.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline nenin

  • Almost regular
  • **
  • Posts: 202
Re: No stdlib, custom startup code - GDB doesn't work
« Reply #17 on: April 25, 2023, 07:42:21 am »
Another question - why it's necessary to install 32bit gcc in order to compile for 32bit target (i.e. -m32)? Why there is no cross-compile option? According to error messages 64bit compiler can't find 32bit libraries when using -m32. So may be there is some way to configure paths instead of using 32bit compiler?
It is long story.  Actually they exists, but likely you dont want them: "https://sourceforge.net/projects/mingw-w64/files/Multilib Toolchains(Targetting Win32 and Win64)/ "
My experience with multiply mingw compilers (I have now 3 actually in use, 32b, 32b for XP and 64b) is like this: I use bat files which switch path and I have different  configs in CB. And there are no big problems to switch.