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

Jeppe Johansen jepjoh2 at es.aau.dk
Tue Feb 15 19:01:33 CET 2011

Den 14-02-2011 18:11, Jonas Maebe skrev:
> 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;
>    begin
>      // translated into:
>      //   .globl NoHandler
>      //   .NoHandler:
>    end;
>    procedure TestProcHandler; [public, alias: 'TestProc'];
>    begin
>      // translated into:
>      //   .globl P$TestProcHandler
>      //   P$TestProcHandler:
>      //   .globl TestProc
>      //   TestProc:
>    end;
> ... 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: 'TestProc';
>    // 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)
>    ...
>    begin
>       TestProc; // This will call TestProcHandler
>    end;
> 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).
> Jonas
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
Okay. I get what you mean now

What about allowing a new keyword "weak" and let it have two 
forms(something like you suggested back in one of the mails from 12th 
january)? Name mangling is an issue of course. It would be easiest to 
handle if weak symbols simply weren't name mangled(something like cdecl 
and co)

procedure TestProc; weak; // This produces a weak symbol

procedure TestProc2; weak default 'SomeDefaultFunction'; // Weak+default 
makes the declaration external by default
procedure TestProc3; weak default 'SomeDefaultFunction'; external name 
'Test_TestProc3'; // Overcome eventual name mangling problems. Symbol is 
called Test_TestProc3 and is linked to SomeDefaultFunction if not 
resolved at linktime
procedure TestProc4; weak; external; // Replaces/does the same as 

More information about the fpc-devel mailing list