Skip to content

[Support][Cygwin] Fix handling of Process symbol lookup. #143072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2025

Conversation

jeremyd2019
Copy link
Contributor

@jeremyd2019 jeremyd2019 commented Jun 6, 2025

In Unix/DynamicLibrary.inc, it was already known that Cygwin required use of RTLD_DEFAULT as the Handle parameter to DLSym to search all modules for a symbol. Unfortunately, RTLD_DEFAULT is defined as NULL, so the existing checks of the Process handle meant DLSym would never be called on Cygwin. Use the existing &Invalid sentinel instead of nullptr for the Process handle.

In Unix/DynamicLibrary.inc, it was already known that Cygwin required
use of RTLD_DEFAULT as the "Handle" parameter to DLSym to search all
modules for a symbol.  Unfortunately RTLD_DEFAULT is defined as NULL, so
the existing checks of the "Process" handle meant DLSym would never be
called on Cygwin.  Add a bool to indicate whether the Process handle was
set instead.
@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2025

@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-llvm-support

Author: None (jeremyd2019)

Changes

In Unix/DynamicLibrary.inc, it was already known that Cygwin required use of RTLD_DEFAULT as the Handle parameter to DLSym to search all modules for a symbol. Unfortunately, RTLD_DEFAULT is defined as NULL, so the existing checks of the Process handle meant DLSym would never be called on Cygwin. Add a bool member to indicate whether the Process handle was set instead.


Full diff: https://github.com/llvm/llvm-project/pull/143072.diff

1 Files Affected:

  • (modified) llvm/lib/Support/DynamicLibrary.cpp (+4-2)
diff --git a/llvm/lib/Support/DynamicLibrary.cpp b/llvm/lib/Support/DynamicLibrary.cpp
index 531c035ab9266..34c2dc2e56227 100644
--- a/llvm/lib/Support/DynamicLibrary.cpp
+++ b/llvm/lib/Support/DynamicLibrary.cpp
@@ -26,6 +26,7 @@ class DynamicLibrary::HandleSet {
   typedef std::vector<void *> HandleList;
   HandleList Handles;
   void *Process = nullptr;
+  bool ProcessAdded = false;
 
 public:
   static void *DLOpen(const char *Filename, std::string *Err);
@@ -66,6 +67,7 @@ class DynamicLibrary::HandleSet {
       }
 #endif
       Process = Handle;
+      ProcessAdded = true;
     }
     return true;
   }
@@ -97,11 +99,11 @@ class DynamicLibrary::HandleSet {
     assert(!((Order & SO_LoadedFirst) && (Order & SO_LoadedLast)) &&
            "Invalid Ordering");
 
-    if (!Process || (Order & SO_LoadedFirst)) {
+    if (!ProcessAdded || (Order & SO_LoadedFirst)) {
       if (void *Ptr = LibLookup(Symbol, Order))
         return Ptr;
     }
-    if (Process) {
+    if (ProcessAdded) {
       // Use OS facilities to search the current binary and all loaded libs.
       if (void *Ptr = DLSym(Process, Symbol))
         return Ptr;

@jeremyd2019
Copy link
Contributor Author

jeremyd2019 commented Jun 6, 2025

cc @mstorsjo

this seemed less invasive than trying to use this as the Process handle, as seems to be done on the Windows/DynamicLibrary.inc side of things (since we don't have to do any additional special handling in the Unix/DynamicLibrary.inc side beyond using RTLD_DEFAULT instead of the executable's handle for Process).

This brought llvm test failures on Cygwin down from 110 to 50.

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@kikairoya
Copy link
Contributor

llvm::DynamicLibrary already has a symbol representing invalid state. How about to use this?

LLVM_ABI static char Invalid;

@jeremyd2019
Copy link
Contributor Author

So instead of having Process initialized to nullptr, initialize it to &Invalid ? That could work I guess

@jeremyd2019 jeremyd2019 requested a review from mstorsjo June 7, 2025 05:44
@jeremyd2019 jeremyd2019 closed this Jun 7, 2025
@jeremyd2019 jeremyd2019 reopened this Jun 7, 2025
@kikairoya
Copy link
Contributor

LGTM (but I don't have access to approve)

@kikairoya
Copy link
Contributor

Could #143246 be the cause of the CI failure?

…ional bool is not needed

also fix platform .inc files
@jeremyd2019
Copy link
Contributor Author

I think it was probably the missing checks in the .inc files, which I've now added

@jeremyd2019
Copy link
Contributor Author

LGTM (but I don't have access to approve)

I'm still waiting on mine, or I'd be looking harder at reviewing and approving your PRs. Hopefully @mstorsjo can take care of this chore in the meantime.

Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mstorsjo mstorsjo merged commit d659364 into llvm:main Jun 9, 2025
8 checks passed
@jeremyd2019 jeremyd2019 deleted the llvm-dylib-cygwin-rtld_default branch June 9, 2025 19:42
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
In Unix/DynamicLibrary.inc, it was already known that Cygwin required
use of `RTLD_DEFAULT` as the `Handle` parameter to `DLSym` to search all
modules for a symbol. Unfortunately, RTLD_DEFAULT is defined as NULL, so
the existing checks of the `Process` handle meant `DLSym` would never be
called on Cygwin. Use the existing `&Invalid` sentinel instead of
`nullptr` for the `Process` handle.
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
In Unix/DynamicLibrary.inc, it was already known that Cygwin required
use of `RTLD_DEFAULT` as the `Handle` parameter to `DLSym` to search all
modules for a symbol. Unfortunately, RTLD_DEFAULT is defined as NULL, so
the existing checks of the `Process` handle meant `DLSym` would never be
called on Cygwin. Use the existing `&Invalid` sentinel instead of
`nullptr` for the `Process` handle.
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
In Unix/DynamicLibrary.inc, it was already known that Cygwin required
use of `RTLD_DEFAULT` as the `Handle` parameter to `DLSym` to search all
modules for a symbol. Unfortunately, RTLD_DEFAULT is defined as NULL, so
the existing checks of the `Process` handle meant `DLSym` would never be
called on Cygwin. Use the existing `&Invalid` sentinel instead of
`nullptr` for the `Process` handle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants