x86-testapp.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stddef.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. void *func[8], **pfunc;
  5. typedef struct xxx xxx_t;
  6. struct xxx {
  7. int dummy;
  8. void **pfunc;
  9. } q;
  10. #define XF_strcpy 3
  11. #define XF_printf 4
  12. #define LABEL(x) \
  13. asm volatile ( \
  14. #if defined(__i386__)
  15. #define EXPORT_FUNC(x) \
  16. asm volatile ( \
  17. " .globl mon_" #x "\n" \
  18. "mon_" #x ":\n" \
  19. " movl %0, %%eax\n" \
  20. " movl pfunc, %%ecx\n" \
  21. " jmp *(%%ecx,%%eax)\n" \
  22. : : "i"(XF_ ## x * sizeof(void *)) : "eax", "ecx");
  23. #elif defined(__powerpc__)
  24. #define EXPORT_FUNC(x) \
  25. asm volatile ( \
  26. " .globl mon_" #x "\n" \
  27. "mon_" #x ":\n" \
  28. " lwz %%r11, %0(%%r2)\n" \
  29. " lwz %%r11, %1(%%r11)\n" \
  30. " mtctr %%r11\n" \
  31. " bctr\n" \
  32. : : "i"(offsetof(xxx_t, pfunc)), "i"(XF_ ## x * sizeof(void *)) : "r11", "r2");
  33. #elif defined(__arm__)
  34. #define EXPORT_FUNC(x) \
  35. asm volatile ( \
  36. " .globl mon_" #x "\n" \
  37. "mon_" #x ":\n" \
  38. " ldr ip, [r8, %0]\n" \
  39. " ldr pc, [ip, %1]\n" \
  40. : : "i"(offsetof(xxx_t, pfunc)), "i"(XF_ ## x * sizeof(void *)) : "ip");
  41. #elif defined(__mips__)
  42. #define EXPORT_FUNC(x) \
  43. asm volatile ( \
  44. " .globl mon_" #x "\n" \
  45. "mon_" #x ":\n" \
  46. " lw $25, %0($26)\n" \
  47. " lw $25, %1($25)\n" \
  48. " jr $25\n" \
  49. : : "i"(offsetof(xxx_t, pfunc)), "i"(XF_ ## x * sizeof(void *)) : "t9");
  50. #else
  51. #error [No stub code for this arch]
  52. #endif
  53. void dummy(void)
  54. {
  55. EXPORT_FUNC(printf)
  56. EXPORT_FUNC(strcpy)
  57. }
  58. int main(void)
  59. {
  60. #if defined(__i386__)
  61. xxx_t *pq;
  62. #elif defined(__powerpc__)
  63. register volatile xxx_t *pq asm("r2");
  64. #elif defined(__arm__)
  65. register volatile xxx_t *pq asm("r8");
  66. #elif defined(__mips__)
  67. register volatile xxx_t *pq asm("k0");
  68. #endif
  69. char buf[32];
  70. func[XF_strcpy] = strcpy;
  71. func[XF_printf] = printf;
  72. pq = &q;
  73. pq->pfunc = pfunc = func;
  74. mon_strcpy(buf, "test");
  75. mon_printf("hi %s %d z\n", buf, 444);
  76. return 0;
  77. }