PR #2147: Fix FreeBSD/PowerPC* issues Imported from GitHub PR https://github.com/abseil/abseil-cpp/pull/2147 Fixes two issues on FreeBSD PowerPC architectures: 1. UnscaledCycleClock::Frequency(): The `kern.timecounter.tc.timebase.frequency` sysctl is registered in the FreeBSD kernel as CTLTYPE_U64 (uint64_t). Previously, sysctlbyname() read directly into a `double` variable without type conversion, causing the raw integer bits to be reinterpreted as a double. 2. StacktracePowerPCGetLRPtr(): When compiler ABI macros are absent, the unwinder falls back to OS checks. Previously, __FreeBSD__ was grouped with __APPLE__ to read the saved Link Register at sp[2]. While sp[2] (offset 16) is correct for 64-bit ELF / AIX / Darwin, the 32-bit PowerPC SysV ELF ABI saves the LR at sp[1] (offset 4). Fix by keying the fallback on __PPC64__ for 64-bit (sp[2]) versus __linux__ and __FreeBSD__ for 32-bit (sp[1]). Merge 8f4431186a50594b7dced86835d4b0a5be708dd6 into 2adadbc666a2da417194f73694346f41969e0777 Merging this change closes #2147 PiperOrigin-RevId: 976301714 Change-Id: I584c7231e5071cde514a02c082a49f829a414963
diff --git a/absl/base/internal/unscaledcycleclock.cc b/absl/base/internal/unscaledcycleclock.cc index 9e93180..28fe66e 100644 --- a/absl/base/internal/unscaledcycleclock.cc +++ b/absl/base/internal/unscaledcycleclock.cc
@@ -99,9 +99,12 @@ static once_flag init_timebase_frequency_once; static double timebase_frequency = 0.0; base_internal::LowLevelCallOnce(&init_timebase_frequency_once, [&]() { - size_t length = sizeof(timebase_frequency); - sysctlbyname("kern.timecounter.tc.timebase.frequency", &timebase_frequency, - &length, nullptr, 0); + uint64_t freq = 0; + size_t length = sizeof(freq); + if (sysctlbyname("kern.timecounter.tc.timebase.frequency", &freq, &length, + nullptr, 0) == 0) { + timebase_frequency = static_cast<double>(freq); + } }); return timebase_frequency; #else
diff --git a/absl/debugging/internal/stacktrace_powerpc-inl.inc b/absl/debugging/internal/stacktrace_powerpc-inl.inc index f23c29c..a20a715 100644 --- a/absl/debugging/internal/stacktrace_powerpc-inl.inc +++ b/absl/debugging/internal/stacktrace_powerpc-inl.inc
@@ -51,11 +51,10 @@ return (sp + 2); #elif defined(_CALL_SYSV) return (sp + 1); -#elif defined(__APPLE__) || defined(__FreeBSD__) || \ - (defined(__linux__) && defined(__PPC64__)) +#elif defined(__APPLE__) || defined(__PPC64__) // This check is in case the compiler doesn't define _CALL_AIX/etc. return (sp + 2); -#elif defined(__linux) +#elif defined(__linux__) || defined(__FreeBSD__) // This check is in case the compiler doesn't define _CALL_SYSV. return (sp + 1); #else