Skip to content

Commit 35c3af2

Browse files
committed
BITS: ctypes: Handle builds with -mrtd -mregparm=3, as used in GRUB
GRUB on i386-pc builds with -mrtd -mregparm=3, which changes the default calling convention to something libffi doesn't know how to handle. Explicitly declare functions called via ctypes (memmove, memset, string_at, wstring_at, and cast) with attributes to make them use the calling convention libffi expects.
1 parent 72c9c58 commit 35c3af2

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5460,7 +5460,23 @@ create_comerror(void)
54605460

54615461
#endif
54625462

5463-
static PyObject *
5463+
#ifdef __i386__
5464+
#define asmlinkage __attribute__((cdecl,regparm(0)))
5465+
#else
5466+
#define asmlinkage
5467+
#endif
5468+
5469+
asmlinkage void *ctypes_memmove(void *dest, const void *src, size_t n)
5470+
{
5471+
return memmove(dest, src, n);
5472+
}
5473+
5474+
asmlinkage void *ctypes_memset(void *s, int c, size_t n)
5475+
{
5476+
return memset(s, c, n);
5477+
}
5478+
5479+
static asmlinkage PyObject *
54645480
string_at(const char *ptr, int size)
54655481
{
54665482
if (size == -1)
@@ -5493,7 +5509,7 @@ cast_check_pointertype(PyObject *arg)
54935509
return 0;
54945510
}
54955511

5496-
static PyObject *
5512+
static asmlinkage PyObject *
54975513
cast(void *ptr, PyObject *src, PyObject *ctype)
54985514
{
54995515
CDataObject *result;
@@ -5545,7 +5561,7 @@ cast(void *ptr, PyObject *src, PyObject *ctype)
55455561
}
55465562

55475563
#ifdef CTYPES_UNICODE
5548-
static PyObject *
5564+
static asmlinkage PyObject *
55495565
wstring_at(const wchar_t *ptr, int size)
55505566
{
55515567
Py_ssize_t ssize = size;
@@ -5703,8 +5719,8 @@ init_ctypes(void)
57035719
PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyInt_FromLong(FUNCFLAG_PYTHONAPI));
57045720
PyModule_AddStringConstant(m, "__version__", "1.1.0");
57055721

5706-
PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
5707-
PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
5722+
PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(ctypes_memmove));
5723+
PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(ctypes_memset));
57085724
PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at));
57095725
PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast));
57105726
#ifdef CTYPES_UNICODE

0 commit comments

Comments
 (0)