Re: [PATCH] Fix call functions command bug in 64-bit programs for AIX

Aditya Kamath1 via Gdb-patches
2022-11-16T11:28:53Z

[-- Attachment #1: Type: text/plain, Size: 5362 bytes --]

Hi Ulrich,

>This indicates that value is already placed incorrectly
>into the register cache, before store_register was even
>called.

You were right about this. Thank you so much for your guidance.

Please find attached the new patch. See 0001-Fix-call-functions-command-bug-in-64-bit-programs.patch.

In AIX for 64-bit programs, we need to zero extend variables of integer data type.

Otherwise, a zero will get dumped in the register as we memset our word to 0 and integer is not extended.

In this patch I changed the same. I have pasted the output and code below..

Let me know what you think.

Thanks and regards,
Aditya.

----------------------------------
Program:

int num2print(long num, float num2, int num3, double num4) {

if (num == 0) {

printf("R0\n");

return 0;

}

if (num == 1) {

printf("R1\n");

return 1;

}

printf("R%ld\n",num);

printf("R%f\n",num2);

printf("R%d\n",num3);

printf("R%lf\n",num4);

return num;

}

int main(int argc, char** argv)

{

printf("Hi Bangalore %x\n",num2print(27, 16, 13, 9.9));

return 0;

}

--------------------------------------
Output before patch in 64 bit mode

Reading symbols from /home/XYZ/gdb_tests...

(gdb) b main

Breakpoint 1 at 0x100007dc: file /home/XYZ/gdb_tests.c, line 22.

(gdb) r

Starting program: /home/XYZ/gdb_tests

BFD: /usr/lib/libc.a(/usr/lib/libc.a(shr_64.o)): wrong auxtype 0xff for storage class 0x2

BFD: /usr/lib/libc.a(/usr/lib/libc.a(shr_64.o)): wrong auxtype 0xff for storage class 0x6b

Breakpoint 1, main (argc=1, argv=0xffffffffffffad0)

at /home/XYZ/gdb_tests.c:22

22 printf("Hi Bangalore %x\n",num2print(27, 16, 13, 9.9));

(gdb) call num2print

$1 = {int (long, float, int, double)} 0x1000006a0 <num2print>

(gdb) call num2print (2, 3, 4, 5)

R2

R3.000000

R0

R5.000000

$2 = 2

(gdb)

--------------------------------------------
Output after patch in 32-bit mode

Reading symbols from /home/XYZ/gdb_tests...

(gdb) b main

Breakpoint 1 at 0x10000664: file /home/XYZ/gdb_tests.c, line 22.

(gdb) r

Starting program: /home/XYZ/gdb_tests

Breakpoint 1, main (argc=1, argv=0x2ff22bf0)

at /home/XYZ/gdb_tests.c:22

22 printf("Hi Bangalore %x\n",num2print(27, 16, 13, 9.9));

(gdb) call num2print

$1 = {int (long, float, int, double)} 0x10000518 <num2print>

(gdb) call num2print (2, 3, 4, 5.43)

R2

R3.000000

R4

R5.430000

$2 = 2

(gdb)
------------------------------------

output after patch in 64-bit mode

Reading symbols from /home/XYZ/gdb_tests...

(gdb) b main

Breakpoint 1 at 0x100007dc: file /home/XYZ/gdb_tests.c, line 22.

(gdb) r

Starting program: /home/XYZ/gdb_tests

BFD: /usr/lib/libc.a(/usr/lib/libc.a(shr_64.o)): wrong auxtype 0xff for storage class 0x2

BFD: /usr/lib/libc.a(/usr/lib/libc.a(shr_64.o)): wrong auxtype 0xff for storage class 0x6b

Breakpoint 1, main (argc=1, argv=0xffffffffffffad0)

at /home/XYZ/gdb_tests.c:22

22 printf("Hi Bangalore %x\n",num2print(27, 16, 13, 9.9));

(gdb) call num2print

$1 = {int (long, float, int, double)} 0x1000006a0 <num2print>

(gdb) call num2print (2, 3.5, 43, 56.2)

R2

R3.500000

R43

R56.200000

$2 = 2

(gdb)

________________________________
From: Ulrich Weigand <Ulrich.Weigand@de.ibm.com>
Sent: 15 November 2022 00:40
To: gdb-patches@sourceware.org <gdb-patches@sourceware.org>; Aditya Kamath1 <Aditya.Kamath1@ibm.com>; simon.marchi@efficios.com <simon.marchi@efficios.com>
Cc: Sangamesh Mallayya <sangamesh.swamy@in.ibm.com>; Sanket Rathi <sanrathi@in.ibm.com>
Subject: Re: [PATCH] Fix call functions command bug in 64-bit programs for AIX

Aditya Kamath1 <Aditya.Kamath1@ibm.com> wrote:

>(gdb) call num2print (2, 3, 4, 6)
>val in regno = 3 via buf is 2 and *addr is 0, regsize = 8
>val in regno = 4 via buf is 4629700416936869888 and *addr is 1077936128, regsize = 8
>val in regno = 5 via buf is 17179869184 and *addr is 4, regsize = 8
>val in regno = 6 via buf is 25769803776 and *addr is 6, regsize = 8....

Thanks!

This indicates that value is already placed incorrectly
into the register cache, before store_register was even
called.

Looking at rs6000_push_dummy_call in rs6000-aix-tdep.c, I see:

> /* Argument can fit in one register. No problem. */
> gdb_byte word[PPC_MAX_REGISTER_SIZE];
>
> memset (word, 0, reg_size);
> memcpy (word, value_contents (arg).data (), len);
> regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);

which places an "int" argument in the *high* bytes of a register
on a 64-bit system. This seems clearly wrong, and would be the
root cause of the problem you're seeing.

I think you'll need to have a closer look at this function and
make sure it implements the AIX ABI correctly, in particular
also on 64-bit systems. (You might want to have a look at the
ppc64_sysv_abi_push_dummy_call routine for comparison, which
implements the Linux 64-bit ABI; I understand this is similar
to the AIX ABI.)

I assume that once you've fixed the push_dummy_call implementation,
no changes to the store_register / fetch_register routines will
be needed at all.

Bye,
Ulrich

[-- Attachment #2: 0001-Fix-call-functions-command-bug-in-64-bit-programs.patch --]
[-- Type: application/octet-stream, Size: 1378 bytes --]

From 44c45c050bfc017e3fffc2d0bb762d2fa9affa7e Mon Sep 17 00:00:00 2001
From: Aditya Vidyadhar Kamath <Aditya.Kamath1@ibm.com>
Date: Wed, 16 Nov 2022 04:30:59 -0600
Subject: [PATCH] Fix call functions command bug in 64 bits programs for AIX

In AIX for 64 bit programs we need to zero extend variables of integer data type.

Otherwise a zero will get dumped in the register as we memset our word to 0 and integer is not extended.

This patch is a fix for the same.
---
gdb/rs6000-aix-tdep.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/gdb/rs6000-aix-tdep.c b/gdb/rs6000-aix-tdep.c
index d47974b51d1..a7d174d8be8 100644
--- a/gdb/rs6000-aix-tdep.c
+++ b/gdb/rs6000-aix-tdep.c
@@ -400,7 +400,12 @@ rs6000_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
gdb_byte word[PPC_MAX_REGISTER_SIZE];

memset (word, 0, reg_size);
- memcpy (word, value_contents (arg).data (), len);
+ if (type->code () == TYPE_CODE_INT)
+ /* Sign or zero extend the "int" into a "word". */
+ store_unsigned_integer (word, tdep->wordsize, byte_order,
+ unpack_long (type, value_contents (arg).data ()));
+ else
+ memcpy (word, value_contents (arg).data (), len);
regcache->cooked_write (tdep->ppc_gp0_regnum + 3 +ii, word);
}
++argno;
--
2.31.1