Rizin
unix-like reverse engineering framework and cli tools
rz_constructor.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 // SPDX-FileCopyrightText: 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 
4 // Taken from https://github.com/GNOME/glib/blob/main/glib/gconstructor.h and adapted to Rizin
5 
6 #ifndef __RZ_CONSTRUCTOR_H__
7 #define __RZ_CONSTRUCTOR_H__
8 
9 /*
10  If RZ_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
11  destructors, in a usable way, including e.g. on library unload. If not you're on
12  your own.
13  Some compilers need #pragma to handle this, which does not work with macros,
14  so the way you need to use this is (for constructors):
15  #ifdef RZ_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
16  #pragma RZ_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
17  #endif
18  RZ_DEFINE_CONSTRUCTOR(my_constructor)
19  static void my_constructor(void) {
20  ...
21  }
22 */
23 
24 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
25 
26 #define RZ_HAS_CONSTRUCTORS 1
27 
28 #define RZ_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func(void);
29 #define RZ_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func(void);
30 
31 #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
32 /* Visual studio 2008 and later has _Pragma */
33 
34 #include <stdlib.h>
35 
36 #define RZ_HAS_CONSTRUCTORS 1
37 
38 /* We do some weird things to avoid the constructors being optimized
39  * away on VS2015 if WholeProgramOptimization is enabled. First we
40  * make a reference to the array from the wrapper to make sure its
41  * references. Then we use a pragma to make sure the wrapper function
42  * symbol is always included at the link stage. Also, the symbols
43  * need to be extern (but not dllexport), even though they are not
44  * really used from another object file.
45  */
46 
47 /* We need to account for differences between the mangling of symbols
48  * for x86 and x64/ARM/ARM64 programs, as symbols on x86 are prefixed
49  * with an underscore but symbols on x64/ARM/ARM64 are not.
50  */
51 #ifdef _M_IX86
52 #define RZ_MSVC_SYMBOL_PREFIX "_"
53 #else
54 #define RZ_MSVC_SYMBOL_PREFIX ""
55 #endif
56 
57 #define RZ_DEFINE_CONSTRUCTOR(_func) RZ_MSVC_CTOR(_func, RZ_MSVC_SYMBOL_PREFIX)
58 #define RZ_DEFINE_DESTRUCTOR(_func) RZ_MSVC_DTOR(_func, RZ_MSVC_SYMBOL_PREFIX)
59 
60 #define RZ_MSVC_CTOR(_func, _sym_prefix) \
61  static void _func(void); \
62  extern int (*_array##_func)(void); \
63  int _func##_wrapper(void) { \
64  _func(); \
65  char *_func##_var = rz_str_new(""); \
66  free(_func##_var); \
67  return 0; \
68  } \
69  __pragma(comment(linker, "/include:" _sym_prefix #_func "_wrapper")) \
70  __pragma(section(".CRT$XCU", read)) __declspec(allocate(".CRT$XCU")) int (*_array##_func)(void) = _func##_wrapper;
71 
72 #define RZ_MSVC_DTOR(_func, _sym_prefix) \
73  static void _func(void); \
74  extern int (*_array##_func)(void); \
75  int _func##_constructor(void) { \
76  atexit(_func); \
77  char *_func##_var = rz_str_new(""); \
78  free(_func##_var); \
79  return 0; \
80  } \
81  __pragma(comment(linker, "/include:" _sym_prefix #_func "_constructor")) \
82  __pragma(section(".CRT$XCU", read)) __declspec(allocate(".CRT$XCU")) int (*_array##_func)(void) = _func##_constructor;
83 
84 #elif defined(_MSC_VER)
85 
86 #define RZ_HAS_CONSTRUCTORS 1
87 
88 /* Pre Visual studio 2008 must use #pragma section */
89 #define RZ_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
90 #define RZ_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
91 
92 #define RZ_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
93  section(".CRT$XCU", read)
94 #define RZ_DEFINE_CONSTRUCTOR(_func) \
95  static void _func(void); \
96  static int _func##_wrapper(void) { \
97  _func(); \
98  return 0; \
99  } \
100  __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func##_wrapper;
101 
102 #define RZ_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
103  section(".CRT$XCU", read)
104 #define RZ_DEFINE_DESTRUCTOR(_func) \
105  static void _func(void); \
106  static int _func##_constructor(void) { \
107  atexit(_func); \
108  return 0; \
109  } \
110  __declspec(allocate(".CRT$XCU")) static int (*_array##_func)(void) = _func##_constructor;
111 
112 #elif defined(__SUNPRO_C)
113 
114 /* This is not tested, but i believe it should work, based on:
115  * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
116  */
117 
118 #define RZ_HAS_CONSTRUCTORS 1
119 
120 #define RZ_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
121 #define RZ_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
122 
123 #define RZ_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
124  init(_func)
125 #define RZ_DEFINE_CONSTRUCTOR(_func) \
126  static void _func(void);
127 
128 #define RZ_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
129  fini(_func)
130 #define RZ_DEFINE_DESTRUCTOR(_func) \
131  static void _func(void);
132 
133 #else
134 
135 /* constructors not supported for this compiler */
136 
137 #endif
138 
139 #endif /* __RZ_CONSTRUCTOR_H__ */