libdwfl: Handle unaligned Dyns in dwfl_segment_report_module
authorMark Wielaard <mark@klomp.org>
Thu, 6 Jan 2022 16:35:13 +0000 (17:35 +0100)
committerMark Wielaard <mark@klomp.org>
Thu, 6 Jan 2022 16:36:52 +0000 (17:36 +0100)
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).

https://sourceware.org/bugzilla/show_bug.cgi?id=28720

Signed-off-by: Mark Wielaard <mark@klomp.org>
libdwfl/ChangeLog
libdwfl/dwfl_segment_report_module.c

index f8319f4..aace969 100644 (file)
@@ -1,5 +1,10 @@
 2022-01-03  Mark Wielaard  <mark@klomp.org>
 
+       * dwfl_segment_report_module.c (dwfl_segment_report_module): Copy
+       dyn_data and set xlatefrom.d_buf to dyns when dyns is not aligned.
+
+2022-01-03  Mark Wielaard  <mark@klomp.org>
+
        * link_map.c (read_addrs): Fix buffer_available nb overflow.
 
 2021-12-23  Mark Wielaard  <mark@klomp.org>
index 5bef249..1461ae2 100644 (file)
@@ -844,7 +844,19 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       xlateto.d_buf = dyns;
       xlateto.d_size = dyn_filesz;
 
+      /* dyn_data 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).  */
       bool is32 = (ei_class == ELFCLASS32);
+      size_t dyn_align = (is32
+                         ? __alignof__ (Elf32_Dyn)
+                         : __alignof__ (Elf64_Dyn));
+      if (((uintptr_t) dyn_data & (dyn_align - 1)) != 0)
+       {
+         memcpy (dyns, dyn_data, dyn_filesz);
+         xlatefrom.d_buf = dyns;
+       }
+
       if ((is32 && elf32_xlatetom (&xlateto, &xlatefrom, ei_data) != NULL)
           || (!is32 && elf64_xlatetom (&xlateto, &xlatefrom, ei_data) != NULL))
         {