[lld] [LLD][COFF] Support anti-dependency symbols (PR #112542)
Jacek Caban via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 16 06:27:31 PDT 2024
cjacek wrote:
Anti-dependency symbols are, unfortunately, not documented. This PR implements their behavior as observed in MSVC.
Anti-dependency symbols are used extensively in ARM64EC. In situations where typical targets would emit an undefined symbol for an external function, ARM64EC object files don't initially know if the implementation will be ARM64EC (in which case the mangled symbol name can be used directly) or x86_64 (in which case the mangled symbol must resolve to a thunk that calls the demangled symbol via an emulator). The setup involves the demangled `func` symbol being an anti-dependency alias to the mangled `#func`, and `#func` being an anti-dependency alias to its guest exit thunk, which ultimately calls `func`. It is expected that at least one of these symbols will be replaced by another input file.
If regular weak aliases were used instead, those symbols would always be resolvable: both `func` and `#func` would resolve to the guest exit thunk. The restriction against anti-dependency chaining prevents this behavior. If none of the symbols are replaced, `func` is treated as unresolvable.
Since each object file referencing the function emits these anti-dependency aliases, they are frequently duplicated, which is acceptable for anti-dependencies.
Usually, functions built as ARM64EC include a `func` to `#func` alias as well. However, `hybrid_patchable` functions differ slightly: their demangled `func` is a regular (not anti-dependency) weak alias that resolves to an undefined symbol `EXP+#func`. The precedence of regular weak aliases ensures that this is reflected in the callers.
Like other weak aliases, an anti-dependency alias prevents the symbol from being resolved via a static library symbol. This means that ARM64EC object files' usage of these aliases would not work for referencing static libraries. According to my testing, MSVC has an ARM64EC-specific feature that detects the mangled `func` to `#func` alias and allows both symbols to resolve to static libraries. I have left these ARM64EC-specific bits out of this PR.
I also observed some unexplained behavior in MSVC where the order of definitions seems to matter. For example, the last two weak-antidep.test tests fail in MSVC if `test.obj` is the first input file.
CC @bylaws
https://github.com/llvm/llvm-project/pull/112542
More information about the llvm-commits
mailing list