www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Creating weak symbols with D

reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Weak symbols (https://en.wikipedia.org/wiki/Weak_symbol) are not often 
needed, but they are, they're needed badly.

C and C++ define __attribute__((weak)) to introduce weak symbols. It 
would be great if D could have a corresponding  weak attribute.
Aug 08 2020
next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 8 August 2020 at 17:59:08 UTC, Andrei Alexandrescu 
wrote:
 Weak symbols (https://en.wikipedia.org/wiki/Weak_symbol) are 
 not often needed, but they are, they're needed badly.

 C and C++ define __attribute__((weak)) to introduce weak 
 symbols. It would be great if D could have a corresponding 
  weak attribute.
pragma(weak) sounds better to me. pragmas are suppose to change codgen, and the usecase is seldom enough that you wouldn't want to take name for it. Out of interested where do you find yourself needing to specify weak linkage?
Aug 08 2020
next sibling parent kinke <noone nowhere.com> writes:
On Saturday, 8 August 2020 at 18:14:34 UTC, Stefan Koch wrote:
 On Saturday, 8 August 2020 at 17:59:08 UTC, Andrei Alexandrescu 
 wrote:
 Weak symbols (https://en.wikipedia.org/wiki/Weak_symbol) are 
 not often needed, but they are, they're needed badly.

 C and C++ define __attribute__((weak)) to introduce weak 
 symbols. It would be great if D could have a corresponding 
  weak attribute.
LDC has weak, even working for MSVC targets since v1.22.
 Out of interested where do you find yourself needing to specify 
 weak linkage?
E.g., https://github.com/ldc-developers/druntime/commit/2eee5ffb17ff15fab20fa18b85bb6a696b7a2213.
Aug 08 2020
prev sibling next sibling parent reply Johan <j j.nl> writes:
On Saturday, 8 August 2020 at 18:14:34 UTC, Stefan Koch wrote:
 On Saturday, 8 August 2020 at 17:59:08 UTC, Andrei Alexandrescu 
 wrote:
 Weak symbols (https://en.wikipedia.org/wiki/Weak_symbol) are 
 not often needed, but they are, they're needed badly.

 C and C++ define __attribute__((weak)) to introduce weak 
 symbols. It would be great if D could have a corresponding 
  weak attribute.
pragma(weak) sounds better to me. pragmas are suppose to change codgen, and the usecase is seldom enough that you wouldn't want to take name for it.
This argumentation doesn't make sense to me. Not all pragmas change codegen, and there are many things that change codegen that are not pragmas. LDC has the ldc.attributes.weak magic UDA (in https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/attributes.d), and I'm sure GDC has something very similar. (LDC also has the (very old) pragma(LDC_extern_weak), which should be changed to a magic UDA.) LDC chose to implement these things as UDAs because it offers a few benefits over pragmas, one of which is that it doesn't need compiler surgery. -Johan
Aug 08 2020
next sibling parent Petar Kirov [ZombineDev] <petar.p.kirov gmail.com> writes:
On Saturday, 8 August 2020 at 18:43:31 UTC, Johan wrote:
 On Saturday, 8 August 2020 at 18:14:34 UTC, Stefan Koch wrote:
 On Saturday, 8 August 2020 at 17:59:08 UTC, Andrei 
 Alexandrescu wrote:
 [...]
pragma(weak) sounds better to me. pragmas are suppose to change codgen, and the usecase is seldom enough that you wouldn't want to take name for it.
This argumentation doesn't make sense to me. Not all pragmas change codegen, and there are many things that change codegen that are not pragmas. LDC has the ldc.attributes.weak magic UDA (in https://github.com/ldc-developers/druntime/blob/ldc/src/ldc/attributes.d), and I'm sure GDC has something very similar. (LDC also has the (very old) pragma(LDC_extern_weak), which should be changed to a magic UDA.) LDC chose to implement these things as UDAs because it offers a few benefits over pragmas, one of which is that it doesn't need compiler surgery. -Johan
Another important advantage is that they can be easily introspected and enabled conditionally as they're just tuples. I think that the design of UDAs in D is one of the underrated language design success stories :)
Aug 08 2020
prev sibling parent reply sarn <sarn theartofmachinery.com> writes:
On Saturday, 8 August 2020 at 18:43:31 UTC, Johan wrote:
 On Saturday, 8 August 2020 at 18:14:34 UTC, Stefan Koch wrote:
 On Saturday, 8 August 2020 at 17:59:08 UTC, Andrei 
 Alexandrescu wrote:
 C and C++ define __attribute__((weak)) to introduce weak 
 symbols. It would be great if D could have a corresponding 
  weak attribute.
pragma(weak) sounds better to me. pragmas are suppose to change codgen, and the usecase is seldom enough that you wouldn't want to take name for it.
This argumentation doesn't make sense to me. Not all pragmas change codegen, and there are many things that change codegen that are not pragmas.
Maybe the ship has already sailed, but existing linker-related tweaks in standard D are done with pragmas (or extern). E.g., pragma(mangle) and pragma(crt_constructor).
Aug 08 2020
parent Johan <j j.nl> writes:
On Sunday, 9 August 2020 at 02:30:22 UTC, sarn wrote:
 On Saturday, 8 August 2020 at 18:43:31 UTC, Johan wrote:
 On Saturday, 8 August 2020 at 18:14:34 UTC, Stefan Koch wrote:
 On Saturday, 8 August 2020 at 17:59:08 UTC, Andrei 
 Alexandrescu wrote:
 C and C++ define __attribute__((weak)) to introduce weak 
 symbols. It would be great if D could have a corresponding 
  weak attribute.
pragma(weak) sounds better to me. pragmas are suppose to change codgen, and the usecase is seldom enough that you wouldn't want to take name for it.
This argumentation doesn't make sense to me. Not all pragmas change codegen, and there are many things that change codegen that are not pragmas.
Maybe the ship has already sailed, but existing linker-related tweaks in standard D are done with pragmas (or extern). E.g., pragma(mangle) and pragma(crt_constructor).
DMD simply doesn't have a lot of linker-related functionality (no: weak, section, alias). GDC and LDC both do these things with magic UDAs, because UDAs fit much better in the language. (introspection, combining into AliasSeq, things like https://github.com/ldc-developers/druntime/blob/2eee5ffb17ff15fab20fa18b85bb6a696b7a2213/src/core/stdcpp/ xception.d#L21-L22, etc.) (Btw, pragma(mangle) and `extern` are not _linker_ related. ) -Johan
Aug 09 2020
prev sibling parent Jacob Carlborg <doob me.com> writes:
On 2020-08-08 20:14, Stefan Koch wrote:

 Out of interested where do you find yourself needing to specify weak 
 linkage?
I don't know about Andrei's use case, but weak linkage is vital to be able to create proper bindings to Apple's SDks. The SDK use weak linkage for most symbols. This allows to link with the latest version of the SDK, which might have added new symbols, while still targeting older versions of the platform. The application then needs to check if a symbol is available or not on the currently running platform. -- /Jacob Carlborg
Aug 09 2020
prev sibling parent Manu <turkeyman gmail.com> writes:
On Sun, 9 Aug 2020, 4:00 am Andrei Alexandrescu via Digitalmars-d, <
digitalmars-d puremagic.com> wrote:

 Weak symbols (https://en.wikipedia.org/wiki/Weak_symbol) are not often
 needed, but they are, they're needed badly.

 C and C++ define __attribute__((weak)) to introduce weak symbols. It
 would be great if D could have a corresponding  weak attribute.
I feel like you're not the first person that's said this...

Aug 10 2020