Skip to content

[dlfcn] Resolve libraries in dependency order (#2128)#6

Draft
esaurez wants to merge 1 commit into
devfrom
fix/dlfcn-topo-sort
Draft

[dlfcn] Resolve libraries in dependency order (#2128)#6
esaurez wants to merge 1 commit into
devfrom
fix/dlfcn-topo-sort

Conversation

@esaurez

@esaurez esaurez commented Apr 19, 2026

Copy link
Copy Markdown
Owner

Summary

Replace arbitrary-order relocation resolution with a topological sort that resolves dependencies before their dependents (leaves first, root last).

Fixes nanvix#2128. Depends on #3 (atomic registry rollback).

Problem

resolve_all_symbols() resolved libraries in arbitrary BTreeMap handle order (sorted by file descriptor number). If a root library was resolved before its dependencies, and those dependencies had their own relocations that needed patching, the resolution could fail.

Expected Behavior (Linux/musl Reference)

Dynamic linkers resolve dependencies before their dependents:

  • glibc builds a BFS-ordered search list in elf/dl-deps.c (_dl_map_object_deps). Leaf dependencies appear earlier and are relocated first in elf/dl-open.c.
  • musl's dlopen in ldso/dynlink.c resolves dependencies before their dependents during relocation.

Fix

  • Topological sort over newly added handles: a library is resolved only after all its dependencies (that are also new in this dlopen call) have been resolved.
  • Added dependency_handles() method to DynamicLibrary.
  • Falls back to arbitrary order with a warning log if a cycle prevents progress.

Affected Files

  • src/libs/syscall/src/dlfcn/syscall/dlopen.rs — topo-sort resolve_all_symbols()
  • src/libs/syscall/src/dlfcn/syscall/dynlib.rsdependency_handles()

Testing

All existing integration tests pass (both HTTP and terminal executors).

@esaurez esaurez changed the title [dlfcn] Resolve libraries in dependency order (topological sort) [dlfcn] Resolve libraries in dependency order (#2128) Apr 20, 2026
@esaurez esaurez force-pushed the fix/dlfcn-topo-sort branch from 320bbc8 to 1b171ce Compare April 20, 2026 19:05
@esaurez esaurez changed the base branch from dev to fix/dlfcn-atomic-registry April 20, 2026 19:05
@esaurez esaurez force-pushed the fix/dlfcn-atomic-registry branch from 1677feb to 0b657d1 Compare April 20, 2026 20:04
@esaurez esaurez force-pushed the fix/dlfcn-topo-sort branch 2 times, most recently from 22f7ba4 to 17e93ab Compare April 20, 2026 21:10
Replace arbitrary-order relocation resolution with a topological sort
that resolves dependencies before their dependents (leaves first,
root last). This ensures that when a library's relocations reference
symbols from its dependencies, those dependencies are already fully
resolved.

Changes:
- resolve_all_symbols(): topological sort over new handles.
- dependency_handles(): new method on DynamicLibrary.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@esaurez esaurez force-pushed the fix/dlfcn-topo-sort branch from 17e93ab to 01a51f4 Compare April 21, 2026 21:44
@esaurez esaurez changed the base branch from fix/dlfcn-atomic-registry to dev April 21, 2026 21:44
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.

[dlfcn] Library relocation resolution order is arbitrary instead of dependency-ordered

1 participant