Skip to content

Commit 63e688c

Browse files
authored
Merge pull request systemd#11031 from poettering/gcc-attr-cleanup
various gcc attribute clean-ups
2 parents 1e8817b + 0df5492 commit 63e688c

File tree

18 files changed

+120
-52
lines changed

18 files changed

+120
-52
lines changed

src/basic/hashmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = {
276276
};
277277

278278
#if VALGRIND
279-
__attribute__((destructor)) static void cleanup_pools(void) {
279+
_destructor_ static void cleanup_pools(void) {
280280
_cleanup_free_ char *t = NULL;
281281
int r;
282282

src/basic/macro.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,31 @@
88
#include <sys/sysmacros.h>
99
#include <sys/types.h>
1010

11-
#define _printf_(a, b) __attribute__ ((__format__(printf, a, b)))
11+
#define _printf_(a, b) __attribute__((__format__(printf, a, b)))
1212
#ifdef __clang__
1313
# define _alloc_(...)
1414
#else
15-
# define _alloc_(...) __attribute__ ((__alloc_size__(__VA_ARGS__)))
15+
# define _alloc_(...) __attribute__((__alloc_size__(__VA_ARGS__)))
1616
#endif
17-
#define _sentinel_ __attribute__ ((__sentinel__))
18-
#define _unused_ __attribute__ ((__unused__))
19-
#define _destructor_ __attribute__ ((__destructor__))
20-
#define _pure_ __attribute__ ((__pure__))
21-
#define _const_ __attribute__ ((__const__))
22-
#define _deprecated_ __attribute__ ((__deprecated__))
23-
#define _packed_ __attribute__ ((__packed__))
24-
#define _malloc_ __attribute__ ((__malloc__))
25-
#define _weak_ __attribute__ ((__weak__))
17+
#define _sentinel_ __attribute__((__sentinel__))
18+
#define _section_(x) __attribute__((__section__(x)))
19+
#define _used_ __attribute__((__used__))
20+
#define _unused_ __attribute__((__unused__))
21+
#define _destructor_ __attribute__((__destructor__))
22+
#define _pure_ __attribute__((__pure__))
23+
#define _const_ __attribute__((__const__))
24+
#define _deprecated_ __attribute__((__deprecated__))
25+
#define _packed_ __attribute__((__packed__))
26+
#define _malloc_ __attribute__((__malloc__))
27+
#define _weak_ __attribute__((__weak__))
2628
#define _likely_(x) (__builtin_expect(!!(x), 1))
2729
#define _unlikely_(x) (__builtin_expect(!!(x), 0))
28-
#define _public_ __attribute__ ((__visibility__("default")))
29-
#define _hidden_ __attribute__ ((__visibility__("hidden")))
30+
#define _public_ __attribute__((__visibility__("default")))
31+
#define _hidden_ __attribute__((__visibility__("hidden")))
3032
#define _weakref_(x) __attribute__((__weakref__(#x)))
33+
#define _align_(x) __attribute__((__aligned__(x)))
3134
#define _alignas_(x) __attribute__((__aligned__(__alignof(x))))
35+
#define _alignptr_ __attribute__((__aligned__(sizeof(void*))))
3236
#define _cleanup_(x) __attribute__((__cleanup__(x)))
3337
#if __GNUC__ >= 7
3438
#define _fallthrough_ __attribute__((__fallthrough__))
@@ -56,6 +60,29 @@
5660
# endif
5761
#endif
5862

63+
#if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
64+
# ifdef __SANITIZE_ADDRESS__
65+
# define HAS_FEATURE_ADDRESS_SANITIZER 1
66+
# elif defined(__has_feature)
67+
# if __has_feature(address_sanitizer)
68+
# define HAS_FEATURE_ADDRESS_SANITIZER 1
69+
# endif
70+
# endif
71+
# if !defined(HAS_FEATURE_ADDRESS_SANITIZER)
72+
# define HAS_FEATURE_ADDRESS_SANITIZER 0
73+
# endif
74+
#endif
75+
76+
/* Note: on GCC "no_sanitize_address" is a function attribute only, on llvm it may also be applied to global
77+
* variables. We define a specific macro which knows this. Note that on GCC we don't need this decorator so much, since
78+
* our primary usecase for this attribute is registration structures placed in named ELF sections which shall not be
79+
* padded, but GCC doesn't pad those anyway if AddressSanitizer is enabled. */
80+
#if HAS_FEATURE_ADDRESS_SANITIZER && defined(__clang__)
81+
#define _variable_no_sanitize_address_ __attribute__((__no_sanitize_address__))
82+
#else
83+
#define _variable_no_sanitize_address_
84+
#endif
85+
5986
/* Temporarily disable some warnings */
6087
#define DISABLE_WARNING_FORMAT_NONLITERAL \
6188
_Pragma("GCC diagnostic push"); \

src/basic/process-util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,7 @@ void reset_cached_pid(void) {
11721172
* headers. __register_atfork() is mostly equivalent to pthread_atfork(), but doesn't require us to link against
11731173
* libpthread, as it is part of glibc anyway. */
11741174
extern int __register_atfork(void (*prepare) (void), void (*parent) (void), void (*child) (void), void *dso_handle);
1175-
extern void* __dso_handle __attribute__ ((__weak__));
1175+
extern void* __dso_handle _weak_;
11761176

11771177
pid_t getpid_cached(void) {
11781178
static bool installed = false;

src/basic/static-destruct.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@ typedef struct StaticDestructor {
2222
typeof(variable) *q = p; \
2323
func(q); \
2424
} \
25-
/* The actual destructor structure */ \
26-
__attribute__ ((__section__("SYSTEMD_STATIC_DESTRUCT"))) \
27-
__attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) \
28-
__attribute__ ((__used__)) \
25+
/* The actual destructor structure we place in a special section to find it */ \
26+
_section_("SYSTEMD_STATIC_DESTRUCT") \
27+
/* We pick pointer alignment, since that is apparently what gcc does for static variables */ \
28+
_alignptr_ \
29+
/* Make sure this is not dropped from the image because not explicitly referenced */ \
30+
_used_ \
31+
/* 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. */ \
32+
_variable_no_sanitize_address_ \
2933
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
3034
.data = &(variable), \
3135
.destroy = UNIQ_T(static_destructor_wrapper, uq), \
3236
}
3337

3438
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work even if
3539
* there's not a single destructor is defined in which case the section will be missing. */
36-
extern const struct StaticDestructor __attribute__((__weak__)) __start_SYSTEMD_STATIC_DESTRUCT[];
37-
extern const struct StaticDestructor __attribute__((__weak__)) __stop_SYSTEMD_STATIC_DESTRUCT[];
40+
extern const struct StaticDestructor _weak_ __start_SYSTEMD_STATIC_DESTRUCT[];
41+
extern const struct StaticDestructor _weak_ __stop_SYSTEMD_STATIC_DESTRUCT[];
3842

3943
/* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in the same
4044
* linking unit as the variables we want to destroy. */
@@ -44,9 +48,9 @@ static inline void static_destruct(void) {
4448
if (!__start_SYSTEMD_STATIC_DESTRUCT)
4549
return;
4650

47-
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, __BIGGEST_ALIGNMENT__);
51+
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, sizeof(void*));
4852
while (d < __stop_SYSTEMD_STATIC_DESTRUCT) {
4953
d->destroy(d->data);
50-
d = ALIGN_TO_PTR(d + 1, __BIGGEST_ALIGNMENT__);
54+
d = ALIGN_TO_PTR(d + 1, sizeof(void*));
5155
}
5256
}

src/basic/util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static inline void _reset_errno_(int *saved_errno) {
173173
}
174174

175175
#define PROTECT_ERRNO \
176-
_cleanup_(_reset_errno_) __attribute__((__unused__)) int _saved_errno_ = errno
176+
_cleanup_(_reset_errno_) _unused_ int _saved_errno_ = errno
177177

178178
static inline int negative_errno(void) {
179179
/* This helper should be used to shut up gcc if you know 'errno' is

src/boot/efi/measure.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ typedef struct {
132132
UINT16 HeaderVersion;
133133
UINT32 PCRIndex;
134134
UINT32 EventType;
135-
} __attribute__ ((packed)) EFI_TCG2_EVENT_HEADER;
135+
} __attribute__((packed)) EFI_TCG2_EVENT_HEADER;
136136

137137
typedef struct tdEFI_TCG2_EVENT {
138138
UINT32 Size;
139139
EFI_TCG2_EVENT_HEADER Header;
140140
UINT8 Event[1];
141-
} __attribute__ ((packed)) EFI_TCG2_EVENT;
141+
} __attribute__((packed)) EFI_TCG2_EVENT;
142142

143143
typedef EFI_STATUS(EFIAPI * EFI_TCG2_GET_CAPABILITY) (IN EFI_TCG2_PROTOCOL * This,
144144
IN OUT EFI_TCG2_BOOT_SERVICE_CAPABILITY * ProtocolCapability);

src/journal/lookup3.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ uint32_t jenkins_hashlittle( const void *key, size_t length, uint32_t initval)
319319
* still catch it and complain. The masking trick does make the hash
320320
* noticeably faster for short strings (like English words).
321321
*/
322-
#if !VALGRIND && !defined(__SANITIZE_ADDRESS__)
322+
#if !VALGRIND && !HAS_FEATURE_ADDRESS_SANITIZER
323323

324324
switch(length)
325325
{
@@ -504,7 +504,7 @@ void jenkins_hashlittle2(
504504
* still catch it and complain. The masking trick does make the hash
505505
* noticeably faster for short strings (like English words).
506506
*/
507-
#if !VALGRIND && !defined(__SANITIZE_ADDRESS__)
507+
#if !VALGRIND && !HAS_FEATURE_ADDRESS_SANITIZER
508508

509509
switch(length)
510510
{
@@ -680,7 +680,7 @@ uint32_t jenkins_hashbig( const void *key, size_t length, uint32_t initval)
680680
* still catch it and complain. The masking trick does make the hash
681681
* noticeably faster for short strings (like English words).
682682
*/
683-
#if !VALGRIND && !defined(__SANITIZE_ADDRESS__)
683+
#if !VALGRIND && !HAS_FEATURE_ADDRESS_SANITIZER
684684

685685
switch(length)
686686
{

src/libsystemd/sd-bus/bus-error.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
5454
};
5555

5656
/* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
57-
extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
58-
extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
57+
extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
58+
extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
5959

6060
/* Additional maps registered with sd_bus_error_add_map() are in this
6161
* NULL terminated array */
@@ -89,17 +89,16 @@ static int bus_error_name_to_errno(const char *name) {
8989
return m->code;
9090
}
9191

92-
m = __start_BUS_ERROR_MAP;
93-
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
94-
while (m < __stop_BUS_ERROR_MAP) {
92+
m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
93+
while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
9594
/* For magic ELF error maps, the end marker might
9695
* appear in the middle of things, since multiple maps
9796
* might appear in the same section. Hence, let's skip
9897
* over it, but realign the pointer to the next 8 byte
9998
* boundary, which is the selected alignment for the
10099
* arrays. */
101100
if (m->code == BUS_ERROR_MAP_END_MARKER) {
102-
m = ALIGN8_PTR(m+1);
101+
m = ALIGN_TO_PTR(m + 1, sizeof(void*));
103102
continue;
104103
}
105104

@@ -108,7 +107,6 @@ static int bus_error_name_to_errno(const char *name) {
108107

109108
m++;
110109
}
111-
#endif
112110

113111
return EIO;
114112
}

src/libsystemd/sd-bus/bus-error.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_lis
3131
*/
3232

3333
#define BUS_ERROR_MAP_ELF_REGISTER \
34-
__attribute__ ((__section__("BUS_ERROR_MAP"))) \
35-
__attribute__ ((__used__)) \
36-
__attribute__ ((__aligned__(8)))
34+
_section_("SYSTEMD_BUS_ERROR_MAP") \
35+
_used_ \
36+
_alignptr_ \
37+
_variable_no_sanitize_address_
3738

3839
#define BUS_ERROR_MAP_ELF_USE(errors) \
3940
extern const sd_bus_error_map errors[]; \
40-
__attribute__ ((__used__)) \
41+
_used_ \
4142
static const sd_bus_error_map * const CONCATENATE(errors ## _copy_, __COUNTER__) = errors;
4243

4344
/* We use something exotic as end marker, to ensure people build the

src/libsystemd/sd-bus/test-bus-error.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ static void test_error(void) {
113113
assert_se(!sd_bus_error_is_set(&error));
114114
}
115115

116-
extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
117-
extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
116+
extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
117+
extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
118118

119119
static void dump_mapping_table(void) {
120120
const sd_bus_error_map *m;
121121

122122
printf("----- errno mappings ------\n");
123-
m = __start_BUS_ERROR_MAP;
124-
while (m < __stop_BUS_ERROR_MAP) {
123+
m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
124+
while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
125125

126126
if (m->code == BUS_ERROR_MAP_END_MARKER) {
127-
m = ALIGN8_PTR(m+1);
127+
m = ALIGN_TO_PTR(m + 1, sizeof(void*));
128128
continue;
129129
}
130130

0 commit comments

Comments
 (0)