1c194f
commit be82bb5f9dfecd854c53eda321d1914f28f19790
1c194f
Author: Mark Wielaard <mark@klomp.org>
1c194f
Date:   Sat Dec 9 23:01:29 2017 +0100
1c194f
1c194f
    Fix gnu debug alt file resolving.
1c194f
    
1c194f
    https://bugs.kde.org/show_bug.cgi?id=387773
1c194f
    
1c194f
    The path to the alt file is relative to the actual debug file.
1c194f
    Make sure that we got the real file, not a (build-id) symlink.
1c194f
    Also handle the case where a debug or alt file is an absolute path.
1c194f
1c194f
diff --git a/coregrind/m_debuginfo/readelf.c b/coregrind/m_debuginfo/readelf.c
1c194f
index e612250..c19ff21 100644
1c194f
--- a/coregrind/m_debuginfo/readelf.c
1c194f
+++ b/coregrind/m_debuginfo/readelf.c
1c194f
@@ -33,6 +33,7 @@
1c194f
 
1c194f
 #include "pub_core_basics.h"
1c194f
 #include "pub_core_vki.h"
1c194f
+#include "pub_core_vkiscnums.h"
1c194f
 #include "pub_core_debuginfo.h"
1c194f
 #include "pub_core_libcbase.h"
1c194f
 #include "pub_core_libcprint.h"
1c194f
@@ -40,6 +41,7 @@
1c194f
 #include "pub_core_machine.h"      /* VG_ELF_CLASS */
1c194f
 #include "pub_core_options.h"
1c194f
 #include "pub_core_oset.h"
1c194f
+#include "pub_core_syscall.h"
1c194f
 #include "pub_core_tooliface.h"    /* VG_(needs) */
1c194f
 #include "pub_core_xarray.h"
1c194f
 #include "priv_misc.h"             /* dinfo_zalloc/free/strdup */
1c194f
@@ -1323,6 +1325,12 @@ DiImage* find_debug_file( struct _DebugInfo* di,
1c194f
                      + (extrapath ? VG_(strlen)(extrapath) : 0)
1c194f
                      + (serverpath ? VG_(strlen)(serverpath) : 0));
1c194f
 
1c194f
+      if (debugname[0] == '/') {
1c194f
+         VG_(sprintf)(debugpath, "%s", debugname);
1c194f
+         dimg = open_debug_file(debugpath, buildid, crc, rel_ok, NULL);
1c194f
+         if (dimg != NULL) goto dimg_ok;
1c194f
+      }
1c194f
+
1c194f
       VG_(sprintf)(debugpath, "%s/%s", objdir, debugname);
1c194f
       dimg = open_debug_file(debugpath, buildid, crc, rel_ok, NULL);
1c194f
       if (dimg != NULL) goto dimg_ok;
1c194f
@@ -1527,6 +1535,56 @@ static Bool check_compression(ElfXX_Shdr* h, DiSlice* s) {
1c194f
     return True;
1c194f
 }
1c194f
 
1c194f
+/* Helper function to get the readlink path. Returns a copy of path if the
1c194f
+   file wasn't a symbolic link. Returns NULL on error. Unless NULL is
1c194f
+   returned the result needs to be released with dinfo_free.
1c194f
+*/
1c194f
+static HChar* readlink_path (const HChar *path)
1c194f
+{
1c194f
+   SizeT bufsiz = VG_(strlen)(path);
1c194f
+   HChar *buf = ML_(dinfo_strdup)("readlink_path.strdup", path);
1c194f
+   UInt   tries = 6;
1c194f
+
1c194f
+   while (tries > 0) {
1c194f
+      SysRes res;
1c194f
+#if defined(VGP_arm64_linux)
1c194f
+      res = VG_(do_syscall4)(__NR_readlinkat, VKI_AT_FDCWD,
1c194f
+                                              (UWord)path, (UWord)buf, bufsiz);
1c194f
+#elif defined(VGO_linux) || defined(VGO_darwin)
1c194f
+      res = VG_(do_syscall3)(__NR_readlink, (UWord)path, (UWord)buf, bufsiz);
1c194f
+#elif defined(VGO_solaris)
1c194f
+      res = VG_(do_syscall4)(__NR_readlinkat, VKI_AT_FDCWD, (UWord)path,
1c194f
+                             (UWord)buf, bufsiz);
1c194f
+#else
1c194f
+#       error Unknown OS
1c194f
+#endif
1c194f
+      if (sr_isError(res)) {
1c194f
+         if (sr_Err(res) == VKI_EINVAL)
1c194f
+            return buf; // It wasn't a symbolic link, return the strdup result.
1c194f
+         ML_(dinfo_free)(buf);
1c194f
+         return NULL;
1c194f
+      }
1c194f
+
1c194f
+      SSizeT r = sr_Res(res);
1c194f
+      if (r < 0) break;
1c194f
+      if (r == bufsiz) {  // buffer too small; increase and retry
1c194f
+         bufsiz *= 2 + 16;
1c194f
+         buf = ML_(dinfo_realloc)("readlink_path.realloc", buf, bufsiz);
1c194f
+         tries--;
1c194f
+         continue;
1c194f
+      }
1c194f
+      buf[r] = '\0';
1c194f
+      break;
1c194f
+   }
1c194f
+
1c194f
+   if (tries == 0) { // We tried, but weird long path?
1c194f
+      ML_(dinfo_free)(buf);
1c194f
+      return NULL;
1c194f
+   }
1c194f
+
1c194f
+  return buf;
1c194f
+}
1c194f
+
1c194f
 /* The central function for reading ELF debug info.  For the
1c194f
    object/exe specified by the DebugInfo, find ELF sections, then read
1c194f
    the symbols, line number info, file name info, CFA (stack-unwind
1c194f
@@ -2926,8 +2984,12 @@ Bool ML_(read_elf_debug_info) ( struct _DebugInfo* di )
1c194f
                                 (debugaltlink_escn.szB - buildid_offset)
1c194f
                                 * 2 + 1);
1c194f
 
1c194f
-         /* The altfile might be relative to the debug file or main file. */
1c194f
+         /* The altfile might be relative to the debug file or main file.
1c194f
+	    Make sure that we got the real file, not a symlink.  */
1c194f
          HChar *dbgname = di->fsm.dbgname ? di->fsm.dbgname : di->fsm.filename;
1c194f
+         HChar* rdbgname = readlink_path (dbgname);
1c194f
+         if (rdbgname == NULL)
1c194f
+            rdbgname = ML_(dinfo_strdup)("rdbgname", dbgname);
1c194f
 
1c194f
          for (j = 0; j < debugaltlink_escn.szB - buildid_offset; j++)
1c194f
             VG_(sprintf)(
1c194f
@@ -2937,9 +2999,11 @@ Bool ML_(read_elf_debug_info) ( struct _DebugInfo* di )
1c194f
                                         + buildid_offset + j));
1c194f
 
1c194f
          /* See if we can find a matching debug file */
1c194f
-         aimg = find_debug_file( di, dbgname, altbuildid,
1c194f
+         aimg = find_debug_file( di, rdbgname, altbuildid,
1c194f
                                  altfile_str_m, 0, True );
1c194f
 
1c194f
+         ML_(dinfo_free)(rdbgname);
1c194f
+
1c194f
          if (altfile_str_m)
1c194f
             ML_(dinfo_free)(altfile_str_m);
1c194f
          ML_(dinfo_free)(altbuildid);
1c194f
1c194f
diff --git a/coregrind/m_debuginfo/readelf.c b/coregrind/m_debuginfo/readelf.c
1c194f
index c19ff212b..70c28e629 100644
1c194f
--- a/coregrind/m_debuginfo/readelf.c
1c194f
+++ b/coregrind/m_debuginfo/readelf.c
1c194f
@@ -1582,6 +1582,24 @@ static HChar* readlink_path (const HChar *path)
1c194f
       return NULL;
1c194f
    }
1c194f
 
1c194f
+  if (buf[0] == '/')
1c194f
+    return buf;
1c194f
+
1c194f
+  /* Relative path, add link dir.  */
1c194f
+  HChar *linkdirptr;
1c194f
+  SizeT linkdir_len = VG_(strlen)(path);
1c194f
+  if ((linkdirptr = VG_(strrchr)(path, '/')) != NULL)
1c194f
+    linkdir_len -= VG_(strlen)(linkdirptr + 1);
1c194f
+
1c194f
+  SizeT buflen = VG_(strlen)(buf);
1c194f
+  SizeT needed = linkdir_len + buflen + 1;
1c194f
+  if (bufsiz < needed)
1c194f
+    buf = ML_(dinfo_realloc)("readlink_path.linkdir", buf, needed);
1c194f
+
1c194f
+  VG_(memmove)(buf + linkdir_len, buf, buflen);
1c194f
+  VG_(memcpy)(buf, path, linkdir_len);
1c194f
+  buf[needed - 1] = '\0';
1c194f
+
1c194f
   return buf;
1c194f
 }
1c194f
 
1c194f