libsframe: write out SFrame FRE start address correctly

Message ID 20221224194012.1889405-1-indu.bhagat@oracle.com
State Accepted
Headers
Series libsframe: write out SFrame FRE start address correctly |

Checks

Context Check Description
snail/binutils-gdb-check success Github commit url

Commit Message

Indu Bhagat Dec. 24, 2022, 7:40 p.m. UTC
  The following test was failing on ppc64 and s390x:
  "FAIL: encode-1: Encode buffer match"

The offending stub was how we memcpy the FRE start address to the buffer
(on-disk format).  When the host is big-endian, the src address of the
buffer needs to point to the uint8_t/uint16_t sized value of the FRE
start addr, not uint32_t sized value; we intend to copy out only the
fre_start_addr_sz number of bytes.

ChangeLog:

	* libsframe/sframe.c (sframe_encoder_write_fre_start_addr): New
	function.
	(sframe_encoder_write_fre): Use it instead of memcpy.
---
 libsframe/sframe.c | 38 +++++++++++++++++++++++++++++++++++---
 1 file changed, 35 insertions(+), 3 deletions(-)
  

Patch

diff --git a/libsframe/sframe.c b/libsframe/sframe.c
index e41c95b9944..c9123404ea7 100644
--- a/libsframe/sframe.c
+++ b/libsframe/sframe.c
@@ -1502,6 +1502,39 @@  sframe_sort_funcdesc (sframe_encoder_ctx *encoder)
   return 0;
 }
 
+/* Write the SFrame FRE start address from the in-memory FRE_START_ADDR
+   to the buffer CONTENTS (on-disk format), given the FRE_TYPE and
+   FRE_START_ADDR_SZ.  */
+
+static int
+sframe_encoder_write_fre_start_addr (char *contents,
+				     uint32_t fre_start_addr,
+				     unsigned int fre_type,
+				     size_t fre_start_addr_sz)
+{
+  int err = 0;
+
+  if (fre_type == SFRAME_FRE_TYPE_ADDR1)
+    {
+      uint8_t uc = fre_start_addr;
+      memcpy (contents, &uc, fre_start_addr_sz);
+    }
+  else if (fre_type == SFRAME_FRE_TYPE_ADDR2)
+    {
+      uint16_t ust = fre_start_addr;
+      memcpy (contents, &ust, fre_start_addr_sz);
+    }
+  else if (fre_type == SFRAME_FRE_TYPE_ADDR4)
+    {
+      uint32_t uit = fre_start_addr;
+      memcpy (contents, &uit, fre_start_addr_sz);
+    }
+  else
+    return sframe_set_errno (&err, SFRAME_ERR_INVAL);
+
+  return 0;
+}
+
 /* Write a frame row entry pointed to by FREP into the buffer CONTENTS.  The
    size in bytes written out are updated in ESZ.
 
@@ -1529,9 +1562,8 @@  sframe_encoder_write_fre (char *contents, sframe_frame_row_entry *frep,
   uint64_t bitmask = SFRAME_BITMASK_OF_SIZE (fre_start_addr_sz);
   sframe_assert ((uint64_t)frep->fre_start_addr <= bitmask);
 
-  memcpy (contents,
-	  &frep->fre_start_addr,
-	  fre_start_addr_sz);
+  sframe_encoder_write_fre_start_addr (contents, frep->fre_start_addr,
+				       fre_type, fre_start_addr_sz);
   contents += fre_start_addr_sz;
 
   memcpy (contents,