git: 4a86b26b84be - main - stdlib: Support compiling with tinyc by omitting compat qsort code

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 21 Jun 2024 15:52:48 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=4a86b26b84be2888c70a04f216734bf1456ee766

commit 4a86b26b84be2888c70a04f216734bf1456ee766
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-06-21 15:48:41 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-06-21 15:53:07 +0000

    stdlib: Support compiling with tinyc by omitting compat qsort code
    
    TinyC doesn't support the .symver assembler directive. Add a generic way
    to signal this and use that not to define __sym_() macros that use
    it. Only use the __sym_* macros in headers when they are defined (which
    currently is only for the qsort_r compat code. Not supporting this for
    tcc is fine: It's an edge case for legacy binaries / code anyway which
    isn't relevant to tinyc.
    
    Sponsored by:           Netflix
    Reviewed by:            kib
    Differential Revision:  https://reviews.freebsd.org/D45651
---
 include/stdlib.h | 17 ++++++++++-------
 sys/sys/cdefs.h  | 13 +++++++++++++
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/include/stdlib.h b/include/stdlib.h
index c1c8ab5eebb3..db339688cb78 100644
--- a/include/stdlib.h
+++ b/include/stdlib.h
@@ -330,23 +330,26 @@ __uint64_t
  * parameter, and both are different from the ones expected by the historical
  * FreeBSD qsort_r() interface.
  *
- * Apply a workaround where we explicitly link against the historical
- * interface, qsort_r@FBSD_1.0, in case when qsort_r() is called with
- * the last parameter with a function pointer that exactly matches the
- * historical FreeBSD qsort_r() comparator signature, so applications
- * written for the historical interface can continue to work without
- * modification.
+ * Apply a workaround where we explicitly link against the historical interface,
+ * qsort_r@FBSD_1.0, in case when qsort_r() is called with the last parameter
+ * with a function pointer that exactly matches the historical FreeBSD qsort_r()
+ * comparator signature, so applications written for the historical interface
+ * can continue to work without modification. Toolchains that don't support
+ * symbol versioning don't define __sym_compat, so only provide this symbol in
+ * supported environments.
  */
+#ifdef __sym_compat
 #if defined(__generic) || defined(__cplusplus)
 void __qsort_r_compat(void *, size_t, size_t, void *,
 	    int (*)(void *, const void *, const void *));
 __sym_compat(qsort_r, __qsort_r_compat, FBSD_1.0);
 #endif
+#endif
 #if defined(__generic) && !defined(__cplusplus)
 #define	qsort_r(base, nel, width, arg4, arg5)				\
     __generic(arg5, int (*)(void *, const void *, const void *),	\
         __qsort_r_compat, qsort_r)(base, nel, width, arg4, arg5)
-#elif defined(__cplusplus)
+#elif defined(__cplusplus) && defined(__sym_compat)
 __END_DECLS
 extern "C++" {
 static inline void qsort_r(void *base, size_t nmemb, size_t size,
diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index 43d179b82020..419fc260f200 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -90,9 +90,18 @@
 #define	__compiler_membar()	__asm __volatile(" " : : : "memory")
 
 #define	__CC_SUPPORTS___INLINE 1
+#define	__CC_SUPPORTS_SYMVER 1
 
 #endif /* __GNUC__ */
 
+/*
+ * TinyC pretends to be gcc 9.3. This is generally good enough to support
+ * everything FreeBSD... except for the .symver assembler directive.
+ */
+#ifdef __TINYC__
+#undef	__CC_SUPPORTS_SYMVER
+#endif
+
 /*
  * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
  * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
@@ -369,10 +378,12 @@
 	__asm__(".section .gnu.warning." #sym);	\
 	__asm__(".asciz \"" msg "\"");	\
 	__asm__(".previous")
+#ifdef	__CC_SUPPORTS_SYMVER
 #define	__sym_compat(sym,impl,verid)	\
 	__asm__(".symver " #impl ", " #sym "@" #verid)
 #define	__sym_default(sym,impl,verid)	\
 	__asm__(".symver " #impl ", " #sym "@@@" #verid)
+#endif
 #else
 #define	__weak_reference(sym,alias)	\
 	__asm__(".weak alias");		\
@@ -381,10 +392,12 @@
 	__asm__(".section .gnu.warning.sym"); \
 	__asm__(".asciz \"msg\"");	\
 	__asm__(".previous")
+#ifdef	__CC_SUPPORTS_SYMVER
 #define	__sym_compat(sym,impl,verid)	\
 	__asm__(".symver impl, sym@verid")
 #define	__sym_default(impl,sym,verid)	\
 	__asm__(".symver impl, sym@@@verid")
+#endif
 #endif	/* __STDC__ */
 
 #define	__GLOBL(sym)	__asm__(".globl " __XSTRING(sym))