diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f8e963..0187b09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -223,6 +223,10 @@ target_compile_features( target_compile_definitions(${target_name} PRIVATE NOMINMAX) +if(HAS_ATTRIBUTE_PACKED) + target_compile_definitions(${target_name} PRIVATE HAS_ATTRIBUTE_PACKED) +endif() + if(NOT CPPTRACE_STD_FORMAT) target_compile_definitions(${target_name} PUBLIC CPPTRACE_NO_STD_FORMAT) endif() diff --git a/cmake/Autoconfig.cmake b/cmake/Autoconfig.cmake index 833b1ef..203a5ee 100644 --- a/cmake/Autoconfig.cmake +++ b/cmake/Autoconfig.cmake @@ -15,6 +15,10 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") check_support(HAS_CXXABI has_cxxabi.cpp "" "" "") endif() +if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + check_support(HAS_ATTRIBUTE_PACKED has_attribute_packed.cpp "" "" "") +endif() + if(NOT WIN32) check_support(HAS_UNWIND has_unwind.cpp "" "" "") check_support(HAS_EXECINFO has_execinfo.cpp "" "" "") diff --git a/cmake/has_attribute_packed.cpp b/cmake/has_attribute_packed.cpp new file mode 100644 index 0000000..9b621b2 --- /dev/null +++ b/cmake/has_attribute_packed.cpp @@ -0,0 +1,6 @@ +struct __attribute__((packed)) foo { + int i; + double d; +}; + +int main() {} diff --git a/src/symbols/dwarf/dwarf_resolver.cpp b/src/symbols/dwarf/dwarf_resolver.cpp index 41b7715..b39f26f 100644 --- a/src/symbols/dwarf/dwarf_resolver.cpp +++ b/src/symbols/dwarf/dwarf_resolver.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -44,19 +45,15 @@ namespace libdwarf { class die_cache { public: struct die_handle { - std::size_t die_index; + std::uint32_t die_index; }; private: - struct basic_range_entry { + struct PACKED basic_range_entry { die_handle die; Dwarf_Addr low; Dwarf_Addr high; }; - static_assert( - sizeof(basic_range_entry) == 3 * sizeof(void*), - "Expected basic_range_entry to be smaller (this is memory critical)" - ); - struct annotated_range_entry { + struct PACKED annotated_range_entry { die_handle die; Dwarf_Addr low; Dwarf_Addr high; @@ -72,7 +69,8 @@ namespace libdwarf { public: die_handle add_die(die_object&& die) { dies.push_back(std::move(die)); - return die_handle{dies.size() - 1}; + VERIFY(dies.size() < std::numeric_limits::max()); + return die_handle{static_cast(dies.size() - 1)}; } template auto insert(die_handle die, Dwarf_Addr low, Dwarf_Addr high) diff --git a/src/utils/common.hpp b/src/utils/common.hpp index 24af611..954f694 100644 --- a/src/utils/common.hpp +++ b/src/utils/common.hpp @@ -30,6 +30,13 @@ #define MSVC_CDECL #endif +// support is pretty good https://godbolt.org/z/djTqv7WMY, checked in cmake during config +#ifdef HAS_ATTRIBUTE_PACKED + #define PACKED __attribute__((packed)) +#else + #define PACKED +#endif + namespace cpptrace { namespace detail { static const stacktrace_frame null_frame {