The semantics for the used attribute is not consistent in the llvm/clang source code base from one vendor implementation to another.
- The general understanding of the semantics for the used attribute are to emit a function or data object in a compiled object file, even if it appears as though the function/data object is not referened (unused static function, for example). This still allows link-time optimizations to exclude such functions and data objects from the linked output file if they are not needed.
- The Mach-O implementation of the used attribute has a different understanding of the semantics for the used attribute. In the Mach-O implementation, a function or data object definition/declaration that is annotated with a used attribute will not only be kept in the compiled object file, but the function/data object symbol will be marked with a “no dead strip” symbol flag to tell the linker that the section in which the symbol is defined must be kept in the linked output file.
- The Mach-O interpretation of the semantics of the used attribute support use cases like forcing the linker to retain the definition of an interrupt vector table written in C that is not referenced elsewhere in an application. However, this interpretation also interferes with link-time optimizations like function specialization that rely on the general understanding of the used attribute semantics to keep specialized versions of a function around until the linker is given the ability to choose the most efficient version of the function for a given application.
- Ideally, there should be a separate attribute for each semantic interpretation.
Is there another attribute besides the used attribute, which can be applied to a function or data object, that can be interpreted to mean that the function or data object definition must be kept in the compiled object file, but may be removed at link-time if the function/data object is not referenced elsewhere in the application? Does the GCC’s “externally_visible” attribute fit this description?
Alternatively, if we were to assume that the used attribute is interpreted to mean that a function/data object definition is kept in the compiled object file, but not necessarily retained in the linked output file, then is there another function/variable attribute that can be interpreted to mean that the definition function/data object must be retained in the linked output file? If not, I speculate that it would be useful to introduce a new function/variable attribute (retain?) that behaves exactly like the used attribute in terms of emitting a definition of a function/data object to the compiled object file even if it doesn’t appear to be referenced, but will also inform the linker that the section in which the function/data object is defined must be retained in the linked output file.
Thoughts? Opinions? Concerns?