Skip to content

Commit c49b560

Browse files
fanquakejanus
authored andcommitted
compat: Consolidate mingw-w64 ASLR workaround for upstream libsecp changes
Achieve this by adding a MAIN_FUNCTION macro, consolidating the docs, and introducing the macro across our distributed binaries. Also update the docs to explain that anyone using binutils < 2.36 is effected by this issue. Release builds are not, because they use binutils 2.37. Currently LTS Linux distros, like Ubuntu Focal, ship with 2.34. https://packages.ubuntu.com/focal/binutils
1 parent 7ddf096 commit c49b560

File tree

7 files changed

+27
-24
lines changed

7 files changed

+27
-24
lines changed

src/BGL-cli.cpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <chainparamsbase.h>
1111
#include <clientversion.h>
12+
#include <compat.h>
1213
#include <compat/stdin.h>
1314
#include <policy/feerate.h>
1415
#include <rpc/client.h>
@@ -1211,19 +1212,11 @@ static int CommandLineRPC(int argc, char *argv[])
12111212
return nRet;
12121213
}
12131214

1214-
#ifdef WIN32
1215-
// Export main() and ensure working ASLR on Windows.
1216-
// Exporting a symbol will prevent the linker from stripping
1217-
// the .reloc section from the binary, which is a requirement
1218-
// for ASLR. This is a temporary workaround until a fixed
1219-
// version of binutils is used for releases.
1220-
__declspec(dllexport) int main(int argc, char* argv[])
1215+
MAIN_FUNCTION
12211216
{
1217+
#ifdef WIN32
12221218
util::WinCmdLineArgs winArgs;
12231219
std::tie(argc, argv) = winArgs.get();
1224-
#else
1225-
int main(int argc, char* argv[])
1226-
{
12271220
#endif
12281221
SetupEnvironment();
12291222
if (!SetupNetworking()) {

src/BGL-tx.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <clientversion.h>
1010
#include <coins.h>
11+
#include <compat.h>
1112
#include <consensus/amount.h>
1213
#include <consensus/consensus.h>
1314
#include <core_io.h>
@@ -853,7 +854,7 @@ static int CommandLineRawTx(int argc, char* argv[])
853854
return nRet;
854855
}
855856

856-
int main(int argc, char* argv[])
857+
MAIN_FUNCTION
857858
{
858859
SetupEnvironment();
859860

src/BGL-util.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <chainparams.h>
1212
#include <chainparamsbase.h>
1313
#include <clientversion.h>
14+
#include <compat.h>
1415
#include <core_io.h>
1516
#include <streams.h>
1617
#include <util/system.h>
@@ -142,16 +143,7 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
142143
return EXIT_SUCCESS;
143144
}
144145

145-
#ifdef WIN32
146-
// Export main() and ensure working ASLR on Windows.
147-
// Exporting a symbol will prevent the linker from stripping
148-
// the .reloc section from the binary, which is a requirement
149-
// for ASLR. This is a temporary workaround until a fixed
150-
// version of binutils is used for releases.
151-
__declspec(dllexport) int main(int argc, char* argv[])
152-
#else
153-
int main(int argc, char* argv[])
154-
#endif
146+
MAIN_FUNCTION
155147
{
156148
ArgsManager& args = gArgs;
157149
SetupEnvironment();

src/BGL-wallet.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <chainparams.h>
1010
#include <chainparamsbase.h>
11+
#include <clientversion.h>
12+
#include <compat.h>
1113
#include <interfaces/init.h>
1214
#include <logging.h>
1315
#include <util/system.h>
@@ -81,7 +83,7 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
8183
return true;
8284
}
8385

84-
int main(int argc, char* argv[])
86+
MAIN_FUNCTION
8587
{
8688
ArgsManager& args = gArgs;
8789
#ifdef WIN32

src/BGLd.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
256256
return fRet;
257257
}
258258

259-
int main(int argc, char* argv[])
259+
MAIN_FUNCTION
260260
{
261261
#ifdef WIN32
262262
util::WinCmdLineArgs winArgs;

src/compat.h

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ typedef void* sockopt_arg_type;
9090
typedef char* sockopt_arg_type;
9191
#endif
9292

93+
#ifdef WIN32
94+
// Export main() and ensure working ASLR when using mingw-w64.
95+
// Exporting a symbol will prevent the linker from stripping
96+
// the .reloc section from the binary, which is a requirement
97+
// for ASLR. While release builds are not affected, anyone
98+
// building with a binutils < 2.36 is subject to this ld bug.
99+
#define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
100+
#else
101+
#define MAIN_FUNCTION int main(int argc, char* argv[])
102+
#endif
103+
93104
// Note these both should work with the current usage of poll, but best to be safe
94105
// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
95106
// __APPLE__ poll is broke https://github.com/BGL/BGL/pull/14336#issuecomment-437384408

src/qt/main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <qt/BGL.h>
66

7+
#include <compat.h>
78
#include <util/translation.h>
89
#include <util/url.h>
910

@@ -18,4 +19,7 @@ extern const std::function<std::string(const char*)> G_TRANSLATION_FUN = [](cons
1819
};
1920
UrlDecodeFn* const URL_DECODE = urlDecode;
2021

21-
int main(int argc, char* argv[]) { return GuiMain(argc, argv); }
22+
MAIN_FUNCTION
23+
{
24+
return GuiMain(argc, argv);
25+
}

0 commit comments

Comments
 (0)