build: Allow building with -flto on gcc-10 and newer

Global asm statements (like .symver directives) do not work reliably
in gcc with link time optimization. Use the symver attribute introduced
with gcc-10 to set symbol versions instead, if available.

[guillem@hadrons.org:
 - Simplify by using __has_attribute fallback from <sys/cdefs.h>.
 - Coding style changes. ]

Signed-off-by: Guillem Jover <guillem@hadrons.org>
This commit is contained in:
Alexander Miller 2021-09-30 03:39:57 +02:00 committed by Guillem Jover
parent 1808d64b77
commit c7a5d780ae

View File

@ -27,16 +27,28 @@
#ifndef LIBBSD_LOCAL_LINK_H
#define LIBBSD_LOCAL_LINK_H
#include <sys/cdefs.h>
#define libbsd_link_warning(symbol, msg) \
static const char libbsd_emit_link_warning_##symbol[] \
__attribute__((__used__,__section__(".gnu.warning." #symbol))) = msg
#ifdef __ELF__
# if __has_attribute(symver)
/* The symver attribute is supported since gcc 10.x. */
#define libbsd_symver_default(alias, symbol, version) \
extern __typeof(symbol) symbol \
__attribute((__symver__(#alias "@@" #version)))
#define libbsd_symver_variant(alias, symbol, version) \
extern __typeof(symbol) symbol \
__attribute((__symver__(#alias "@" #version)))
# else
#define libbsd_symver_default(alias, symbol, version) \
__asm__(".symver " #symbol "," #alias "@@" #version)
#define libbsd_symver_variant(alias, symbol, version) \
__asm__(".symver " #symbol "," #alias "@" #version)
# endif
#else
#define libbsd_symver_default(alias, symbol, version) \
extern __typeof(symbol) alias __attribute__((__alias__(#symbol)))