Wrong (overlapping) addresses in Dwarf info (DW_TAG_subprogram and line-info) / FPC 3.3.1 from Dec-10-2021
Compile the attached project with
fpc.exe -O1 -gw project1.lpr
Generates the following dwarf info (only relevant lines are copied):
<0><dc>: Abbrev Number: 1 (DW_TAG_compile_unit)
<dd> DW_AT_name : unit1.pas
<e7> DW_AT_producer : Free Pascal 3.3.1 2021/12/10
<121> DW_AT_low_pc : 0x10000fc20
<129> DW_AT_high_pc : 0x10000ff10
<1><131>: Abbrev Number: 2 (DW_TAG_subprogram)
<132> DW_AT_name : FOO
<139> DW_AT_low_pc : 0x10000fcf0
<141> DW_AT_high_pc : 0x10000fe4c
<1><15e>: Abbrev Number: 5 (DW_TAG_subprogram)
<15f> DW_AT_name : fin$00000007
<16e> DW_AT_low_pc : 0x10000fcb0
<176> DW_AT_high_pc : 0x10000fce5
<1><190>: Abbrev Number: 2 (DW_TAG_subprogram)
<191> DW_AT_name : XYZ
<198> DW_AT_low_pc : 0x0
<1a0> DW_AT_high_pc : 0x0
<1><1b3>: Abbrev Number: 2 (DW_TAG_subprogram)
<1b4> DW_AT_name : BAR
<1bb> DW_AT_low_pc : 0x10000fe70
<1c3> DW_AT_high_pc : 0x10000ff21
<1><1d6>: Abbrev Number: 5 (DW_TAG_subprogram)
<1d7> DW_AT_name : fin$00000008
<1e6> DW_AT_low_pc : 0x10000fe40
<1ee> DW_AT_high_pc : 0x10000fe63
-
fin$00000008(the implicit finally handler forBar) starts at DW_AT_low_pc0x10000fe40. - yet
FOOends at DW_AT_high_pc0x10000fe4c
So according to the generated dwarf, 'FOO' overlaps 12 bytes with the finally handler. The actual code of FOO ends before this.
The line info also is affected by this error.
Line Number Statements:
// procedure Foo (finally)
[0x00000121] Extended opcode 2: set Address to 0x10000fcb0
[0x0000012c] Set column to 1
[0x0000012e] Special opcode 34: advance Address by 0 to 0x10000fcb0 and Line by 35 to 36
[0x0000012f] Advance PC by 9 to 0x10000fcb9
[0x00000131] Copy
[0x00000132] Advance PC by 36 to 0x10000fcdd
[0x00000134] Advance Line by -9 to 27
[0x00000136] Copy
[0x00000137] Extended opcode 2: set Address to 0x10000fce5
[0x00000142] Extended opcode 1: End of Sequence
// procedure Foo
[0x00000145] Extended opcode 2: set Address to 0x10000fcf0
[0x00000150] Set column to 1
[0x00000152] Special opcode 25: advance Address by 0 to 0x10000fcf0 and Line by 26 to 27
[0x00000153] Advance PC by 22 to 0x10000fd06
[0x00000155] Copy
[0x00000156] Advance PC by 25 to 0x10000fd1f
...
[0x0000017d] Special opcode 0: advance Address by 0 to 0x10000fe12 and Line by 1 to 35
[0x0000017e] Advance PC by 19 to 0x10000fe25
[0x00000180] Set column to 1
[0x00000182] Advance Line by -8 to 27
[0x00000184] Copy
[0x00000185] Advance PC by 9 to 0x10000fe2e
[0x00000187] Special opcode 8: advance Address by 0 to 0x10000fe2e and Line by 9 to 36
[0x00000188] Extended opcode 2: set Address to 0x10000fe4c
[0x00000193] Extended opcode 1: End of Sequence
// procedure Xyz
[0x00000196] Extended opcode 2: set Address to 0x0
...
// procedure Bar (finally)
[0x000001c1] Extended opcode 2: set Address to 0x10000fe40
[0x000001cc] Set column to 1
[0x000001ce] Special opcode 48: advance Address by 0 to 0x10000fe40 and Line by 49 to 50
[0x000001cf] Advance PC by 9 to 0x10000fe49
[0x000001d1] Copy
[0x000001d2] Advance PC by 18 to 0x10000fe5b
[0x000001d4] Advance Line by -5 to 45
[0x000001d6] Copy
[0x000001d7] Extended opcode 2: set Address to 0x10000fe63
[0x000001e2] Extended opcode 1: End of Sequence
// procedure Bar
[0x000001e5] Extended opcode 2: set Address to 0x10000fe70
[0x000001f0] Set column to 1
[0x000001f2] Special opcode 43: advance Address by 0 to 0x10000fe70 and Line by 44 to 45
[0x000001f3] Advance PC by 18 to 0x10000fe82
[0x000001f5] Copy
...
Foo ends with
[0x00000188] Extended opcode 2: set Address to 0x10000fe4c
[0x00000193] Extended opcode 1: End of Sequence
And the implicit finally of Bar starts with
[0x000001c1] Extended opcode 2: set Address to 0x10000fe40
Due to this issue, a debugger can not correctly determine the addresses for some lines in the code. This may be related to issue #38117