aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.dg/ext/attr-ifunc-1.C
blob: 4a29e8bb4d69aef2aff03266b16c7d00dd2f1c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* { dg-do run }  */
/* { dg-require-ifunc "" } */
/* { dg-options "-Wno-pmf-conversions" } */

struct Klass
{
  int a[4];

  int implementation ();
  int magic ();

  /* An ifunc resolver must return a pointer to an ordinary (non-member)
     function.  To make it possible to use ifunc with member functions,
     the resolver must convert a member function pointer to an ordinary
     function pointer (slicing off the high word).  */
  typedef int Func (Klass*);

  static Func* resolver ();
};

int Klass::implementation ()
{
  __builtin_printf ("'ere I am JH\n");
  return a[0] + a[1] + a[2] + a[3];
}

Klass::Func* Klass::resolver (void)
{
  /* GCC guarantees this conversion to be safe and the resulting pointer
     usable to call the member function using ordinary (i.e., non-member)
     function call syntax.  */

  return reinterpret_cast<Func*>(&Klass::implementation);
}

int f (void) __attribute__ ((ifunc ("foo")));

typedef int (F)(void);
extern "C" F* foo () { return 0; }


int Klass::magic () __attribute__ ((ifunc ("_ZN5Klass8resolverEv")));

int main ()
{
  Klass obj;

  obj.a[0] = 1;
  obj.a[1] = 2;
  obj.a[2] = 3;
  obj.a[3] = 4;

  return !(obj.magic () == 10);
}