Problem with extern function declarations

Johannes Pfau via D.gnu d.gnu at puremagic.com
Tue Apr 7 12:31:24 PDT 2015


The recently added weak attribute shows a small difference in behavior
compared to c:

------------------------
#include <stdio.h>
__attribute__((weak)) int test();
int main()
{
    printf("%p %d\n", &test, (int)&test);
    return 0;
}
------------------------
(nil) 0
------------------------
import core.stdc.stdio, gcc.attribute;
@attribute("weak") void test();
int main()
{
    printf("%p %d\n", &test, cast(int)&test);
    return 0;
}
------------------------
=> unresolved symbol test

The problem is that we mark the function decl as TREE_STATIC. The C
compiler marks it as DECL_EXTERNAL instead. Interestingly for variables
things work as 'expected':

---------------------------------------------
__gshared @attribute("weak") extern int test; => address (nil) / 0
__gshared @attribute("weak") int test; => real address
---------------------------------------------


So:
1) Can we mark int test(); as DECL_EXTERNAL or am I missing something?
2) Is this change correct or is there a better way:

@@ -1717,7 +1717,7 @@ setup_symbol_storage (Dsymbol *dsym, tree decl,
bool public_p) }
 
       VarDeclaration *vd = rd ? rd->isVarDeclaration() : NULL;
-      if (!local_p || (vd && vd->storage_class & STCextern))
+      if (!local_p || (vd && vd->storage_class & STCextern) || (rd &&
rd->isFuncDeclaration() && !rd->isFuncDeclaration()->fbody)) {
 	  DECL_EXTERNAL (decl) = 1;
 	  TREE_STATIC (decl) = 0;


More information about the D.gnu mailing list