Skip to content

fix: Properly merge .ctors/.dtors sections into .init_array/.fini_array#1479

Merged
lapla-cogito merged 2 commits into
wild-linker:mainfrom
lapla-cogito:sort_ctors
Jan 25, 2026
Merged

fix: Properly merge .ctors/.dtors sections into .init_array/.fini_array#1479
lapla-cogito merged 2 commits into
wild-linker:mainfrom
lapla-cogito:sort_ctors

Conversation

@lapla-cogito

Copy link
Copy Markdown
Member

.ctors/.dtors are fundamentally different from .init_array/.fini_array in their execution order. .ctors/.dtors execute function pointers in reverse order (last to first), while .init_array/.fini_array execute them in forward order (first to last). Additionally, their priority numbering is inverted - .ctors/.dtors use descending priority (u16::MAX = lowest, 0 = highest), whereas .init_array/.fini_array use ascending priority (0 = highest, u16::MAX = lowest).

Until now, Wild has not properly integrated .ctors/.dtors sections into .init_array/.fini_array. This change implements the integration by inverting the priority of .ctors/.dtors sections (u16::MAX.saturating_sub(p)) to match the .init_array/.fini_array ordering system and reversing the pointer array contents during write operations to preserve correct execution order.

For testing, simply removing the DiffIgnores from ctors.c should be sufficient.

Comment thread libwild/src/elf_writer.rs Outdated

@marxin marxin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM apart from the nits.

Comment thread libwild/src/elf_writer.rs Outdated
Comment thread libwild/src/layout.rs Outdated
@lapla-cogito lapla-cogito force-pushed the sort_ctors branch 5 times, most recently from 6704b52 to 6a6810f Compare January 25, 2026 09:43
Comment thread libwild/src/resolution.rs
@lapla-cogito lapla-cogito merged commit 06468e2 into wild-linker:main Jan 25, 2026
20 checks passed
@lapla-cogito lapla-cogito deleted the sort_ctors branch January 25, 2026 11:01
Comment thread libwild/src/layout.rs
part_id: PartId,
) -> Result<Section> {
let size = object_state.object.section_size(header)?;
let section_name = object_state.object.section_name(header)?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note that fetching a C string is somewhat expensive and we're doing it here for all sections.

Benchmark 1 (337 runs): /home/david/save/zed/run-with /home/d/wild-builds/2026-02-09.ctor-dtor-order-revert --no-fork
  measurement          mean ± σ            min … max           outliers         delta
  wall_time           534ms ± 12.9ms     497ms …  567ms          0 ( 0%)        0%
  peak_rss           3.47GB ± 1.24MB    3.47GB … 3.47GB          2 ( 1%)        0%
  cpu_cycles         21.9G  ±  179M     21.3G  … 22.5G           5 ( 1%)        0%
  instructions       20.2G  ± 72.2M     20.1G  … 20.7G          10 ( 3%)        0%
  cache_references    476M  ± 3.50M      467M  …  489M          12 ( 4%)        0%
  cache_misses        124M  ±  710K      123M  …  127M           4 ( 1%)        0%
  branch_misses      39.3M  ±  203K     38.3M  … 40.2M          13 ( 4%)        0%
Benchmark 2 (328 runs): /home/david/save/zed/run-with /home/d/wild-builds/2026-02-09 --no-fork
  measurement          mean ± σ            min … max           outliers         delta
  wall_time           549ms ± 14.2ms     503ms …  576ms          1 ( 0%)        💩+  2.8% ±  0.4%
  peak_rss           3.47GB ± 1.20MB    3.47GB … 3.47GB          1 ( 0%)          +  0.0% ±  0.0%
  cpu_cycles         23.1G  ±  178M     22.6G  … 23.6G           1 ( 0%)        💩+  5.2% ±  0.1%
  instructions       20.6G  ± 62.1M     20.5G  … 20.8G           7 ( 2%)        💩+  1.9% ±  0.1%
  cache_references    492M  ± 3.15M      486M  …  504M           9 ( 3%)        💩+  3.5% ±  0.1%
  cache_misses        133M  ±  613K      131M  …  135M          11 ( 3%)        💩+  6.7% ±  0.1%
  branch_misses      41.0M  ±  199K     39.9M  … 41.4M           4 ( 1%)        💩+  4.4% ±  0.1%

Fixed by #1522

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.

3 participants