]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
libelf: Make elf_version thread-safe
authorHeather McIntyre <hsm2@rice.edu>
Tue, 10 Oct 2023 10:39:53 +0000 (12:39 +0200)
committerMark Wielaard <mark@klomp.org>
Sat, 14 Oct 2023 14:49:25 +0000 (16:49 +0200)
* elf_version.c (version_once): Define once.
(initialize_version): New static function.
(elf_version): Use initialize_version version_once.

Signed-off-by: Heather S. McIntyre <hsm2@rice.edu>
Signed-off-by: Mark Wielaard <mark@klomp.org>
libelf/elf_version.c

index 6ec534ab105b120ff941818c1ed78d460f30cf7a..8296bb657af580fa608aae29972e9a01c3cacc73 100644 (file)
 #endif
 
 #include <libelfP.h>
+#include <pthread.h>
 
+/* Multiple threads may initialize __libelf_version.
+   pthread_once() ensures that __libelf_version is initialized only once. */
+once_define(static, version_once);
 
 /* Currently selected version.  Should be EV_CURRENT.
    Will be EV_NONE if elf_version () has not been called yet.  */
 unsigned int __libelf_version = EV_NONE;
 
+static void initialize_version(void)
+{
+  __libelf_version = EV_CURRENT;
+}
+
 unsigned int
 elf_version (unsigned int version)
 {
@@ -49,7 +58,7 @@ elf_version (unsigned int version)
       /* Phew, we know this version.  */
 
       /* Signal that the version is now initialized.  */
-      __libelf_version = EV_CURRENT;
+      once(version_once, initialize_version);
 
       /* And return the last (or initial) version.  */
       return EV_CURRENT;