Skip to content

Add DynamicLibrary.Path function#171

Merged
elezar merged 2 commits intoNVIDIA:mainfrom
elezar:add-dlinfo-origin
Feb 12, 2026
Merged

Add DynamicLibrary.Path function#171
elezar merged 2 commits intoNVIDIA:mainfrom
elezar:add-dlinfo-origin

Conversation

@elezar
Copy link
Member

@elezar elezar commented Feb 10, 2026

This change adds an Path function to the dl.DynamicLibrary type so as to allow for the path of a loaded library to be determined.

This is inspired by: https://github.com/NixOS/nixpkgs/blob/953355a9568f324d232bd920ad09791009f2c4c0/pkgs/by-name/nv/nvidia-container-toolkit/0001-Add-dlopen-discoverer.patch#L65

and allows a dlopen-based library locator to be added to the NVIDIA Container Toolkit: NVIDIA/nvidia-container-toolkit#1646

Using the included example:

./dl libnvidia-ml.so.1
2026/02/11 15:43:44 libraryPath=/lib/x86_64-linux-gnu/libnvidia-ml.so.1
2026/02/11 15:43:44 resolvedPath=/usr/lib/x86_64-linux-gnu/libnvidia-ml.so.575.57.08
2026/02/11 15:43:44 library closed

And also:

./dl ../../../../usr/lib/x86_64-linux-gnu/libnvidia-ml.so.575.57.08
2026/02/11 15:43:30 libraryPath=../../../../usr/lib/x86_64-linux-gnu/libnvidia-ml.so.575.57.08
2026/02/11 15:43:30 resolvedPath=../../../../usr/lib/x86_64-linux-gnu/libnvidia-ml.so.575.57.08
2026/02/11 15:43:30 library closed

klueska
klueska previously approved these changes Feb 10, 2026
Comment on lines +24 to +35
// Path is NOT supported on Darwin.
// TODO: dlinfo is not supported on darwin and dladdr should be used.
// See for example: https://github.com/Manu343726/siplasplas/issues/82
// This requires the resolution of an expected symbol.
// For now we return an error.
func (dl *DynamicLibrary) Path() (string, error) {
if dl.handle == nil {
return "", fmt.Errorf("%v not opened", dl.Name)
}

return "", fmt.Errorf("not implemented")
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we care about darwin at all?

Copy link
Member Author

@elezar elezar Feb 11, 2026

Choose a reason for hiding this comment

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

I care becase I develop on a mac and having these stubs defined there allows me to run unit tests locally. (Even if we don't expect to trigger the error there).

Copy link
Member Author

Choose a reason for hiding this comment

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

This can be made more general though. I think dl_other.go may be better than keeping this Darwin specific.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
@elezar elezar force-pushed the add-dlinfo-origin branch 3 times, most recently from 8c7a1da to 7882a5b Compare February 11, 2026 13:33
@elezar elezar requested a review from klueska February 11, 2026 13:36
Comment on lines +58 to +62
resolved, err := filepath.EvalSymlinks(path)
if err != nil {
// Ignore errors resolving the symlink.s
resolved = path
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it ok to simply ignore the error here?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was trying to avoid unknown unknowns here. I have a resonable understanding on how this should behave with the libraries we actually call this with (libnvidia-ml.so.1), but don't know if there's edge cases I'm missing. With that said, maybe it's the wrong thing to do the Symlink evaluation here. Let me rather simplify this so that the resolution of links (and handling the subsequent errors) is left to the caller.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Given your comment, did you mean to remove the call to filepath.EvalSymlinks(path)?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I did. (Let me double-check the diff).

@elezar elezar changed the title Add DynamicLibrary.Origin function Add DynamicLibrary.Path function Feb 11, 2026
@elezar elezar force-pushed the add-dlinfo-origin branch 3 times, most recently from 08e2b74 to bf61cb2 Compare February 11, 2026 15:37
Comment on lines +58 to +59
dl.path = filepath.Join(C.GoString((*C.char)(libParentPathBuffer)), dl.Name)
libPath = dl.path
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: I would assign the local variable libPath first and then set dl.path second. Non-blocking though,

Copy link
Collaborator

Choose a reason for hiding this comment

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

Or maybe not, to be consistent with the other assignment.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated.

klueska
klueska previously approved these changes Feb 11, 2026
This change adds a a function to get the path for a DynamicLibrary.
This is currently only supported on Linux.

Signed-off-by: Evan Lezar <elezar@nvidia.com>
@elezar elezar merged commit 92cf8c9 into NVIDIA:main Feb 12, 2026
4 checks passed
@elezar elezar deleted the add-dlinfo-origin branch February 12, 2026 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants