This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC 10/13] Create debug_frame_hdr in target byte order


Signed-off-by: Crestez Dan Leonard <cdleonard@gmail.com>
---
 translate.cxx | 32 ++++++++++++++++++++++++--------
 1 file changed, 24 insertions(+), 8 deletions(-)

diff --git a/translate.cxx b/translate.cxx
index ac6377b..158f76f 100644
--- a/translate.cxx
+++ b/translate.cxx
@@ -21,6 +21,7 @@
 #include "runtime/k_syms.h"
 #include "dwflpp.h"
 #include "stapregex.h"
+#include <byteswap.h>
 
 #include <cstdlib>
 #include <iostream>
@@ -5537,6 +5538,17 @@ struct unwindsym_dump_context
   set<string> undone_unwindsym_modules;
 };
 
+static bool need_byte_swap_for_target (const unsigned char e_ident[])
+{
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+  return (e_ident[EI_DATA] == ELFDATA2MSB);
+#elif __BYTE_ORDER == __BIG_ENDIAN
+  return (e_ident[EI_DATA] == ELFDATA2LSB);
+#else
+  #error Bad host __BYTE_ORDER
+#endif
+}
+
 static void create_debug_frame_hdr (const unsigned char e_ident[],
 				    Elf_Data *debug_frame,
 				    void **debug_frame_hdr,
@@ -5556,6 +5568,10 @@ static void create_debug_frame_hdr (const unsigned char e_ident[],
   // So there is no need to read the CIEs.  And the size is either 4
   // or 8, depending on the elf class from e_ident.
   int size = (e_ident[EI_CLASS] == ELFCLASS32) ? 4 : 8;
+  bool need_byte_swap = need_byte_swap_for_target (e_ident);
+#define host_to_target_64(x) (need_byte_swap ? bswap_64((x)) : (x))
+#define host_to_target_32(x) (need_byte_swap ? bswap_32((x)) : (x))
+
   int res = 0;
   Dwarf_Off off = 0;
   Dwarf_CFI_Entry entry;
@@ -5620,23 +5636,23 @@ static void create_debug_frame_hdr (const unsigned char e_ident[],
   if (size == 4)
     {
       uint32_t *table = (uint32_t *)(hdr + 4);
-      *table++ = (uint32_t) 0; // eh_frame_ptr, unused
-      *table++ = (uint32_t) fdes.size();
+      *table++ = host_to_target_32 ((uint32_t) 0); // eh_frame_ptr, unused
+      *table++ = host_to_target_32 ((uint32_t) fdes.size());
       for (it = fdes.begin(); it != fdes.end(); it++)
 	{
-	  *table++ = (*it).first;
-	  *table++ = (*it).second;
+	  *table++ = host_to_target_32 ((*it).first);
+	  *table++ = host_to_target_32 ((*it).second);
 	}
     }
   else
     {
       uint64_t *table = (uint64_t *)(hdr + 4);
-      *table++ = (uint64_t) 0; // eh_frame_ptr, unused
-      *table++ = (uint64_t) fdes.size();
+      *table++ = host_to_target_64 ((uint64_t) 0); // eh_frame_ptr, unused
+      *table++ = host_to_target_64 ((uint64_t) fdes.size());
       for (it = fdes.begin(); it != fdes.end(); it++)
 	{
-	  *table++ = (*it).first;
-	  *table++ = (*it).second;
+	  *table++ = host_to_target_64 ((*it).first);
+	  *table++ = host_to_target_64 ((*it).second);
 	}
     }
 }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]