Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.os.linux.AUDIT.ARCH needs to be rewritten #20743

Closed
alexrp opened this issue Jul 22, 2024 · 0 comments · Fixed by #20772
Closed

std.os.linux.AUDIT.ARCH needs to be rewritten #20743

alexrp opened this issue Jul 22, 2024 · 0 comments · Fixed by #20772
Labels
os-linux standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@alexrp
Copy link
Contributor

alexrp commented Jul 22, 2024

zig/lib/std/os/linux.zig

Lines 7271 to 7331 in eac7fd4

// TODO: Add the rest of the AUDIT defines?
pub const AUDIT = struct {
pub const ARCH = enum(u32) {
const @"64BIT" = 0x80000000;
const LE = 0x40000000;
pub const current: AUDIT.ARCH = switch (native_arch) {
.x86 => .X86,
.x86_64 => .X86_64,
.aarch64 => .AARCH64,
.arm, .thumb => .ARM,
.riscv64 => .RISCV64,
.sparc64 => .SPARC64,
.mips => .MIPS,
.mipsel => .MIPSEL,
.powerpc => .PPC,
.powerpc64 => .PPC64,
.powerpc64le => .PPC64LE,
else => @compileError("unsupported architecture"),
};
AARCH64 = toAudit(.aarch64),
ARM = toAudit(.arm),
ARMEB = toAudit(.armeb),
CSKY = toAudit(.csky),
HEXAGON = @intFromEnum(std.elf.EM.HEXAGON),
X86 = toAudit(.x86),
M68K = toAudit(.m68k),
MIPS = toAudit(.mips),
MIPSEL = toAudit(.mips) | LE,
MIPS64 = toAudit(.mips64),
MIPSEL64 = toAudit(.mips64) | LE,
PPC = toAudit(.powerpc),
PPC64 = toAudit(.powerpc64),
PPC64LE = toAudit(.powerpc64le),
RISCV32 = toAudit(.riscv32),
RISCV64 = toAudit(.riscv64),
S390X = toAudit(.s390x),
SPARC = toAudit(.sparc),
SPARC64 = toAudit(.sparc64),
X86_64 = toAudit(.x86_64),
fn toAudit(arch: std.Target.Cpu.Arch) u32 {
var res: u32 = @intFromEnum(arch.toElfMachine());
if (arch.endian() == .little) res |= LE;
switch (arch) {
.aarch64,
.mips64,
.mips64el,
.powerpc64,
.powerpc64le,
.riscv64,
.sparc64,
.x86_64,
=> res |= @"64BIT",
else => {},
}
return res;
}
};
};

There are two problems with this code:

  • Implementing the bindings in terms of std.Target.Cpu.Arch mapped to std.elf.EM is too clever. There is not a 1:1 mapping here; it would at least need to take an std.Target.Abi too. But really, just use std.elf.EM.
  • Detection for current is broken for cases where the ABI affects the decision. It's also missing some architectures.
@Vexu Vexu added the standard library This issue involves writing Zig code for the standard library. label Jul 22, 2024
@Vexu Vexu added this to the 0.14.0 milestone Jul 22, 2024
alexrp added a commit to alexrp/zig that referenced this issue Jul 24, 2024
SammyJames pushed a commit to SammyJames/zig that referenced this issue Aug 7, 2024
igor84 pushed a commit to igor84/zig that referenced this issue Aug 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os-linux standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants