[patch] Handle return small struct in rs600 (size is not 4/8)

Yao Qi
2011-08-15T15:22:38Z

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

Hi,
It looks to me that ppc-sysv-tdep.c:do_ppc_sysv_return_value doesn't
consider the case that returning a small struct (size <= 8) whose size
is not 4 or 8.

Supposing we have a struct defined as below,

struct C
{char c1; char c2; char c3;};
struct C c;
c.c1 = 'a'; c.c2 = 'b'; c.c3 = 'c';

The raw memory content of c is 0x616263XX (big-endian) or 0xXX636261
(little-endian). When returning c, according to Power Arch ABI:
"Aggregates or unions whose size is less than or equal to eight bytes
shall be returned in r3 and r4, as if they were first stored in memory
area and then the low-addressed word were loaded in r3 and the
high-addressed word were loaded into r4.", the content of r3 should be
0x616263 (big-endian) or 0x636261 (little-endian).

When gdb reads r3's content via regcache_cooked_read into a buf, the
content of buf looks like this,
buf: [0] [1] [2] [3]
big-endian : 00 61 62 63
little-endian : 61 62 63 00

later, when we copy the contents of buf to readbuf, we should skip 00.
Current code in gdb doesn't consider this, but it works in
little-endian. This patch is going to fix this issue.

Regression tested on a powerpc variant board. Many fails in
gdb.base/structs.exp are fixed. Is this patch OK?

--
Yao (é½å°§)

[-- Attachment #2: 0001-return_value-read-write-endianess.patch --]
[-- Type: text/x-patch, Size: 1889 bytes --]

gdb/
* ppc-sysv-tdep.c (do_ppc_sysv_return_value): Handle return small struct whose size is not 4 or 8.
---
gdb/ppc-sysv-tdep.c | 16 ++++++++++++++--
1 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/gdb/ppc-sysv-tdep.c b/gdb/ppc-sysv-tdep.c
index afe3bd2..48afc7a 100644
--- a/gdb/ppc-sysv-tdep.c
+++ b/gdb/ppc-sysv-tdep.c
@@ -795,12 +795,18 @@ do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct value *function,
/* The value is right-padded to 8 bytes and then loaded, as
two "words", into r3/r4. */
gdb_byte regvals[MAX_REGISTER_SIZE * 2];
+ int offset = (2 * tdep->wordsize - TYPE_LENGTH (type)) % tdep->wordsize;
+
regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 3,
regvals + 0 * tdep->wordsize);
if (TYPE_LENGTH (type) > tdep->wordsize)
regcache_cooked_read (regcache, tdep->ppc_gp0_regnum + 4,
regvals + 1 * tdep->wordsize);
- memcpy (readbuf, regvals, TYPE_LENGTH (type));
+
+ if (byte_order == BFD_ENDIAN_BIG)
+ memcpy (readbuf, regvals + offset, TYPE_LENGTH (type));
+ else
+ memcpy (readbuf, regvals, TYPE_LENGTH (type));
}
if (writebuf)
{
@@ -808,8 +814,14 @@ do_ppc_sysv_return_value (struct gdbarch *gdbarch, struct value *function,
/* The value is padded out to 8 bytes and then loaded, as
two "words" into r3/r4. */
gdb_byte regvals[MAX_REGISTER_SIZE * 2];
+ int offset = (2 * tdep->wordsize - TYPE_LENGTH (type)) % tdep->wordsize;
+
memset (regvals, 0, sizeof regvals);
- memcpy (regvals, writebuf, TYPE_LENGTH (type));
+ if (byte_order == BFD_ENDIAN_BIG)
+ memcpy (regvals + offset, writebuf, TYPE_LENGTH (type));
+ else
+ memcpy (regvals, writebuf, TYPE_LENGTH (type));
+
regcache_cooked_write (regcache, tdep->ppc_gp0_regnum + 3,
regvals + 0 * tdep->wordsize);
if (TYPE_LENGTH (type) > tdep->wordsize)
--
1.7.0.4