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

Jonas Maebe jonas.maebe at elis.ugent.be
Wed Jan 12 20:47:17 CET 2011

On 12 Jan 2011, at 19:16, Jeppe Johansen wrote:

> Den 12-01-2011 12:42, Jonas Maebe skrev:
>> How is it normally done in C? At first sight, using the weakexternal directive to /define/ symbols seems like a wrong approach.
> With C it's done using compiler dependent attributes. With gcc you can do it using __attribute__((weak,alias("SomeFunction")));. Though often it'll just be done using assembler using the .weak and .set/thumb_set directives

FPC also has an "alias" procedural directive, so that one should probably be used instead. Giving weakexternal (or anything else) a syntax that is not used for any other procedure directive is not a good idea because it reduces uniformity.

> The weakexternal directive already "defines" a symbol, it will exist at runtime, but it might be nil.

What I meant is that there is a semantic difference between referencing a symbol that may or may not be defined somewhere else (regardless of whether a programmer or the linker creates the referenced symbol), and defining a new symbol. This is independent of whether a symbol is weak or not.

Defining a weak symbol also exists, but is not yet supported by FPC. In fact, the example you gave would seem more logical to me as follows (assuming that FPC could define "weak" symbols using the "weak" modifier):

   procedure IRQHandler; weak;
       { default dummy implementation, the "NoHandler" from your example }

   procedure MyIRQHandler; public; alias: 'IRQHandler';
       { real implementation, overrides the weak definition of IRQHandler; if there
         is no such definition, the dummy one is used }

(and of course, if we'd have a plain "weak" modifier, then "weakexternal;" could be "weak; external;", although this is annoying to still change because "weakexternal" support is already in released FPC versions)

If you have no control over the original definition and it is not weak (maybe the reason you did that is because FPC does not yet support defining weak symbols), then we indeed need some way to define weak symbols whose "default value" is something else than nil, because I guess wrapping is not good enough (although it's not that bad, since it only results in an extra call in the case where the weak symbol is not replaced):

   procedure NoHandler;
       { dummy implementation }

   procedure MyIRQHandler; weak;
       { default behaviour: one extra call }

   { overriding implementation }
   procedure RealIRQHandler; public; alias: 'MyIRQHandler';
       { real code }

I can't immediately think of a good syntax to directly define MyIRQHandler as a weak symbol with the address of NoHandler as default value. Maybe this, although I'm not 100% happy with it either:

   procedure MyIRQHandler; weak 'NoHandler';


More information about the fpc-devel mailing list