|
| 1 | +const builtin = @import("builtin"); |
| 2 | + |
| 3 | +/// Elements are const_linksection, var_linksection, fn_linksection |
| 4 | +const linksections: ?[3][]const u8 = switch (builtin.target.ofmt) { |
| 5 | + .elf => .{ ".rodata", ".data", ".text" }, |
| 6 | + .coff => .{ ".rdata", ".data", ".text" }, |
| 7 | + .macho => .{ "__TEXT,__const", "__DATA,__data", "__TEXT,__text" }, |
| 8 | + else => null, |
| 9 | +}; |
| 10 | +const const_linksection = linksections.?[0]; |
| 11 | +const var_linksection = linksections.?[1]; |
| 12 | +const fn_linksection = linksections.?[2]; |
| 13 | + |
| 14 | +test "addrspace on container-level const" { |
| 15 | + const S = struct { |
| 16 | + const a: u32 addrspace(.generic) = 123; |
| 17 | + fn check(ptr: anytype) !void { |
| 18 | + if (ptr.* != 123) return error.TestFailed; |
| 19 | + } |
| 20 | + }; |
| 21 | + try S.check(&S.a); |
| 22 | + try comptime S.check(&S.a); |
| 23 | +} |
| 24 | + |
| 25 | +test "linksection on container-level const" { |
| 26 | + if (linksections == null) return; |
| 27 | + const S = struct { |
| 28 | + const a: u32 linksection(const_linksection) = 123; |
| 29 | + fn check(ptr: anytype) !void { |
| 30 | + if (ptr.* != 123) return error.TestFailed; |
| 31 | + } |
| 32 | + }; |
| 33 | + try S.check(&S.a); |
| 34 | + try comptime S.check(&S.a); |
| 35 | +} |
| 36 | + |
| 37 | +test "addrspace and linksection on container-level const" { |
| 38 | + if (linksections == null) return; |
| 39 | + const S = struct { |
| 40 | + const a: u32 addrspace(.generic) linksection(const_linksection) = 123; |
| 41 | + fn check(ptr: anytype) !void { |
| 42 | + if (ptr.* != 123) return error.TestFailed; |
| 43 | + } |
| 44 | + }; |
| 45 | + try S.check(&S.a); |
| 46 | + try comptime S.check(&S.a); |
| 47 | +} |
| 48 | + |
| 49 | +test "addrspace on container-level var" { |
| 50 | + const S = struct { |
| 51 | + var a: u32 addrspace(.generic) = 123; |
| 52 | + fn check(ptr: anytype) !void { |
| 53 | + if (ptr.* != 123) return error.TestFailed; |
| 54 | + } |
| 55 | + }; |
| 56 | + try S.check(&S.a); |
| 57 | +} |
| 58 | + |
| 59 | +test "linksection on container-level var" { |
| 60 | + if (linksections == null) return; |
| 61 | + const S = struct { |
| 62 | + var a: u32 linksection(var_linksection) = 123; |
| 63 | + fn check(ptr: anytype) !void { |
| 64 | + if (ptr.* != 123) return error.TestFailed; |
| 65 | + } |
| 66 | + }; |
| 67 | + try S.check(&S.a); |
| 68 | +} |
| 69 | + |
| 70 | +test "addrspace and linksection on container-level var" { |
| 71 | + if (linksections == null) return; |
| 72 | + const S = struct { |
| 73 | + var a: u32 addrspace(.generic) linksection(var_linksection) = 123; |
| 74 | + fn check(ptr: anytype) !void { |
| 75 | + if (ptr.* != 123) return error.TestFailed; |
| 76 | + } |
| 77 | + }; |
| 78 | + try S.check(&S.a); |
| 79 | +} |
| 80 | + |
| 81 | +test "addrspace on fn" { |
| 82 | + const S = struct { |
| 83 | + fn f() addrspace(.generic) u32 { |
| 84 | + return 123; |
| 85 | + } |
| 86 | + fn check(fnPtr: anytype) !void { |
| 87 | + if (fnPtr() != 123) return error.TestFailed; |
| 88 | + } |
| 89 | + }; |
| 90 | + try S.check(&S.f); |
| 91 | + try comptime S.check(&S.f); |
| 92 | +} |
| 93 | + |
| 94 | +test "linksection on fn" { |
| 95 | + if (linksections == null) return; |
| 96 | + const S = struct { |
| 97 | + fn f() linksection(fn_linksection) u32 { |
| 98 | + return 123; |
| 99 | + } |
| 100 | + fn check(fnPtr: anytype) !void { |
| 101 | + if (fnPtr() != 123) return error.TestFailed; |
| 102 | + } |
| 103 | + }; |
| 104 | + try S.check(&S.f); |
| 105 | + try comptime S.check(&S.f); |
| 106 | +} |
| 107 | + |
| 108 | +test "addrspace and linksection on fn" { |
| 109 | + if (linksections == null) return; |
| 110 | + const S = struct { |
| 111 | + fn f() addrspace(.generic) linksection(fn_linksection) u32 { |
| 112 | + return 123; |
| 113 | + } |
| 114 | + fn check(fnPtr: anytype) !void { |
| 115 | + if (fnPtr() != 123) return error.TestFailed; |
| 116 | + } |
| 117 | + }; |
| 118 | + try S.check(&S.f); |
| 119 | + try comptime S.check(&S.f); |
| 120 | +} |
0 commit comments