================
@@ -247,10 +247,35 @@ llvm::Constant 
*CodeGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
       Name = Context.BuiltinInfo.getName(BuiltinID).substr(10);
   }
 
+  // If no standard library declaration was found in the AST (freestanding
+  // code calling builtins directly without headers), perform automatic
+  // resolution of MSVC CRT symbol names and DLL import attributes.
+  bool IsNoHeader = (D == GlobalDecl(FD));
+  if (IsNoHeader && getTriple().isWindowsMSVCEnvironment() &&
+      Name == "hypotf") {
+    // Map C99 float math functions to their MSVC CRT prefixed names on 
Windows.
+    // (Only hypotf needs mapping as it is not exported without prefix in UCRT
+    // on both x86 and x64).
+    Name = "_hypotf";
----------------
atetubou wrote:

I actually don't know the reason of why hypotf is so special in UCRT, but I 
scanned the export symbols of the 32-bit/64-bit Windows UCRT libraries 
(`ucrt.lib` and `libucrt.lib` in Windows SDK 10.0.26100.0) and confirmed the 
following:

1. **Other C99 float functions are exported normally:**
   Functions like `logbf`, `nextafterf`, `sinf`, etc., are exported under their 
normal names (without prefix) from the UCRT DLL.
2. **`hypotf` is the only exception:**
   `hypotf` is **not exported** under the normal name `hypotf` in UCRT. UCRT 
only exports it as `_hypotf` (with the leading underscore), both in the dynamic 
import library (`ucrt.lib` -> `__imp__hypotf`) and the static library 
(`libucrt.lib` -> `_hypotf`).
   (This applies to architectures: x86, x64, and ARM64, but not sure for ARM as 
my machine doesn't have libraries for the architecture).
3. **MSVC SDK header implementation:**
   In `<corecrt_math.h>`, MSVC defines `hypotf` as an inline function wrapping 
`_hypotf`:
   ```cpp
   __inline float hypotf(float _X, float _Y) { return _hypotf(_X, _Y); }
   ```
   Microsoft likely omitted the normal `hypotf` symbol from the UCRT binaries 
because they assumed all callers would include the header and inline it to 
`_hypotf`. 

Since Clang emits CodeGen for the freestanding `__builtin_hypotf` using its 
default name `hypotf`, this missing DLL export causes unresolved symbol errors 
at link time. Because other C99 float math functions are exported normally, 
only `hypotf` needs this compiler-side fallback redirection to `_hypotf`.


https://github.com/llvm/llvm-project/pull/207135
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to