Browse files

Add glibc back compat code

To ensure that the binaries compiled by gitian run across many linux
distributions, enforce 2.17 as the minimum libc version supported.
  • Loading branch information...
TheCharlatan committed Oct 13, 2018
1 parent 84dd674 commit 5a76933903ef2f29532588d359862831a3644128
Showing with 113 additions and 165 deletions.
  1. +8 −1 CMakeLists.txt
  2. +2 −0
  3. +1 −1 contrib/gitian/gitian-linux.yml
  4. +0 −163 contrib/gitian/
  5. +4 −0 src/common/CMakeLists.txt
  6. +98 −0 src/common/compat/glibc_compat.cpp
@@ -696,6 +696,13 @@ else()
set(LD_SECURITY_FLAGS "${LD_SECURITY_FLAGS} -Wl,-z,noexecheap")
add_definitions(-DFDELT_TYPE=long\ int)
add_linker_flag_if_supported(-Wl,--wrap=__divmoddi4 LD_BACKCOMPAT_FLAGS)
add_linker_flag_if_supported(-Wl,--wrap=glob LD_BACKCOMPAT_FLAGS)
message(STATUS "Using Lib C back compat flags: ${LD_BACKCOMPAT_FLAGS}")
# some windows linker bits
if (WIN32)
add_linker_flag_if_supported(-Wl,--dynamicbase LD_SECURITY_FLAGS)
@@ -708,7 +715,7 @@ else()
# With GCC 6.1.1 the compiled binary malfunctions due to aliasing. Until that
# is fixed in the code (Issue #847), force compiler to be conservative.
@@ -521,6 +521,8 @@ The required packages are the names for each toolchain on apt. Depending on your
Using `depends` might also be easier to compile Monero on Windows than using MSYS. Activate Windows Subsystem for Linux (WSL) with a distro (for example Ubuntu), install the apt build-essentials and follow the `depends` steps as depicted above.
The produced binaries still link libc dynamically. If the binary is compiled on a current distribution, it might not run on an older distribution with an older installation of libc. Passing `-DBACKCOMPAT=ON` to cmake will make sure that the binary will run on systems having at least libc version 2.17.
## Installing Monero from a package
**DISCLAIMER: These packages are not part of this repository or maintained by this project's contributors, and as such, do not go through the same review process to ensure their trustworthiness and security.**
@@ -152,7 +152,7 @@ script: |
for i in ${HOSTS}; do
export PATH=${BASEPREFIX}/${i}/native/bin:${ORIGPATH}
mkdir build && cd build
cmake .. -DCMAKE_TOOLCHAIN_FILE=${BASEPREFIX}/${i}/share/toolchain.cmake
cmake .. -DCMAKE_TOOLCHAIN_FILE=${BASEPREFIX}/${i}/share/toolchain.cmake -DBACKCOMPAT=ON
mv bin ${DISTNAME}

This file was deleted.

Oops, something went wrong.
@@ -49,6 +49,10 @@ if (STACK_TRACE)
list(APPEND common_sources stack_trace.cpp)
list(APPEND common_sources compat/glibc_compat.cpp)
@@ -0,0 +1,98 @@
#include <cstddef>
#include <cstdint>
#include <strings.h>
#include <string.h>
#include <glob.h>
#include <unistd.h>
#include <fnmatch.h>
#if defined(HAVE_SYS_SELECT_H)
#include <sys/select.h>
// Prior to GLIBC_2.14, memcpy was aliased to memmove.
extern "C" void* memmove(void* a, const void* b, size_t c);
//extern "C" void* memset(void* a, int b, long unsigned int c);
extern "C" void* memcpy(void* a, const void* b, size_t c)
return memmove(a, b, c);
extern "C" void __chk_fail(void) __attribute__((__noreturn__));
#if defined(__i386__) || defined(__arm__)
extern "C" int64_t __udivmoddi4(uint64_t u, uint64_t v, uint64_t* rp);
extern "C" int64_t __wrap___divmoddi4(int64_t u, int64_t v, int64_t* rp)
int32_t c1 = 0, c2 = 0;
int64_t uu = u, vv = v;
int64_t w;
int64_t r;
if (uu < 0) {
c1 = ~c1, c2 = ~c2, uu = -uu;
if (vv < 0) {
c1 = ~c1, vv = -vv;
w = __udivmoddi4(uu, vv, (uint64_t*)&r);
if (c1)
w = -w;
if (c2)
r = -r;
*rp = r;
return w;
/* glibc-internal users use __explicit_bzero_chk, and explicit_bzero
redirects to that. */
#undef explicit_bzero
/* Set LEN bytes of S to 0. The compiler will not delete a call to
this function, even if S is dead after the call. */
explicit_bzero (void *s, size_t len)
memset (s, '\0', len);
/* Compiler barrier. */
asm volatile ("" ::: "memory");
// Redefine explicit_bzero_chk
__explicit_bzero_chk (void *dst, size_t len, size_t dstlen)
/* Inline __memset_chk to avoid a PLT reference to __memset_chk. */
if (__glibc_unlikely (dstlen < len))
__chk_fail ();
memset (dst, '\0', len);
/* Compiler barrier. */
asm volatile ("" ::: "memory");
/* libc-internal references use the hidden
__explicit_bzero_chk_internal symbol. This is necessary if
__explicit_bzero_chk is implemented as an IFUNC because some
targets do not support hidden references to IFUNC symbols. */
#define strong_alias (__explicit_bzero_chk, __explicit_bzero_chk_internal)
#undef glob
extern "C" int glob_old(const char * pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob);
#ifdef __i386__
__asm__(".symver glob_old,glob@GLIBC_2.1");
#elif defined(__amd64__)
__asm__(".symver glob_old,glob@GLIBC_2.2.5");
#elif defined(__arm__)
__asm(".symver glob_old,glob@GLIBC_2.4");
#elif defined(__aarch64__)
__asm__(".symver glob_old,glob@GLIBC_2.17");
extern "C" int __wrap_glob(const char * pattern, int flags, int (*errfunc) (const char *epath, int eerrno), glob_t *pglob)
return glob_old(pattern, flags, errfunc, pglob);

0 comments on commit 5a76933

Please sign in to comment.