Commit Graph

43 Commits

Author SHA1 Message Date
auronandace 9ea36a0c6c verify POSIX header includes 2026-06-08 18:27:53 +01:00
auronandace e6d4994c90 add descriptions to constants and struct members in dlfcn header 2026-05-28 08:36:12 +01:00
auronandace 3c5d474000 eliminate function struct export in dlfcn header 2026-05-12 17:22:53 +01:00
Peter Limkilde Svendsen 55f9c611b2 Fix most rustdoc warnings 2026-04-02 00:43:20 +02:00
auronandace 4a84b2f04c tidy up lints and add zero_ptr 2026-02-07 14:58:02 +00:00
auronandace ce0cb927e0 verify dlfcn header includes 2026-02-04 08:11:30 +00:00
sourceturner defd2ce7c3 Deny deny-unsafe_op_in_unsafe_fn on the crate level 2026-01-21 00:41:57 +01:00
Jeremy Soller 09f0e57f1b Fix dladdr definition 2025-12-19 11:55:34 -07:00
Wildan M 8e4e43dcc4 Fix Dl_info alias not exported 2025-12-05 16:14:29 +07:00
auronandace 5e6c9b8868 make Dl_info_t the type and Dl_info the alias 2025-12-02 20:26:28 +00:00
auronandace a702cb9555 only imported needed types in dlfcn 2025-12-02 20:24:05 +00:00
auronandace 329db0dd9c add Dl_info_t alias for Dl_info 2025-11-29 15:33:31 +00:00
auronandace a25cf92162 update header reference and add references to functions 2025-11-29 15:22:39 +00:00
Jeremy Soller 82084440ad Support nightly-2025-10-03 2025-10-03 21:51:10 -06:00
Josh Megnauth 51c04d2470 Use C string literals & deprecate c_str! 2025-03-14 21:17:53 -04:00
Anhad Singh 4a08220664 fix(ld.so): copy relocations
Signed-off-by: Anhad Singh <andypython@protonmail.com>
2025-01-18 23:51:53 +11:00
Anhad Singh 01e6eda258 misc(dlfcn): cleanup
Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-31 19:03:05 +11:00
Anhad Singh 42d884c6a3 fix(ld.so): errors UB
Currently, how ld.so errors are handled by functions in dlfcn is wrong.
dlopen calls `linker.load_library` which returns a
`Result<ObjectHandler, goblin::Error>`. Now `goblin::Error` may have
variants that are heap allocated. For example:
`Error::Malformed(format!("invalid path: '{}': {}", path, err))`. The
error string would be allocated by ld.so's allocator but will be dropped
inside libc. This is UB.

After this patch, we now return a custom `DlError` instead. To get more
information about the error, `LD_DEBUG=all` can be set.

Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-31 18:35:20 +11:00
Anhad Singh 1ffd7187ef feat(dlfcn): define RTLD_DEFAULT
Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-25 19:27:31 +11:00
Anhad Singh 2be4316aec feat(dlfcn): return DSO as the handle
Fine because:
> The value of this handle should not be interpreted in any way by the
> caller.
From: https://pubs.opengroup.org/onlinepubs/009696599/functions/dlopen.html

* Add tests for unload
* Implement NOLOAD so we can test that the loaded object has been
  destroyed.
* Minor cleanup.

Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-25 16:17:51 +11:00
Anhad Singh 1db68ab978 feat(ld.so): scopes
Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-24 20:51:53 +11:00
Anhad Singh 5ae1b8e974 feat(ld.so): lazy binding
Signed-off-by: Anhad Singh <andypython@protonmail.com>
2024-12-24 20:51:53 +11:00
Josh Megnauth a7cb1c4176 Unsafe blocks: dl-tls, dlfcn 2024-12-10 02:13:32 -05:00
4lDO2 75d1c67ca2 Switch to a lightweight CStr wrapper 2023-11-04 18:29:38 +00:00
Mateusz Tabaka a7480ea656 Fix global symbols relocations
Instead of a single source of symbols, now linker keeps a list of DSO (former Library) objects
with their own symbols map. That helps to process R_X86_64_COPY relocations correctly.
For example, if 'a.out' executable with dependencies ['libstdc++.so', 'libc.so'] is being loaded
and 'a.out' uses 'stdout' symbol from 'libc.so', its relocation process goes as follows:
- linker processes relocation entry 'stdout' of type R_X86_64_GLOB_DAT from 'libc.so',
- it goes through object list ['a.out', 'libstdc++.so', 'libc.so'] to find first object
  that exports 'stdout' symbol. The symbol is in 'a.out' with the value e.g. '0x404070',
- linker sets 'stdout' symbol GOT entry in 'libc.so' to '0x404070',
....
- linker processes relocation entry 'stdout' of type R_X86_64_COPY from 'a.out',
- it goes through object list excluding 'a.out': ['libstdc++.so', 'libc.so']. The symbol is found in 'libc.so',
- linker copies the 'stdout' symbol content from 'libc.so' to memory at address '0x404070' (in 'a.out' object).

Objects are relocated in reverse order they were loaded. So in the example above, linker starts with relocating
'libc.so' and ends with 'a.out'. It is necessary e.g. when linking with 'libstdc++.so' - there are many
relocations which symbols are found in 'libstdc++.so', so they need to be resolved before their contents are
copied to 'a.out'. That also matches GNU ld.so behavior.
2021-01-03 16:04:40 +01:00
Mateusz Tabaka c11aad71b8 Add support for dlopen(NULL, ...) 2020-09-30 11:04:10 +02:00
jD91mZM2 0178565f71 Move text section of linker away
Seems to collide with the program being loaded
2020-08-04 12:24:51 +00:00
no name 890a9ed033 Implement dlopen/close 2020-07-19 21:27:38 +02:00
oddcoder 6d0c9dccd5 Allow Linker struct to specify with library name space to operate on 2020-07-19 21:21:48 +02:00
jD91mZM2 593925ceb4 Unambiguate all include guards
GNU binutils also uses _FNMATCH_H for a guard, maybe along others!
2020-06-29 11:34:51 +02:00
oddcoder 6aeb2d6fa2 Implement code that use .init_array and .fini_array
This patch implements ld.so code that makes use of both .init_array and
.fini_array. .init_array is fully utilized and is used in the correct
manner. However .fini_array is not used yet although the function that
runs .fini_array exists
2020-04-19 14:21:23 +02:00
oddcoder cc305fc574 Use Kernel mapped binaries when available.
At least in linux kernel, assuming that a.out is an elf that is linked
against relibc's own ld.so. When a user attempts `./a.out`, Linux kernel
will map `./a.out`, then map `ld.so` and jump into ld.so entry point.
In relibc ld.so will simply ignore the kernel mapped a.out and create
its own mapping. This patch forces relic ld.so to use the already mapped
`a.out` when ever possible. This would normally have slight performance
improvement (especially that currently relibc doesn't map a.out but
instead copy the data into empty mmaped memory).

The real motivation behind this patch is while impelemnting Runtime
linker debugging protocol for relibc. part of the protocol is ld.so
inseting address of some ld.so managed data structure into .dynamic
seciton of a.out then the debugger would check it there. The thing is
that debuggers have information about the kernel loaded ./a.out and they
check that one specifically which is in our case totally ignored by
relibc.
2020-04-07 21:26:58 +02:00
oddcoder 04ea2f9397 Refactor Linker::Link
This patch does basically two things:
- First make `global` variable not public, And make it accessable via a
function `get_sym`.
- Isolate the procedure that collect global symbols into single function
that does that and call it `collect_syms`.

The motivation of this patch is the second one where this procedure is
extended, thus it needs a seamless way to access those symbols
2020-03-07 23:52:27 +02:00
Jeremy Soller a3f7a174f6 WIP - implementation of dlfcn 2019-12-18 21:21:25 -07:00
jD91mZM2 b8c50c7c64 Format 2019-08-04 19:05:45 +02:00
jD91mZM2 c7d499d4f2 Upgrade to the 2018 edition
I didn't think it'd be this useful first, but thank god for `cargo fix --edition`!
2019-08-04 19:05:45 +02:00
Mateusz Mikuła 63a1319e50 Cbindgen update 2019-07-10 10:08:39 +00:00
jD91mZM2 6d4ac9dba8 Finally, get rid of all rust warnings
Just a small step along the way to reduce the massive wall of spam
every time you compile.

This was done partly automagically with `cargo fix`. The rest was me
deleting or commenting out a bunch of variables. Hope nothing was
important...
2019-04-21 21:12:16 +02:00
Jeremy Soller c71088e768 Cleanup and format 2019-01-14 21:07:24 -07:00
Jeremy Soller aefb4fb116 Improvements for dlfcn 2019-01-06 16:28:22 -07:00
Jeremy Soller dc4aa9cf0b Use typedef for Dl_info 2019-01-06 12:53:43 -07:00
Jeremy Soller 53874213fa Add dladdr 2019-01-06 12:48:36 -07:00
Jeremy Soller 46020f466f Add dlfcn, fixes for glib and gstreamer 2019-01-06 07:30:42 -07:00