pub const DynamicLinker = struct pub fn init(maybe_path: ?[]const u8) DynamicLinkerAsserts that the length is less than or equal to 255 bytes.
pub fn initFmt(comptime fmt_str: []const u8, args: anytype) !DynamicLinkerpub fn get(dl: *const DynamicLinker) ?[]const u8The returned memory has the same lifetime as the DynamicLinker.
pub fn set(dl: *DynamicLinker, maybe_path: ?[]const u8) voidAsserts that the length is less than or equal to 255 bytes.
pub fn setFmt(dl: *DynamicLinker, comptime fmt_str: []const u8, args: anytype) !voidAsserts that the length is less than or equal to 255 bytes.
pub fn eql(lhs: DynamicLinker, rhs: DynamicLinker) boolpub fn kind(os: Os.Tag) Kindpub fn standard(cpu: Cpu, os: Os, abi: Abi) DynamicLinkerThe strictness of this function depends on the value of kind(os.tag):
.none: Ignores all arguments and just returns none..arch_os: Ignores abi and returns the dynamic linker matching cpu and os..arch_os_abi: Returns the dynamic linker matching cpu, os, and abi.In the case of .arch_os in particular, callers should be aware that a valid dynamic linker being returned only means that the cpu + os combination represents a platform that actually exists and which has an established dynamic linker path that does not change with the ABI; it does not necessarily mean that abi makes any sense at all for that platform. The responsibility for determining whether abi is valid in this case rests with the caller. Abi.default() can be used to pick a best-effort default ABI for such platforms.
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968pub const DynamicLinker = struct {
/// Contains the memory used to store the dynamic linker path. This field
/// should not be used directly. See `get` and `set`. This field exists so
/// that this API requires no allocator.
buffer: [255]u8,
/// Used to construct the dynamic linker path. This field should not be used
/// directly. See `get` and `set`.
len: u8,
pub const none: DynamicLinker = .{ .buffer = undefined, .len = 0 };
/// Asserts that the length is less than or equal to 255 bytes.
pub fn init(maybe_path: ?[]const u8) DynamicLinker {
var dl: DynamicLinker = undefined;
dl.set(maybe_path);
return dl;
}
pub fn initFmt(comptime fmt_str: []const u8, args: anytype) !DynamicLinker {
var dl: DynamicLinker = undefined;
try dl.setFmt(fmt_str, args);
return dl;
}
/// The returned memory has the same lifetime as the `DynamicLinker`.
pub fn get(dl: *const DynamicLinker) ?[]const u8 {
return if (dl.len > 0) dl.buffer[0..dl.len] else null;
}
/// Asserts that the length is less than or equal to 255 bytes.
pub fn set(dl: *DynamicLinker, maybe_path: ?[]const u8) void {
const path = maybe_path orelse "";
@memcpy(dl.buffer[0..path.len], path);
dl.len = @intCast(path.len);
}
/// Asserts that the length is less than or equal to 255 bytes.
pub fn setFmt(dl: *DynamicLinker, comptime fmt_str: []const u8, args: anytype) !void {
dl.len = @intCast((try std.mem.print(&dl.buffer, fmt_str, args)).len);
}
pub fn eql(lhs: DynamicLinker, rhs: DynamicLinker) bool {
return std.mem.eql(u8, lhs.buffer[0..lhs.len], rhs.buffer[0..rhs.len]);
}
pub const Kind = enum {
/// No dynamic linker.
none,
/// Dynamic linker path is determined by the arch/OS components.
arch_os,
/// Dynamic linker path is determined by the arch/OS/ABI components.
arch_os_abi,
};
pub fn kind(os: Os.Tag) Kind {
return switch (os) {
.fuchsia,
.haiku,
.illumos,
.serenity,
.dragonfly,
.freebsd,
.netbsd,
.openbsd,
.driverkit,
.ios,
.maccatalyst,
.macos,
.tvos,
.visionos,
.watchos,
=> .arch_os,
.hurd,
.linux,
=> .arch_os_abi,
.freestanding,
.other,
.contiki,
.hermit,
.managarm, // Needs to be double-checked.
.plan9,
.rtems,
.uefi,
.windows,
.@"3ds",
.wiiu,
.@"switch",
.gba,
.emscripten,
.wasi,
.amdhsa,
.amdpal,
.cuda,
.mesa3d,
.nvcl,
.opencl,
.opengl,
.vulkan,
.psx,
.ps3,
.ps4,
.ps5,
.psp,
.vita,
.tios,
.ashetos,
=> .none,
};
}
/// The strictness of this function depends on the value of `kind(os.tag)`:
///
/// * `.none`: Ignores all arguments and just returns `none`.
/// * `.arch_os`: Ignores `abi` and returns the dynamic linker matching `cpu` and `os`.
/// * `.arch_os_abi`: Returns the dynamic linker matching `cpu`, `os`, and `abi`.
///
/// In the case of `.arch_os` in particular, callers should be aware that a valid dynamic linker
/// being returned only means that the `cpu` + `os` combination represents a platform that
/// actually exists and which has an established dynamic linker path that does not change with
/// the ABI; it does not necessarily mean that `abi` makes any sense at all for that platform.
/// The responsibility for determining whether `abi` is valid in this case rests with the
/// caller. `Abi.default()` can be used to pick a best-effort default ABI for such platforms.
pub fn standard(cpu: Cpu, os: Os, abi: Abi) DynamicLinker {
return switch (os.tag) {
.fuchsia => switch (cpu.arch) {
.arm,
.aarch64,
.riscv64,
.thumb,
.x86_64,
=> init("ld.so.1"), // Fuchsia is unusual in that `DT_INTERP` is just a basename.
else => none,
},
.haiku => switch (cpu.arch) {
.arm,
.aarch64,
.riscv64,
.x86,
.x86_64,
=> init("/system/runtime_loader"),
else => none,
},
.hurd => switch (cpu.arch) {
.aarch64,
.aarch64_be,
=> |arch| if (abi == .gnu) initFmt("/lib/ld-{s}.so.1", .{@tagName(arch)}) else none,
.x86 => if (abi == .gnu) init("/lib/ld.so.1") else none,
.x86_64 => initFmt("/lib/ld-{s}.so.1", .{switch (abi) {
.gnu => "x86-64",
.gnux32 => "x32",
else => return none,
}}),
else => none,
},
.illumos,
=> switch (cpu.arch) {
.x86,
.x86_64,
=> initFmt("/lib/{s}ld.so.1", .{if (ptrBitWidth_cpu_abi(cpu, .none) == 64) "64/" else ""}),
else => none,
},
.linux => if (abi.isAndroid())
switch (cpu.arch) {
.arm => if (abi == .androideabi) init("/system/bin/linker") else none,
.aarch64,
.riscv64,
.x86,
.x86_64,
=> if (abi == .android) initFmt("/system/bin/linker{s}", .{
if (ptrBitWidth_cpu_abi(cpu, abi) == 64) "64" else "",
}) else none,
else => none,
}
else if (abi.isMusl())
switch (cpu.arch) {
.arm,
.armeb,
.thumb,
.thumbeb,
=> |arch| initFmt("/lib/ld-musl-arm{s}{s}.so.1", .{
if (arch == .armeb or arch == .thumbeb) "eb" else "",
switch (abi) {
.musleabi => "",
.musleabihf => "hf",
else => return none,
},
}),
.loongarch32,
.loongarch64,
=> |arch| initFmt("/lib/ld-musl-{s}{s}.so.1", .{
@tagName(arch),
switch (abi) {
.musl => "",
.muslf32 => "-sp",
.muslsf => "-sf",
else => return none,
},
}),
.aarch64,
.aarch64_be,
.hexagon,
.kvx,
.m68k,
.microblaze,
.microblazeel,
.or1k,
.powerpc64,
.powerpc64le,
.s390x,
=> |arch| if (abi == .musl) initFmt("/lib/ld-musl-{s}.so.1", .{@tagName(arch)}) else none,
.mips,
.mipsel,
=> |arch| initFmt("/lib/ld-musl-mips{s}{s}{s}.so.1", .{
if (cpu.has(.mips, .mips32r6)) "r6" else "",
if (arch == .mipsel) "el" else "",
switch (abi) {
.musleabi => "-sf",
.musleabihf => "",
else => return none,
},
}),
.mips64,
.mips64el,
=> |arch| initFmt("/lib/ld-musl-mips{s}{s}{s}.so.1", .{
switch (abi) {
.muslabi64 => "64",
.muslabin32 => "n32",
else => return none,
},
if (cpu.has(.mips, .mips64r6)) "r6" else "",
if (arch == .mips64el) "el" else "",
}),
.powerpc => initFmt("/lib/ld-musl-powerpc{s}.so.1", .{switch (abi) {
.musleabi => "-sf",
.musleabihf => "",
else => return none,
}}),
.sh,
.sheb,
=> |arch| initFmt("/lib/ld-musl-{t}{s}.so.1", .{
arch,
switch (abi) {
.musleabi => "-nofpu",
.musleabihf => "",
else => return none,
},
}),
.riscv32,
.riscv64,
=> |arch| if (abi == .musl) initFmt("/lib/ld-musl-{s}{s}.so.1", .{
@tagName(arch),
if (cpu.has(.riscv, .d))
""
else if (cpu.has(.riscv, .f))
"-sp"
else
"-sf",
}) else none,
.x86 => if (abi == .musl) init("/lib/ld-musl-i386.so.1") else none,
.x86_64 => initFmt("/lib/ld-musl-{s}.so.1", .{switch (abi) {
.musl => "x86_64",
.muslx32 => "x32",
else => return none,
}}),
else => none,
}
else if (abi.isGnu())
switch (cpu.arch) {
.arc,
.arceb,
=> |arch| if (abi == .gnu) initFmt("/lib/ld-linux-{t}.so.2", .{arch}) else none,
.arm,
.armeb,
=> initFmt("/lib/ld-linux{s}.so.3", .{switch (abi) {
.gnueabi => "",
.gnueabihf => "-armhf",
else => return none,
}}),
.aarch64,
.aarch64_be,
.or1k,
=> |arch| if (abi == .gnu) initFmt("/lib/ld-linux-{s}.so.1", .{@tagName(arch)}) else none,
.csky => initFmt("/lib/ld-linux-cskyv2{s}.so.1", .{switch (abi) {
.gnueabi => "",
.gnueabihf => "-hf",
else => return none,
}}),
.loongarch32,
.loongarch64,
=> |arch| initFmt("/lib{s}/ld-linux-{s}{s}.so.1", .{
switch (arch) {
.loongarch32 => "32",
.loongarch64 => "64",
else => unreachable,
},
switch (arch) {
.loongarch32 => "loongarch-ilp32",
.loongarch64 => "loongarch-lp64",
else => unreachable,
},
switch (abi) {
.gnu => "d",
.gnuf32 => "f",
.gnusf => "s",
else => return none,
},
}),
.hppa,
.m68k,
.microblaze,
.microblazeel,
.xtensa,
.xtensaeb,
=> if (abi == .gnu) init("/lib/ld.so.1") else none,
.mips,
.mipsel,
=> switch (abi) {
.gnueabi,
.gnueabihf,
=> initFmt("/lib/ld{s}.so.1", .{
if (cpu.has(.mips, .nan2008)) "-linux-mipsn8" else "",
}),
else => none,
},
.mips64,
.mips64el,
=> initFmt("/lib{s}/ld{s}.so.1", .{
switch (abi) {
.gnuabi64 => "64",
.gnuabin32 => "32",
else => return none,
},
if (cpu.has(.mips, .nan2008)) "-linux-mipsn8" else "",
}),
.powerpc => switch (abi) {
.gnueabi,
.gnueabihf,
=> init("/lib/ld.so.1"),
else => none,
},
.powerpc64 => if (abi == .gnu) init("/lib64/ld64.so.1") else none,
.powerpc64le => if (abi == .gnu) init("/lib64/ld64.so.2") else none,
.riscv32,
.riscv64,
=> |arch| if (abi == .gnu) initFmt("/lib/ld-linux-{s}{s}.so.1", .{
switch (arch) {
.riscv32 => "riscv32-ilp32",
.riscv64 => "riscv64-lp64",
else => unreachable,
},
if (cpu.has(.riscv, .d))
"d"
else if (cpu.has(.riscv, .f))
"f"
else
"",
}) else none,
.s390x => if (abi == .gnu) init("/lib/ld64.so.1") else none,
.sh,
.sheb,
=> switch (abi) {
.gnueabi,
.gnueabihf,
=> init("/lib/ld-linux.so.2"),
else => none,
},
.alpha,
.sparc,
.x86,
=> if (abi == .gnu) init("/lib/ld-linux.so.2") else none,
.sparc64 => if (abi == .gnu) init("/lib64/ld-linux.so.2") else none,
.x86_64 => switch (abi) {
.gnu => init("/lib64/ld-linux-x86-64.so.2"),
.gnux32 => init("/libx32/ld-linux-x32.so.2"),
else => none,
},
else => none,
}
else
none, // Not a known Linux libc.
.serenity => switch (cpu.arch) {
.aarch64,
.riscv64,
.x86_64,
=> init("/usr/lib/Loader.so"),
else => none,
},
.dragonfly => if (cpu.arch == .x86_64) initFmt("{s}/libexec/ld-elf.so.2", .{
if (os.version_range.semver.isAtLeast(.{ .major = 3, .minor = 8, .patch = 0 }) orelse false)
""
else
"/usr",
}) else none,
.freebsd => switch (cpu.arch) {
.arm,
.aarch64,
.powerpc,
.powerpc64,
.powerpc64le,
.riscv64,
.x86,
.x86_64,
=> initFmt("{s}/libexec/ld-elf.so.1", .{
if (os.version_range.semver.isAtLeast(.{ .major = 6, .minor = 0, .patch = 0 }) orelse false)
""
else
"/usr",
}),
else => none,
},
.netbsd => switch (cpu.arch) {
.alpha,
.arm,
.armeb,
.aarch64,
.aarch64_be,
.hppa,
.m68k,
.mips,
.mipsel,
.mips64,
.mips64el,
.powerpc,
.riscv32,
.riscv64,
.sh,
.sheb,
.sparc,
.sparc64,
.x86,
.x86_64,
=> init("/libexec/ld.elf_so"),
else => none,
},
.openbsd => switch (cpu.arch) {
.alpha,
.arm,
.aarch64,
.hppa,
.m88k,
.mips64,
.mips64el,
.powerpc,
.powerpc64,
.riscv64,
.sh,
.sparc64,
.x86,
.x86_64,
=> init("/usr/libexec/ld.so"),
else => none,
},
.driverkit,
.ios,
.maccatalyst,
.macos,
.tvos,
.visionos,
.watchos,
=> switch (cpu.arch) {
.aarch64,
.x86_64,
=> init("/usr/lib/dyld"),
else => none,
},
// Operating systems in this list have been verified as not having a standard
// dynamic linker path.
.freestanding,
.other,
.contiki,
.hermit,
.plan9,
.rtems,
.uefi,
.windows,
.@"3ds",
.wiiu,
.@"switch",
.gba,
.psx,
.psp,
.vita,
.emscripten,
.wasi,
.amdhsa,
.amdpal,
.cuda,
.mesa3d,
.nvcl,
.opencl,
.opengl,
.vulkan,
.tios,
.ashetos,
=> none,
// TODO go over each item in this list and either move it to the above list, or
// implement the standard dynamic linker path code for it.
.managarm,
.ps3,
.ps4,
.ps5,
=> none,
} catch unreachable;
}
}