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 {...@@ -435,13 +435,13 @@ const BinaryElfOutput = struct {
435435
436 var program_headers = elf_hdr.iterateProgramHeaders(in);436 var program_headers = elf_hdr.iterateProgramHeaders(in);
437 while (try program_headers.next()) |phdr| {437 while (try program_headers.next()) |phdr| {
438 if (phdr.p_type == elf.PT_LOAD) {438 if (phdr.type == .LOAD) {
439 const newSegment = try allocator.create(BinaryElfSegment);439 const newSegment = try allocator.create(BinaryElfSegment);
440440
441 newSegment.physicalAddress = phdr.p_paddr;441 newSegment.physicalAddress = phdr.paddr;
442 newSegment.virtualAddress = phdr.p_vaddr;442 newSegment.virtualAddress = phdr.vaddr;
443 newSegment.fileSize = @intCast(phdr.p_filesz);443 newSegment.fileSize = @intCast(phdr.filesz);
444 newSegment.elfOffset = phdr.p_offset;444 newSegment.elfOffset = phdr.offset;
445 newSegment.binaryOffset = 0;445 newSegment.binaryOffset = 0;
446 newSegment.firstSection = null;446 newSegment.firstSection = null;
447447
...@@ -495,8 +495,8 @@ const BinaryElfOutput = struct {...@@ -495,8 +495,8 @@ const BinaryElfOutput = struct {
495 return self;495 return self;
496 }496 }
497497
498 fn sectionWithinSegment(section: *BinaryElfSection, segment: elf.Elf64_Phdr) bool {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);499 return segment.offset <= section.elfOffset and (segment.offset + segment.filesz) >= (section.elfOffset + section.fileSize);
500 }500 }
501501
502 fn sectionValidForOutput(shdr: anytype) bool {502 fn sectionValidForOutput(shdr: anytype) bool {
lib/std/Build/Step/Compile.zig+1-1
...@@ -101,7 +101,7 @@ each_lib_rpath: ?bool = null,...@@ -101,7 +101,7 @@ each_lib_rpath: ?bool = null,
101/// This option overrides the CLI argument passed to `zig build`.101/// This option overrides the CLI argument passed to `zig build`.
102build_id: ?std.zig.BuildId = null,102build_id: ?std.zig.BuildId = null,
103103
104/// Create a .eh_frame_hdr section and a PT_GNU_EH_FRAME segment in the ELF104/// Create a .eh_frame_hdr section and a PT.GNU_EH_FRAME segment in the ELF
105/// file.105/// file.
106link_eh_frame_hdr: bool = false,106link_eh_frame_hdr: bool = false,
107link_emit_relocs: bool = false,107link_emit_relocs: bool = false,
lib/std/debug/SelfInfo/Elf.zig+1-1
...@@ -518,7 +518,7 @@ const DlIterContext = struct {...@@ -518,7 +518,7 @@ const DlIterContext = struct {
518 for (info.phdr[0..info.phnum]) |phdr| {518 for (info.phdr[0..info.phnum]) |phdr| {
519 if (phdr.type != .LOAD) continue;519 if (phdr.type != .LOAD) continue;
520 try context.si.ranges.append(gpa, .{520 try context.si.ranges.append(gpa, .{
521 // Overflowing addition handles VSDOs having p_vaddr = 0xffffffffff700000521 // Overflowing addition handles VSDOs having vaddr = 0xffffffffff700000
522 .start = info.addr +% phdr.vaddr,522 .start = info.addr +% phdr.vaddr,
523 .len = phdr.memsz,523 .len = phdr.memsz,
524 .module_index = module_index,524 .module_index = module_index,
lib/std/dynamic_library.zig+17-17
...@@ -103,7 +103,7 @@ pub fn get_DYNAMIC() ?[*]const elf.Dyn {...@@ -103,7 +103,7 @@ pub fn get_DYNAMIC() ?[*]const elf.Dyn {
103103
104pub fn linkmap_iterator() error{InvalidExe}!LinkMap.Iterator {104pub fn linkmap_iterator() error{InvalidExe}!LinkMap.Iterator {
105 const _DYNAMIC = get_DYNAMIC() orelse {105 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.
107 return .{ .current = null };107 return .{ .current = null };
108 };108 };
109109
...@@ -261,10 +261,10 @@ pub const ElfDynLib = struct {...@@ -261,10 +261,10 @@ pub const ElfDynLib = struct {
261 i += 1;261 i += 1;
262 ph_addr += eh.e_phentsize;262 ph_addr += eh.e_phentsize;
263 }) {263 }) {
264 const ph = @as(*elf.Phdr, @ptrFromInt(ph_addr));264 const ph = @as(*elf.ElfN.Phdr, @ptrFromInt(ph_addr));
265 switch (ph.p_type) {265 switch (ph.type) {
266 elf.PT_LOAD => virt_addr_end = @max(virt_addr_end, ph.p_vaddr + ph.p_memsz),266 .LOAD => virt_addr_end = @max(virt_addr_end, ph.vaddr + ph.memsz),
267 elf.PT_DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(elf_addr + ph.p_offset)),267 .DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(elf_addr + ph.offset)),
268 else => {},268 else => {},
269 }269 }
270 }270 }
...@@ -292,23 +292,23 @@ pub const ElfDynLib = struct {...@@ -292,23 +292,23 @@ pub const ElfDynLib = struct {
292 i += 1;292 i += 1;
293 ph_addr += eh.e_phentsize;293 ph_addr += eh.e_phentsize;
294 }) {294 }) {
295 const ph = @as(*elf.Phdr, @ptrFromInt(ph_addr));295 const ph = @as(*elf.ElfN.Phdr, @ptrFromInt(ph_addr));
296 switch (ph.p_type) {296 switch (ph.type) {
297 elf.PT_LOAD => {297 .LOAD => {
298 // The VirtAddr may not be page-aligned; in such case there will be298 // The VirtAddr may not be page-aligned; in such case there will be
299 // extra nonsense mapped before/after the VirtAddr,MemSiz299 // extra nonsense mapped before/after the VirtAddr,MemSiz
300 const aligned_addr = (base + ph.p_vaddr) & ~(@as(usize, page_size) - 1);300 const aligned_addr = (base + ph.vaddr) & ~(@as(usize, page_size) - 1);
301 const extra_bytes = (base + ph.p_vaddr) - aligned_addr;301 const extra_bytes = (base + ph.vaddr) - aligned_addr;
302 const extended_memsz = mem.alignForward(usize, ph.p_memsz + extra_bytes, page_size);302 const extended_memsz = mem.alignForward(usize, ph.memsz + extra_bytes, page_size);
303 const ptr = @as([*]align(std.heap.page_size_min) u8, @ptrFromInt(aligned_addr));303 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);
305 _ = try posix.mmap(305 _ = try posix.mmap(
306 ptr,306 ptr,
307 extended_memsz,307 extended_memsz,
308 prot,308 prot,
309 .{ .TYPE = .PRIVATE, .FIXED = true },309 .{ .TYPE = .PRIVATE, .FIXED = true },
310 file.handle,310 file.handle,
311 ph.p_offset - extra_bytes,311 ph.offset - extra_bytes,
312 );312 );
313 },313 },
314 else => {},314 else => {},
...@@ -517,11 +517,11 @@ pub const ElfDynLib = struct {...@@ -517,11 +517,11 @@ pub const ElfDynLib = struct {
517 return null;517 return null;
518 }518 }
519519
520 fn elfToProt(elf_prot: u64) posix.PROT {520 fn elfToProt(elf_prot: elf.PF) posix.PROT {
521 return .{521 return .{
522 .READ = (elf_prot & elf.PF_R) != 0,522 .READ = elf_prot.R,
523 .WRITE = (elf_prot & elf.PF_W) != 0,523 .WRITE = elf_prot.W,
524 .EXEC = (elf_prot & elf.PF_X) != 0,524 .EXEC = elf_prot.X,
525 };525 };
526 }526 }
527};527};
lib/std/elf.zig+15-84
...@@ -290,47 +290,6 @@ pub const VER_FLG_BASE = 1;...@@ -290,47 +290,6 @@ pub const VER_FLG_BASE = 1;
290/// Weak version identifier290/// Weak version identifier
291pub const VER_FLG_WEAK = 2;291pub 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
334pub const PN_XNUM = 0xffff;293pub const PN_XNUM = 0xffff;
335294
336/// Deprecated, use `@intFromEnum(std.elf.SHT.NULL)`295/// Deprecated, use `@intFromEnum(std.elf.SHT.NULL)`
...@@ -848,11 +807,11 @@ pub const ProgramHeaderIterator = struct {...@@ -848,11 +807,11 @@ pub const ProgramHeaderIterator = struct {
848 file_reader: *Io.File.Reader,807 file_reader: *Io.File.Reader,
849 index: usize = 0,808 index: usize = 0,
850809
851 pub fn next(it: *ProgramHeaderIterator) !?Elf64_Phdr {810 pub fn next(it: *ProgramHeaderIterator) !?Elf64.Phdr {
852 if (it.index >= it.phnum) return null;811 if (it.index >= it.phnum) return null;
853 defer it.index += 1;812 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);
856 const offset = it.phoff + size * it.index;815 const offset = it.phoff + size * it.index;
857 try it.file_reader.seekTo(offset);816 try it.file_reader.seekTo(offset);
858817
...@@ -869,11 +828,11 @@ pub const ProgramHeaderBufferIterator = struct {...@@ -869,11 +828,11 @@ pub const ProgramHeaderBufferIterator = struct {
869 buf: []const u8,828 buf: []const u8,
870 index: usize = 0,829 index: usize = 0,
871830
872 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64_Phdr {831 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64.Phdr {
873 if (it.index >= it.phnum) return null;832 if (it.index >= it.phnum) return null;
874 defer it.index += 1;833 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);
877 const offset = @as(usize, @intCast(it.phoff)) + size * it.index;836 const offset = @as(usize, @intCast(it.phoff)) + size * it.index;
878 var reader = Io.Reader.fixed(it.buf[offset..]);837 var reader = Io.Reader.fixed(it.buf[offset..]);
879838
...@@ -881,22 +840,22 @@ pub const ProgramHeaderBufferIterator = struct {...@@ -881,22 +840,22 @@ pub const ProgramHeaderBufferIterator = struct {
881 }840 }
882};841};
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 {
885 if (is_64) {844 if (is_64) {
886 const phdr = try reader.takeStruct(Elf64_Phdr, endian);845 const phdr = try reader.takeStruct(Elf64.Phdr, endian);
887 return phdr;846 return phdr;
888 }847 }
889848
890 const phdr = try reader.takeStruct(Elf32_Phdr, endian);849 const phdr = try reader.takeStruct(Elf32.Phdr, endian);
891 return .{850 return .{
892 .p_type = phdr.p_type,851 .type = phdr.type,
893 .p_offset = phdr.p_offset,852 .offset = phdr.offset,
894 .p_vaddr = phdr.p_vaddr,853 .vaddr = phdr.vaddr,
895 .p_paddr = phdr.p_paddr,854 .paddr = phdr.paddr,
896 .p_filesz = phdr.p_filesz,855 .filesz = phdr.filesz,
897 .p_memsz = phdr.p_memsz,856 .memsz = phdr.memsz,
898 .p_flags = phdr.p_flags,857 .flags = phdr.flags,
899 .p_align = phdr.p_align,858 .@"align" = phdr.@"align",
900 };859 };
901}860}
902861
...@@ -1276,28 +1235,6 @@ pub const Elf64_Ehdr = extern struct {...@@ -1276,28 +1235,6 @@ pub const Elf64_Ehdr = extern struct {
1276 e_shnum: Half,1235 e_shnum: Half,
1277 e_shstrndx: Half,1236 e_shstrndx: Half,
1278};1237};
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};
1301/// Deprecated, use `std.elf.Elf32.Shdr`1238/// Deprecated, use `std.elf.Elf32.Shdr`
1302pub const Elf32_Shdr = extern struct {1239pub const Elf32_Shdr = extern struct {
1303 sh_name: Word,1240 sh_name: Word,
...@@ -1568,12 +1505,6 @@ pub const Ehdr = switch (@sizeOf(usize)) {...@@ -1568,12 +1505,6 @@ pub const Ehdr = switch (@sizeOf(usize)) {
1568 8 => Elf64_Ehdr,1505 8 => Elf64_Ehdr,
1569 else => @compileError("expected pointer size of 32 or 64"),1506 else => @compileError("expected pointer size of 32 or 64"),
1570};1507};
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};
1577pub const Dyn = switch (@sizeOf(usize)) {1508pub const Dyn = switch (@sizeOf(usize)) {
1578 4 => Elf32_Dyn,1509 4 => Elf32_Dyn,
1579 8 => Elf64_Dyn,1510 8 => Elf64_Dyn,
lib/std/os/emscripten.zig+1-1
...@@ -730,7 +730,7 @@ pub const clock_t = i32;...@@ -730,7 +730,7 @@ pub const clock_t = i32;
730pub const dl_phdr_info = extern struct {730pub const dl_phdr_info = extern struct {
731 addr: usize,731 addr: usize,
732 name: ?[*:0]const u8,732 name: ?[*:0]const u8,
733 phdr: [*]std.elf.Phdr,733 phdr: [*]std.elf.ElfN.Phdr,
734 phnum: u16,734 phnum: u16,
735};735};
736736
lib/std/os/linux/tls.zig+12-12
...@@ -22,7 +22,7 @@ const page_size_min = std.heap.page_size_min;...@@ -22,7 +22,7 @@ const page_size_min = std.heap.page_size_min;
22/// Represents an ELF TLS variant.22/// Represents an ELF TLS variant.
23///23///
24/// In all variants, the TP and the TLS blocks must be aligned to the `p_align` value in the24/// 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.
26///26///
27/// The location of the DTV does not actually matter. For simplicity, we put it in the TLS area, but27/// The location of the DTV does not actually matter. For simplicity, we put it in the TLS area, but
28/// there is no actual ABI requirement that it reside there.28/// there is no actual ABI requirement that it reside there.
...@@ -480,17 +480,17 @@ pub fn getThreadPointer() usize {...@@ -480,17 +480,17 @@ pub fn getThreadPointer() usize {
480 };480 };
481}481}
482482
483fn computeAreaDesc(phdrs: []elf.Phdr) void {483fn computeAreaDesc(phdrs: []elf.ElfN.Phdr) void {
484 @setRuntimeSafety(false);484 @setRuntimeSafety(false);
485 @disableInstrumentation();485 @disableInstrumentation();
486486
487 var tls_phdr: ?*elf.Phdr = null;487 var tls_phdr: ?*elf.ElfN.Phdr = null;
488 var img_base: usize = 0;488 var img_base: usize = 0;
489489
490 for (phdrs) |*phdr| {490 for (phdrs) |*phdr| {
491 switch (phdr.p_type) {491 switch (phdr.type) {
492 elf.PT_PHDR => img_base = @intFromPtr(phdrs.ptr) - phdr.p_vaddr,492 .PHDR => img_base = @intFromPtr(phdrs.ptr) - phdr.vaddr,
493 elf.PT_TLS => tls_phdr = phdr,493 .TLS => tls_phdr = phdr,
494 else => {},494 else => {},
495 }495 }
496 }496 }
...@@ -500,12 +500,12 @@ fn computeAreaDesc(phdrs: []elf.Phdr) void {...@@ -500,12 +500,12 @@ fn computeAreaDesc(phdrs: []elf.Phdr) void {
500 var block_size: usize = undefined;500 var block_size: usize = undefined;
501501
502 if (tls_phdr) |phdr| {502 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 stored505 // The effective size in memory is represented by `memsz`; the length of the data stored
506 // in the `PT_TLS` segment is `p_filesz` and may be less than the former.506 // in the `PT.TLS` segment is `filesz` and may be less than the former.
507 block_init = @as([*]u8, @ptrFromInt(img_base + phdr.p_vaddr))[0..phdr.p_filesz];507 block_init = @as([*]u8, @ptrFromInt(img_base + phdr.vaddr))[0..phdr.filesz];
508 block_size = phdr.p_memsz;508 block_size = phdr.memsz;
509 } else {509 } else {
510 align_factor = @alignOf(usize);510 align_factor = @alignOf(usize);
511511
...@@ -651,7 +651,7 @@ var main_thread_area_buffer: [0x1000]u8 align(page_size_min) = undefined;...@@ -651,7 +651,7 @@ var main_thread_area_buffer: [0x1000]u8 align(page_size_min) = undefined;
651651
652/// Computes the layout of the static TLS area, allocates the area, initializes all of its fields,652/// Computes the layout of the static TLS area, allocates the area, initializes all of its fields,
653/// and assigns the architecture-specific value to the TP register.653/// 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 {
655 @setRuntimeSafety(false);655 @setRuntimeSafety(false);
656 @disableInstrumentation();656 @disableInstrumentation();
657657
lib/std/os/linux/vdso.zig+5-5
...@@ -19,14 +19,14 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {...@@ -19,14 +19,14 @@ pub fn lookup(vername: []const u8, name: []const u8) usize {
19 i += 1;19 i += 1;
20 ph_addr += eh.e_phentsize;20 ph_addr += eh.e_phentsize;
21 }) {21 }) {
22 const this_ph = @as(*elf.Phdr, @ptrFromInt(ph_addr));22 const this_ph = @as(*elf.ElfN.Phdr, @ptrFromInt(ph_addr));
23 switch (this_ph.p_type) {23 switch (this_ph.type) {
24 // On WSL1 as well as older kernels, the VDSO ELF image is pre-linked in the upper half24 // 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).
26 // Wrapping operations are used on this line as well as subsequent calculations relative to base26 // Wrapping operations are used on this line as well as subsequent calculations relative to base
27 // (lines 47, 78) to ensure no overflow check is tripped.27 // (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,28 .LOAD => base = vdso_addr +% this_ph.offset -% this_ph.vaddr,
29 elf.PT_DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(vdso_addr + this_ph.p_offset)),29 .DYNAMIC => maybe_dynv = @as([*]usize, @ptrFromInt(vdso_addr + this_ph.offset)),
30 else => {},30 else => {},
31 }31 }
32 }32 }
lib/std/pie.zig+3-3
...@@ -293,7 +293,7 @@ inline fn getDynamicSymbol() [*]const elf.Dyn {...@@ -293,7 +293,7 @@ inline fn getDynamicSymbol() [*]const elf.Dyn {
293 };293 };
294}294}
295295
296pub fn relocate(phdrs: []const elf.Phdr) void {296pub fn relocate(phdrs: []const elf.ElfN.Phdr) void {
297 @setRuntimeSafety(false);297 @setRuntimeSafety(false);
298 @disableInstrumentation();298 @disableInstrumentation();
299299
...@@ -303,8 +303,8 @@ pub fn relocate(phdrs: []const elf.Phdr) void {...@@ -303,8 +303,8 @@ pub fn relocate(phdrs: []const elf.Phdr) void {
303 // the theoretical load addresses for the `_DYNAMIC` symbol.303 // the theoretical load addresses for the `_DYNAMIC` symbol.
304 const base_addr = base: {304 const base_addr = base: {
305 for (phdrs) |*phdr| {305 for (phdrs) |*phdr| {
306 if (phdr.p_type != elf.PT_DYNAMIC) continue;306 if (phdr.type != .DYNAMIC) continue;
307 break :base @intFromPtr(dynv) - phdr.p_vaddr;307 break :base @intFromPtr(dynv) - phdr.vaddr;
308 }308 }
309 // This is not supposed to happen for well-formed binaries.309 // This is not supposed to happen for well-formed binaries.
310 @trap();310 @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 {...@@ -75,7 +75,7 @@ fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
75 // Count how many libraries are loaded75 // Count how many libraries are loaded
76 counter.* += @as(usize, 1);76 counter.* += @as(usize, 1);
7777
78 // The image should contain at least a PT_LOAD segment78 // The image should contain at least a PT.LOAD segment
79 if (info.phnum < 1) return error.MissingPtLoadSegment;79 if (info.phnum < 1) return error.MissingPtLoadSegment;
8080
81 // Quick & dirty validation of the phdr pointers, make sure we're not81 // 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 {...@@ -589,7 +589,7 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
589 else => continue,589 else => continue,
590 }590 }
591 }591 }
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];
593 };593 };
594594
595 // Apply the initial relocations as early as possible in the startup process. We cannot595 // 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 {...@@ -621,7 +621,7 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
621 std.os.linux.tls.initStatic(phdrs);621 std.os.linux.tls.initStatic(phdrs);
622 }622 }
623623
624 // The way Linux executables represent stack size is via the PT_GNU_STACK624 // The way Linux executables represent stack size is via the PT.GNU_STACK
625 // program header. However the kernel does not recognize it; it always gives 8 MiB.625 // program header. However the kernel does not recognize it; it always gives 8 MiB.
626 // Here we look for the stack size in our program headers and use setrlimit626 // Here we look for the stack size in our program headers and use setrlimit
627 // to ask for more stack space.627 // to ask for more stack space.
...@@ -645,19 +645,19 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {...@@ -645,19 +645,19 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
645 std.process.exit(callMainWithArgs(argc, argv, envp));645 std.process.exit(callMainWithArgs(argc, argv, envp));
646}646}
647647
648fn expandStackSize(phdrs: []elf.Phdr) void {648fn expandStackSize(phdrs: []elf.ElfN.Phdr) void {
649 @disableInstrumentation();649 @disableInstrumentation();
650 for (phdrs) |*phdr| {650 for (phdrs) |*phdr| {
651 switch (phdr.p_type) {651 switch (phdr.type) {
652 elf.PT_GNU_STACK => {652 .GNU_STACK => {
653 if (phdr.p_memsz == 0) break;653 if (phdr.memsz == 0) break;
654 assert(phdr.p_memsz % std.heap.page_size_min == 0);654 assert(phdr.memsz % std.heap.page_size_min == 0);
655655
656 // Silently fail if we are unable to get limits.656 // Silently fail if we are unable to get limits.
657 const limits = std.posix.getrlimit(.STACK) catch break;657 const limits = std.posix.getrlimit(.STACK) catch break;
658658
659 // Clamp to limits.max .659 // 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
662 if (wanted_stack_size > limits.cur) {662 if (wanted_stack_size > limits.cur) {
663 std.posix.setrlimit(.STACK, .{663 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...@@ -702,7 +702,7 @@ fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) cal
702 .linux => {702 .linux => {
703 const at_phdr = std.c.getauxval(elf.AT_PHDR);703 const at_phdr = std.c.getauxval(elf.AT_PHDR);
704 const at_phnum = std.c.getauxval(elf.AT_PHNUM);704 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];
706 expandStackSize(phdrs);706 expandStackSize(phdrs);
707 },707 },
708 .windows => {708 .windows => {
lib/std/zig/system.zig+8-8
...@@ -597,23 +597,23 @@ fn abiAndDynamicLinkerFromFile(...@@ -597,23 +597,23 @@ fn abiAndDynamicLinkerFromFile(
597 .ofmt = query.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch),597 .ofmt = query.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch),
598 .dynamic_linker = query.dynamic_linker orelse .none,598 .dynamic_linker = query.dynamic_linker orelse .none,
599 };599 };
600 var rpath_offset: ?u64 = null; // Found inside PT_DYNAMIC600 var rpath_offset: ?u64 = null; // Found inside PT.DYNAMIC
601 const look_for_ld = query.dynamic_linker == null;601 const look_for_ld = query.dynamic_linker == null;
602602
603 var got_dyn_section: bool = false;603 var got_dyn_section: bool = false;
604 {604 {
605 var it = header.iterateProgramHeaders(file_reader);605 var it = header.iterateProgramHeaders(file_reader);
606 while (try it.next()) |phdr| switch (phdr.p_type) {606 while (try it.next()) |phdr| switch (phdr.type) {
607 elf.PT_INTERP => {607 .INTERP => {
608 got_dyn_section = true;608 got_dyn_section = true;
609609
610 if (look_for_ld) {610 if (look_for_ld) {
611 const p_filesz = phdr.p_filesz;611 const p_filesz = phdr.filesz;
612 if (p_filesz > result.dynamic_linker.buffer.len) return error.NameTooLong;612 if (p_filesz > result.dynamic_linker.buffer.len) return error.NameTooLong;
613 const filesz: usize = @intCast(p_filesz);613 const filesz: usize = @intCast(p_filesz);
614 try file_reader.seekTo(phdr.p_offset);614 try file_reader.seekTo(phdr.offset);
615 try file_reader.interface.readSliceAll(result.dynamic_linker.buffer[0..filesz]);615 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.
617 const len = filesz - 1;617 const len = filesz - 1;
618 // dynamic_linker.max_byte is "max", not "len".618 // dynamic_linker.max_byte is "max", not "len".
619 // We know it will fit in u8 because we check against dynamic_linker.buffer.len above.619 // We know it will fit in u8 because we check against dynamic_linker.buffer.len above.
...@@ -631,11 +631,11 @@ fn abiAndDynamicLinkerFromFile(...@@ -631,11 +631,11 @@ fn abiAndDynamicLinkerFromFile(
631 }631 }
632 },632 },
633 // We only need this for detecting glibc version.633 // We only need this for detecting glibc version.
634 elf.PT_DYNAMIC => {634 .DYNAMIC => {
635 got_dyn_section = true;635 got_dyn_section = true;
636636
637 if (builtin.target.os.tag == .linux and result.isGnuLibC() and query.glibc_version == null) {637 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);
639 while (try dyn_it.next()) |dyn| {639 while (try dyn_it.next()) |dyn| {
640 if (dyn.d_tag == elf.DT_RUNPATH) {640 if (dyn.d_tag == elf.DT_RUNPATH) {
641 rpath_offset = dyn.d_val;641 rpath_offset = dyn.d_val;
src/link/Elf.zig+125-125
...@@ -127,7 +127,7 @@ const SectionIndexes = struct {...@@ -127,7 +127,7 @@ const SectionIndexes = struct {
127 symtab: ?u32 = null,127 symtab: ?u32 = null,
128};128};
129129
130const ProgramHeaderList = std.ArrayList(elf.Elf64_Phdr);130const ProgramHeaderList = std.ArrayList(elf.Elf64.Phdr);
131131
132const OptionalProgramHeaderIndex = enum(u16) {132const OptionalProgramHeaderIndex = enum(u16) {
133 none = std.math.maxInt(u16),133 none = std.math.maxInt(u16),
...@@ -159,21 +159,21 @@ const ProgramHeaderIndex = enum(u16) {...@@ -159,21 +159,21 @@ const ProgramHeaderIndex = enum(u16) {
159};159};
160160
161const ProgramHeaderIndexes = struct {161const ProgramHeaderIndexes = struct {
162 /// PT_PHDR162 /// PT.PHDR
163 table: OptionalProgramHeaderIndex = .none,163 table: OptionalProgramHeaderIndex = .none,
164 /// PT_LOAD for PHDR table164 /// PT.LOAD for PHDR table
165 /// We add this special load segment to ensure the EHDR and PHDR table are always165 /// We add this special load segment to ensure the EHDR and PHDR table are always
166 /// loaded into memory.166 /// loaded into memory.
167 table_load: OptionalProgramHeaderIndex = .none,167 table_load: OptionalProgramHeaderIndex = .none,
168 /// PT_INTERP168 /// PT.INTERP
169 interp: OptionalProgramHeaderIndex = .none,169 interp: OptionalProgramHeaderIndex = .none,
170 /// PT_DYNAMIC170 /// PT.DYNAMIC
171 dynamic: OptionalProgramHeaderIndex = .none,171 dynamic: OptionalProgramHeaderIndex = .none,
172 /// PT_GNU_EH_FRAME172 /// PT.GNU_EH_FRAME
173 gnu_eh_frame: OptionalProgramHeaderIndex = .none,173 gnu_eh_frame: OptionalProgramHeaderIndex = .none,
174 /// PT_GNU_STACK174 /// PT.GNU_STACK
175 gnu_stack: OptionalProgramHeaderIndex = .none,175 gnu_stack: OptionalProgramHeaderIndex = .none,
176 /// PT_TLS176 /// PT.TLS
177 /// TODO I think ELF permits multiple TLS segments but for now, assume one per file.177 /// TODO I think ELF permits multiple TLS segments but for now, assume one per file.
178 tls: OptionalProgramHeaderIndex = .none,178 tls: OptionalProgramHeaderIndex = .none,
179};179};
...@@ -334,23 +334,23 @@ pub fn createEmpty(...@@ -334,23 +334,23 @@ pub fn createEmpty(
334 if (!is_obj_or_ar) {334 if (!is_obj_or_ar) {
335 try self.dynstrtab.append(gpa, 0);335 try self.dynstrtab.append(gpa, 0);
336336
337 // Initialize PT_PHDR program header337 // Initialize PT.PHDR program header
338 const p_align: u16 = switch (self.ptr_width) {338 const p_align: u16 = switch (self.ptr_width) {
339 .p32 => @alignOf(elf.Elf32_Phdr),339 .p32 => @alignOf(elf.Elf32.Phdr),
340 .p64 => @alignOf(elf.Elf64_Phdr),340 .p64 => @alignOf(elf.Elf64.Phdr),
341 };341 };
342 const ehsize: u64 = switch (self.ptr_width) {342 const ehsize: u64 = switch (self.ptr_width) {
343 .p32 => @sizeOf(elf.Elf32_Ehdr),343 .p32 => @sizeOf(elf.Elf32_Ehdr),
344 .p64 => @sizeOf(elf.Elf64_Ehdr),344 .p64 => @sizeOf(elf.Elf64_Ehdr),
345 };345 };
346 const phsize: u64 = switch (self.ptr_width) {346 const phsize: u64 = switch (self.ptr_width) {
347 .p32 => @sizeOf(elf.Elf32_Phdr),347 .p32 => @sizeOf(elf.Elf32.Phdr),
348 .p64 => @sizeOf(elf.Elf64_Phdr),348 .p64 => @sizeOf(elf.Elf64.Phdr),
349 };349 };
350 const max_nphdrs = comptime getMaxNumberOfPhdrs();350 const max_nphdrs = comptime getMaxNumberOfPhdrs();
351 const reserved: u64 = mem.alignForward(u64, padToIdeal(max_nphdrs * phsize), self.page_size);351 const reserved: u64 = mem.alignForward(u64, padToIdeal(max_nphdrs * phsize), self.page_size);
352 self.phdr_indexes.table = (try self.addPhdr(.{352 self.phdr_indexes.table = (try self.addPhdr(.{
353 .type = elf.PT_PHDR,353 .type = @backingInt(elf.PT.PHDR),
354 .flags = elf.PF_R,354 .flags = elf.PF_R,
355 .@"align" = p_align,355 .@"align" = p_align,
356 .addr = self.image_base + ehsize,356 .addr = self.image_base + ehsize,
...@@ -359,7 +359,7 @@ pub fn createEmpty(...@@ -359,7 +359,7 @@ pub fn createEmpty(
359 .memsz = reserved,359 .memsz = reserved,
360 })).toOptional();360 })).toOptional();
361 self.phdr_indexes.table_load = (try self.addPhdr(.{361 self.phdr_indexes.table_load = (try self.addPhdr(.{
362 .type = elf.PT_LOAD,362 .type = @backingInt(elf.PT.LOAD),
363 .flags = elf.PF_R,363 .flags = elf.PF_R,
364 .@"align" = self.page_size,364 .@"align" = self.page_size,
365 .addr = self.image_base,365 .addr = self.image_base,
...@@ -514,11 +514,11 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) !?u64 {...@@ -514,11 +514,11 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) !?u64 {
514 }514 }
515515
516 for (self.phdrs.items) |phdr| {516 for (self.phdrs.items) |phdr| {
517 if (phdr.p_type != elf.PT_LOAD) continue;517 if (phdr.type != .LOAD) continue;
518 const increased_size = padToIdeal(phdr.p_filesz);518 const increased_size = padToIdeal(phdr.filesz);
519 const test_end = phdr.p_offset +| increased_size;519 const test_end = phdr.offset +| increased_size;
520 if (start < test_end) {520 if (start < test_end) {
521 if (end > phdr.p_offset) return test_end;521 if (end > phdr.offset) return test_end;
522 if (test_end < std.math.maxInt(u64)) at_end = false;522 if (test_end < std.math.maxInt(u64)) at_end = false;
523 }523 }
524 }524 }
...@@ -538,8 +538,8 @@ pub fn allocatedSize(self: *Elf, start: u64) u64 {...@@ -538,8 +538,8 @@ pub fn allocatedSize(self: *Elf, start: u64) u64 {
538 if (section.sh_offset < min_pos) min_pos = section.sh_offset;538 if (section.sh_offset < min_pos) min_pos = section.sh_offset;
539 }539 }
540 for (self.phdrs.items) |phdr| {540 for (self.phdrs.items) |phdr| {
541 if (phdr.p_offset <= start) continue;541 if (phdr.offset <= start) continue;
542 if (phdr.p_offset < min_pos) min_pos = phdr.p_offset;542 if (phdr.offset < min_pos) min_pos = phdr.offset;
543 }543 }
544 return min_pos - start;544 return min_pos - start;
545}545}
...@@ -1471,34 +1471,34 @@ fn writePhdrTable(self: *Elf) !void {...@@ -1471,34 +1471,34 @@ fn writePhdrTable(self: *Elf) !void {
1471 const phdr_table = &self.phdrs.items[self.phdr_indexes.table.int().?];1471 const phdr_table = &self.phdrs.items[self.phdr_indexes.table.int().?];
14721472
1473 log.debug("writing program headers from 0x{x} to 0x{x}", .{1473 log.debug("writing program headers from 0x{x} to 0x{x}", .{
1474 phdr_table.p_offset,1474 phdr_table.offset,
1475 phdr_table.p_offset + phdr_table.p_filesz,1475 phdr_table.offset + phdr_table.filesz,
1476 });1476 });
14771477
1478 switch (self.ptr_width) {1478 switch (self.ptr_width) {
1479 .p32 => {1479 .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);
1481 defer gpa.free(buf);1481 defer gpa.free(buf);
14821482
1483 for (buf, 0..) |*phdr, i| {1483 for (buf, 0..) |*phdr, i| {
1484 phdr.* = phdrTo32(self.phdrs.items[i]);1484 phdr.* = phdrTo32(self.phdrs.items[i]);
1485 if (foreign_endian) {1485 if (foreign_endian) {
1486 mem.byteSwapAllFields(elf.Elf32_Phdr, phdr);1486 mem.byteSwapAllFields(elf.Elf32.Phdr, phdr);
1487 }1487 }
1488 }1488 }
1489 try self.pwriteAll(@ptrCast(buf), phdr_table.p_offset);1489 try self.pwriteAll(@ptrCast(buf), phdr_table.offset);
1490 },1490 },
1491 .p64 => {1491 .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);
1493 defer gpa.free(buf);1493 defer gpa.free(buf);
14941494
1495 for (buf, 0..) |*phdr, i| {1495 for (buf, 0..) |*phdr, i| {
1496 phdr.* = self.phdrs.items[i];1496 phdr.* = self.phdrs.items[i];
1497 if (foreign_endian) {1497 if (foreign_endian) {
1498 mem.byteSwapAllFields(elf.Elf64_Phdr, phdr);1498 mem.byteSwapAllFields(elf.Elf64.Phdr, phdr);
1499 }1499 }
1500 }1500 }
1501 try self.pwriteAll(@ptrCast(buf), phdr_table.p_offset);1501 try self.pwriteAll(@ptrCast(buf), phdr_table.offset);
1502 },1502 },
1503 }1503 }
1504}1504}
...@@ -1581,7 +1581,7 @@ pub fn writeElfHeader(self: *Elf) !void {...@@ -1581,7 +1581,7 @@ pub fn writeElfHeader(self: *Elf) !void {
1581 const entry_sym = obj.entrySymbol(self) orelse break :blk 0;1581 const entry_sym = obj.entrySymbol(self) orelse break :blk 0;
1582 break :blk @intCast(entry_sym.address(.{}, self));1582 break :blk @intCast(entry_sym.address(.{}, self));
1583 } else 0;1583 } 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;
1585 switch (self.ptr_width) {1585 switch (self.ptr_width) {
1586 .p32 => {1586 .p32 => {
1587 mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(e_entry), endian);1587 mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(e_entry), endian);
...@@ -1622,8 +1622,8 @@ pub fn writeElfHeader(self: *Elf) !void {...@@ -1622,8 +1622,8 @@ pub fn writeElfHeader(self: *Elf) !void {
1622 index += 2;1622 index += 2;
16231623
1624 const e_phentsize: u16 = switch (self.ptr_width) {1624 const e_phentsize: u16 = switch (self.ptr_width) {
1625 .p32 => @sizeOf(elf.Elf32_Phdr),1625 .p32 => @sizeOf(elf.Elf32.Phdr),
1626 .p64 => @sizeOf(elf.Elf64_Phdr),1626 .p64 => @sizeOf(elf.Elf64.Phdr),
1627 };1627 };
1628 mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian);1628 mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian);
1629 index += 2;1629 index += 2;
...@@ -2091,26 +2091,26 @@ fn initSpecialPhdrs(self: *Elf) !void {...@@ -2091,26 +2091,26 @@ fn initSpecialPhdrs(self: *Elf) !void {
20912091
2092 if (self.section_indexes.interp != null and self.phdr_indexes.interp == .none) {2092 if (self.section_indexes.interp != null and self.phdr_indexes.interp == .none) {
2093 self.phdr_indexes.interp = (try self.addPhdr(.{2093 self.phdr_indexes.interp = (try self.addPhdr(.{
2094 .type = elf.PT_INTERP,2094 .type = @backingInt(elf.PT.INTERP),
2095 .flags = elf.PF_R,2095 .flags = elf.PF_R,
2096 .@"align" = 1,2096 .@"align" = 1,
2097 })).toOptional();2097 })).toOptional();
2098 }2098 }
2099 if (self.section_indexes.dynamic != null and self.phdr_indexes.dynamic == .none) {2099 if (self.section_indexes.dynamic != null and self.phdr_indexes.dynamic == .none) {
2100 self.phdr_indexes.dynamic = (try self.addPhdr(.{2100 self.phdr_indexes.dynamic = (try self.addPhdr(.{
2101 .type = elf.PT_DYNAMIC,2101 .type = @backingInt(elf.PT.DYNAMIC),
2102 .flags = elf.PF_R | elf.PF_W,2102 .flags = elf.PF_R | elf.PF_W,
2103 })).toOptional();2103 })).toOptional();
2104 }2104 }
2105 if (self.section_indexes.eh_frame_hdr != null and self.phdr_indexes.gnu_eh_frame == .none) {2105 if (self.section_indexes.eh_frame_hdr != null and self.phdr_indexes.gnu_eh_frame == .none) {
2106 self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{2106 self.phdr_indexes.gnu_eh_frame = (try self.addPhdr(.{
2107 .type = elf.PT_GNU_EH_FRAME,2107 .type = @backingInt(elf.PT.GNU_EH_FRAME),
2108 .flags = elf.PF_R,2108 .flags = elf.PF_R,
2109 })).toOptional();2109 })).toOptional();
2110 }2110 }
2111 if (self.phdr_indexes.gnu_stack == .none) {2111 if (self.phdr_indexes.gnu_stack == .none) {
2112 self.phdr_indexes.gnu_stack = (try self.addPhdr(.{2112 self.phdr_indexes.gnu_stack = (try self.addPhdr(.{
2113 .type = elf.PT_GNU_STACK,2113 .type = @backingInt(elf.PT.GNU_STACK),
2114 .flags = elf.PF_W | elf.PF_R,2114 .flags = elf.PF_W | elf.PF_R,
2115 .memsz = self.base.stack_size,2115 .memsz = self.base.stack_size,
2116 .@"align" = 1,2116 .@"align" = 1,
...@@ -2122,7 +2122,7 @@ fn initSpecialPhdrs(self: *Elf) !void {...@@ -2122,7 +2122,7 @@ fn initSpecialPhdrs(self: *Elf) !void {
2122 } else false;2122 } else false;
2123 if (has_tls and self.phdr_indexes.tls == .none) {2123 if (has_tls and self.phdr_indexes.tls == .none) {
2124 self.phdr_indexes.tls = (try self.addPhdr(.{2124 self.phdr_indexes.tls = (try self.addPhdr(.{
2125 .type = elf.PT_TLS,2125 .type = @backingInt(elf.PT.TLS),
2126 .flags = elf.PF_R,2126 .flags = elf.PF_R,
2127 .@"align" = 1,2127 .@"align" = 1,
2128 })).toOptional();2128 })).toOptional();
...@@ -2260,15 +2260,15 @@ fn setHashSections(self: *Elf) !void {...@@ -2260,15 +2260,15 @@ fn setHashSections(self: *Elf) !void {
2260 }2260 }
2261}2261}
22622262
2263fn phdrRank(phdr: elf.Elf64_Phdr) u8 {2263fn phdrRank(phdr: elf.Elf64.Phdr) u8 {
2264 return switch (phdr.p_type) {2264 return switch (phdr.type) {
2265 elf.PT_NULL => 0,2265 .NULL => 0,
2266 elf.PT_PHDR => 1,2266 .PHDR => 1,
2267 elf.PT_INTERP => 2,2267 .INTERP => 2,
2268 elf.PT_LOAD => 3,2268 .LOAD => 3,
2269 elf.PT_DYNAMIC, elf.PT_TLS => 4,2269 .DYNAMIC, .TLS => 4,
2270 elf.PT_GNU_EH_FRAME => 5,2270 .GNU_EH_FRAME => 5,
2271 elf.PT_GNU_STACK => 6,2271 .GNU_STACK => 6,
2272 else => 7,2272 else => 7,
2273 };2273 };
2274}2274}
...@@ -2282,12 +2282,12 @@ fn sortPhdrs(...@@ -2282,12 +2282,12 @@ fn sortPhdrs(
2282 const Entry = struct {2282 const Entry = struct {
2283 phndx: u16,2283 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 {
2286 const lhs_phdr = program_headers[lhs.phndx];2286 const lhs_phdr = program_headers[lhs.phndx];
2287 const rhs_phdr = program_headers[rhs.phndx];2287 const rhs_phdr = program_headers[rhs.phndx];
2288 const lhs_rank = phdrRank(lhs_phdr);2288 const lhs_rank = phdrRank(lhs_phdr);
2289 const rhs_rank = phdrRank(rhs_phdr);2289 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;
2291 return lhs_rank < rhs_rank;2291 return lhs_rank < rhs_rank;
2292 }2292 }
2293 };2293 };
...@@ -2299,7 +2299,7 @@ fn sortPhdrs(...@@ -2299,7 +2299,7 @@ fn sortPhdrs(
2299 }2299 }
23002300
2301 // The `@as` here works around a bug in the C backend.2301 // 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
2304 const backlinks = try gpa.alloc(u16, entries.len);2304 const backlinks = try gpa.alloc(u16, entries.len);
2305 defer gpa.free(backlinks);2305 defer gpa.free(backlinks);
...@@ -2655,8 +2655,8 @@ fn addLoadPhdrs(self: *Elf) error{OutOfMemory}!void {...@@ -2655,8 +2655,8 @@ fn addLoadPhdrs(self: *Elf) error{OutOfMemory}!void {
2655 if (shdr.sh_type == elf.SHT_NULL) continue;2655 if (shdr.sh_type == elf.SHT_NULL) continue;
2656 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;2656 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
2657 const flags = shdrToPhdrFlags(shdr.sh_flags);2657 const flags = shdrToPhdrFlags(shdr.sh_flags);
2658 if (self.getPhdr(.{ .flags = flags, .type = elf.PT_LOAD }) == .none) {2658 if (self.getPhdr(.{ .flags = flags, .type = @backingInt(elf.PT.LOAD) }) == .none) {
2659 _ = try self.addPhdr(.{ .flags = flags, .type = elf.PT_LOAD });2659 _ = try self.addPhdr(.{ .flags = flags, .type = @backingInt(elf.PT.LOAD) });
2660 }2660 }
2661 }2661 }
2662}2662}
...@@ -2672,11 +2672,11 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {...@@ -2672,11 +2672,11 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {
2672 .p64 => @sizeOf(elf.Elf64_Ehdr),2672 .p64 => @sizeOf(elf.Elf64_Ehdr),
2673 };2673 };
2674 const phsize: u64 = switch (self.ptr_width) {2674 const phsize: u64 = switch (self.ptr_width) {
2675 .p32 => @sizeOf(elf.Elf32_Phdr),2675 .p32 => @sizeOf(elf.Elf32.Phdr),
2676 .p64 => @sizeOf(elf.Elf64_Phdr),2676 .p64 => @sizeOf(elf.Elf64.Phdr),
2677 };2677 };
2678 const needed_size = self.phdrs.items.len * phsize;2678 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
2681 if (needed_size > available_space) {2681 if (needed_size > available_space) {
2682 // In this case, we have two options:2682 // In this case, we have two options:
...@@ -2689,10 +2689,10 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {...@@ -2689,10 +2689,10 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {
2689 err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space });2689 err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space });
2690 }2690 }
26912691
2692 phdr_table_load.p_filesz = needed_size + ehsize;2692 phdr_table_load.filesz = needed_size + ehsize;
2693 phdr_table_load.p_memsz = needed_size + ehsize;2693 phdr_table_load.memsz = needed_size + ehsize;
2694 phdr_table.p_filesz = needed_size;2694 phdr_table.filesz = needed_size;
2695 phdr_table.p_memsz = needed_size;2695 phdr_table.memsz = needed_size;
2696}2696}
26972697
2698/// Allocates alloc sections and creates load segments for sections2698/// Allocates alloc sections and creates load segments for sections
...@@ -2758,7 +2758,7 @@ pub fn allocateAllocSections(self: *Elf) !void {...@@ -2758,7 +2758,7 @@ pub fn allocateAllocSections(self: *Elf) !void {
2758 // of any section that is contained in a cover and use it to align2758 // of any section that is contained in a cover and use it to align
2759 // the start address of the segement (and first section).2759 // the start address of the segement (and first section).
2760 const phdr_table = &self.phdrs.items[self.phdr_indexes.table_load.int().?];2760 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
2763 for (covers) |cover| {2763 for (covers) |cover| {
2764 if (cover.items.len == 0) continue;2764 if (cover.items.len == 0) continue;
...@@ -2817,14 +2817,14 @@ pub fn allocateAllocSections(self: *Elf) !void {...@@ -2817,14 +2817,14 @@ pub fn allocateAllocSections(self: *Elf) !void {
2817 }2817 }
28182818
2819 const first = slice.items(.shdr)[cover.items[0]];2819 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().?;
2821 const phdr = &self.phdrs.items[phndx.int()];2821 const phdr = &self.phdrs.items[phndx.int()];
2822 const allocated_size = self.allocatedSize(phdr.p_offset);2822 const allocated_size = self.allocatedSize(phdr.offset);
2823 if (filesz > allocated_size) {2823 if (filesz > allocated_size) {
2824 const old_offset = phdr.p_offset;2824 const old_offset = phdr.offset;
2825 phdr.p_offset = 0;2825 phdr.offset = 0;
2826 var new_offset = try self.findFreeSpace(filesz, @"align");2826 var new_offset = try self.findFreeSpace(filesz, @"align");
2827 phdr.p_offset = new_offset;2827 phdr.offset = new_offset;
28282828
2829 log.debug("moving phdr({d}) from 0x{x} to 0x{x}", .{ phndx, old_offset, new_offset });2829 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 {...@@ -2854,11 +2854,11 @@ pub fn allocateAllocSections(self: *Elf) !void {
2854 }2854 }
2855 }2855 }
28562856
2857 phdr.p_vaddr = first.sh_addr;2857 phdr.vaddr = first.sh_addr;
2858 phdr.p_paddr = first.sh_addr;2858 phdr.paddr = first.sh_addr;
2859 phdr.p_memsz = memsz;2859 phdr.memsz = memsz;
2860 phdr.p_filesz = filesz;2860 phdr.filesz = filesz;
2861 phdr.p_align = @"align";2861 phdr.@"align" = @"align";
28622862
2863 addr = mem.alignForward(u64, addr, self.page_size);2863 addr = mem.alignForward(u64, addr, self.page_size);
2864 }2864 }
...@@ -2902,12 +2902,12 @@ fn allocateSpecialPhdrs(self: *Elf) void {...@@ -2902,12 +2902,12 @@ fn allocateSpecialPhdrs(self: *Elf) void {
2902 if (pair[0].int()) |index| {2902 if (pair[0].int()) |index| {
2903 const shdr = slice.items(.shdr)[pair[1].?];2903 const shdr = slice.items(.shdr)[pair[1].?];
2904 const phdr = &self.phdrs.items[index];2904 const phdr = &self.phdrs.items[index];
2905 phdr.p_align = shdr.sh_addralign;2905 phdr.@"align" = shdr.sh_addralign;
2906 phdr.p_offset = shdr.sh_offset;2906 phdr.offset = shdr.sh_offset;
2907 phdr.p_vaddr = shdr.sh_addr;2907 phdr.vaddr = shdr.sh_addr;
2908 phdr.p_paddr = shdr.sh_addr;2908 phdr.paddr = shdr.sh_addr;
2909 phdr.p_filesz = shdr.sh_size;2909 phdr.filesz = shdr.sh_size;
2910 phdr.p_memsz = shdr.sh_size;2910 phdr.memsz = shdr.sh_size;
2911 }2911 }
2912 }2912 }
29132913
...@@ -2924,25 +2924,25 @@ fn allocateSpecialPhdrs(self: *Elf) void {...@@ -2924,25 +2924,25 @@ fn allocateSpecialPhdrs(self: *Elf) void {
2924 shndx += 1;2924 shndx += 1;
2925 continue;2925 continue;
2926 }2926 }
2927 phdr.p_offset = shdr.sh_offset;2927 phdr.offset = shdr.sh_offset;
2928 phdr.p_vaddr = shdr.sh_addr;2928 phdr.vaddr = shdr.sh_addr;
2929 phdr.p_paddr = shdr.sh_addr;2929 phdr.paddr = shdr.sh_addr;
2930 phdr.p_align = shdr.sh_addralign;2930 phdr.@"align" = shdr.sh_addralign;
2931 shndx += 1;2931 shndx += 1;
2932 phdr.p_align = @max(phdr.p_align, shdr.sh_addralign);2932 phdr.@"align" = @max(phdr.@"align", shdr.sh_addralign);
2933 if (shdr.sh_type != elf.SHT_NOBITS) {2933 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;
2935 }2935 }
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
2938 while (shndx < shdrs.len) : (shndx += 1) {2938 while (shndx < shdrs.len) : (shndx += 1) {
2939 const next = shdrs[shndx];2939 const next = shdrs[shndx];
2940 if (next.sh_flags & elf.SHF_TLS == 0) break;2940 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);
2942 if (next.sh_type != elf.SHT_NOBITS) {2942 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;
2944 }2944 }
2945 phdr.p_memsz = next.sh_addr + next.sh_size - phdr.p_vaddr;2945 phdr.memsz = next.sh_addr + next.sh_size - phdr.vaddr;
2946 }2946 }
2947 }2947 }
2948 }2948 }
...@@ -3347,16 +3347,16 @@ pub fn archPtrWidthBytes(self: Elf) u8 {...@@ -3347,16 +3347,16 @@ pub fn archPtrWidthBytes(self: Elf) u8 {
3347 return @intCast(@divExact(self.getTarget().ptrBitWidth(), 8));3347 return @intCast(@divExact(self.getTarget().ptrBitWidth(), 8));
3348}3348}
33493349
3350fn phdrTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr {3350fn phdrTo32(phdr: elf.Elf64.Phdr) elf.Elf32.Phdr {
3351 return .{3351 return .{
3352 .p_type = phdr.p_type,3352 .type = phdr.type,
3353 .p_flags = phdr.p_flags,3353 .flags = phdr.flags,
3354 .p_offset = @as(u32, @intCast(phdr.p_offset)),3354 .offset = @intCast(phdr.offset),
3355 .p_vaddr = @as(u32, @intCast(phdr.p_vaddr)),3355 .vaddr = @intCast(phdr.vaddr),
3356 .p_paddr = @as(u32, @intCast(phdr.p_paddr)),3356 .paddr = @intCast(phdr.paddr),
3357 .p_filesz = @as(u32, @intCast(phdr.p_filesz)),3357 .filesz = @intCast(phdr.filesz),
3358 .p_memsz = @as(u32, @intCast(phdr.p_memsz)),3358 .memsz = @intCast(phdr.memsz),
3359 .p_align = @as(u32, @intCast(phdr.p_align)),3359 .@"align" = @intCast(phdr.@"align"),
3360 };3360 };
3361}3361}
33623362
...@@ -3397,7 +3397,7 @@ fn getPhdr(self: *Elf, opts: struct {...@@ -3397,7 +3397,7 @@ fn getPhdr(self: *Elf, opts: struct {
3397 if (self.phdr_indexes.table_load.int()) |index| {3397 if (self.phdr_indexes.table_load.int()) |index| {
3398 if (phndx == index) continue;3398 if (phndx == index) continue;
3399 }3399 }
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)
3401 return @fromBackingInt(@intCast(phndx));3401 return @fromBackingInt(@intCast(phndx));
3402 }3402 }
3403 return .none;3403 return .none;
...@@ -3415,14 +3415,14 @@ fn addPhdr(self: *Elf, opts: struct {...@@ -3415,14 +3415,14 @@ fn addPhdr(self: *Elf, opts: struct {
3415 const gpa = self.base.comp.gpa;3415 const gpa = self.base.comp.gpa;
3416 const index: ProgramHeaderIndex = @fromBackingInt(@intCast(self.phdrs.items.len));3416 const index: ProgramHeaderIndex = @fromBackingInt(@intCast(self.phdrs.items.len));
3417 try self.phdrs.append(gpa, .{3417 try self.phdrs.append(gpa, .{
3418 .p_type = opts.type,3418 .type = @fromBackingInt(opts.type),
3419 .p_flags = opts.flags,3419 .flags = @fromBackingInt(opts.flags),
3420 .p_offset = opts.offset,3420 .offset = opts.offset,
3421 .p_vaddr = opts.addr,3421 .vaddr = opts.addr,
3422 .p_paddr = opts.addr,3422 .paddr = opts.addr,
3423 .p_filesz = opts.filesz,3423 .filesz = opts.filesz,
3424 .p_memsz = opts.memsz,3424 .memsz = opts.memsz,
3425 .p_align = opts.@"align",3425 .@"align" = opts.@"align",
3426 });3426 });
3427 return index;3427 return index;
3428}3428}
...@@ -3673,9 +3673,9 @@ pub fn tpAddress(self: *Elf) i64 {...@@ -3673,9 +3673,9 @@ pub fn tpAddress(self: *Elf) i64 {
3673 const index = self.phdr_indexes.tls.int() orelse return 0;3673 const index = self.phdr_indexes.tls.int() orelse return 0;
3674 const phdr = self.phdrs.items[index];3674 const phdr = self.phdrs.items[index];
3675 const addr = switch (self.getTarget().cpu.arch) {3675 const addr = switch (self.getTarget().cpu.arch) {
3676 .x86_64 => mem.alignForward(u64, phdr.p_vaddr + phdr.p_memsz, phdr.p_align),3676 .x86_64 => mem.alignForward(u64, phdr.vaddr + phdr.memsz, phdr.@"align"),
3677 .aarch64, .aarch64_be => mem.alignBackward(u64, phdr.p_vaddr - 16, phdr.p_align),3677 .aarch64, .aarch64_be => mem.alignBackward(u64, phdr.vaddr - 16, phdr.@"align"),
3678 .riscv64, .riscv64be => phdr.p_vaddr,3678 .riscv64, .riscv64be => phdr.vaddr,
3679 else => |arch| std.debug.panic("TODO implement getTpAddress for {s}", .{@tagName(arch)}),3679 else => |arch| std.debug.panic("TODO implement getTpAddress for {s}", .{@tagName(arch)}),
3680 };3680 };
3681 return @intCast(addr);3681 return @intCast(addr);
...@@ -3684,13 +3684,13 @@ pub fn tpAddress(self: *Elf) i64 {...@@ -3684,13 +3684,13 @@ pub fn tpAddress(self: *Elf) i64 {
3684pub fn dtpAddress(self: *Elf) i64 {3684pub fn dtpAddress(self: *Elf) i64 {
3685 const index = self.phdr_indexes.tls.int() orelse return 0;3685 const index = self.phdr_indexes.tls.int() orelse return 0;
3686 const phdr = self.phdrs.items[index];3686 const phdr = self.phdrs.items[index];
3687 return @intCast(phdr.p_vaddr);3687 return @intCast(phdr.vaddr);
3688}3688}
36893689
3690pub fn tlsAddress(self: *Elf) i64 {3690pub fn tlsAddress(self: *Elf) i64 {
3691 const index = self.phdr_indexes.tls.int() orelse return 0;3691 const index = self.phdr_indexes.tls.int() orelse return 0;
3692 const phdr = self.phdrs.items[index];3692 const phdr = self.phdrs.items[index];
3693 return @intCast(phdr.p_vaddr);3693 return @intCast(phdr.vaddr);
3694}3694}
36953695
3696pub fn getShString(self: Elf, off: u32) [:0]const u8 {3696pub 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...@@ -3886,10 +3886,10 @@ fn formatShdrFlags(sh_flags: u64, writer: *std.Io.Writer) std.Io.Writer.Error!vo
38863886
3887const FormatPhdr = struct {3887const FormatPhdr = struct {
3888 elf_file: *Elf,3888 elf_file: *Elf,
3889 phdr: elf.Elf64_Phdr,3889 phdr: elf.Elf64.Phdr,
3890};3890};
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) {
3893 return .{ .data = .{3893 return .{ .data = .{
3894 .phdr = phdr,3894 .phdr = phdr,
3895 .elf_file = self,3895 .elf_file = self,
...@@ -3898,28 +3898,28 @@ fn fmtPhdr(self: *Elf, phdr: elf.Elf64_Phdr) std.fmt.Alt(FormatPhdr, formatPhdr)...@@ -3898,28 +3898,28 @@ fn fmtPhdr(self: *Elf, phdr: elf.Elf64_Phdr) std.fmt.Alt(FormatPhdr, formatPhdr)
38983898
3899fn formatPhdr(ctx: FormatPhdr, writer: *std.Io.Writer) std.Io.Writer.Error!void {3899fn formatPhdr(ctx: FormatPhdr, writer: *std.Io.Writer) std.Io.Writer.Error!void {
3900 const phdr = ctx.phdr;3900 const phdr = ctx.phdr;
3901 const write = phdr.p_flags & elf.PF_W != 0;3901 const write = phdr.flags.W;
3902 const read = phdr.p_flags & elf.PF_R != 0;3902 const read = phdr.flags.R;
3903 const exec = phdr.p_flags & elf.PF_X != 0;3903 const exec = phdr.flags.X;
3904 var flags: [3]u8 = @splat('_');3904 var flags: [3]u8 = @splat('_');
3905 if (exec) flags[0] = 'X';3905 if (exec) flags[0] = 'X';
3906 if (write) flags[1] = 'W';3906 if (write) flags[1] = 'W';
3907 if (read) flags[2] = 'R';3907 if (read) flags[2] = 'R';
3908 const p_type = switch (phdr.p_type) {3908 const p_type = switch (phdr.type) {
3909 elf.PT_LOAD => "LOAD",3909 .LOAD => "LOAD",
3910 elf.PT_TLS => "TLS",3910 .TLS => "TLS",
3911 elf.PT_GNU_EH_FRAME => "GNU_EH_FRAME",3911 .GNU_EH_FRAME => "GNU_EH_FRAME",
3912 elf.PT_GNU_STACK => "GNU_STACK",3912 .GNU_STACK => "GNU_STACK",
3913 elf.PT_DYNAMIC => "DYNAMIC",3913 .DYNAMIC => "DYNAMIC",
3914 elf.PT_INTERP => "INTERP",3914 .INTERP => "INTERP",
3915 elf.PT_NULL => "NULL",3915 .NULL => "NULL",
3916 elf.PT_PHDR => "PHDR",3916 .PHDR => "PHDR",
3917 elf.PT_NOTE => "NOTE",3917 .NOTE => "NOTE",
3918 else => "UNKNOWN",3918 else => "UNKNOWN",
3919 };3919 };
3920 try writer.print("{s} : {s} : @{x} ({x}) : align({x}) : filesz({x}) : memsz({x})", .{3920 try writer.print("{s} : {s} : @{x} ({x}) : align({x}) : filesz({x}) : memsz({x})", .{
3921 p_type, flags, phdr.p_offset, phdr.p_vaddr,3921 p_type, flags, phdr.offset, phdr.vaddr,
3922 phdr.p_align, phdr.p_filesz, phdr.p_memsz,3922 phdr.@"align", phdr.filesz, phdr.memsz,
3923 });3923 });
3924}3924}
39253925
src/link/Lld.zig+2-2
...@@ -1023,8 +1023,8 @@ fn elfLink(lld: *Lld, arena: Allocator) !void {...@@ -1023,8 +1023,8 @@ fn elfLink(lld: *Lld, arena: Allocator) !void {
1023 }1023 }
10241024
1025 if (is_exe_or_dyn_lib and target.os.tag == .netbsd) {1025 if (is_exe_or_dyn_lib and target.os.tag == .netbsd) {
1026 // Add options to produce shared objects with only 2 PT_LOAD segments.1026 // Add options to produce shared objects with only 2 PT.LOAD segments.
1027 // NetBSD expects 2 PT_LOAD segments in a shared object, otherwise1027 // NetBSD expects 2 PT.LOAD segments in a shared object, otherwise
1028 // ld.elf_so fails loading dynamic libraries with "not found" error.1028 // ld.elf_so fails loading dynamic libraries with "not found" error.
1029 // See https://github.com/ziglang/zig/issues/9109 .1029 // See https://github.com/ziglang/zig/issues/9109 .
1030 try argv.append("--no-rosegment");1030 try argv.append("--no-rosegment");