]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: Change the way we provide builtins
authorJan Janssen <medhefgo@web.de>
Sat, 10 Sep 2022 14:56:48 +0000 (16:56 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 20 Sep 2022 10:43:48 +0000 (12:43 +0200)
Relying on symbol aliasing seems to be rather unreliable. Instead just
use some light #ifdefery.

Fixes: #24630
src/boot/efi/efi-string.c
src/boot/efi/efi-string.h
src/boot/efi/util.h

index dfb2b06a75924609807c16f1c32e39f59259a6d3..6929e8667efedc0c0c7a1b1bcd506279898953d8 100644 (file)
@@ -128,7 +128,8 @@ DEFINE_STRCHR(char16_t, strchr16);
                 size_t size = len * sizeof(type);         \
                                                           \
                 type *dup = xmalloc(size + sizeof(type)); \
-                efi_memcpy(dup, s, size);                 \
+                if (size > 0)                             \
+                        memcpy(dup, s, size);             \
                 dup[len] = '\0';                          \
                                                           \
                 return dup;                               \
@@ -271,7 +272,19 @@ bool efi_fnmatch(const char16_t *pattern, const char16_t *haystack) {
 DEFINE_PARSE_NUMBER(char, parse_number8);
 DEFINE_PARSE_NUMBER(char16_t, parse_number16);
 
-int efi_memcmp(const void *p1, const void *p2, size_t n) {
+#ifdef SD_BOOT
+/* To provide the actual implementation for these we need to remove the redirection to the builtins. */
+#  undef memcmp
+#  undef memcpy
+#  undef memset
+#else
+/* And for userpsace unit testing we need to give them an efi_ prefix. */
+#  define memcmp efi_memcmp
+#  define memcpy efi_memcpy
+#  define memset efi_memset
+#endif
+
+_used_ int memcmp(const void *p1, const void *p2, size_t n) {
         const uint8_t *up1 = p1, *up2 = p2;
         int r;
 
@@ -291,7 +304,7 @@ int efi_memcmp(const void *p1, const void *p2, size_t n) {
         return 0;
 }
 
-void *efi_memcpy(void * restrict dest, const void * restrict src, size_t n) {
+_used_ _weak_ void *memcpy(void * restrict dest, const void * restrict src, size_t n) {
         if (!dest || !src || n == 0)
                 return dest;
 
@@ -318,7 +331,7 @@ void *efi_memcpy(void * restrict dest, const void * restrict src, size_t n) {
         return dest;
 }
 
-void *efi_memset(void *p, int c, size_t n) {
+_used_ _weak_ void *memset(void *p, int c, size_t n) {
         if (!p || n == 0)
                 return p;
 
@@ -339,16 +352,3 @@ void *efi_memset(void *p, int c, size_t n) {
 
         return p;
 }
-
-#ifdef SD_BOOT
-#  undef memcmp
-#  undef memcpy
-#  undef memset
-/* Provide the actual implementation for the builtins by providing aliases. These need to be marked as used,
- * as otherwise the compiler might remove them but still emit calls, which would break when linking.
- * To prevent a different linker error, we mark memcpy/memset as weak, because gnu-efi is currently
- * providing them. */
-__attribute__((used, alias("efi_memcmp"))) int memcmp(const void *p1, const void *p2, size_t n);
-__attribute__((used, weak, alias("efi_memcpy"))) void *memcpy(void * restrict dest, const void * restrict src, size_t n);
-__attribute__((used, weak, alias("efi_memset"))) void *memset(void *p, int c, size_t n);
-#endif
index 57899c978bf7a1e2a54c225696629ea5cb6b4bf3..1ebd5fd6b7d42b3c0821af465f085706b483f070 100644 (file)
@@ -119,9 +119,9 @@ static inline void *mempcpy(void * restrict dest, const void * restrict src, siz
         memcpy(dest, src, n);
         return (uint8_t *) dest + n;
 }
-#endif
-
-/* The actual implementations of builtins with efi_ prefix so we can unit test them. */
+#else
+/* For unit testing. */
 int efi_memcmp(const void *p1, const void *p2, size_t n);
 void *efi_memcpy(void * restrict dest, const void * restrict src, size_t n);
 void *efi_memset(void *p, int c, size_t n);
+#endif
index afbc217d5357d3b9e64725fee1c7a7bdd2ee4de6..684930d5d66d3ab283d55fb7aea21e3ba7d42092 100644 (file)
@@ -60,7 +60,9 @@ static inline void *xmalloc_multiply(size_t size, size_t n) {
 _malloc_ _alloc_(3) _returns_nonnull_ _warn_unused_result_
 static inline void *xrealloc(void *p, size_t old_size, size_t new_size) {
         void *r = xmalloc(new_size);
-        efi_memcpy(r, p, MIN(old_size, new_size));
+        new_size = MIN(old_size, new_size);
+        if (new_size > 0)
+                memcpy(r, p, new_size);
         free(p);
         return r;
 }