Skip to content

Commit 5b792bc

Browse files
authored
fix powerpc64le
1 parent 2ec4837 commit 5b792bc

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/stub.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
#define CACHEFLUSH(addr, size) __builtin___clear_cache(addr, addr + size);VALGRIND_CACHE_FLUSH(addr, size)
4040
#endif
4141

42+
#if !defined(__BYTE_ORDER__) || !defined(__ORDER_LITTLE_ENDIAN__) || !defined(__ORDER_BIG_ENDIAN__)
43+
#if defined(_WIN32)
44+
#define __ORDER_LITTLE_ENDIAN__ 1234
45+
#define __ORDER_BIG_ENDIAN__ 4321
46+
#define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
47+
#else
48+
#include <endian.h>
49+
#endif
50+
#endif
51+
4252
#if defined(__aarch64__) || defined(_M_ARM64)
4353
#define CODESIZE 16U
4454
#define CODESIZE_MIN 16U
@@ -76,7 +86,7 @@
7686
// NOP
7787
// LDR.W PC, [PC]
7888
#define REPLACE_FAR(t, fn, fn_stub)\
79-
uint32_t clearBit0 = fn & 0xfffffffe;\
89+
uint32_t clearBit0 = (uintptr_t)fn & 0xfffffffe;\
8090
char *f = (char *)clearBit0;\
8191
if (clearBit0 % 4 != 0) {\
8292
*(uint16_t *)&f[0] = 0xbe00;\
@@ -208,14 +218,15 @@
208218
// For ppc64le (Little Endian, ELFv2 ABI)
209219
// https://maskray.me/blog/2023-02-26-linker-notes-on-power-isa
210220
// https://llvm.org/devmtg/2014-04/PDFs/Talks/Euro-LLVM-2014-Weigand.pdf
211-
// PowerPC64 LE (ELFv2 ABI) - Direct jump, no TOC descriptor needed
221+
// PowerPC64 LE (ELFv2 ABI) - LEP
222+
// readelf -h test_function : Flags: 0x2, abiv2
212223
#define CODESIZE 28U
213224
#define CODESIZE_MIN 28U
214225
#define CODESIZE_MAX CODESIZE
215226
#define REPLACE_FAR(t, fn, fn_stub) \
216227
do { \
217228
uint64_t addr = (uint64_t)(fn_stub); \
218-
uint32_t* p = (uint32_t*)(fn); \
229+
uint32_t* p = (uint32_t*)(fn + 8); \
219230
p[0] = 0x3d800000 | ((addr >> 48) & 0xFFFF); /* lis r12, hi16 */ \
220231
p[1] = 0x618c0000 | ((addr >> 32) & 0xFFFF); /* ori r12, mid16 */ \
221232
p[2] = 0x798c07c6; /* rldicr r12, r12, 32, 31 */ \
@@ -227,7 +238,7 @@
227238
} while (0)
228239
#define REPLACE_NEAR(t, fn, fn_stub) REPLACE_FAR(t, fn, fn_stub)
229240
#elif defined(__powerpc64__)
230-
// PowerPC64 BE (ELFv1 ABI) - Use function descriptor to load code + TOC
241+
// PowerPC64 BE (ELFv1 ABI) - Use function descriptor to load code + TOC(Table of Contents)
231242
#define CODESIZE 36U
232243
#define CODESIZE_MIN 36U
233244
#define CODESIZE_MAX CODESIZE

0 commit comments

Comments
 (0)