Skip to content

Commit ffc6b67

Browse files
committed
build: add glibc/libstdc++ back-compat stubs
glibc/libstdc++ have added new symbols in later releases. When running a new binary against an older glibc, the run-time linker is unable to resolve the new symbols and the binary refuses to run. This can be fixed by adding our own versions of those functions, so that the build-time linker does not emit undefined symbols for them. This enables our binary releases to work on older Linux distros, while not incurring the downsides of a fully static binary.
1 parent 4c6cab2 commit ffc6b67

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

src/compat/glibc_compat.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "bitcoin-config.h"
2+
#include <cstddef>
3+
#include <sys/select.h>
4+
5+
// Prior to GLIBC_2.14, memcpy was aliased to memmove.
6+
extern "C" void* memmove(void* a, const void* b, size_t c);
7+
extern "C" void* memcpy(void* a, const void* b, size_t c)
8+
{
9+
return memmove(a, b, c);
10+
}
11+
12+
extern "C" void __chk_fail (void) __attribute__((__noreturn__));
13+
extern "C" FDELT_TYPE __fdelt_warn(FDELT_TYPE a)
14+
{
15+
if (a >= FD_SETSIZE)
16+
__chk_fail ();
17+
return a / __NFDBITS;
18+
}
19+
extern "C" FDELT_TYPE __fdelt_chk(FDELT_TYPE) __attribute__((weak, alias("__fdelt_warn")));

src/compat/glibcxx_compat.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <cstddef>
2+
#include <istream>
3+
#include <stdexcept>
4+
#include <typeinfo>
5+
6+
#ifndef _GLIBCXX_USE_NOEXCEPT
7+
#define _GLIBCXX_USE_NOEXCEPT throw()
8+
#endif
9+
10+
namespace std {
11+
12+
const char* bad_exception::what() const throw()
13+
{
14+
return "std::bad_exception";
15+
}
16+
17+
const char* bad_cast::what() const throw()
18+
{
19+
return "std::bad_cast";
20+
}
21+
22+
const char* bad_alloc::what() const throw()
23+
{
24+
return "std::bad_alloc";
25+
}
26+
27+
namespace __detail
28+
{
29+
struct _List_node_base
30+
{
31+
void _M_hook(std::__detail::_List_node_base* const __position) throw () __attribute__((used))
32+
{
33+
_M_next = __position;
34+
_M_prev = __position->_M_prev;
35+
__position->_M_prev->_M_next = this;
36+
__position->_M_prev = this;
37+
}
38+
void _M_unhook() __attribute__((used))
39+
{
40+
_List_node_base* const __next_node = _M_next;
41+
_List_node_base* const __prev_node = _M_prev;
42+
__prev_node->_M_next = __next_node;
43+
__next_node->_M_prev = __prev_node;
44+
}
45+
_List_node_base* _M_next;
46+
_List_node_base* _M_prev;
47+
};
48+
} // namespace detail
49+
50+
template ostream& ostream::_M_insert(bool);
51+
template ostream& ostream::_M_insert(long);
52+
template ostream& ostream::_M_insert(double);
53+
template ostream& ostream::_M_insert(unsigned long);
54+
template ostream& ostream::_M_insert(const void*);
55+
template ostream& __ostream_insert(ostream&, const char*, streamsize);
56+
template istream& istream::_M_extract(long&);
57+
template istream& istream::_M_extract(unsigned short&);
58+
59+
out_of_range::~out_of_range() _GLIBCXX_USE_NOEXCEPT { }
60+
61+
// Used with permission.
62+
// See: https://github.com/madlib/madlib/commit/c3db418c0d34d6813608f2137fef1012ce03043d
63+
64+
void
65+
ctype<char>::_M_widen_init() const {
66+
char __tmp[sizeof(_M_widen)];
67+
for (unsigned __i = 0; __i < sizeof(_M_widen); ++__i)
68+
__tmp[__i] = __i;
69+
do_widen(__tmp, __tmp + sizeof(__tmp), _M_widen);
70+
71+
_M_widen_ok = 1;
72+
// Set _M_widen_ok to 2 if memcpy can't be used.
73+
for (unsigned __i = 0; __i < sizeof(_M_widen); ++__i)
74+
if (__tmp[__i] != _M_widen[__i]) {
75+
_M_widen_ok = 2;
76+
break;
77+
}
78+
}
79+
80+
}// namespace std

0 commit comments

Comments
 (0)