1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
From 9497a56f7158339b56421b932760851feb39e5ad Mon Sep 17 00:00:00 2001
From: Florian Weimer <fweimer@redhat.com>
Date: Wed, 20 May 2020 19:38:35 +0200
Subject: [PATCH] open, open64: Eliminate dependency on ELF constructor
ordering
Another shared object may call open or open64 before the nosync
ELF constructor has run. Relocation dependencies do not factor
into ELF constructor ordering.
---
open.c | 33 ++++++++++++++++-----------------
1 file changed, 16 insertions(+), 17 deletions(-)
diff --git a/open.c b/open.c
index 89c896d..f96f542 100644
--- a/open.c
+++ b/open.c
@@ -17,33 +17,32 @@
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
+#include <stddef.h>
#include <sys/stat.h>
#include <sys/types.h>
+/* Avoid infinite recursion if called from dlsym(). The implementation
+ assumes that open is called before the process goes multi-threaded. */
static int dlsym_pending;
#define OPEN(open) \
\
-static int (*real_ ## open)(const char *, int, ...); \
- \
int __nosync_ ## open(const char *path, int flags, mode_t mode) \
{ \
- /* Avoid infinite recursion if called from dlsym(). */ \
- if (__builtin_expect(dlsym_pending, 0)) { \
- errno = ENOSYS; \
- return -1; \
+ static int (*real)(const char *, int, ...); \
+ int (*real_copy)(const char *, int, ...) \
+ = __atomic_load_n(&real, __ATOMIC_RELAXED); \
+ if (real_copy == NULL) { \
+ if (dlsym_pending) { \
+ errno = ENOSYS; \
+ return -1; \
+ } \
+ dlsym_pending = 1; \
+ real_copy = dlsym(RTLD_NEXT, #open); \
+ dlsym_pending = 0; \
+ __atomic_store_n(&real, real_copy, __ATOMIC_RELAXED); \
} \
- \
- return real_ ## open(path, flags & ~(O_SYNC | O_DSYNC), mode); \
-} \
- \
-__attribute__((constructor)) \
-static void \
-init_ ## open(void) \
-{ \
- dlsym_pending = 1; \
- real_ ## open = dlsym(RTLD_NEXT, #open); \
- dlsym_pending = 0; \
+ return real_copy(path, flags & ~(O_SYNC | O_DSYNC), mode); \
} \
\
int open(const char *, int, ...) \
|