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
This commit is contained in:
Warner Losh
2024-06-21 09:48:41 -06:00
parent 3fdf11fe8f
commit 4a86b26b84
2 changed files with 23 additions and 7 deletions
+10 -7
View File
@@ -330,23 +330,26 @@ __uint64_t
* parameter, and both are different from the ones expected by the historical * parameter, and both are different from the ones expected by the historical
* FreeBSD qsort_r() interface. * FreeBSD qsort_r() interface.
* *
* Apply a workaround where we explicitly link against the historical * Apply a workaround where we explicitly link against the historical interface,
* interface, qsort_r@FBSD_1.0, in case when qsort_r() is called with * qsort_r@FBSD_1.0, in case when qsort_r() is called with the last parameter
* the last parameter with a function pointer that exactly matches the * with a function pointer that exactly matches the historical FreeBSD qsort_r()
* historical FreeBSD qsort_r() comparator signature, so applications * comparator signature, so applications written for the historical interface
* written for the historical interface can continue to work without * can continue to work without modification. Toolchains that don't support
* modification. * symbol versioning don't define __sym_compat, so only provide this symbol in
* supported environments.
*/ */
#ifdef __sym_compat
#if defined(__generic) || defined(__cplusplus) #if defined(__generic) || defined(__cplusplus)
void __qsort_r_compat(void *, size_t, size_t, void *, void __qsort_r_compat(void *, size_t, size_t, void *,
int (*)(void *, const void *, const void *)); int (*)(void *, const void *, const void *));
__sym_compat(qsort_r, __qsort_r_compat, FBSD_1.0); __sym_compat(qsort_r, __qsort_r_compat, FBSD_1.0);
#endif #endif
#endif
#if defined(__generic) && !defined(__cplusplus) #if defined(__generic) && !defined(__cplusplus)
#define qsort_r(base, nel, width, arg4, arg5) \ #define qsort_r(base, nel, width, arg4, arg5) \
__generic(arg5, int (*)(void *, const void *, const void *), \ __generic(arg5, int (*)(void *, const void *, const void *), \
__qsort_r_compat, qsort_r)(base, nel, width, arg4, arg5) __qsort_r_compat, qsort_r)(base, nel, width, arg4, arg5)
#elif defined(__cplusplus) #elif defined(__cplusplus) && defined(__sym_compat)
__END_DECLS __END_DECLS
extern "C++" { extern "C++" {
static inline void qsort_r(void *base, size_t nmemb, size_t size, static inline void qsort_r(void *base, size_t nmemb, size_t size,
+13
View File
@@ -90,9 +90,18 @@
#define __compiler_membar() __asm __volatile(" " : : : "memory") #define __compiler_membar() __asm __volatile(" " : : : "memory")
#define __CC_SUPPORTS___INLINE 1 #define __CC_SUPPORTS___INLINE 1
#define __CC_SUPPORTS_SYMVER 1
#endif /* __GNUC__ */ #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. * The __CONCAT macro is used to concatenate parts of symbol names, e.g.
* with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo. * with "#define OLD(foo) __CONCAT(old,foo)", OLD(foo) produces oldfoo.
@@ -369,10 +378,12 @@
__asm__(".section .gnu.warning." #sym); \ __asm__(".section .gnu.warning." #sym); \
__asm__(".asciz \"" msg "\""); \ __asm__(".asciz \"" msg "\""); \
__asm__(".previous") __asm__(".previous")
#ifdef __CC_SUPPORTS_SYMVER
#define __sym_compat(sym,impl,verid) \ #define __sym_compat(sym,impl,verid) \
__asm__(".symver " #impl ", " #sym "@" #verid) __asm__(".symver " #impl ", " #sym "@" #verid)
#define __sym_default(sym,impl,verid) \ #define __sym_default(sym,impl,verid) \
__asm__(".symver " #impl ", " #sym "@@@" #verid) __asm__(".symver " #impl ", " #sym "@@@" #verid)
#endif
#else #else
#define __weak_reference(sym,alias) \ #define __weak_reference(sym,alias) \
__asm__(".weak alias"); \ __asm__(".weak alias"); \
@@ -381,10 +392,12 @@
__asm__(".section .gnu.warning.sym"); \ __asm__(".section .gnu.warning.sym"); \
__asm__(".asciz \"msg\""); \ __asm__(".asciz \"msg\""); \
__asm__(".previous") __asm__(".previous")
#ifdef __CC_SUPPORTS_SYMVER
#define __sym_compat(sym,impl,verid) \ #define __sym_compat(sym,impl,verid) \
__asm__(".symver impl, sym@verid") __asm__(".symver impl, sym@verid")
#define __sym_default(impl,sym,verid) \ #define __sym_default(impl,sym,verid) \
__asm__(".symver impl, sym@@@verid") __asm__(".symver impl, sym@@@verid")
#endif
#endif /* __STDC__ */ #endif /* __STDC__ */
#define __GLOBL(sym) __asm__(".globl " __XSTRING(sym)) #define __GLOBL(sym) __asm__(".globl " __XSTRING(sym))