Skip to content

Commit fbae8c5

Browse files
committed
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 b71d37d commit fbae8c5

File tree

7 files changed

+26
-24
lines changed

7 files changed

+26
-24
lines changed

src/bitcoin-cli.cpp

Lines changed: 3 additions & 10 deletions
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>
@@ -1212,19 +1213,11 @@ static int CommandLineRPC(int argc, char *argv[])
12121213
return nRet;
12131214
}
12141215

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

src/bitcoin-tx.cpp

Lines changed: 2 additions & 1 deletion
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>
@@ -854,7 +855,7 @@ static int CommandLineRawTx(int argc, char* argv[])
854855
return nRet;
855856
}
856857

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

src/bitcoin-util.cpp

Lines changed: 2 additions & 10 deletions
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/bitcoin-wallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <chainparams.h>
1010
#include <chainparamsbase.h>
1111
#include <clientversion.h>
12+
#include <compat.h>
1213
#include <interfaces/init.h>
1314
#include <key.h>
1415
#include <logging.h>
@@ -88,7 +89,7 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
8889
return true;
8990
}
9091

91-
int main(int argc, char* argv[])
92+
MAIN_FUNCTION
9293
{
9394
ArgsManager& args = gArgs;
9495
#ifdef WIN32

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ typedef void* sockopt_arg_type;
8686
typedef char* sockopt_arg_type;
8787
#endif
8888

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

src/qt/main.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <qt/bitcoin.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)