[Openmp-commits] [openmp] [OpenMP] Improve dladdr error handling in ompd_init() (PR #201043)

Mark Zhuang via Openmp-commits openmp-commits at lists.llvm.org
Tue Jun 2 02:10:16 PDT 2026


================
@@ -87,8 +87,10 @@ void ompd_init() {
   Dl_info dl_info;
   int ret = dladdr((void *)ompd_init, &dl_info);
   if (!ret) {
-    fprintf(stderr, "%s\n", dlerror());
-  } else if (strrchr(dl_info.dli_fname, '/')) {
+    const char *err = dlerror();
+    if (err)
+      fprintf(stderr, "dladdr failed in ompd_init: %s\n", err);
----------------
zqb-all wrote:

 Thanks for the review. 

However, according to the dladdr(3) man page (https://man7.org/linux/man-pages/man3/dladdr.3.html):

> If the address specified in addr could not be matched to a shared
> object, then these functions return 0.  In this case, an error
> message is not available via dlerror

So dlerror() returning NULL after a dladdr() failure is documented, expected behavior. The static linking case is a common and reproducible example of this:

>   /* test_dladdr.c */
>   #define _GNU_SOURCE
>   #include <stdio.h>
>   #include <dlfcn.h>
> 
>   void test_func() {}
> 
>   int main() {
>       Dl_info info;
>       int ret = dladdr((void *)test_func, &info);
>       printf("dladdr returned: %d\n", ret);
>       if (!ret) {
>           const char *err = dlerror();
>           printf("dlerror: %s\n", err ? err : "(null)");
>       } else {
>           printf("dli_fname: %s\n", info.dli_fname ? info.dli_fname : "(null)");
>       }
>       return 0;
>   }
> 
>   $ gcc -o test_static test_dladdr.c -static -ldl && ./test_static
>   dladdr returned: 0
>   dlerror: (null)

https://godbolt.org/z/x3s7YKE45

Given that static linking may be quite common, I'd lean toward not printing `(null)` here.

https://github.com/llvm/llvm-project/pull/201043


More information about the Openmp-commits mailing list