chiark / gitweb /
base/asm-common.h: Introduce bad literal-pool handling macros.
[catacomb] / base / asm-common.h
index 7e62eb5463c0b9f951e0a652404152019f08955e..aa6b4c3e40696794d60b255bb1a110c4158d7b49 100644 (file)
 ///--------------------------------------------------------------------------
 /// General definitions.
 
+// Some useful variables.
+       .L$_subsec = 0
+
+// Literal pools done the hard way.
+#define _LIT .text .L$_subsec + 1
+#define _ENDLIT .text .L$_subsec
+
 // Announcing an external function.
 #define FUNC(name)                                                     \
        .globl  F(name);                                                \
@@ -40,7 +47,9 @@ F(name):                                                              \
 #define _ENDFUNC(name)                                                 \
        .purgem ENDFUNC;                                                \
        SIZE_OBJ(name);                                                 \
-       ENDFUNC_HOOK(name)
+       ENDFUNC_HOOK(name);                                             \
+       .L$_subsec = .L$_subsec + 2;                                    \
+       .text .L$_subsec
 
 ///--------------------------------------------------------------------------
 /// ELF-specific hacking.
@@ -58,9 +67,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 +108,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 +116,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 +127,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 +147,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
@@ -123,6 +157,50 @@ _where_am_i.\got :
 
 #endif
 
+///--------------------------------------------------------------------------
+/// ARM-specific hacking.
+
+#if CPUFAM_ARM
+
+// Set the function hooks.
+#define FUNC_PREHOOK(_) .balign 4
+#define ENDFUNC_HOOK(name) .ltorg
+
+// Call external subroutine at ADDR, possibly via PLT.
+       .macro  callext addr, cond=
+#if WANT_PIC
+       bl\cond \addr(PLT)
+#else
+       bl\cond \addr
+#endif
+       .endm
+
+// Do I need to arrange a spare GOT register?
+#if WANT_PIC
+#  define NEED_GOT 1
+#endif
+#define GOTREG r9
+
+// Maybe load GOT address into GOT.
+       .macro  ldgot   cond=, got=GOTREG
+#if WANT_PIC
+       ldr\cond \got, =_GLOBAL_OFFSET_TABLE_ - . - 12
+       add\cond \got, pc, \got
+#endif
+       .endm
+
+// Load address of external symbol ADDR into REG, maybe using GOT.
+       .macro  leaext  reg, addr, cond=, got=GOTREG
+#if WANT_PIC
+       ldr\cond \reg, =\addr(GOT)
+       ldr\cond \reg, [\got, \reg]
+#else
+       ldr\cond \reg, =\addr
+#endif
+       .endm
+
+#endif
+
 ///--------------------------------------------------------------------------
 /// Final stuff.