blob: 1e16ec7e0e8475e431b72da622aee8164daef441 [file] [log] [blame]
wdenk77846742003-07-26 08:08:08 +00001#include <stddef.h>
2#include <stdio.h>
3#include <string.h>
4
5void *func[8], **pfunc;
6
7typedef struct xxx xxx_t;
8struct xxx {
9 int dummy;
10 void **pfunc;
11} q;
12
13#define XF_strcpy 3
14#define XF_printf 4
15
16#define LABEL(x) \
17asm volatile ( \
18
19#if defined(__i386__)
20#define EXPORT_FUNC(x) \
21asm volatile ( \
22" .globl mon_" #x "\n" \
23"mon_" #x ":\n" \
24" movl %0, %%eax\n" \
25" movl pfunc, %%ecx\n" \
26" jmp *(%%ecx,%%eax)\n" \
27 : : "i"(XF_ ## x * sizeof(void *)) : "eax", "ecx");
28#elif defined(__powerpc__)
29#define EXPORT_FUNC(x) \
30asm volatile ( \
31" .globl mon_" #x "\n" \
32"mon_" #x ":\n" \
Wolfgang Denke7670f62008-02-14 22:43:22 +010033" lwz %%r11, %0(%%r2)\n" \
wdenk77846742003-07-26 08:08:08 +000034" lwz %%r11, %1(%%r11)\n" \
35" mtctr %%r11\n" \
36" bctr\n" \
Wolfgang Denke7670f62008-02-14 22:43:22 +010037 : : "i"(offsetof(xxx_t, pfunc)), "i"(XF_ ## x * sizeof(void *)) : "r11", "r2");
wdenk77846742003-07-26 08:08:08 +000038#elif defined(__arm__)
39#define EXPORT_FUNC(x) \
40asm volatile ( \
41" .globl mon_" #x "\n" \
42"mon_" #x ":\n" \
43" ldr ip, [r8, %0]\n" \
44" ldr pc, [ip, %1]\n" \
45 : : "i"(offsetof(xxx_t, pfunc)), "i"(XF_ ## x * sizeof(void *)) : "ip");
46#elif defined(__mips__)
47#define EXPORT_FUNC(x) \
48asm volatile ( \
49" .globl mon_" #x "\n" \
50"mon_" #x ":\n" \
51" lw $25, %0($26)\n" \
52" lw $25, %1($25)\n" \
53" jr $25\n" \
54 : : "i"(offsetof(xxx_t, pfunc)), "i"(XF_ ## x * sizeof(void *)) : "t9");
Macpaul Lin72c73dd2011-10-11 22:33:20 +000055#elif defined(__nds32__)
56#define EXPORT_FUNC(x) \
57asm volatile ( \
58" .globl mon_" #x "\n" \
59"mon_" #x ":\n" \
60" lwi $r16, [$gp + (%0)]\n" \
61" lwi $r16, [$r16 + (%1)]\n" \
62" jr $r16\n" \
63: : "i"(offsetof(xxx_t, pfunc)), \
64"i"(XF_ ## x * sizeof(void *)) : "$r16");
65
wdenk77846742003-07-26 08:08:08 +000066#else
67#error [No stub code for this arch]
68#endif
69
70void dummy(void)
71{
72EXPORT_FUNC(printf)
73EXPORT_FUNC(strcpy)
74}
75
76int main(void)
77{
78#if defined(__i386__)
79 xxx_t *pq;
80#elif defined(__powerpc__)
Wolfgang Denke7670f62008-02-14 22:43:22 +010081 register volatile xxx_t *pq asm("r2");
wdenk77846742003-07-26 08:08:08 +000082#elif defined(__arm__)
83 register volatile xxx_t *pq asm("r8");
84#elif defined(__mips__)
85 register volatile xxx_t *pq asm("k0");
Macpaul Lin72c73dd2011-10-11 22:33:20 +000086#elif defined(__nds32__)
87 register volatile xxx_t *pq asm("$r16");
wdenk77846742003-07-26 08:08:08 +000088#endif
89 char buf[32];
90
91 func[XF_strcpy] = strcpy;
92 func[XF_printf] = printf;
93 pq = &q;
94 pq->pfunc = pfunc = func;
95
96 mon_strcpy(buf, "test");
97 mon_printf("hi %s %d z\n", buf, 444);
98
99 return 0;
100}