If I build FPC (i.e. including runtimes etc.) for DWARF 4 and then run the test script I posted earlier, I get
FPC compiler/runtimes built with -gl -gw4.
fpc -odummy_ dummy.pas
fpc -gl -odummy_gl dummy.pas
Compilation Unit @ offset 0:
Length: 0x1eda (32-bit)
Version: 4
fpc -gl -gw2 -odummy_gl_gw2 dummy.pas
Compilation Unit @ offset 0:
Length: 0x1eda (32-bit)
Version: 4
fpc -gl -gw3 -odummy_gl_gw3 dummy.pas
Compilation Unit @ offset 0:
Length: 0x1eda (32-bit)
Version: 4
fpc -gw2 -odummy_gw2 dummy.pas
Compilation Unit @ offset 0:
Length: 0x1eda (32-bit)
Version: 4
fpc -gw3 -odummy_gw3 dummy.pas
Compilation Unit @ offset 0:
Length: 0x1eda (32-bit)
Version: 4
3427178416 1029376 dummy_gl_gw3
3447956569 231872 dummy_
3581180241 975368 dummy_gw2
3719963618 1029376 dummy_gl
3719963618 1029376 dummy_gl_gw2
656825139 975368 dummy_gw3
So if nothing else it appears to imply that using readelf is a fairly reliable way of getting the basic DWARF version even if the interplay between the various compile-time options might possibly be slightly non-intuitive (I'm certainly losing track of quite where we're at :-)
Having said which,
$ readelf --debug-dump=info dummy_gw2 | grep -A 2 'Compilation Unit @'
Compilation Unit @ offset 0:
Length: 0x1eda (32-bit)
Version: 4
--
Compilation Unit @ offset 0x1ede:
Length: 0x6f (32-bit)
Version: 2
--
Compilation Unit @ offset 0x1f51:
Length: 0x3c006 (32-bit)
Version: 4
So the versioning exercise really boils down to looking at both the RTL's startup code which could be reasonably expected to determine the compiler's choice of default DWARF version, and the version which has actually been used for the application code.
For this particular system based on Debian "Bookworm" x86_64, I'm going to build FPC with DWARF 3 then ditto the Lazarus IDE. Once I've done that, I'll see how happy the IDE is with gdb and FpDebug.
Noting Martin's comment that in principle the debugger should be happy with a mix of DWARF (and possibly even stabs) versions, in practice IDE+gdb appears unhappy with DWARF 2 and I think somebody's said that FpDebug wanted DWARF 3 which seems to contradict his position. And while I'm not a heavy user of Lazarus packages (i.e. via FpUpDeluxe etc.) the possibility of a package carrying an incompatible DWARF-version option around with it is slightly troubling.
Plus there's still also that dialogue which told me to specify a DWARF version (including DWARF 2) without giving any information as to why what a binary was actually using (which happened to be DWARF 2) was considered to be a problem.
MarkMLl