Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMD generates bad DWARF for static array return values #20554

Open
dlangBugzillaToGithub opened this issue Nov 28, 2024 · 0 comments
Open

DMD generates bad DWARF for static array return values #20554

dlangBugzillaToGithub opened this issue Nov 28, 2024 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

Vladimir Panteleev (@CyberShadow) reported this on 2024-11-28T07:22:49Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=24888

Description

If an extern(C) function in a D program has a return value larger than 16 bytes, trying to call it and print its return value from gdb results in the error message "Too few arguments in function call.".

Reproducer:

cat > bug.d <<EOF
extern(C) auto a() { return (ubyte[16]).init; }
extern(C) auto b() { return (ubyte[17]).init; }
void main(){}
EOF

dmd -g bug

gdb -ex start \
	-ex 'echo === a() ===
' \
	-ex 'p a()' \
	-ex 'echo === b() ===
' \
	-ex 'p b()' \
	-ex 'stop' \
	-ex 'quit' \
	./bug

liushuyu on the gdb bugtracker ( https://sourceware.org/bugzilla/show_bug.cgi?id=30576 ) provided the following explanation:

> (In reply to Vladimir Panteleev from comment #2)
> > It still happens here. GDB 15.1, DMD v2.109.1.
> 
> Oh, I see now, you were using the DMD compiler.
> 
> After some investigations, I think it's a DMD bug. Here's the DWARF dump of
> the function bug.b():
> 
> 0x000000a9:   DW_TAG_subprogram
>                 DW_AT_sibling   (0x000000e7)
>                 DW_AT_MIPS_linkage_name ("b")
>                 DW_AT_type      (0x00000095 "ubyte[17]")
>                 DW_AT_external  (0x01)
>                 DW_AT_pure      (0x01)
>                 DW_AT_name      ("bug.b")
>                 DW_AT_decl_file ("/tmp/./bug.d")
>                 DW_AT_decl_line (2)
>                 DW_AT_decl_column       (16)
>                 DW_AT_low_pc    (0x0000000000042728)
>                 DW_AT_high_pc   (0x0000000000042754)
>                 DW_AT_frame_base        (0x0000004c:
>                    [0x0000000000042728, 0x0000000000042729): DW_OP_breg7
> RSP+8
>                    [0x0000000000042729, 0x000000000004272b): DW_OP_breg7
> RSP+16
>                    [0x000000000004272b, 0x0000000000042754): DW_OP_breg6
> RBP+16)
> 
> 0x000000d3:     DW_TAG_formal_parameter
>                   DW_AT_name    ("__HID1")
>                   DW_AT_type    (0x000000a4 "ubyte (*)[17]")
>                   DW_AT_artificial      (0x00)
>                   DW_AT_decl_file       ("/tmp/./bug.d")
>                   DW_AT_decl_line       (0)
>                   DW_AT_decl_column     (0)
>                   DW_AT_location        (DW_OP_fbreg -24)
> 
> It seems like DMD accidentally included the stack return argument in the
> debug data, which is incorrect (according to the DWARF v5 spec,
> DW_TAG_formal_parameter is only valid for parameters passing into the
> function, not implicit return values defined by ABI).
> 
> If the DWARF dump looks abstract the debug information describes
> the function as:
> 
> extern(C) ubyte[17] b(ubyte[17] __HID1);
> 
> So GDB thought you should pass something to the function.
> 
> In contrast, the GDC compiler wrote the debug information like this:
> 
> 0x00000184:   DW_TAG_subprogram
>                 DW_AT_external  (true)
>                 DW_AT_name      ("b")
>                 DW_AT_decl_file ("/tmp/bug.d")
>                 DW_AT_decl_line (2)
>                 DW_AT_decl_column       (16)
>                 DW_AT_type      (0x00000166 "ubyte[17]")
>                 DW_AT_low_pc    (0x00000000000251c9)
>                 DW_AT_high_pc   (0x00000000000251e6)
>                 DW_AT_frame_base        (DW_OP_call_frame_cfa)
>                 DW_AT_call_all_calls    (true)
> 
> Which is the correct definition for your bug.b function.
@thewilsonator thewilsonator added Feature:Debug Info DWARF, PDB and removed Arch:x86_64 Issues specific to x86_64 P1 labels Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants