[libcxx-commits] [PATCH] D77606: [WIP][libcxxabi] Add macro for changing functions to support the relative vtables ABI

Leonard Chan via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 6 16:22:16 PDT 2020


leonardchan created this revision.
leonardchan added reviewers: ldionne, howard.hinnant, mcgrathr.
leonardchan added a project: libc++abi.
Herald added subscribers: dexonsmith, mgorny.
Herald added a reviewer: libc++abi.
leonardchan planned changes to this revision.
leonardchan added a comment.

Marking WIP for now since (I think for testing), this will require some separate clang changes first.


This patch adds `LIBCXXABI_RELATIVE_VTABLES_ABI_ENABLED` which changes libcxxabi functions to operate properly under the relative vtables ABI (see D72959 <https://reviews.llvm.org/D72959>) which replaces the 64 function pointers in the vtable with integer offsets between the function and the vtable. This is also applied to RTTI, which replaces an 8-byte pointer to a typeinfo struct with a 4-byte int offset.

Under this ABI, `__dynamic_cast` will not work since it assumes the vtable pointer is 2 `ptrdiff_t`s away from the start of the vtable (8-byte offset to top + 8-byte pointer to typeinfo) when it is actually 12 bytes away (8-byte offset to top + 4-byte offset to typeinfo). This also adjusts the logic under `__dynamic_cast` to support this ABI.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77606

Files:
  libcxxabi/CMakeLists.txt
  libcxxabi/src/private_typeinfo.cpp


Index: libcxxabi/src/private_typeinfo.cpp
===================================================================
--- libcxxabi/src/private_typeinfo.cpp
+++ libcxxabi/src/private_typeinfo.cpp
@@ -614,10 +614,26 @@
     // Possible future optimization:  Take advantage of src2dst_offset
 
     // Get (dynamic_ptr, dynamic_type) from static_ptr
+#ifdef _LIBCXXABI_RELATIVE_VTABLES_ABI_ENABLED
+    // The vtable address will point to the first virtual function, which is 12
+    // bytes after the start of the vtable (8 for the offset from top + 4 for the typeinfo component).
+    uint8_t* vtable = *reinterpret_cast<uint8_t* const*>(static_ptr);
+    ptrdiff_t offset_to_derived = *(reinterpret_cast<ptrdiff_t*>(vtable - 12));
+
+    const void* dynamic_ptr =
+        static_cast<const char*>(static_ptr) + offset_to_derived;
+
+    // The typeinfo component is now a relative offset to a proxy.
+    int32_t offset_to_ti_proxy = *reinterpret_cast<int32_t*>(vtable - 4);
+    uint8_t* ptr_to_ti_proxy = vtable + offset_to_ti_proxy;
+    const __class_type_info* dynamic_type =
+        *(reinterpret_cast<__class_type_info**>(ptr_to_ti_proxy));
+#else
     void **vtable = *static_cast<void ** const *>(static_ptr);
     ptrdiff_t offset_to_derived = reinterpret_cast<ptrdiff_t>(vtable[-2]);
     const void* dynamic_ptr = static_cast<const char*>(static_ptr) + offset_to_derived;
     const __class_type_info* dynamic_type = static_cast<const __class_type_info*>(vtable[-1]);
+#endif
 
     // Initialize answer to nullptr.  This will be changed from the search
     //    results if a non-null answer is found.  Regardless, this is what will
Index: libcxxabi/CMakeLists.txt
===================================================================
--- libcxxabi/CMakeLists.txt
+++ libcxxabi/CMakeLists.txt
@@ -171,6 +171,11 @@
 option(LIBCXXABI_HERMETIC_STATIC_LIBRARY
   "Do not export any symbols from the static library." OFF)
 
+option(LIBCXXABI_RELATIVE_VTABLES_ABI_ENABLED
+  "Use different configurations of libcxxabi functions supported for the \
+  Relative VTables ABI, which replaces vtable function pointers with offsets \
+  between those virtual functions and the vtable itself." OFF)
+
 #===============================================================================
 # Configure System
 #===============================================================================
@@ -470,6 +475,10 @@
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FLAGS}")
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBCXXABI_C_FLAGS}")
 
+if (LIBCXXABI_RELATIVE_VTABLES_ABI_ENABLED)
+  add_definitions(-D_LIBCXXABI_RELATIVE_VTABLES_ABI_ENABLED)
+endif()
+
 #===============================================================================
 # Setup Source Code
 #===============================================================================


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77606.255529.patch
Type: text/x-patch
Size: 2832 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20200406/c5a10862/attachment-0001.bin>


More information about the libcxx-commits mailing list