[compiler-rt] r343510 - [Cfi] Compiling cfi library on FreeBSD and NetBSD

David Carlier via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 1 11:01:55 PDT 2018


Author: devnexen
Date: Mon Oct  1 11:01:55 2018
New Revision: 343510

URL: http://llvm.org/viewvc/llvm-project?rev=343510&view=rev
Log:
[Cfi] Compiling cfi library on FreeBSD and NetBSD

Making the library slighty more portable.

Reviewers: vitalybuka, krytarowski

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D51682

Modified:
    compiler-rt/trunk/lib/cfi/CMakeLists.txt
    compiler-rt/trunk/lib/cfi/cfi.cc
    compiler-rt/trunk/test/cfi/cross-dso/lit.local.cfg
    compiler-rt/trunk/test/lit.common.cfg

Modified: compiler-rt/trunk/lib/cfi/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/cfi/CMakeLists.txt?rev=343510&r1=343509&r2=343510&view=diff
==============================================================================
--- compiler-rt/trunk/lib/cfi/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/cfi/CMakeLists.txt Mon Oct  1 11:01:55 2018
@@ -1,6 +1,6 @@
 add_compiler_rt_component(cfi)
 
-if(OS_NAME MATCHES "Linux")
+if(OS_NAME MATCHES "Linux" OR OS_NAME MATCHES "FreeBSD" OR OS_NAME MATCHES "NetBSD")
   set(CFI_SOURCES cfi.cc)
 
   include_directories(..)

Modified: compiler-rt/trunk/lib/cfi/cfi.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/cfi/cfi.cc?rev=343510&r1=343509&r2=343510&view=diff
==============================================================================
--- compiler-rt/trunk/lib/cfi/cfi.cc (original)
+++ compiler-rt/trunk/lib/cfi/cfi.cc Mon Oct  1 11:01:55 2018
@@ -13,15 +13,32 @@
 
 #include <assert.h>
 #include <elf.h>
+
+#include "sanitizer_common/sanitizer_common.h"
+#if SANITIZER_FREEBSD
+#include <sys/link_elf.h>
+#endif
 #include <link.h>
 #include <string.h>
+#include <stdlib.h>
 #include <sys/mman.h>
 
+#if SANITIZER_LINUX
 typedef ElfW(Phdr) Elf_Phdr;
 typedef ElfW(Ehdr) Elf_Ehdr;
+typedef ElfW(Sym) Elf_Sym;
+typedef ElfW(Dyn) Elf_Dyn;
+#elif SANITIZER_FREEBSD
+#if SANITIZER_WORDSIZE == 64
+#define ElfW64_Dyn Elf_Dyn
+#define ElfW64_Sym Elf_Sym
+#else
+#define ElfW32_Dyn Elf_Dyn
+#define ElfW32_Sym Elf_Sym
+#endif
+#endif
 
 #include "interception/interception.h"
-#include "sanitizer_common/sanitizer_common.h"
 #include "sanitizer_common/sanitizer_flag_parser.h"
 #include "ubsan/ubsan_init.h"
 #include "ubsan/ubsan_flags.h"
@@ -154,15 +171,25 @@ void ShadowBuilder::Add(uptr begin, uptr
     *s = sv;
 }
 
-#if SANITIZER_LINUX
+#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD
 void ShadowBuilder::Install() {
   MprotectReadOnly(shadow_, GetShadowSize());
   uptr main_shadow = GetShadow();
   if (main_shadow) {
     // Update.
+#if SANITIZER_LINUX
     void *res = mremap((void *)shadow_, GetShadowSize(), GetShadowSize(),
                        MREMAP_MAYMOVE | MREMAP_FIXED, (void *)main_shadow);
     CHECK(res != MAP_FAILED);
+#elif SANITIZER_NETBSD
+    void *res = mremap((void *)shadow_, GetShadowSize(), (void *)main_shadow,
+                       GetShadowSize(), MAP_FIXED);
+    CHECK(res != MAP_FAILED);
+#else
+    void *res = MmapFixedOrDie(shadow_, GetShadowSize());
+    CHECK(res != MAP_FAILED);
+    ::memcpy(&shadow_, &main_shadow, GetShadowSize());
+#endif
   } else {
     // Initial setup.
     CHECK_EQ(kCfiShadowLimitsStorageSize, GetPageSizeCached());
@@ -183,17 +210,17 @@ void ShadowBuilder::Install() {
 //    dlopen(RTLD_NOLOAD | RTLD_LAZY)
 //    dlsym("__cfi_check").
 uptr find_cfi_check_in_dso(dl_phdr_info *info) {
-  const ElfW(Dyn) *dynamic = nullptr;
+  const Elf_Dyn *dynamic = nullptr;
   for (int i = 0; i < info->dlpi_phnum; ++i) {
     if (info->dlpi_phdr[i].p_type == PT_DYNAMIC) {
       dynamic =
-          (const ElfW(Dyn) *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
+          (const Elf_Dyn *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
       break;
     }
   }
   if (!dynamic) return 0;
   uptr strtab = 0, symtab = 0, strsz = 0;
-  for (const ElfW(Dyn) *p = dynamic; p->d_tag != PT_NULL; ++p) {
+  for (const Elf_Dyn *p = dynamic; p->d_tag != PT_NULL; ++p) {
     if (p->d_tag == DT_SYMTAB)
       symtab = p->d_un.d_ptr;
     else if (p->d_tag == DT_STRTAB)
@@ -227,7 +254,7 @@ uptr find_cfi_check_in_dso(dl_phdr_info
     return 0;
   }
 
-  for (const ElfW(Sym) *p = (const ElfW(Sym) *)symtab; (ElfW(Addr))p < strtab;
+  for (const Elf_Sym *p = (const Elf_Sym *)symtab; (Elf_Addr)p < strtab;
        ++p) {
     // There is no reliable way to find the end of the symbol table. In
     // lld-produces files, there are other sections between symtab and strtab.

Modified: compiler-rt/trunk/test/cfi/cross-dso/lit.local.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/cfi/cross-dso/lit.local.cfg?rev=343510&r1=343509&r2=343510&view=diff
==============================================================================
--- compiler-rt/trunk/test/cfi/cross-dso/lit.local.cfg (original)
+++ compiler-rt/trunk/test/cfi/cross-dso/lit.local.cfg Mon Oct  1 11:01:55 2018
@@ -5,7 +5,7 @@ def getRoot(config):
 
 root = getRoot(config)
 
-if root.host_os not in ['Linux']:
+if root.host_os not in ['Linux', 'FreeBSD', 'NetBSD']:
   config.unsupported = True
 
 # Android O (API level 26) has support for cross-dso cfi in libdl.so.

Modified: compiler-rt/trunk/test/lit.common.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/lit.common.cfg?rev=343510&r1=343509&r2=343510&view=diff
==============================================================================
--- compiler-rt/trunk/test/lit.common.cfg (original)
+++ compiler-rt/trunk/test/lit.common.cfg Mon Oct  1 11:01:55 2018
@@ -317,7 +317,7 @@ if config.host_os == 'Darwin' and is_dar
   config.lto_supported = True
   config.lto_launch = ["env", "DYLD_LIBRARY_PATH=" + config.llvm_shlib_dir]
   config.lto_flags = []
-elif config.host_os == 'Linux' and is_linux_lto_supported():
+elif config.host_os in ['Linux', 'FreeBSD', 'NetBSD'] and is_linux_lto_supported():
   config.lto_supported = True
   config.lto_launch = []
   if config.use_lld:




More information about the llvm-commits mailing list