-
Notifications
You must be signed in to change notification settings - Fork 21
refactor(got-patching): extract shared GOT-patching primitives into libdd-got-hook #2282
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d965e6
refactor: extract shared GOT-patching primitives into libdd-got-hook
gyuheon0h 0a853a1
Comment clarity and rename pkg
gyuheon0h baeeae6
slice api
gyuheon0h 6966e28
Safety comments, in code documentation, test for proc maps parsing
gyuheon0h 5509c44
Initial reelase
gyuheon0h File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # Copyright 2025-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| [package] | ||
| name = "libdd-gotter" | ||
| version = "0.1.0" | ||
| description = "ELF GOT-patching primitives for runtime function interposition on 64-bit Linux." | ||
| homepage = "https://github.com/DataDog/libdatadog/tree/main/libdd-gotter" | ||
| repository = "https://github.com/DataDog/libdatadog/tree/main/libdd-gotter" | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
|
gyuheon0h marked this conversation as resolved.
|
||
|
|
||
| [lib] | ||
| bench = false | ||
|
|
||
| [dependencies] | ||
| libc.workspace = true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # libdd-gotter | ||
|
|
||
| > [!WARNING] | ||
| > This library does runtime function interposition by patching the Global Offset Table (GOT) of loaded ELF objects. This is a substantial intervention in a running process — it modifies function pointers that the dynamic linker has already resolved, affecting all code that calls through those GOT entries. Incorrect use can cause crashes, infinite recursion, heap corruption, or silent data loss. Understand the ELF dynamic linking model before using this crate. | ||
|
|
||
| ## What it does | ||
|
|
||
| When a shared library calls an external function like `malloc`, it jumps through a pointer in its **Global Offset Table** -- a writable table that the dynamic linker fills at load time. This crate walks every loaded ELF object via `dl_iterate_phdr`, parses its `PT_DYNAMIC` segment, and rewrites GOT entries so calls are redirected to a hook function. The original function address is resolved and returned so the hook can forward to it. | ||
|
gyuheon0h marked this conversation as resolved.
|
||
|
|
||
| ## Usage | ||
|
|
||
| ### Single-symbol hook (crashtracker intercepting `__assert_fail`) | ||
|
|
||
| ```rust | ||
| use libdd_got_hook::hook_symbol; | ||
|
|
||
| static ORIG_FN: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| unsafe extern "C" fn my_hook(/* same signature as target */) { | ||
|
gyuheon0h marked this conversation as resolved.
|
||
| // ... do work ... | ||
| // forward to original via ORIG_FN | ||
| } | ||
|
|
||
| let mut orig_addr: usize = 0; | ||
| unsafe { | ||
| hook_symbol(c"__assert_fail", my_hook as *const () as usize, &mut orig_addr); | ||
| } | ||
| // Release pairs with the Acquire load in my_hook, ensuring the GOT | ||
| // patches from hook_symbol are visible before the hook reads orig_addr. | ||
| ORIG_FN.store(orig_addr, Ordering::Release); | ||
|
gyuheon0h marked this conversation as resolved.
|
||
| ``` | ||
|
|
||
| ### Multi-symbol registry (heap profiling hooking malloc/free/calloc/realloc) | ||
|
|
||
| See [`libdd-profiling-heap-gotter`](../libdd-profiling-heap-gotter) which builds a `SymbolOverrides` registry on top of the primitives exported by this crate. | ||
|
|
||
| ## Support | ||
| This library can be used on ARM64 and AMD64 Linux in processes using glibc or musl runtimes. | ||
| Only symbols that have been dynamically linked can be intercept. | ||
| For instance, if you want to intercept the `malloc` of your C runtime, | ||
| you _cannot_ do so if the application has been statically linked against musl. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.