authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-05 03:38:32+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-08-05 03:38:32+02:00
logf8c6193e4c4a2e404def6c641bbd958a0226c486
treefa6b099b60ef0836f1cc348dda42471c7a75754c
parente88566253d65a83e3564f95504fe034b4287c340
parent9bc9544bc8df2b0fe5ad82bd5459a9ce7ca55bac

Merge pull request 'elf: remove deprecated elf.ElfXX_Phdr, elf.Phdr and elf.PR_*' (#36353) from kada49/deprecated-phdr-remove into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36353 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

14 files changed, 207 insertions(+), 276 deletions(-)

lib/compiler/objcopy.zig+7-7
......@@ -435,13 +435,13 @@ const BinaryElfOutput = struct {
435435
436436 var program_headers = elf_hdr.iterateProgramHeaders(in);
437437 while (try program_headers.next()) |phdr| {
438 if (phdr.p_type == elf.PT_LOAD) {
438 if (phdr.type == .LOAD) {
439439 const newSegment = try allocator.create(BinaryElfSegment);
440440
441 newSegment.physicalAddress = phdr.p_paddr;
442 newSegment.virtualAddress = phdr.p_vaddr;
443 newSegment.fileSize = @intCast(phdr.p_filesz);
444 newSegment.elfOffset = phdr.p_offset;
441 newSegment.physicalAddress = phdr.paddr;
442 newSegment.virtualAddress = phdr.vaddr;
443 newSegment.fileSize = @intCast(phdr.filesz);
444 newSegment.elfOffset = phdr.offset;
445445 newSegment.binaryOffset = 0;
446446 newSegment.firstSection = null;
447447
......@@ -495,8 +495,8 @@ const BinaryElfOutput = struct {
495495 return self;
496496 }
497497
498 fn sectionWithinSegment(section: *BinaryElfSection, segment: elf.Elf64_Phdr) bool {
499 return segment.p_offset <= section.elfOffset and (segment.p_offset + segment.p_filesz) >= (section.elfOffset + section.fileSize);
498 fn sectionWithinSegment(section: *BinaryElfSection, segment: elf.Elf64.Phdr) bool {
499 return segment.offset <= section.elfOffset and (segment.offset + segment.filesz) >= (section.elfOffset + section.fileSize);
500500 }
501501
502502 fn sectionValidForOutput(shdr: anytype) bool {
lib/std/Build/Step/Compile.zig+1-1
......@@ -101,7 +101,7 @@ each_lib_rpath: ?bool = null,
101101/// This option overrides the CLI argument passed to `zig build`.
102102build_id: ?std.zig.BuildId = null,
103103
104/// Create a .eh_frame_hdr section and a PT_GNU_EH_FRAME segment in the ELF
104/// Create a .eh_frame_hdr section and a PT.GNU_EH_FRAME segment in the ELF
105105/// file.
106106link_eh_frame_hdr: bool = false,
107107link_emit_relocs: bool = false,
lib/std/debug/SelfInfo/Elf.zig+1-1
......@@ -518,7 +518,7 @@ const DlIterContext = struct {
518518 for (info.phdr[0..info.phnum]) |phdr| {
519519 if (phdr.type != .LOAD) continue;
520520 try context.si.ranges.append(gpa, .{
521 // Overflowing addition handles VSDOs having p_vaddr = 0xffffffffff700000
521 // Overflowing addition handles VSDOs having vaddr = 0xffffffffff700000
522522 .start = info.addr +% phdr.vaddr,
523523 .len = phdr.memsz,
524524 .module_index = module_index,
lib/std/dynamic_library.zig+17-17
......@@ -103,7 +103,7 @@ pub fn get_DYNAMIC() ?[*]const elf.Dyn {
103103
104104pub fn linkmap_iterator() error{InvalidExe}!LinkMap.Iterator {
105105 const _DYNAMIC = get_DYNAMIC() orelse {
106 // No PT_DYNAMIC means this is a statically-linked non-PIE program.
106 // No PT.DYNAMIC means this is a statically-linked non-PIE program.
107107 return .{ .current = null };
108108 };
109109
......@@ -261,10 +261,10 @@ pub const ElfDynLib = struct {
261261 i += 1;
262262 ph_addr += eh.e_phentsize;
263263 }) {
264 const ph = @as(*elf.Phdr, @ptrFromInt(ph_addr));
265 switch (ph.p_type) {
266 elf.PT_LOAD => virt_addr_end = @max(virt_addr_end, ph.p_vaddr + ph.p_memsz),
267 elf.PT_DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(elf_addr + ph.p_offset)),
264 const ph = @as(*elf.ElfN.Phdr, @ptrFromInt(ph_addr));
265 switch (ph.type) {
266 .LOAD => virt_addr_end = @max(virt_addr_end, ph.vaddr + ph.memsz),
267 .DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(elf_addr + ph.offset)),
268268 else => {},
269269 }
270270 }
......@@ -292,23 +292,23 @@ pub const ElfDynLib = struct {
292292 i += 1;
293293 ph_addr += eh.e_phentsize;
294294 }) {
295 const ph = @as(*elf.Phdr, @ptrFromInt(ph_addr));
296 switch (ph.p_type) {
297 elf.PT_LOAD => {
295 const ph = @as(*elf.ElfN.Phdr, @ptrFromInt(ph_addr));
296 switch (ph.type) {
297 .LOAD => {
298298 // The VirtAddr may not be page-aligned; in such case there will be
299299 // extra nonsense mapped before/after the VirtAddr,MemSiz
300 const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, page_size) - 1);
301 const extra_bytes = (base + ph.p_vaddr) - aligned_addr;
302 const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, page_size);
300 const aligned_addr = (base + ph.vaddr) & ~(@as(usize, page_size) - 1);
301 const extra_bytes = (base + ph.vaddr) - aligned_addr;
302 const extended_memsz = mem.alignForward(usize, ph.memsz + extra_bytes, page_size);
303303 const ptr = @as([*]align(std.heap.page_size_min) u8, @ptrFromInt(aligned_addr));
304 const prot = elfToProt(ph.p_flags);
304 const prot = elfToProt(ph.flags);
305305 _ = try posix.mmap(
306306 ptr,
307307 extended_memsz,
308308 prot,
309309 .{ .TYPE = .PRIVATE, .FIXED = true },
310310 file.handle,
311 ph.p_offset - extra_bytes,
311 ph.offset - extra_bytes,
312312 );
313313 },
314314 else => {},
......@@ -517,11 +517,11 @@ pub const ElfDynLib = struct {
517517 return null;
518518 }
519519
520 fn elfToProt(elf_prot: u64) posix.PROT {
520 fn elfToProt(elf_prot: elf.PF) posix.PROT {
521521 return .{
522 .READ = (elf_prot & elf.PF_R) != 0,
523 .WRITE = (elf_prot & elf.PF_W) != 0,
524 .EXEC = (elf_prot & elf.PF_X) != 0,
522 .READ = elf_prot.R,
523 .WRITE = elf_prot.W,
524 .EXEC = elf_prot.X,
525525 };
526526 }
527527};
lib/std/elf.zig+15-84
......@@ -290,47 +290,6 @@ pub const VER_FLG_BASE = 1;
290290/// Weak version identifier
291291pub const VER_FLG_WEAK = 2;
292292
293/// Deprecated, use `@intFromEnum(std.elf.PT.NULL)`
294pub const PT_NULL = @backingInt(std.elf.PT.NULL);
295/// Deprecated, use `@intFromEnum(std.elf.PT.LOAD)`
296pub const PT_LOAD = @backingInt(std.elf.PT.LOAD);
297/// Deprecated, use `@intFromEnum(std.elf.PT.DYNAMIC)`
298pub const PT_DYNAMIC = @backingInt(std.elf.PT.DYNAMIC);
299/// Deprecated, use `@intFromEnum(std.elf.PT.INTERP)`
300pub const PT_INTERP = @backingInt(std.elf.PT.INTERP);
301/// Deprecated, use `@intFromEnum(std.elf.PT.NOTE)`
302pub const PT_NOTE = @backingInt(std.elf.PT.NOTE);
303/// Deprecated, use `@intFromEnum(std.elf.PT.SHLIB)`
304pub const PT_SHLIB = @backingInt(std.elf.PT.SHLIB);
305/// Deprecated, use `@intFromEnum(std.elf.PT.PHDR)`
306pub const PT_PHDR = @backingInt(std.elf.PT.PHDR);
307/// Deprecated, use `@intFromEnum(std.elf.PT.TLS)`
308pub const PT_TLS = @backingInt(std.elf.PT.TLS);
309/// Deprecated, use `std.elf.PT.NUM`.
310pub const PT_NUM = PT.NUM;
311/// Deprecated, use `@intFromEnum(std.elf.PT.LOOS)`
312pub const PT_LOOS = @backingInt(std.elf.PT.LOOS);
313/// Deprecated, use `@intFromEnum(std.elf.PT.GNU_EH_FRAME)`
314pub const PT_GNU_EH_FRAME = @backingInt(std.elf.PT.GNU_EH_FRAME);
315/// Deprecated, use `@intFromEnum(std.elf.PT.GNU_STACK)`
316pub const PT_GNU_STACK = @backingInt(std.elf.PT.GNU_STACK);
317/// Deprecated, use `@intFromEnum(std.elf.PT.GNU_RELRO)`
318pub const PT_GNU_RELRO = @backingInt(std.elf.PT.GNU_RELRO);
319/// Deprecated, use `@intFromEnum(std.elf.PT.LOSUNW)`
320pub const PT_LOSUNW = @backingInt(std.elf.PT.LOSUNW);
321/// Deprecated, use `@intFromEnum(std.elf.PT.SUNWBSS)`
322pub const PT_SUNWBSS = @backingInt(std.elf.PT.SUNWBSS);
323/// Deprecated, use `@intFromEnum(std.elf.PT.SUNWSTACK)`
324pub const PT_SUNWSTACK = @backingInt(std.elf.PT.SUNWSTACK);
325/// Deprecated, use `@intFromEnum(std.elf.PT.HISUNW)`
326pub const PT_HISUNW = @backingInt(std.elf.PT.HISUNW);
327/// Deprecated, use `@intFromEnum(std.elf.PT.HIOS)`
328pub const PT_HIOS = @backingInt(std.elf.PT.HIOS);
329/// Deprecated, use `@intFromEnum(std.elf.PT.LOPROC)`
330pub const PT_LOPROC = @backingInt(std.elf.PT.LOPROC);
331/// Deprecated, use `@intFromEnum(std.elf.PT.HIPROC)`
332pub const PT_HIPROC = @backingInt(std.elf.PT.HIPROC);
333
334293pub const PN_XNUM = 0xffff;
335294
336295/// Deprecated, use `@intFromEnum(std.elf.SHT.NULL)`
......@@ -848,11 +807,11 @@ pub const ProgramHeaderIterator = struct {
848807 file_reader: *Io.File.Reader,
849808 index: usize = 0,
850809
851 pub fn next(it: *ProgramHeaderIterator) !?Elf64_Phdr {
810 pub fn next(it: *ProgramHeaderIterator) !?Elf64.Phdr {
852811 if (it.index >= it.phnum) return null;
853812 defer it.index += 1;
854813
855 const size: u64 = if (it.is_64) @sizeOf(Elf64_Phdr) else @sizeOf(Elf32_Phdr);
814 const size: u64 = if (it.is_64) @sizeOf(Elf64.Phdr) else @sizeOf(Elf32.Phdr);
856815 const offset = it.phoff + size * it.index;
857816 try it.file_reader.seekTo(offset);
858817
......@@ -869,11 +828,11 @@ pub const ProgramHeaderBufferIterator = struct {
869828 buf: []const u8,
870829 index: usize = 0,
871830
872 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64_Phdr {
831 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64.Phdr {
873832 if (it.index >= it.phnum) return null;
874833 defer it.index += 1;
875834
876 const size: usize = if (it.is_64) @sizeOf(Elf64_Phdr) else @sizeOf(Elf32_Phdr);
835 const size: usize = if (it.is_64) @sizeOf(Elf64.Phdr) else @sizeOf(Elf32.Phdr);
877836 const offset = @as(usize, @intCast(it.phoff)) + size * it.index;
878837 var reader = Io.Reader.fixed(it.buf[offset..]);
879838
......@@ -881,22 +840,22 @@ pub const ProgramHeaderBufferIterator = struct {
881840 }
882841};
883842
884pub fn takeProgramHeader(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64_Phdr {
843pub fn takeProgramHeader(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64.Phdr {
885844 if (is_64) {
886 const phdr = try reader.takeStruct(Elf64_Phdr, endian);
845 const phdr = try reader.takeStruct(Elf64.Phdr, endian);
887846 return phdr;
888847 }
889848
890 const phdr = try reader.takeStruct(Elf32_Phdr, endian);
849 const phdr = try reader.takeStruct(Elf32.Phdr, endian);
891850 return .{
892 .p_type = phdr.p_type,
893 .p_offset = phdr.p_offset,
894 .p_vaddr = phdr.p_vaddr,
895 .p_paddr = phdr.p_paddr,
896 .p_filesz = phdr.p_filesz,
897 .p_memsz = phdr.p_memsz,
898 .p_flags = phdr.p_flags,
899 .p_align = phdr.p_align,
851 .type = phdr.type,
852 .offset = phdr.offset,
853 .vaddr = phdr.vaddr,
854 .paddr = phdr.paddr,
855 .filesz = phdr.filesz,
856 .memsz = phdr.memsz,
857 .flags = phdr.flags,
858 .@"align" = phdr.@"align",
900859 };
901860}
902861
......@@ -1276,28 +1235,6 @@ pub const Elf64_Ehdr = extern struct {
12761235 e_shnum: Half,
12771236 e_shstrndx: Half,
12781237};
1279/// Deprecated, use `std.elf.Elf32.Phdr`
1280pub const Elf32_Phdr = extern struct {
1281 p_type: Word,
1282 p_offset: Elf32_Off,
1283 p_vaddr: Elf32_Addr,
1284 p_paddr: Elf32_Addr,
1285 p_filesz: Word,
1286 p_memsz: Word,
1287 p_flags: Word,
1288 p_align: Word,
1289};
1290/// Deprecated, use `std.elf.Elf64.Phdr`
1291pub const Elf64_Phdr = extern struct {
1292 p_type: Word,
1293 p_flags: Word,
1294 p_offset: Elf64_Off,
1295 p_vaddr: Elf64_Addr,
1296 p_paddr: Elf64_Addr,
1297 p_filesz: Elf64_Xword,
1298 p_memsz: Elf64_Xword,
1299 p_align: Elf64_Xword,
1300};
13011238/// Deprecated, use `std.elf.Elf32.Shdr`
13021239pub const Elf32_Shdr = extern struct {
13031240 sh_name: Word,
......@@ -1568,12 +1505,6 @@ pub const Ehdr = switch (@sizeOf(usize)) {
15681505 8 => Elf64_Ehdr,
15691506 else => @compileError("expected pointer size of 32 or 64"),
15701507};
1571/// Deprecated, use `std.elf.ElfN.Phdr`
1572pub const Phdr = switch (@sizeOf(usize)) {
1573 4 => Elf32_Phdr,
1574 8 => Elf64_Phdr,
1575 else => @compileError("expected pointer size of 32 or 64"),
1576};
15771508pub const Dyn = switch (@sizeOf(usize)) {
15781509 4 => Elf32_Dyn,
15791510 8 => Elf64_Dyn,
lib/std/os/emscripten.zig+1-1
......@@ -730,7 +730,7 @@ pub const clock_t = i32;
730730pub const dl_phdr_info = extern struct {
731731 addr: usize,
732732 name: ?[*:0]const u8,
733 phdr: [*]std.elf.Phdr,
733 phdr: [*]std.elf.ElfN.Phdr,
734734 phnum: u16,
735735};
736736
lib/std/os/linux/tls.zig+12-12
......@@ -22,7 +22,7 @@ const page_size_min = std.heap.page_size_min;
2222/// Represents an ELF TLS variant.
2323///
2424/// In all variants, the TP and the TLS blocks must be aligned to the `p_align` value in the
25/// `PT_TLS` ELF program header. Everything else has natural alignment.
25/// `PT.TLS` ELF program header. Everything else has natural alignment.
2626///
2727/// The location of the DTV does not actually matter. For simplicity, we put it in the TLS area, but
2828/// there is no actual ABI requirement that it reside there.
......@@ -480,17 +480,17 @@ pub fn getThreadPointer() usize {
480480 };
481481}
482482
483fn computeAreaDesc(phdrs: []elf.Phdr) void {
483fn computeAreaDesc(phdrs: []elf.ElfN.Phdr) void {
484484 @setRuntimeSafety(false);
485485 @disableInstrumentation();
486486
487 var tls_phdr: ?*elf.Phdr = null;
487 var tls_phdr: ?*elf.ElfN.Phdr = null;
488488 var img_base: usize = 0;
489489
490490 for (phdrs) |*phdr| {
491 switch (phdr.p_type) {
492 elf.PT_PHDR => img_base = @intFromPtr(phdrs.ptr) - phdr.p_vaddr,
493 elf.PT_TLS => tls_phdr = phdr,
491 switch (phdr.type) {
492 .PHDR => img_base = @intFromPtr(phdrs.ptr) - phdr.vaddr,
493 .TLS => tls_phdr = phdr,
494494 else => {},
495495 }
496496 }
......@@ -500,12 +500,12 @@ fn computeAreaDesc(phdrs: []elf.Phdr) void {
500500 var block_size: usize = undefined;
501501
502502 if (tls_phdr) |phdr| {
503 align_factor = phdr.p_align;
503 align_factor = phdr.@"align";
504504
505 // The effective size in memory is represented by `p_memsz`; the length of the data stored
506 // in the `PT_TLS` segment is `p_filesz` and may be less than the former.
507 block_init = @as([*]u8, @ptrFromInt(img_base + phdr.p_vaddr))[0..phdr.p_filesz];
508 block_size = phdr.p_memsz;
505 // The effective size in memory is represented by `memsz`; the length of the data stored
506 // in the `PT.TLS` segment is `filesz` and may be less than the former.
507 block_init = @as([*]u8, @ptrFromInt(img_base + phdr.vaddr))[0..phdr.filesz];
508 block_size = phdr.memsz;
509509 } else {
510510 align_factor = @alignOf(usize);
511511
......@@ -651,7 +651,7 @@ var main_thread_area_buffer: [0x1000]u8 align(page_size_min) = undefined;
651651
652652/// Computes the layout of the static TLS area, allocates the area, initializes all of its fields,
653653/// and assigns the architecture-specific value to the TP register.
654pub fn initStatic(phdrs: []elf.Phdr) void {
654pub fn initStatic(phdrs: []elf.ElfN.Phdr) void {
655655 @setRuntimeSafety(false);
656656 @disableInstrumentation();
657657
lib/std/os/linux/vdso.zig+5-5
......@@ -19,14 +19,14 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
1919 i += 1;
2020 ph_addr += eh.e_phentsize;
2121 }) {
22 const this_ph = @as(*elf.Phdr, @ptrFromInt(ph_addr));
23 switch (this_ph.p_type) {
22 const this_ph = @as(*elf.ElfN.Phdr, @ptrFromInt(ph_addr));
23 switch (this_ph.type) {
2424 // On WSL1 as well as older kernels, the VDSO ELF image is pre-linked in the upper half
25 // of the memory space (e.g. p_vaddr = 0xffffffffff700000 on WSL1).
25 // of the memory space (e.g. vaddr = 0xffffffffff700000 on WSL1).
2626 // Wrapping operations are used on this line as well as subsequent calculations relative to base
2727 // (lines 47, 78) to ensure no overflow check is tripped.
28 elf.PT_LOAD => base = vdso_addr +% this_ph.p_offset -% this_ph.p_vaddr,
29 elf.PT_DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(vdso_addr + this_ph.p_offset)),
28 .LOAD => base = vdso_addr +% this_ph.offset -% this_ph.vaddr,
29 .DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(vdso_addr + this_ph.offset)),
3030 else => {},
3131 }
3232 }
lib/std/pie.zig+3-3
......@@ -293,7 +293,7 @@ inline fn getDynamicSymbol() [*]const elf.Dyn {
293293 };
294294}
295295
296pub fn relocate(phdrs: []const elf.Phdr) void {
296pub fn relocate(phdrs: []const elf.ElfN.Phdr) void {
297297 @setRuntimeSafety(false);
298298 @disableInstrumentation();
299299
......@@ -303,8 +303,8 @@ pub fn relocate(phdrs: []const elf.Phdr) void {
303303 // the theoretical load addresses for the `_DYNAMIC` symbol.
304304 const base_addr = base: {
305305 for (phdrs) |*phdr| {
306 if (phdr.p_type != elf.PT_DYNAMIC) continue;
307 break :base @intFromPtr(dynv) - phdr.p_vaddr;
306 if (phdr.type != .DYNAMIC) continue;
307 break :base @intFromPtr(dynv) - phdr.vaddr;
308308 }
309309 // This is not supposed to happen for well-formed binaries.
310310 @trap();
lib/std/posix/test.zig+1-1
......@@ -75,7 +75,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
7575 // Count how many libraries are loaded
7676 counter.* += @as(usize, 1);
7777
78 // The image should contain at least a PT_LOAD segment
78 // The image should contain at least a PT.LOAD segment
7979 if (info.phnum < 1) return error.MissingPtLoadSegment;
8080
8181 // Quick & dirty validation of the phdr pointers, make sure we're not
lib/std/start.zig+9-9
......@@ -589,7 +589,7 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
589589 else => continue,
590590 }
591591 }
592 break :init @as([*]elf.Phdr, @ptrFromInt(at_phdr))[0..at_phnum];
592 break :init @as([*]elf.ElfN.Phdr, @ptrFromInt(at_phdr))[0..at_phnum];
593593 };
594594
595595 // Apply the initial relocations as early as possible in the startup process. We cannot
......@@ -621,7 +621,7 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
621621 std.os.linux.tls.initStatic(phdrs);
622622 }
623623
624 // The way Linux executables represent stack size is via the PT_GNU_STACK
624 // The way Linux executables represent stack size is via the PT.GNU_STACK
625625 // program header. However the kernel does not recognize it; it always gives 8 MiB.
626626 // Here we look for the stack size in our program headers and use setrlimit
627627 // to ask for more stack space.
......@@ -645,19 +645,19 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
645645 std.process.exit(callMainWithArgs(argc, argv, envp));
646646}
647647
648fn expandStackSize(phdrs: []elf.Phdr) void {
648fn expandStackSize(phdrs: []elf.ElfN.Phdr) void {
649649 @disableInstrumentation();
650650 for (phdrs) |*phdr| {
651 switch (phdr.p_type) {
652 elf.PT_GNU_STACK => {
653 if (phdr.p_memsz == 0) break;
654 assert(phdr.p_memsz % std.heap.page_size_min == 0);
651 switch (phdr.type) {
652 .GNU_STACK => {
653 if (phdr.memsz == 0) break;
654 assert(phdr.memsz % std.heap.page_size_min == 0);
655655
656656 // Silently fail if we are unable to get limits.
657657 const limits = std.posix.getrlimit(.STACK) catch break;
658658
659659 // Clamp to limits.max .
660 const wanted_stack_size = @min(phdr.p_memsz, limits.max);
660 const wanted_stack_size = @min(phdr.memsz, limits.max);
661661
662662 if (wanted_stack_size > limits.cur) {
663663 std.posix.setrlimit(.STACK, .{
......@@ -702,7 +702,7 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal
702702 .linux => {
703703 const at_phdr = std.c.getauxval(elf.AT_PHDR);
704704 const at_phnum = std.c.getauxval(elf.AT_PHNUM);
705 const phdrs = (@as([*]elf.Phdr, @ptrFromInt(at_phdr)))[0..at_phnum];
705 const phdrs = (@as([*]elf.ElfN.Phdr, @ptrFromInt(at_phdr)))[0..at_phnum];
706706 expandStackSize(phdrs);
707707 },
708708 .windows => {
lib/std/zig/system.zig+8-8
......@@ -597,23 +597,23 @@ fn abiAndDynamicLinkerFromFile(
597597 .ofmt = query.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch),
598598 .dynamic_linker = query.dynamic_linker orelse .none,
599599 };
600 var rpath_offset: ?u64 = null; // Found inside PT_DYNAMIC
600 var rpath_offset: ?u64 = null; // Found inside PT.DYNAMIC
601601 const look_for_ld = query.dynamic_linker == null;
602602
603603 var got_dyn_section: bool = false;
604604 {
605605 var it = header.iterateProgramHeaders(file_reader);
606 while (try it.next()) |phdr| switch (phdr.p_type) {
607 elf.PT_INTERP => {
606 while (try it.next()) |phdr| switch (phdr.type) {
607 .INTERP => {
608608 got_dyn_section = true;
609609
610610 if (look_for_ld) {
611 const p_filesz = phdr.p_filesz;
611 const p_filesz = phdr.filesz;
612612 if (p_filesz > result.dynamic_linker.buffer.len) return error.NameTooLong;
613613 const filesz: usize = @intCast(p_filesz);
614 try file_reader.seekTo(phdr.p_offset);
614 try file_reader.seekTo(phdr.offset);
615615 try file_reader.interface.readSliceAll(result.dynamic_linker.buffer[0..filesz]);
616 // PT_INTERP includes a null byte in filesz.
616 // PT.INTERP includes a null byte in filesz.
617617 const len = filesz - 1;
618618 // dynamic_linker.max_byte is "max", not "len".
619619 // We know it will fit in u8 because we check against dynamic_linker.buffer.len above.
......@@ -631,11 +631,11 @@ fn abiAndDynamicLinkerFromFile(
631631 }
632632 },
633633 // We only need this for detecting glibc version.
634 elf.PT_DYNAMIC => {
634 .DYNAMIC => {
635635 got_dyn_section = true;
636636
637637 if (builtin.target.os.tag == .linux and result.isGnuLibC() and query.glibc_version == null) {
638 var dyn_it = header.iterateDynamicSection(file_reader, phdr.p_offset, phdr.p_filesz);
638 var dyn_it = header.iterateDynamicSection(file_reader, phdr.offset, phdr.filesz);
639639 while (try dyn_it.next()) |dyn| {
640640 if (dyn.d_tag == elf.DT_RUNPATH) {
641641 rpath_offset = dyn.d_val;
src/link/Elf.zig+125-125
......@@ -127,7 +127,7 @@ const SectionIndexes = struct {
127127 symtab: ?u32 = null,
128128};
129129
130const ProgramHeaderList = std.ArrayList(elf.Elf64_Phdr);
130const ProgramHeaderList = std.ArrayList(elf.Elf64.Phdr);
131131
132132const OptionalProgramHeaderIndex = enum(u16) {
133133 none = std.math.maxInt(u16),
......@@ -159,21 +159,21 @@ const ProgramHeaderIndex = enum(u16) {
159159};
160160
161161const ProgramHeaderIndexes = struct {
162 /// PT_PHDR
162 /// PT.PHDR
163163 table: OptionalProgramHeaderIndex = .none,
164 /// PT_LOAD for PHDR table
164 /// PT.LOAD for PHDR table
165165 /// We add this special load segment to ensure the EHDR and PHDR table are always
166166 /// loaded into memory.
167167 table_load: OptionalProgramHeaderIndex = .none,
168 /// PT_INTERP
168 /// PT.INTERP
169169 interp: OptionalProgramHeaderIndex = .none,
170 /// PT_DYNAMIC
170 /// PT.DYNAMIC
171171 dynamic: OptionalProgramHeaderIndex = .none,
172 /// PT_GNU_EH_FRAME
172 /// PT.GNU_EH_FRAME
173173 gnu_eh_frame: OptionalProgramHeaderIndex = .none,
174 /// PT_GNU_STACK
174 /// PT.GNU_STACK
175175 gnu_stack: OptionalProgramHeaderIndex = .none,
176 /// PT_TLS
176 /// PT.TLS
177177 /// TODO I think ELF permits multiple TLS segments but for now, assume one per file.
178178 tls: OptionalProgramHeaderIndex = .none,
179179};
......@@ -334,23 +334,23 @@ pub fn createEmpty(
334334 if (!is_obj_or_ar) {
335335 try self.dynstrtab.append(gpa, 0);
336336
337 // Initialize PT_PHDR program header
337 // Initialize PT.PHDR program header
338338 const p_align: u16 = switch (self.ptr_width) {
339 .p32 => @alignOf(elf.Elf32_Phdr),
340 .p64 => @alignOf(elf.Elf64_Phdr),
339 .p32 => @alignOf(elf.Elf32.Phdr),
340 .p64 => @alignOf(elf.Elf64.Phdr),
341341 };
342342 const ehsize: u64 = switch (self.ptr_width) {
343343 .p32 => @sizeOf(elf.Elf32_Ehdr),
344344 .p64 => @sizeOf(elf.Elf64_Ehdr),
345345 };
346346 const phsize: u64 = switch (self.ptr_width) {
347 .p32 => @sizeOf(elf.Elf32_Phdr),
348 .p64 => @sizeOf(elf.Elf64_Phdr),
347 .p32 => @sizeOf(elf.Elf32.Phdr),
348 .p64 => @sizeOf(elf.Elf64.Phdr),
349349 };
350350 const max_nphdrs = comptime getMaxNumberOfPhdrs();
351351 const reserved: u64 = mem.alignForward(u64, padToIdeal(max_nphdrs * phsize), self.page_size);
352352 self.phdr_indexes.table = (try self.addPhdr(.{
353 .type = elf.PT_PHDR,
353 .type = @backingInt(elf.PT.PHDR),
354354 .flags = elf.PF_R,
355355 .@"align" = p_align,
356356 .addr = self.image_base + ehsize,
......@@ -359,7 +359,7 @@ pub fn createEmpty(
359359 .memsz = reserved,
360360 })).toOptional();
361361 self.phdr_indexes.table_load = (try self.addPhdr(.{
362 .type = elf.PT_LOAD,
362 .type = @backingInt(elf.PT.LOAD),
363363 .flags = elf.PF_R,
364364 .@"align" = self.page_size,
365365 .addr = self.image_base,
......@@ -514,11 +514,11 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) !?u64 {
514514 }
515515
516516 for (self.phdrs.items) |phdr| {
517 if (phdr.p_type != elf.PT_LOAD) continue;
518 const increased_size = padToIdeal(phdr.p_filesz);
519 const test_end = phdr.p_offset +| increased_size;
517 if (phdr.type != .LOAD) continue;
518 const increased_size = padToIdeal(phdr.filesz);
519 const test_end = phdr.offset +| increased_size;
520520 if (start < test_end) {
521 if (end > phdr.p_offset) return test_end;
521 if (end > phdr.offset) return test_end;
522522 if (test_end < std.math.maxInt(u64)) at_end = false;
523523 }
524524 }
......@@ -538,8 +538,8 @@ pub fn allocatedSize(self: *Elf, start: u64) u64 {
538538 if (section.sh_offset < min_pos) min_pos = section.sh_offset;
539539 }
540540 for (self.phdrs.items) |phdr| {
541 if (phdr.p_offset <= start) continue;
542 if (phdr.p_offset < min_pos) min_pos = phdr.p_offset;
541 if (phdr.offset <= start) continue;
542 if (phdr.offset < min_pos) min_pos = phdr.offset;
543543 }
544544 return min_pos - start;
545545}
......@@ -1471,34 +1471,34 @@ fn writePhdrTable(self: *Elf) !void {
14711471 const phdr_table = &self.phdrs.items[self.phdr_indexes.table.int().?];
14721472
14731473 log.debug("writing program headers from 0x{x} to 0x{x}", .{
1474 phdr_table.p_offset,
1475 phdr_table.p_offset + phdr_table.p_filesz,
1474 phdr_table.offset,
1475 phdr_table.offset + phdr_table.filesz,
14761476 });
14771477
14781478 switch (self.ptr_width) {
14791479 .p32 => {
1480 const buf = try gpa.alloc(elf.Elf32_Phdr, self.phdrs.items.len);
1480 const buf = try gpa.alloc(elf.Elf32.Phdr, self.phdrs.items.len);
14811481 defer gpa.free(buf);
14821482
14831483 for (buf, 0..) |*phdr, i| {
14841484 phdr.* = phdrTo32(self.phdrs.items[i]);
14851485 if (foreign_endian) {
1486 mem.byteSwapAllFields(elf.Elf32_Phdr, phdr);
1486 mem.byteSwapAllFields(elf.Elf32.Phdr, phdr);
14871487 }
14881488 }
1489 try self.pwriteAll(@ptrCast(buf), phdr_table.p_offset);
1489 try self.pwriteAll(@ptrCast(buf), phdr_table.offset);
14901490 },
14911491 .p64 => {
1492 const buf = try gpa.alloc(elf.Elf64_Phdr, self.phdrs.items.len);
1492 const buf = try gpa.alloc(elf.Elf64.Phdr, self.phdrs.items.len);
14931493 defer gpa.free(buf);
14941494
14951495 for (buf, 0..) |*phdr, i| {
14961496 phdr.* = self.phdrs.items[i];
14971497 if (foreign_endian) {
1498 mem.byteSwapAllFields(elf.Elf64_Phdr, phdr);
1498 mem.byteSwapAllFields(elf.Elf64.Phdr, phdr);
14991499 }
15001500 }
1501 try self.pwriteAll(@ptrCast(buf), phdr_table.p_offset);
1501 try self.pwriteAll(@ptrCast(buf), phdr_table.offset);
15021502 },
15031503 }
15041504}
......@@ -1581,7 +1581,7 @@ pub fn writeElfHeader(self: *Elf) !void {
15811581 const entry_sym = obj.entrySymbol(self) orelse break :blk 0;
15821582 break :blk @intCast(entry_sym.address(.{}, self));
15831583 } else 0;
1584 const phdr_table_offset = if (self.phdr_indexes.table.int()) |phndx| self.phdrs.items[phndx].p_offset else 0;
1584 const phdr_table_offset = if (self.phdr_indexes.table.int()) |phndx| self.phdrs.items[phndx].offset else 0;
15851585 switch (self.ptr_width) {
15861586 .p32 => {
15871587 mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(e_entry), endian);
......@@ -1622,8 +1622,8 @@ pub fn writeElfHeader(self: *Elf) !void {
16221622 index += 2;
16231623
16241624 const e_phentsize: u16 = switch (self.ptr_width) {
1625 .p32 => @sizeOf(elf.Elf32_Phdr),
1626 .p64 => @sizeOf(elf.Elf64_Phdr),
1625 .p32 => @sizeOf(elf.Elf32.Phdr),
1626 .p64 => @sizeOf(elf.Elf64.Phdr),
16271627 };
16281628 mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian);
16291629 index += 2;
......@@ -2091,26 +2091,26 @@ fn initSpecialPhdrs(self: *Elf) !void {
20912091
20922092 if (self.section_indexes.interp != null and self.phdr_indexes.interp == .none) {
20932093 self.phdr_indexes.interp = (try self.addPhdr(.{
2094 .type = elf.PT_INTERP,
2094 .type = @backingInt(elf.PT.INTERP),
20952095 .flags = elf.PF_R,
20962096 .@"align" = 1,
20972097 })).toOptional();
20982098 }
20992099 if (self.section_indexes.dynamic != null and self.phdr_indexes.dynamic == .none) {
21002100 self.phdr_indexes.dynamic = (try self.addPhdr(.{
2101 .type = elf.PT_DYNAMIC,
2101 .type = @backingInt(elf.PT.DYNAMIC),
21022102 .flags = elf.PF_R | elf.PF_W,
21032103 })).toOptional();
21042104 }
21052105 if (self.section_indexes.eh_frame_hdr != null and self.phdr_indexes.gnu_eh_frame == .none) {
21062106 self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{
2107 .type = elf.PT_GNU_EH_FRAME,
2107 .type = @backingInt(elf.PT.GNU_EH_FRAME),
21082108 .flags = elf.PF_R,
21092109 })).toOptional();
21102110 }
21112111 if (self.phdr_indexes.gnu_stack == .none) {
21122112 self.phdr_indexes.gnu_stack = (try self.addPhdr(.{
2113 .type = elf.PT_GNU_STACK,
2113 .type = @backingInt(elf.PT.GNU_STACK),
21142114 .flags = elf.PF_W | elf.PF_R,
21152115 .memsz = self.base.stack_size,
21162116 .@"align" = 1,
......@@ -2122,7 +2122,7 @@ fn initSpecialPhdrs(self: *Elf) !void {
21222122 } else false;
21232123 if (has_tls and self.phdr_indexes.tls == .none) {
21242124 self.phdr_indexes.tls = (try self.addPhdr(.{
2125 .type = elf.PT_TLS,
2125 .type = @backingInt(elf.PT.TLS),
21262126 .flags = elf.PF_R,
21272127 .@"align" = 1,
21282128 })).toOptional();
......@@ -2260,15 +2260,15 @@ fn setHashSections(self: *Elf) !void {
22602260 }
22612261}
22622262
2263fn phdrRank(phdr: elf.Elf64_Phdr) u8 {
2264 return switch (phdr.p_type) {
2265 elf.PT_NULL => 0,
2266 elf.PT_PHDR => 1,
2267 elf.PT_INTERP => 2,
2268 elf.PT_LOAD => 3,
2269 elf.PT_DYNAMIC, elf.PT_TLS => 4,
2270 elf.PT_GNU_EH_FRAME => 5,
2271 elf.PT_GNU_STACK => 6,
2263fn phdrRank(phdr: elf.Elf64.Phdr) u8 {
2264 return switch (phdr.type) {
2265 .NULL => 0,
2266 .PHDR => 1,
2267 .INTERP => 2,
2268 .LOAD => 3,
2269 .DYNAMIC, .TLS => 4,
2270 .GNU_EH_FRAME => 5,
2271 .GNU_STACK => 6,
22722272 else => 7,
22732273 };
22742274}
......@@ -2282,12 +2282,12 @@ fn sortPhdrs(
22822282 const Entry = struct {
22832283 phndx: u16,
22842284
2285 pub fn lessThan(program_headers: []const elf.Elf64_Phdr, lhs: @This(), rhs: @This()) bool {
2285 pub fn lessThan(program_headers: []const elf.Elf64.Phdr, lhs: @This(), rhs: @This()) bool {
22862286 const lhs_phdr = program_headers[lhs.phndx];
22872287 const rhs_phdr = program_headers[rhs.phndx];
22882288 const lhs_rank = phdrRank(lhs_phdr);
22892289 const rhs_rank = phdrRank(rhs_phdr);
2290 if (lhs_rank == rhs_rank) return lhs_phdr.p_vaddr < rhs_phdr.p_vaddr;
2290 if (lhs_rank == rhs_rank) return lhs_phdr.vaddr < rhs_phdr.vaddr;
22912291 return lhs_rank < rhs_rank;
22922292 }
22932293 };
......@@ -2299,7 +2299,7 @@ fn sortPhdrs(
22992299 }
23002300
23012301 // The `@as` here works around a bug in the C backend.
2302 mem.sort(Entry, entries, @as([]const elf.Elf64_Phdr, phdrs.items), Entry.lessThan);
2302 mem.sort(Entry, entries, @as([]const elf.Elf64.Phdr, phdrs.items), Entry.lessThan);
23032303
23042304 const backlinks = try gpa.alloc(u16, entries.len);
23052305 defer gpa.free(backlinks);
......@@ -2655,8 +2655,8 @@ fn addLoadPhdrs(self: *Elf) error{OutOfMemory}!void {
26552655 if (shdr.sh_type == elf.SHT_NULL) continue;
26562656 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
26572657 const flags = shdrToPhdrFlags(shdr.sh_flags);
2658 if (self.getPhdr(.{ .flags = flags, .type = elf.PT_LOAD }) == .none) {
2659 _ = try self.addPhdr(.{ .flags = flags, .type = elf.PT_LOAD });
2658 if (self.getPhdr(.{ .flags = flags, .type = @backingInt(elf.PT.LOAD) }) == .none) {
2659 _ = try self.addPhdr(.{ .flags = flags, .type = @backingInt(elf.PT.LOAD) });
26602660 }
26612661 }
26622662}
......@@ -2672,11 +2672,11 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {
26722672 .p64 => @sizeOf(elf.Elf64_Ehdr),
26732673 };
26742674 const phsize: u64 = switch (self.ptr_width) {
2675 .p32 => @sizeOf(elf.Elf32_Phdr),
2676 .p64 => @sizeOf(elf.Elf64_Phdr),
2675 .p32 => @sizeOf(elf.Elf32.Phdr),
2676 .p64 => @sizeOf(elf.Elf64.Phdr),
26772677 };
26782678 const needed_size = self.phdrs.items.len * phsize;
2679 const available_space = self.allocatedSize(phdr_table.p_offset);
2679 const available_space = self.allocatedSize(phdr_table.offset);
26802680
26812681 if (needed_size > available_space) {
26822682 // In this case, we have two options:
......@@ -2689,10 +2689,10 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {
26892689 err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space });
26902690 }
26912691
2692 phdr_table_load.p_filesz = needed_size + ehsize;
2693 phdr_table_load.p_memsz = needed_size + ehsize;
2694 phdr_table.p_filesz = needed_size;
2695 phdr_table.p_memsz = needed_size;
2692 phdr_table_load.filesz = needed_size + ehsize;
2693 phdr_table_load.memsz = needed_size + ehsize;
2694 phdr_table.filesz = needed_size;
2695 phdr_table.memsz = needed_size;
26962696}
26972697
26982698/// Allocates alloc sections and creates load segments for sections
......@@ -2758,7 +2758,7 @@ pub fn allocateAllocSections(self: *Elf) !void {
27582758 // of any section that is contained in a cover and use it to align
27592759 // the start address of the segement (and first section).
27602760 const phdr_table = &self.phdrs.items[self.phdr_indexes.table_load.int().?];
2761 var addr = phdr_table.p_vaddr + phdr_table.p_memsz;
2761 var addr = phdr_table.vaddr + phdr_table.memsz;
27622762
27632763 for (covers) |cover| {
27642764 if (cover.items.len == 0) continue;
......@@ -2817,14 +2817,14 @@ pub fn allocateAllocSections(self: *Elf) !void {
28172817 }
28182818
28192819 const first = slice.items(.shdr)[cover.items[0]];
2820 const phndx = self.getPhdr(.{ .type = elf.PT_LOAD, .flags = shdrToPhdrFlags(first.sh_flags) }).unwrap().?;
2820 const phndx = self.getPhdr(.{ .type = @backingInt(elf.PT.LOAD), .flags = shdrToPhdrFlags(first.sh_flags) }).unwrap().?;
28212821 const phdr = &self.phdrs.items[phndx.int()];
2822 const allocated_size = self.allocatedSize(phdr.p_offset);
2822 const allocated_size = self.allocatedSize(phdr.offset);
28232823 if (filesz > allocated_size) {
2824 const old_offset = phdr.p_offset;
2825 phdr.p_offset = 0;
2824 const old_offset = phdr.offset;
2825 phdr.offset = 0;
28262826 var new_offset = try self.findFreeSpace(filesz, @"align");
2827 phdr.p_offset = new_offset;
2827 phdr.offset = new_offset;
28282828
28292829 log.debug("moving phdr({d}) from 0x{x} to 0x{x}", .{ phndx, old_offset, new_offset });
28302830
......@@ -2854,11 +2854,11 @@ pub fn allocateAllocSections(self: *Elf) !void {
28542854 }
28552855 }
28562856
2857 phdr.p_vaddr = first.sh_addr;
2858 phdr.p_paddr = first.sh_addr;
2859 phdr.p_memsz = memsz;
2860 phdr.p_filesz = filesz;
2861 phdr.p_align = @"align";
2857 phdr.vaddr = first.sh_addr;
2858 phdr.paddr = first.sh_addr;
2859 phdr.memsz = memsz;
2860 phdr.filesz = filesz;
2861 phdr.@"align" = @"align";
28622862
28632863 addr = mem.alignForward(u64, addr, self.page_size);
28642864 }
......@@ -2902,12 +2902,12 @@ fn allocateSpecialPhdrs(self: *Elf) void {
29022902 if (pair[0].int()) |index| {
29032903 const shdr = slice.items(.shdr)[pair[1].?];
29042904 const phdr = &self.phdrs.items[index];
2905 phdr.p_align = shdr.sh_addralign;
2906 phdr.p_offset = shdr.sh_offset;
2907 phdr.p_vaddr = shdr.sh_addr;
2908 phdr.p_paddr = shdr.sh_addr;
2909 phdr.p_filesz = shdr.sh_size;
2910 phdr.p_memsz = shdr.sh_size;
2905 phdr.@"align" = shdr.sh_addralign;
2906 phdr.offset = shdr.sh_offset;
2907 phdr.vaddr = shdr.sh_addr;
2908 phdr.paddr = shdr.sh_addr;
2909 phdr.filesz = shdr.sh_size;
2910 phdr.memsz = shdr.sh_size;
29112911 }
29122912 }
29132913
......@@ -2924,25 +2924,25 @@ fn allocateSpecialPhdrs(self: *Elf) void {
29242924 shndx += 1;
29252925 continue;
29262926 }
2927 phdr.p_offset = shdr.sh_offset;
2928 phdr.p_vaddr = shdr.sh_addr;
2929 phdr.p_paddr = shdr.sh_addr;
2930 phdr.p_align = shdr.sh_addralign;
2927 phdr.offset = shdr.sh_offset;
2928 phdr.vaddr = shdr.sh_addr;
2929 phdr.paddr = shdr.sh_addr;
2930 phdr.@"align" = shdr.sh_addralign;
29312931 shndx += 1;
2932 phdr.p_align = @max(phdr.p_align, shdr.sh_addralign);
2932 phdr.@"align" = @max(phdr.@"align", shdr.sh_addralign);
29332933 if (shdr.sh_type != elf.SHT_NOBITS) {
2934 phdr.p_filesz = shdr.sh_offset + shdr.sh_size - phdr.p_offset;
2934 phdr.filesz = shdr.sh_offset + shdr.sh_size - phdr.offset;
29352935 }
2936 phdr.p_memsz = shdr.sh_addr + shdr.sh_size - phdr.p_vaddr;
2936 phdr.memsz = shdr.sh_addr + shdr.sh_size - phdr.vaddr;
29372937
29382938 while (shndx < shdrs.len) : (shndx += 1) {
29392939 const next = shdrs[shndx];
29402940 if (next.sh_flags & elf.SHF_TLS == 0) break;
2941 phdr.p_align = @max(phdr.p_align, next.sh_addralign);
2941 phdr.@"align" = @max(phdr.@"align", next.sh_addralign);
29422942 if (next.sh_type != elf.SHT_NOBITS) {
2943 phdr.p_filesz = next.sh_offset + next.sh_size - phdr.p_offset;
2943 phdr.filesz = next.sh_offset + next.sh_size - phdr.offset;
29442944 }
2945 phdr.p_memsz = next.sh_addr + next.sh_size - phdr.p_vaddr;
2945 phdr.memsz = next.sh_addr + next.sh_size - phdr.vaddr;
29462946 }
29472947 }
29482948 }
......@@ -3347,16 +3347,16 @@ pub fn archPtrWidthBytes(self: Elf) u8 {
33473347 return @intCast(@divExact(self.getTarget().ptrBitWidth(), 8));
33483348}
33493349
3350fn phdrTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr {
3350fn phdrTo32(phdr: elf.Elf64.Phdr) elf.Elf32.Phdr {
33513351 return .{
3352 .p_type = phdr.p_type,
3353 .p_flags = phdr.p_flags,
3354 .p_offset = @as(u32, @intCast(phdr.p_offset)),
3355 .p_vaddr = @as(u32, @intCast(phdr.p_vaddr)),
3356 .p_paddr = @as(u32, @intCast(phdr.p_paddr)),
3357 .p_filesz = @as(u32, @intCast(phdr.p_filesz)),
3358 .p_memsz = @as(u32, @intCast(phdr.p_memsz)),
3359 .p_align = @as(u32, @intCast(phdr.p_align)),
3352 .type = phdr.type,
3353 .flags = phdr.flags,
3354 .offset = @intCast(phdr.offset),
3355 .vaddr = @intCast(phdr.vaddr),
3356 .paddr = @intCast(phdr.paddr),
3357 .filesz = @intCast(phdr.filesz),
3358 .memsz = @intCast(phdr.memsz),
3359 .@"align" = @intCast(phdr.@"align"),
33603360 };
33613361}
33623362
......@@ -3397,7 +3397,7 @@ fn getPhdr(self: *Elf, opts: struct {
33973397 if (self.phdr_indexes.table_load.int()) |index| {
33983398 if (phndx == index) continue;
33993399 }
3400 if (phdr.p_type == opts.type and phdr.p_flags == opts.flags)
3400 if (@backingInt(phdr.type) == opts.type and @backingInt(phdr.flags) == opts.flags)
34013401 return @fromBackingInt(@intCast(phndx));
34023402 }
34033403 return .none;
......@@ -3415,14 +3415,14 @@ fn addPhdr(self: *Elf, opts: struct {
34153415 const gpa = self.base.comp.gpa;
34163416 const index: ProgramHeaderIndex = @fromBackingInt(@intCast(self.phdrs.items.len));
34173417 try self.phdrs.append(gpa, .{
3418 .p_type = opts.type,
3419 .p_flags = opts.flags,
3420 .p_offset = opts.offset,
3421 .p_vaddr = opts.addr,
3422 .p_paddr = opts.addr,
3423 .p_filesz = opts.filesz,
3424 .p_memsz = opts.memsz,
3425 .p_align = opts.@"align",
3418 .type = @fromBackingInt(opts.type),
3419 .flags = @fromBackingInt(opts.flags),
3420 .offset = opts.offset,
3421 .vaddr = opts.addr,
3422 .paddr = opts.addr,
3423 .filesz = opts.filesz,
3424 .memsz = opts.memsz,
3425 .@"align" = opts.@"align",
34263426 });
34273427 return index;
34283428}
......@@ -3673,9 +3673,9 @@ pub fn tpAddress(self: *Elf) i64 {
36733673 const index = self.phdr_indexes.tls.int() orelse return 0;
36743674 const phdr = self.phdrs.items[index];
36753675 const addr = switch (self.getTarget().cpu.arch) {
3676 .x86_64 => mem.alignForward(u64, phdr.p_vaddr + phdr.p_memsz, phdr.p_align),
3677 .aarch64, .aarch64_be => mem.alignBackward(u64, phdr.p_vaddr - 16, phdr.p_align),
3678 .riscv64, .riscv64be => phdr.p_vaddr,
3676 .x86_64 => mem.alignForward(u64, phdr.vaddr + phdr.memsz, phdr.@"align"),
3677 .aarch64, .aarch64_be => mem.alignBackward(u64, phdr.vaddr - 16, phdr.@"align"),
3678 .riscv64, .riscv64be => phdr.vaddr,
36793679 else => |arch| std.debug.panic("TODO implement getTpAddress for {s}", .{@tagName(arch)}),
36803680 };
36813681 return @intCast(addr);
......@@ -3684,13 +3684,13 @@ pub fn tpAddress(self: *Elf) i64 {
36843684pub fn dtpAddress(self: *Elf) i64 {
36853685 const index = self.phdr_indexes.tls.int() orelse return 0;
36863686 const phdr = self.phdrs.items[index];
3687 return @intCast(phdr.p_vaddr);
3687 return @intCast(phdr.vaddr);
36883688}
36893689
36903690pub fn tlsAddress(self: *Elf) i64 {
36913691 const index = self.phdr_indexes.tls.int() orelse return 0;
36923692 const phdr = self.phdrs.items[index];
3693 return @intCast(phdr.p_vaddr);
3693 return @intCast(phdr.vaddr);
36943694}
36953695
36963696pub fn getShString(self: Elf, off: u32) [:0]const u8 {
......@@ -3886,10 +3886,10 @@ fn formatShdrFlags(sh_flags: u64, writer: *std.Io.Writer) std.Io.Writer.Error!vo
38863886
38873887const FormatPhdr = struct {
38883888 elf_file: *Elf,
3889 phdr: elf.Elf64_Phdr,
3889 phdr: elf.Elf64.Phdr,
38903890};
38913891
3892fn fmtPhdr(self: *Elf, phdr: elf.Elf64_Phdr) std.fmt.Alt(FormatPhdr, formatPhdr) {
3892fn fmtPhdr(self: *Elf, phdr: elf.Elf64.Phdr) std.fmt.Alt(FormatPhdr, formatPhdr) {
38933893 return .{ .data = .{
38943894 .phdr = phdr,
38953895 .elf_file = self,
......@@ -3898,28 +3898,28 @@ fn fmtPhdr(self: *Elf, phdr: elf.Elf64_Phdr) std.fmt.Alt(FormatPhdr, formatPhdr)
38983898
38993899fn formatPhdr(ctx: FormatPhdr, writer: *std.Io.Writer) std.Io.Writer.Error!void {
39003900 const phdr = ctx.phdr;
3901 const write = phdr.p_flags & elf.PF_W != 0;
3902 const read = phdr.p_flags & elf.PF_R != 0;
3903 const exec = phdr.p_flags & elf.PF_X != 0;
3901 const write = phdr.flags.W;
3902 const read = phdr.flags.R;
3903 const exec = phdr.flags.X;
39043904 var flags: [3]u8 = @splat('_');
39053905 if (exec) flags[0] = 'X';
39063906 if (write) flags[1] = 'W';
39073907 if (read) flags[2] = 'R';
3908 const p_type = switch (phdr.p_type) {
3909 elf.PT_LOAD => "LOAD",
3910 elf.PT_TLS => "TLS",
3911 elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME",
3912 elf.PT_GNU_STACK => "GNU_STACK",
3913 elf.PT_DYNAMIC => "DYNAMIC",
3914 elf.PT_INTERP => "INTERP",
3915 elf.PT_NULL => "NULL",
3916 elf.PT_PHDR => "PHDR",
3917 elf.PT_NOTE => "NOTE",
3908 const p_type = switch (phdr.type) {
3909 .LOAD => "LOAD",
3910 .TLS => "TLS",
3911 .GNU_EH_FRAME => "GNU_EH_FRAME",
3912 .GNU_STACK => "GNU_STACK",
3913 .DYNAMIC => "DYNAMIC",
3914 .INTERP => "INTERP",
3915 .NULL => "NULL",
3916 .PHDR => "PHDR",
3917 .NOTE => "NOTE",
39183918 else => "UNKNOWN",
39193919 };
39203920 try writer.print("{s} : {s} : @{x} ({x}) : align({x}) : filesz({x}) : memsz({x})", .{
3921 p_type, flags, phdr.p_offset, phdr.p_vaddr,
3922 phdr.p_align, phdr.p_filesz, phdr.p_memsz,
3921 p_type, flags, phdr.offset, phdr.vaddr,
3922 phdr.@"align", phdr.filesz, phdr.memsz,
39233923 });
39243924}
39253925
src/link/Lld.zig+2-2
......@@ -1023,8 +1023,8 @@ fn elfLink(lld: *Lld, arena: Allocator) !void {
10231023 }
10241024
10251025 if (is_exe_or_dyn_lib and target.os.tag == .netbsd) {
1026 // Add options to produce shared objects with only 2 PT_LOAD segments.
1027 // NetBSD expects 2 PT_LOAD segments in a shared object, otherwise
1026 // Add options to produce shared objects with only 2 PT.LOAD segments.
1027 // NetBSD expects 2 PT.LOAD segments in a shared object, otherwise
10281028 // ld.elf_so fails loading dynamic libraries with "not found" error.
10291029 // See https://github.com/ziglang/zig/issues/9109 .
10301030 try argv.append("--no-rosegment");