Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Fix usage of unavailable weak symbol clock_gettime() on Apple platform. #3048
Latest Apple SDK declares clock_gettime() and it is available during compile time but it may not always be available during run time - weak symbol. E.g. code compiles but later will crash if clock_gettime() is used on unsupported version of Apple OS.
clock_gettime() is supported since these OS versions: macOS 10.12, iOS 10, tvOS 10, watchOS 3.
This fix uses __builtin_available() Clang's extension which tests OS version and does not allow to use clock_gettime() on unsupported OS version.
There is only small overhead on Apple OS due to have_clock_gettime variable check but it makes binary portable (clock_gettime() will not be used on unsupported OS version) and ready for usage of high-resolution monotonic clock on Apple OS (clock_gettime() will be used on supported OS version).
Other platforms are unaffected by this implementation due to HAVE_CLOCK_GETTIME_CHECK define used in place.
nickzman left a comment
I still don't like this. Is there any reason why we can't use traditional weak-linking here instead of __builtin_available()? __builtin_available() won't work with compilers other than LLVM.
If we can't use traditional weak-linking, then why don't we use the HAVE_BUILTIN_AVAILABLE preprocessor macro instead of making a new one?
If you mean by weak-linking the
Proposed changes do not affect GCC users in any sense, so there is no limitation, code compiles and works successfully. If GCC user defines
But the main problem with GCC on Apple platform - it is depreciated by Apple and thus it does not make sense to try to use GCC + recent Apple SDK based on LLVM/Clang to compile binaries for production. I do not think anybody will do that. And older Apple SDK, which is using/compatible with GCC. does not have
To avoid confusion with public defines of
@bagder, agree that variable may be local. Also replaced check for
#define HAVE_BUILTIN_AVAILABLE 0
I suppose this is fine then. By "traditional" weak linking, I meant the way it was done prior to __builtin_available(), which was to check at run-time if the symbol was defined or not. The darwinssl.c code still does this for backward compatibility with older compilers, and it hasn't caused any problems. But using __builtin_available() will solve this for the vast majority of users.