[PATCH] D73990: [Sanitizers] Get link map on FreeBSD via documented API

Dimitry Andric via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 4 13:09:58 PST 2020


dim created this revision.
dim added reviewers: devnexen, emaste, MaskRay.
Herald added projects: Sanitizers, LLVM.
Herald added a subscriber: Sanitizers.

Instead of hand-crafting an offset into the structure returned by
dlopen(3) to get at the link map, use the documented API.  This is
described in dlinfo(3): by calling it with `RTLD_DI_LINKMAP`, the
dynamic linker ensures the right address is returned.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73990

Files:
  compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
  compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h


Index: compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
+++ compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
@@ -21,15 +21,17 @@
 
 #include "sanitizer_platform_limits_posix.h"
 
-// FreeBSD's dlopen() returns a pointer to an Obj_Entry structure that
-// incorporates the map structure.
-# define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
-    ((link_map*)((handle) == nullptr ? nullptr : ((char*)(handle) + 560)))
 // Get sys/_types.h, because that tells us whether 64-bit inodes are
 // used in struct dirent below.
 #include <sys/_types.h>
 
 namespace __sanitizer {
+// FreeBSD's dlopen() returns a pointer to an Obj_Entry structure that
+// incorporates the map structure.
+void *__sanitizer_get_link_map_by_dlopen_handle(void *handle);
+# define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
+    __sanitizer_get_link_map_by_dlopen_handle(handle)
+
 extern unsigned struct_utsname_sz;
 extern unsigned struct_stat_sz;
 #if defined(__powerpc64__)
Index: compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
===================================================================
--- compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
+++ compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
@@ -17,6 +17,7 @@
 
 #include <arpa/inet.h>
 #include <dirent.h>
+#include <dlfcn.h>
 #include <fts.h>
 #include <fstab.h>
 #include <grp.h>
@@ -91,6 +92,12 @@
 #include "sanitizer_platform_limits_freebsd.h"
 
 namespace __sanitizer {
+  void *__sanitizer_get_link_map_by_dlopen_handle(void* handle)
+  {
+    void *p = nullptr;
+    return dlinfo(handle, RTLD_DI_LINKMAP, &p) == 0 ? p : nullptr;
+  }
+
   unsigned struct_cap_rights_sz = sizeof(cap_rights_t);
   unsigned struct_utsname_sz = sizeof(struct utsname);
   unsigned struct_stat_sz = sizeof(struct stat);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73990.242410.patch
Type: text/x-patch
Size: 1988 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200204/5dbf20ad/attachment.bin>


More information about the llvm-commits mailing list