Merge pull request #11031 from poettering/gcc-attr-cleanup

various gcc attribute clean-ups
This commit is contained in:
Lennart Poettering
2018-12-03 21:59:00 +01:00
committed by GitHub
18 changed files with 120 additions and 52 deletions
+1 -1
View File
@@ -276,7 +276,7 @@ static const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = {
};
#if VALGRIND
__attribute__((destructor)) static void cleanup_pools(void) {
_destructor_ static void cleanup_pools(void) {
_cleanup_free_ char *t = NULL;
int r;
+40 -13
View File
@@ -8,27 +8,31 @@
#include <sys/sysmacros.h>
#include <sys/types.h>
#define _printf_(a, b) __attribute__ ((__format__(printf, a, b)))
#define _printf_(a, b) __attribute__((__format__(printf, a, b)))
#ifdef __clang__
# define _alloc_(...)
#else
# define _alloc_(...) __attribute__ ((__alloc_size__(__VA_ARGS__)))
# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
#endif
#define _sentinel_ __attribute__ ((__sentinel__))
#define _unused_ __attribute__ ((__unused__))
#define _destructor_ __attribute__ ((__destructor__))
#define _pure_ __attribute__ ((__pure__))
#define _const_ __attribute__ ((__const__))
#define _deprecated_ __attribute__ ((__deprecated__))
#define _packed_ __attribute__ ((__packed__))
#define _malloc_ __attribute__ ((__malloc__))
#define _weak_ __attribute__ ((__weak__))
#define _sentinel_ __attribute__((__sentinel__))
#define _section_(x) __attribute__((__section__(x)))
#define _used_ __attribute__((__used__))
#define _unused_ __attribute__((__unused__))
#define _destructor_ __attribute__((__destructor__))
#define _pure_ __attribute__((__pure__))
#define _const_ __attribute__((__const__))
#define _deprecated_ __attribute__((__deprecated__))
#define _packed_ __attribute__((__packed__))
#define _malloc_ __attribute__((__malloc__))
#define _weak_ __attribute__((__weak__))
#define _likely_(x) (__builtin_expect(!!(x), 1))
#define _unlikely_(x) (__builtin_expect(!!(x), 0))
#define _public_ __attribute__ ((__visibility__("default")))
#define _hidden_ __attribute__ ((__visibility__("hidden")))
#define _public_ __attribute__((__visibility__("default")))
#define _hidden_ __attribute__((__visibility__("hidden")))
#define _weakref_(x) __attribute__((__weakref__(#x)))
#define _align_(x) __attribute__((__aligned__(x)))
#define _alignas_(x) __attribute__((__aligned__(__alignof(x))))
#define _alignptr_ __attribute__((__aligned__(sizeof(void*))))
#define _cleanup_(x) __attribute__((__cleanup__(x)))
#if __GNUC__ >= 7
#define _fallthrough_ __attribute__((__fallthrough__))
@@ -56,6 +60,29 @@
# endif
#endif
#if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
# ifdef __SANITIZE_ADDRESS__
# define HAS_FEATURE_ADDRESS_SANITIZER 1
# elif defined(__has_feature)
# if __has_feature(address_sanitizer)
# define HAS_FEATURE_ADDRESS_SANITIZER 1
# endif
# endif
# if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
# define HAS_FEATURE_ADDRESS_SANITIZER 0
# endif
#endif
/* Note: on GCC "no_sanitize_address" is a function attribute only, on llvm it may also be applied to global
* variables. We define a specific macro which knows this. Note that on GCC we don't need this decorator so much, since
* our primary usecase for this attribute is registration structures placed in named ELF sections which shall not be
* padded, but GCC doesn't pad those anyway if AddressSanitizer is enabled. */
#if HAS_FEATURE_ADDRESS_SANITIZER && defined(__clang__)
#define _variable_no_sanitize_address_ __attribute__((__no_sanitize_address__))
#else
#define _variable_no_sanitize_address_
#endif
/* Temporarily disable some warnings */
#define DISABLE_WARNING_FORMAT_NONLITERAL \
_Pragma("GCC diagnostic push"); \
+1 -1
View File
@@ -1172,7 +1172,7 @@ void reset_cached_pid(void) {
* headers. __register_atfork() is mostly equivalent to pthread_atfork(), but doesn't require us to link against
* libpthread, as it is part of glibc anyway. */
extern int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void *dso_handle);
extern void* __dso_handle __attribute__ ((__weak__));
extern void* __dso_handle _weak_;
pid_t getpid_cached(void) {
static bool installed = false;
+12 -8
View File
@@ -22,10 +22,14 @@ typedef struct StaticDestructor {
typeof(variable) *q = p; \
func(q); \
} \
/* The actual destructor structure */ \
__attribute__ ((__section__("SYSTEMD_STATIC_DESTRUCT"))) \
__attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) \
__attribute__ ((__used__)) \
/* The actual destructor structure we place in a special section to find it */ \
_section_("SYSTEMD_STATIC_DESTRUCT") \
/* We pick pointer alignment, since that is apparently what gcc does for static variables */ \
_alignptr_ \
/* Make sure this is not dropped from the image because not explicitly referenced */ \
_used_ \
/* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section packed next to each other so that we can enumerate it. */ \
_variable_no_sanitize_address_ \
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
.data = &(variable), \
.destroy = UNIQ_T(static_destructor_wrapper, uq), \
@@ -33,8 +37,8 @@ typedef struct StaticDestructor {
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work even if
* there's not a single destructor is defined in which case the section will be missing. */
extern const struct StaticDestructor __attribute__((__weak__)) __start_SYSTEMD_STATIC_DESTRUCT[];
extern const struct StaticDestructor __attribute__((__weak__)) __stop_SYSTEMD_STATIC_DESTRUCT[];
extern const struct StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[];
extern const struct StaticDestructor _weak_ __stop_SYSTEMD_STATIC_DESTRUCT[];
/* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in the same
* linking unit as the variables we want to destroy. */
@@ -44,9 +48,9 @@ static inline void static_destruct(void) {
if (!__start_SYSTEMD_STATIC_DESTRUCT)
return;
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, __BIGGEST_ALIGNMENT__);
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, sizeof(void*));
while (d < __stop_SYSTEMD_STATIC_DESTRUCT) {
d->destroy(d->data);
d = ALIGN_TO_PTR(d + 1, __BIGGEST_ALIGNMENT__);
d = ALIGN_TO_PTR(d + 1, sizeof(void*));
}
}
+1 -1
View File
@@ -173,7 +173,7 @@ static inline void _reset_errno_(int *saved_errno) {
}
#define PROTECT_ERRNO \
_cleanup_(_reset_errno_) __attribute__((__unused__)) int _saved_errno_ = errno
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
static inline int negative_errno(void) {
/* This helper should be used to shut up gcc if you know 'errno' is