From 789fb9165c33e9bbd755933d144b820ac9e1ad27 Mon Sep 17 00:00:00 2001 From: Carmen Date: Mon, 10 Mar 2025 13:46:39 +0700 Subject: [PATCH] lib/std/elf.zig: use Word-sized enum for Phdr.p_type fields --- lib/std/elf.zig | 21 +++++++++++++++++++-- src/link/Elf.zig | 42 +++++++++++++++++++++--------------------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/lib/std/elf.zig b/lib/std/elf.zig index 28f7cc248222..2687613aaf67 100644 --- a/lib/std/elf.zig +++ b/lib/std/elf.zig @@ -758,8 +758,25 @@ pub const Elf64_Ehdr = extern struct { e_shnum: Half, e_shstrndx: Half, }; + +pub const PhdrType = enum(Word) { + null = PT_NULL, + load = PT_LOAD, + dynamic = PT_DYNAMIC, + interp = PT_INTERP, + note = PT_NOTE, + shlib = PT_SHLIB, + phdr = PT_PHDR, + tls = PT_TLS, + loos = PT_LOOS, + hiios = PT_HIOS, + loproc = PT_LOPROC, + hiproc = PT_HIPROC, + _, +}; + pub const Elf32_Phdr = extern struct { - p_type: Word, + p_type: PhdrType, p_offset: Elf32_Off, p_vaddr: Elf32_Addr, p_paddr: Elf32_Addr, @@ -769,7 +786,7 @@ pub const Elf32_Phdr = extern struct { p_align: Word, }; pub const Elf64_Phdr = extern struct { - p_type: Word, + p_type: PhdrType, p_flags: Word, p_offset: Elf64_Off, p_vaddr: Elf64_Addr, diff --git a/src/link/Elf.zig b/src/link/Elf.zig index 53f88101b199..f96f10c9b8f0 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -410,7 +410,7 @@ pub fn createEmpty( .memsz = reserved, })).toOptional(); self.phdr_indexes.table_load = (try self.addPhdr(.{ - .type = elf.PT_LOAD, + .type = .load, .flags = elf.PF_R, .@"align" = self.page_size, .addr = self.image_base, @@ -566,7 +566,7 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) !?u64 { } for (self.phdrs.items) |phdr| { - if (phdr.p_type != elf.PT_LOAD) continue; + if (phdr.p_type != .load) continue; const increased_size = padToIdeal(phdr.p_filesz); const test_end = phdr.p_offset +| increased_size; if (start < test_end) { @@ -2992,13 +2992,13 @@ fn setHashSections(self: *Elf) !void { fn phdrRank(phdr: elf.Elf64_Phdr) u8 { return switch (phdr.p_type) { - elf.PT_NULL => 0, - elf.PT_PHDR => 1, - elf.PT_INTERP => 2, - elf.PT_LOAD => 3, - elf.PT_DYNAMIC, elf.PT_TLS => 4, - elf.PT_GNU_EH_FRAME => 5, - elf.PT_GNU_STACK => 6, + .null => 0, + .phdr => 1, + .interp => 2, + .load => 3, + .dynamic, .tls => 4, + .gnu_eh_frame => 5, + .gnu_stack => 6, else => 7, }; } @@ -4213,7 +4213,7 @@ pub fn isEffectivelyDynLib(self: Elf) bool { } fn getPhdr(self: *Elf, opts: struct { - type: u32 = 0, + type: elf.PhdrType = .null, flags: u32 = 0, }) OptionalProgramHeaderIndex { for (self.phdrs.items, 0..) |phdr, phndx| { @@ -4227,7 +4227,7 @@ fn getPhdr(self: *Elf, opts: struct { } fn addPhdr(self: *Elf, opts: struct { - type: u32 = 0, + type: elf.PhdrType = .null, flags: u32 = 0, @"align": u64 = 0, offset: u64 = 0, @@ -4750,18 +4750,18 @@ fn formatPhdr( if (write) flags[1] = 'W'; if (read) flags[2] = 'R'; const p_type = switch (phdr.p_type) { - elf.PT_LOAD => "LOAD", - elf.PT_TLS => "TLS", - elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME", - elf.PT_GNU_STACK => "GNU_STACK", - elf.PT_DYNAMIC => "DYNAMIC", - elf.PT_INTERP => "INTERP", - elf.PT_NULL => "NULL", - elf.PT_PHDR => "PHDR", - elf.PT_NOTE => "NOTE", + .load => "LOAD", + .tls => "TLS", + .gnu_eh_frame => "GNU_EH_FRAME", + .gnu_stack => "GNU_STACK", + .dynamic => "DYNAMIC", + .interp => "INTERP", + .null => "NULL", + .phdr => "PHDR", + .note => "NOTE", else => "UNKNOWN", }; - try writer.print("{s} : {s} : @{x} ({x}) : align({x}) : filesz({x}) : memsz({x})", .{ + try writer.print("{any} : {s} : @{x} ({x}) : align({x}) : filesz({x}) : memsz({x})", .{ p_type, flags, phdr.p_offset, phdr.p_vaddr, phdr.p_align, phdr.p_filesz, phdr.p_memsz, });