]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-bus,static-destruct: clean up how we do our ELF section magic
authorLennart Poettering <lennart@poettering.net>
Mon, 3 Dec 2018 12:22:15 +0000 (13:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 3 Dec 2018 12:28:26 +0000 (13:28 +0100)
This cleans up a bit how we set up things for the ELF section magic:

1. Let's always use our gcc macros, instead of __attribute__ directly

2. Align our structures to sizeof(void*), i.e. the pointer size, rather
   than a fixed 8 or __BIGGEST_ALIGNMENT__. The former is unnecessarily
   high for 32bit systems, the latter too high for 64bit systems. gcc
   seems to use ptr alignment for static variables itself, hence this
   should be good enough for us too. Moreover, the Linux kernel also
   uses pointer alginment for all its ELF section registration magic,
   hence this should be good enough for us too.

3. Let's always prefix the sections we create ourself with SYSTEMD_,
   just to make clear where they come from.

4. Always align the pointer we start from when iterating through these
   lists. This should be unnecessary, but makes things nicely
   systematic, as we'll align all pointers we use to access these
   sections properly.

src/basic/static-destruct.h
src/libsystemd/sd-bus/bus-error.c
src/libsystemd/sd-bus/bus-error.h
src/libsystemd/sd-bus/test-bus-error.c

index b780985d117d4b4f609d0890392bfc4ae28e0b95..5c0bea31a6fd6d114c41bdf1790e7472289b95b6 100644 (file)
@@ -22,9 +22,9 @@ typedef struct StaticDestructor {
                 func(q);                                                \
         }                                                               \
         /* The actual destructor structure */                           \
-        __attribute__ ((__section__("SYSTEMD_STATIC_DESTRUCT")))        \
-        __attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__)))            \
-        __attribute__ ((__used__))                                      \
+        _section_("SYSTEMD_STATIC_DESTRUCT")                            \
+        _alignptr_                                                      \
+        _used_                                                          \
         static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
                 .data = &(variable),                                    \
                 .destroy = UNIQ_T(static_destructor_wrapper, uq),       \
@@ -43,9 +43,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*));
         }
 }
index 5ef643134eabfcb167c42651982c2d2e1c222a8a..06097d0b489872d76f0d5292dde2b4687df13e26 100644 (file)
@@ -54,8 +54,8 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_standard_errors[] = {
 };
 
 /* GCC maps this magically to the beginning and end of the BUS_ERROR_MAP section */
-extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
-extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
+extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
+extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
 
 /* Additional maps registered with sd_bus_error_add_map() are in this
  * NULL terminated array */
@@ -89,9 +89,9 @@ static int bus_error_name_to_errno(const char *name) {
                                         return m->code;
                         }
 
-        m = __start_BUS_ERROR_MAP;
+        m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
-        while (m < __stop_BUS_ERROR_MAP) {
+        while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
                 /* For magic ELF error maps, the end marker might
                  * appear in the middle of things, since multiple maps
                  * might appear in the same section. Hence, let's skip
@@ -99,7 +99,7 @@ static int bus_error_name_to_errno(const char *name) {
                  * boundary, which is the selected alignment for the
                  * arrays. */
                 if (m->code == BUS_ERROR_MAP_END_MARKER) {
-                        m = ALIGN8_PTR(m+1);
+                        m = ALIGN_TO_PTR(m + 1, sizeof(void*));
                         continue;
                 }
 
index e8e743c0d75a378325a342219dc4badcd48ddc75..c1758c46bb12397fe7a09aa6bc40db74b7c6a539 100644 (file)
@@ -31,13 +31,13 @@ int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_lis
  */
 
 #define BUS_ERROR_MAP_ELF_REGISTER                                      \
-        __attribute__ ((__section__("BUS_ERROR_MAP")))                  \
-        __attribute__ ((__used__))                                      \
-        __attribute__ ((__aligned__(8)))
+        _section_("SYSTEMD_BUS_ERROR_MAP")                              \
+        _used_                                                          \
+        _alignptr_
 
 #define BUS_ERROR_MAP_ELF_USE(errors)                                   \
         extern const sd_bus_error_map errors[];                         \
-        __attribute__ ((__used__))                                      \
+        _used_                                                          \
         static const sd_bus_error_map * const CONCATENATE(errors ## _copy_, __COUNTER__) = errors;
 
 /* We use something exotic as end marker, to ensure people build the
index 8f1fc54520597eafe9818f2082f5df96017e92fa..f464b5b23de42b7715489957e60ad932b6925f56 100644 (file)
@@ -113,18 +113,18 @@ static void test_error(void) {
         assert_se(!sd_bus_error_is_set(&error));
 }
 
-extern const sd_bus_error_map __start_BUS_ERROR_MAP[];
-extern const sd_bus_error_map __stop_BUS_ERROR_MAP[];
+extern const sd_bus_error_map __start_SYSTEMD_BUS_ERROR_MAP[];
+extern const sd_bus_error_map __stop_SYSTEMD_BUS_ERROR_MAP[];
 
 static void dump_mapping_table(void) {
         const sd_bus_error_map *m;
 
         printf("----- errno mappings ------\n");
-        m = __start_BUS_ERROR_MAP;
-        while (m < __stop_BUS_ERROR_MAP) {
+        m = ALIGN_TO_PTR(__start_SYSTEMD_BUS_ERROR_MAP, sizeof(void*));
+        while (m < __stop_SYSTEMD_BUS_ERROR_MAP) {
 
                 if (m->code == BUS_ERROR_MAP_END_MARKER) {
-                        m = ALIGN8_PTR(m+1);
+                        m = ALIGN_TO_PTR(m + 1, sizeof(void*));
                         continue;
                 }