Skip to content

Commit

Permalink
compat: Consolidate mingw-w64 ASLR workaround for upstream libsecp ch…
Browse files Browse the repository at this point in the history
…anges

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
  • Loading branch information
fanquake authored and janus committed Aug 3, 2022
1 parent 7ddf096 commit c49b560
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
13 changes: 3 additions & 10 deletions src/BGL-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <chainparamsbase.h>
#include <clientversion.h>
#include <compat.h>
#include <compat/stdin.h>
#include <policy/feerate.h>
#include <rpc/client.h>
Expand Down Expand Up @@ -1211,19 +1212,11 @@ static int CommandLineRPC(int argc, char *argv[])
return nRet;
}

#ifdef WIN32
// Export main() and ensure working ASLR on Windows.
// Exporting a symbol will prevent the linker from stripping
// the .reloc section from the binary, which is a requirement
// for ASLR. This is a temporary workaround until a fixed
// version of binutils is used for releases.
__declspec(dllexport) int main(int argc, char* argv[])
MAIN_FUNCTION
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
std::tie(argc, argv) = winArgs.get();
#else
int main(int argc, char* argv[])
{
#endif
SetupEnvironment();
if (!SetupNetworking()) {
Expand Down
3 changes: 2 additions & 1 deletion src/BGL-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <clientversion.h>
#include <coins.h>
#include <compat.h>
#include <consensus/amount.h>
#include <consensus/consensus.h>
#include <core_io.h>
Expand Down Expand Up @@ -853,7 +854,7 @@ static int CommandLineRawTx(int argc, char* argv[])
return nRet;
}

int main(int argc, char* argv[])
MAIN_FUNCTION
{
SetupEnvironment();

Expand Down
12 changes: 2 additions & 10 deletions src/BGL-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <chainparams.h>
#include <chainparamsbase.h>
#include <clientversion.h>
#include <compat.h>
#include <core_io.h>
#include <streams.h>
#include <util/system.h>
Expand Down Expand Up @@ -142,16 +143,7 @@ static int Grind(const std::vector<std::string>& args, std::string& strPrint)
return EXIT_SUCCESS;
}

#ifdef WIN32
// Export main() and ensure working ASLR on Windows.
// Exporting a symbol will prevent the linker from stripping
// the .reloc section from the binary, which is a requirement
// for ASLR. This is a temporary workaround until a fixed
// version of binutils is used for releases.
__declspec(dllexport) int main(int argc, char* argv[])
#else
int main(int argc, char* argv[])
#endif
MAIN_FUNCTION
{
ArgsManager& args = gArgs;
SetupEnvironment();
Expand Down
4 changes: 3 additions & 1 deletion src/BGL-wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include <chainparams.h>
#include <chainparamsbase.h>
#include <clientversion.h>
#include <compat.h>
#include <interfaces/init.h>
#include <logging.h>
#include <util/system.h>
Expand Down Expand Up @@ -81,7 +83,7 @@ static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])
return true;
}

int main(int argc, char* argv[])
MAIN_FUNCTION
{
ArgsManager& args = gArgs;
#ifdef WIN32
Expand Down
2 changes: 1 addition & 1 deletion src/BGLd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
return fRet;
}

int main(int argc, char* argv[])
MAIN_FUNCTION
{
#ifdef WIN32
util::WinCmdLineArgs winArgs;
Expand Down
11 changes: 11 additions & 0 deletions src/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ typedef void* sockopt_arg_type;
typedef char* sockopt_arg_type;
#endif

#ifdef WIN32
// Export main() and ensure working ASLR when using mingw-w64.
// Exporting a symbol will prevent the linker from stripping
// the .reloc section from the binary, which is a requirement
// for ASLR. While release builds are not affected, anyone
// building with a binutils < 2.36 is subject to this ld bug.
#define MAIN_FUNCTION __declspec(dllexport) int main(int argc, char* argv[])
#else
#define MAIN_FUNCTION int main(int argc, char* argv[])
#endif

// Note these both should work with the current usage of poll, but best to be safe
// WIN32 poll is broken https://daniel.haxx.se/blog/2012/10/10/wsapoll-is-broken/
// __APPLE__ poll is broke https://github.com/BGL/BGL/pull/14336#issuecomment-437384408
Expand Down
6 changes: 5 additions & 1 deletion src/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <qt/BGL.h>

#include <compat.h>
#include <util/translation.h>
#include <util/url.h>

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

int main(int argc, char* argv[]) { return GuiMain(argc, argv); }
MAIN_FUNCTION
{
return GuiMain(argc, argv);
}

0 comments on commit c49b560

Please sign in to comment.