MISRA Discussion Forums
Rule 6.2.1 weak linkage - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2023 guidelines (https://forum.misra.org.uk/forumdisplay.php?fid=188)
+---- Forum: 4.6 Basic concepts (https://forum.misra.org.uk/forumdisplay.php?fid=192)
+---- Thread: Rule 6.2.1 weak linkage (/showthread.php?tid=1727)



Rule 6.2.1 weak linkage - hahn - 28-02-2025

Dear MISRA group,

I have a question regarding the interpretation of the one-definition-rule in the presence of weak linkage (i.e. __attribute__((weak))).
In particular, is it considered a violation of the odr rule/rule 6.2.1 if a function with "strong" linkage "overrides" a function with "weak" linkage (as typically found in (standard) libraries)?

A special case within this discussion are global allocation and deallocation functions. The C++ standard calls them explicitly "replaceable".
Implementation-wise in any standard library I know, this is expressed by the weak linkage attribute.
Since the standard allows these few functions to be replaced explicitly, I conclude this does not constitute a violation of the one-definition-rule.

But how about any other weak-linkage function?

Thanks in advance!


RE: Rule 6.2.1 weak linkage - misra cpp - 28-02-2025

We don't believe that __attribute__((weak)) is defined inĀ  C++17, so any use must be part of a language extension, which is out of the scope of MISRA C++

However, if you are using such an extension and you are getting violation messages from your tools, then the expected behaviour of weak vs. strong attributes is a justification to deviate.

As far as "replaceable" functions are concerned, the standard explicitly permits you to replace these functions with a user supplied version. This is not considered an ODR violation.