[fpc-devel] Proposal: Supporting initial references for weakexternal

Jonas Maebe jonas.maebe at elis.ugent.be
Mon Feb 14 18:11:43 CET 2011

On 14 Feb 2011, at 15:47, Jeppe Johansen wrote:

> Okay, I understand what you mean. But then something more than  
> "weak" functionality should be added. Aliasing isn't the same,  
> that's what the "default" part does
>  procedure EmptyFunc; [public, alias: 'EmptyFunc'];
>    begin // Empty function
>    end;
> procedure Func1; weak; external name 'Func1'; default 'EmptyFunc';
> procedure Func2; weak; external name 'Func2'; default 'EmptyFunc';

The string after "name" always defines the name of what is referred to  
by the external declaration, not to the name of whatever symbol you  
are currently declaring. So the "default" actually means the same as  
what "external" already means. We cannot change that specifically for  
weak externals (regardless of the used syntax), as it would be  
inconsistent with how regular external symbols are defined.

To add assembler names to a symbol definition, we use "alias" in FPC:  
it means "define an alias with name 'name' for the current function".

I.e., you original example would become:

    procedure NoHandler;
      // translated into:
      //   .globl NoHandler
      //   .NoHandler:

    procedure TestProcHandler; [public, alias: 'TestProc'];
      // translated into:
      //   .globl P$TestProcHandler
      //   P$TestProcHandler:
      //   .globl TestProc
      //   TestProc:

... and then in some other unit, I guess (I'm not sure what the  
interaction between the earlier regular "TestProc" defintion and the  
weak "TestProc" declaration now would be otherwise):

    procedure TestProc; weak; external name NoHandler'; alias:  
    // translated into:
    //  a) on Linux
    //   .weak P$TestProc
    //   .set  P$TestProc, NoHandler
    //   .weak TestProc
    //   .set  TestProc, NoHandler
    //  b) on Mac OS X (and possibly on other non-ELF platforms):
    //   fail compilation, since weak symbol aliases don't exist and  
there is no way to emulate this in the compiler
    //   (neither the specified name nor "alias" can be supported here  
for weak externals)

       TestProc; // This will call TestProcHandler

It has to be handled at the linker level, because the compiler has no  
view of all code involved (there may be non-Pascal code too).


More information about the fpc-devel mailing list