Add support for AMD64 processors and Microsoft Windows.
[catacomb] / base / asm-common.h
index 7e62eb5..8745ea4 100644 (file)
@@ -58,9 +58,22 @@ F(name):                                                             \
 #endif
 
 ///--------------------------------------------------------------------------
-/// x86-specific hacking.
+/// Windows-specific hacking.
+
+#if ABI_WIN
 
 #if CPUFAM_X86
+#  define F(name) _##name
+#endif
+
+#endif
+
+///--------------------------------------------------------------------------
+/// x86- and amd64-specific hacking.
+///
+/// It's (slightly) easier to deal with both of these in one go.
+
+#if CPUFAM_X86 || CPUFAM_AMD64
 
 // Set the function hooks.
 #define FUNC_PREHOOK(_) .balign 16
@@ -86,7 +99,7 @@ F(name):                                                              \
 
 // Maybe load GOT address into GOT.
        .macro  ldgot got=GOTREG
-#if WANT_PIC
+#if WANT_PIC && CPUFAM_X86
        call    _where_am_i.\got
        add     \got, offset _GLOBAL_OFFSET_TABLE_
 #endif
@@ -94,7 +107,7 @@ F(name):                                                             \
 
 // Maybe build a helper subroutine for `ldgot GOT'.
        .macro  gotaux got=GOTREG
-#if WANT_PIC
+#if WANT_PIC && CPUFAM_X86
        .align  16
 _where_am_i.\got :
        mov     \got, [esp]
@@ -105,9 +118,19 @@ _where_am_i.\got :
 // Load address of external symbol ADDR into REG, maybe using GOT.
        .macro  leaext reg, addr, got=GOTREG
 #if WANT_PIC
+#  if CPUFAM_X86
        mov     \reg, [\got + \addr@GOT]
+#  endif
+#  if CPUFAM_AMD64
+       mov     \reg, \addr@GOTPCREL[rip]
+#  endif
 #else
+#  if CPUFAM_X86
        mov     \reg, offset \addr
+#  endif
+#  if CPUFAM_AMD64
+       lea     \reg, \addr[rip]
+#  endif
 #endif
        .endm
 
@@ -115,7 +138,9 @@ _where_am_i.\got :
 // referring to ADDR, which is within our module, maybe using GOT.
 #define INTADDR(...) INTADDR__0(__VA_ARGS__, GOTREG, dummy)
 #define INTADDR__0(addr, got, ...) INTADDR__1(addr, got)
-#if WANT_PIC
+#if CPUFAM_AMD64
+#  define INTADDR__1(addr, got) addr + rip
+#elif WANT_PIC
 #  define INTADDR__1(addr, got) got + addr@GOTOFF
 #else
 #  define INTADDR__1(addr, got) addr