-1

I want to write a software for an ARM target system and debug it remotely. I use gcc, which builds the program correctly. Then I want to debug the program with gdb. Unfortunately I use a x86 Windows on my host system. For debugging I have the gcc linaro 7.5 mingw32 arm linux gnueabi toolchain, together with the included GDB. This version of gdb seems not support the newest version of DWARF. So I get the following error when starting the debugger:

Reading symbols from testProgram...Dwarf Error: wrong version in compilation unit header (is 5, should be 2, 3, or 4) [in
module C:\Tools\gcc-linaro-7.5.0-2019.12-i686-mingw32_arm-linux-gnueabi\bin\testProgram]
(no debugging symbols found)...done.

For building I use the following gcc command:

gcc test.c -gdwarf-4 -gstrict-dwarf -o testProgram

I have also tried to select the Dwarf versions 2 and 3. Each with and without the -gstrict-dwarf flag. But without success.

This is my test program:

#include "stdio.h"
int main(void)
{
    printf("Hello World\n");
    return 0;
}

If I check the DWARF Version as in this answer I can see that there are multiple compilation units. Here only at one the DWARF vrsion changes. The others always remain at version 5:

> readelf --debug-dump=info testProgram| grep -A 2 'Compilation Unit @'

  Compilation Unit @ offset 0x0:
   Length:        0x20 (32-bit)
   Version:       5
--
  Compilation Unit @ offset 0x24:
   Length:        0x4b9 (32-bit)
   Version:       5
--
  Compilation Unit @ offset 0x4e1:
   Length:        0x39 (32-bit)
   Version:       5
--
  Compilation Unit @ offset 0x51e:
   Length:        0x1f (32-bit)
   Version:       5
--
  Compilation Unit @ offset 0x541:
   Length:        0xe7 (32-bit)
   Version:       4                 // <- only this on changes
--
  Compilation Unit @ offset 0x62c:
   Length:        0x1f (32-bit)
   Version:       5

So I wanted to ask how to make sure that gcc uses the DWARF version you select. The flags seem to me otherwise relatively pointless, if this is used only for individual compilation units and not the entire program.

schande
  • 576
  • 12
  • 27
  • 1
    You shouldn't need to specify debugging information format. If you use a proper cross-compiler setup it will by default generate the "native" debug information for the target system. Also remember that you need a debugger built to handle the target system. You can't use the native debugger. – Some programmer dude Jun 13 '23 at 07:16
  • 2
    You compiled one source file "test.c" creating (temporarily) "test.o". You didn't compile all the object files in the C runtime library. If `-gdwarf`-4` affects just the compilation phase I don't see a way to apply it to the library's object files. – harper Jun 13 '23 at 08:31

1 Answers1

0

When you link your program, only part of it comes from the test.o object you compiled. The rest comes from libc and compiler support files (crt0.o, etc.).

You can see this with gcc test.c -o testProgram -Wl,-t.

Your problem comes from the fact that the other object files linked into your program have DWARF5 debug info in them.

You have a few choices:

  • Get an updated GDB which does support DWARF5 -- GDB doesn't have to match your toolchain
  • Get libc that is compiled without DWARF5
  • If you are not planning to debug libc itself, you could strip -g libc.a to remove all debug info from libc, and then you should be able to debug your test program.
Employed Russian
  • 199,314
  • 34
  • 295
  • 362