open-watcom / open-watcom-v2

Open Watcom V2.0 - Source code repository, Wiki, Latest Binary build, Archived builds including all installers for download.
Other
992 stars 163 forks source link

Support for weak or lazy symbols #1324

Open jmalak opened 3 months ago

jmalak commented 3 months ago

Discussed in https://github.com/open-watcom/open-watcom-v2/discussions/1323

Originally posted by **ghaerr** August 14, 2024 Hello @jmalak, I have a need to mark a symbol such that, when linking, other libraries will not normally be searched to find and pull in the referenced symbol unless the symbol has already been defined in object files already committed to being linked. In GCC, this is known as weak symbols, and can be specified using the GCC `__attribute__((__weak__))` extension. Can the OpenWatcom linker support weak symbols? I can't seem to find any direct documentation on it, although by looking at the OW source, I see that OBJ EXTRN comment records are apparently used (for import) to specify weak and lazy symbol attributes. (See https://github.com/open-watcom/open-watcom-v2/blob/8eb58ceb85ec90db64c0504560e82b6e5bc6a765/bld/cg/intel/h/x86objd.h#L87 as well as `IS_SYM_LINK_WEAK` in wl/c/objpass1.c, and `WEAK` in many more places). There is also some documentation identified on google but which I cannot find the full manual for, saying "Open Watcom Linker User's Guide ... The "EXPORT" directive is used to tell the Open Watcom Linker which symbols are available for import ... A lazy external reference is one which has two" but when I folllow the link I can't find the referenced text. So possibly there is a way to use the EXPORT or IMPORT directives to specify a weak symbol, rather than C source. My requirement to use weak symbols is for optionally including various routines only when needed in the ELKS C Library, e.g. for including another function `printf` might call to perform float-to-string display only when floats are used, etc. Can you point me into the right direction for how to take advantage of this possible internal capability in OpenWatcom? Thank you!
jmalak commented 3 months ago

It is not supported by OW compilers for user code. It is used internally by code generator for common block (fortran) or for C++ code I think. May be other reason. The linker is fully capable to process it, but you cannot define it in linker. It must be defined in generated code (in modules) for real code. But use of it can be danger and it is not required for current user code. We can do it as extension but it needs review of OW code. Probably we could extend #pragma aux for it.