public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] libdwfl: Handle unaligned Ehdr in dwfl_segment_report_module
@ 2021-12-19 23:04 Mark Wielaard
  0 siblings, 0 replies; only message in thread
From: Mark Wielaard @ 2021-12-19 23:04 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

The xlate functions only handle correctly aligned buffers. But they do
handle src == dest. So if the source buffer isn't aligned correctly
just copy it first into the destination (which is already correctly
aligned).

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog                    |  5 +++++
 libdwfl/dwfl_segment_report_module.c | 14 ++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 18ffc347..6c7e0c4a 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,8 @@
+2021-12-19  Mark Wielaard  <mark@klomp.org>
+
+	* dwfl_segment_report_module.c (dwfl_segment_report_module): Copy
+	buffer and set xlatefrom.d_buf to ehdr when buffer is not aligned.
+
 2021-12-19  Mark Wielaard  <mark@klomp.org>
 
 	* dwfl_segment_report_module.c (dwfl_segment_report_module): Check
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index a6d6be85..7f756392 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -367,6 +367,20 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
   e_ident = ((const unsigned char *) buffer);
   ei_class = e_ident[EI_CLASS];
   ei_data = e_ident[EI_DATA];
+  /* buffer may be unaligned, in which case xlatetom would not work.
+     xlatetom does work when the in and out d_buf are equal (but not
+     for any other overlap).  */
+  size_t ehdr_align = (ei_class == ELFCLASS32
+		       ? __alignof__ (Elf32_Ehdr)
+		       : __alignof__ (Elf64_Ehdr));
+  if (((uintptr_t) buffer & (ehdr_align - 1)) != 0)
+    {
+      memcpy (&ehdr, buffer,
+	      (ei_class == ELFCLASS32
+	       ? sizeof (Elf32_Ehdr)
+	       : sizeof (Elf64_Ehdr)));
+      xlatefrom.d_buf = &ehdr;
+    }
   switch (ei_class)
     {
     case ELFCLASS32:
-- 
2.30.2


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-12-19 23:04 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-19 23:04 [PATCH] libdwfl: Handle unaligned Ehdr in dwfl_segment_report_module Mark Wielaard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).