Bug 387773

Summary: Files in .gnu_debugaltlink should be resolved relative to .debug file, not symlink.
Product: [Developer tools] valgrind Reporter: Mark Wielaard <mark>
Component: generalAssignee: Julian Seward <jseward>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version First Reported In: 3.14 SVN   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:
Attachments: Fix gnu debug alt file resolving.

Description Mark Wielaard 2017-12-10 13:39:16 UTC
Created attachment 109296 [details]
Fix gnu debug alt file resolving.

In some situations (Fedora 27) the .debug file is available through a build-id symlink, but the debug alt file isn't (which arguably isn't ideal, and probably should be fixed). In such cases resolving the debug alt file from the relative path in the .gnu_debugaltlink should be done against the actual .debug file, not against the (build-id) symlink that pointed to the .debug file.

This causes issues like:

$ valgrind -q /usr/libexec/pcp/bin/telnet-probe
--17775-- WARNING: Serious error when reading debug info
--17775-- When reading debug info from /usr/libexec/pcp/bin/telnet-probe:
--17775-- get_Form_contents: DW_FORM_GNU_strp_alt used, but no alternate .debug_str

This is caused because the relative debug alt file path is resolved against the build-id symlink file, instead of against the actual .debug file.

/usr/lib/debug/.build-id/87/1a7e7175751df1a2017ef448e5ebf390270c6f.debug -> ../../../../../usr/lib/debug/usr/libexec/pcp/bin/telnet-probe-3.12.1-3.fc27.x86_64.debug

The attached patch fixes this by making sure that the file against which any relative paths are resolved isn't a symlink.
Comment 1 Mark Wielaard 2017-12-12 23:24:49 UTC
commit be82bb5f9dfecd854c53eda321d1914f28f19790
Author: Mark Wielaard <mark@klomp.org>
Date:   Sat Dec 9 23:01:29 2017 +0100

    Fix gnu debug alt file resolving.
    
    https://bugs.kde.org/show_bug.cgi?id=387773
    
    The path to the alt file is relative to the actual debug file.
    Make sure that we got the real file, not a (build-id) symlink.
    Also handle the case where a debug or alt file is an absolute path.
Comment 2 Mark Wielaard 2018-01-02 22:44:37 UTC
This is still not completely correct. If the symlink itself contains a relative path then we need to add the symlink dir before it. Something like the following:

diff --git a/coregrind/m_debuginfo/readelf.c b/coregrind/m_debuginfo/readelf.c
index c19ff212b..70c28e629 100644
--- a/coregrind/m_debuginfo/readelf.c
+++ b/coregrind/m_debuginfo/readelf.c
@@ -1582,6 +1582,24 @@ static HChar* readlink_path (const HChar *path)
       return NULL;
    }
 
+  if (buf[0] == '/')
+    return buf;
+
+  /* Relative path, add link dir.  */
+  HChar *linkdirptr;
+  SizeT linkdir_len = VG_(strlen)(path);
+  if ((linkdirptr = VG_(strrchr)(path, '/')) != NULL)
+    linkdir_len -= VG_(strlen)(linkdirptr + 1);
+
+  SizeT buflen = VG_(strlen)(buf);
+  SizeT needed = linkdir_len + buflen + 1;
+  if (bufsiz < needed)
+    buf = ML_(dinfo_realloc)("readlink_path.linkdir", buf, needed);
+
+  VG_(memmove)(buf + linkdir_len, buf, buflen);
+  VG_(memcpy)(buf, path, linkdir_len);
+  buf[needed - 1] = '\0';
+
   return buf;
 }
Comment 3 Mark Wielaard 2018-01-13 13:42:31 UTC
Tested additional fix in Fedora and committed now as

commit 7d0403032250c8985ae99a96af7bcd9190ad654b
Author: Mark Wielaard <mark@klomp.org>
Date:   Sat Jan 13 14:33:50 2018 +0100

    Additional fix for gnu debug alt file resolving.
    
    Also handle the case where the symlink itself contains a relative path.
    Then we need to add the symlink dir before it.
    
    https://bugs.kde.org/show_bug.cgi?id=387773