title: How to assign dlsym()'s return value to function type?
body: <p>My problem is as follows:</p>
  
  <p>I have a structure containing function pointers like this:</p>
  
  <pre><code>typedef void (CALLING_COVNENTION * functionType_t) (/*...*/);
  
  typedef struct myFuncpointerStruc_s
  {
      /*...*/
      functionType_t funcIdentifier;
      /*...*/
  }myFuncpointerStruc_t;
  </code></pre>
  
  <p>Now I want to assign a functionpointer of exactly that type to it.
  But sadly <code>dlsym()</code> is returning a <code>void *</code> instead of something like <code>function_poitner_type</code></p>
  
  <p>So my first try:</p>
  
  <pre><code>functionscope ()
  {
      myFuncpointerStruc_t structIdentifier;
  
      structIdentifier.funcIdentifier= dlsym (hAndle, "calledName");
  }
  </code></pre>
  
  <p>ended in the warning:</p>
  
  <blockquote>
    <p>WARNING: ISO C forbids assignment between function pointer and 'void *'</p>
  </blockquote>
  
  <p>Ok, that confused me... gcc was never talking that harsh to me.
  But OK lets get tricky I thought there will probably be any backdoor to keep it standard conform. So I tried this:</p>
  
  <pre><code>functionscope ()
  {
      myFuncpointerStruc_t structIdentifier;
  
      *(void **) &amp;structIdentifier.funcIdentifier = dlsym (hAndle, "calledName");
  }
  </code></pre>
  
  <p>Hm.... obviously...</p>
  
  <blockquote>
    <p>warning: dereferencing type-punned pointer will break strict aliasing rules</p>
  </blockquote>
  
  <p>As I don't know what happens <code>dlsym()</code> internally I can't know I will break, as I get returned a real <code>void *</code> that aliases my function, or something quite different, can I?</p>
  
  <p>So I cant know, will the use of this function pointer now break strict-aliasing rules or won't it?</p>
  
  <p>But anyway: For my company it is law to use the compilerflag <code>-fstrict-aliasing</code>.
  So this is also not a possible solution for me, even if it would be conform.</p>
  
  <p>So I tried it by going on. </p>
  
  <p>My next idea was: what about to parse a <code>char *</code> version of <code>dlsym()</code> into memcopy?</p>
  
  <p>The result:</p>
  
  <pre><code>functionscope ()
  {
      myFuncpointerStruc_t structIdentifier;
  
      unsigned char *localTestPtr = ((unsigned char *) dlsym (hAndle, "calledName");  
  
      memcpy (g_interfaceSvCheck.pfnPFD_SVCHK_GET_VERSION, localTestPtr, sizeof (localTestPtr));
  }
  </code></pre>
  
  <p>The warning I'm getting now is pretty specific again:</p>
  
  <blockquote>
    <p>warning: ISO C forbids passing argument 1 of ´memcpy´ between function pointer and ´void *´</p>
  </blockquote>
  
  <p>So my next idea was kind of "lets go back to the bytes" maybe there I will get a way it will be working on.
  (But slowly I'm running out of ideas...)</p>
  
  <p>So I did:</p>
  
  <pre><code>functionscope ()
  {
      size_t testIndex;
      myFuncpointerStruc_t structIdentifier;
      unsigned char *localTestPtr = ((unsigned char *) dlsym (hAndle, "calledName");
  
      for (testIndex = 0; testIndex &lt; sizeof (localTestPtr); testIndex++)
      {
          ((unsigned char *)(structIdentifier.funcIdentifier))[testIndex] = ((unsigned char *)&amp;localTestPtr)[testIndex];
      }
  
  }
  </code></pre>
  
  <p>This ended in a quiet different warning:</p>
  
  <blockquote>
    <p>WARNING: ISO C forbids conversion of function pointer to object pointer type</p>
  </blockquote>
  
  <p>So finally my freakiest try was:</p>
  
  <pre><code>functionscope ()
  {
      size_t testIndex;
      myFuncpointerStruc_t structIdentifier;
      unsigned char *localTestPtr = ((unsigned char *) dlsym (hAndle, "calledName");  
      unsigned char * FakeFunctionPointer;
  
      FakeFunctionPointer = 
          (((unsigned char *)structIdentifier) + offsetof (myFuncpointerStruc_t , funcIdentifier));
  
      for (testIndex = 0; testIndex &lt; sizeof (localTestPtr); testIndex++)
      {
          FakeFunctionPointer[testIndex] = ((unsigned char *)&amp;localTestPtr)[testIndex];
      }
  }
  </code></pre>
  
  <p>And even here the problem is, that <code>offsetoff</code>'s second parameter can't get converted to <code>void *</code> as it is a function pointer.</p>
  
  <p>I'm stucked and don't know how to get further, could anyone help me please finding a solution?</p>
  
  <p>I just have these warnings by using gcc 4.2 not with clang.</p>
  
  <p>The targeted C version is <code>-std=c99</code> to avoid is <code>-std=POSIX</code> and <code>-std=gnu99</code></p>
  
  <p>But this gcc warnings don't sound outdated to me.
  I'm working currently on FreeBSD and I have really no idea left how to solve this. </p>