authorgravatar for seda18@rolmail.netDavid Senoner <seda18@rolmail.net> 2026-07-30 18:38:51+02:00
committergravatar for seda18@rolmail.netDavid Senoner <seda18@rolmail.net> 2026-07-30 21:23:58+02:00
log574c80351da46a4ac836504c6591d5db068eaa06
tree7e32b0b637b2fb9af179d5b37ffb8937630f081c
parent4843120b4c3d3bb8cc8cb56b0185af24676616ff

elf: remove usages of deprecated Elf64_Phdr


4 files changed, 128 insertions(+), 128 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 == @backingInt(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/elf.zig+14-14
...@@ -807,11 +807,11 @@ pub const ProgramHeaderIterator = struct {...@@ -807,11 +807,11 @@ pub const ProgramHeaderIterator = struct {
807 file_reader: *Io.File.Reader,807 file_reader: *Io.File.Reader,
808 index: usize = 0,808 index: usize = 0,
809809
810 pub fn next(it: *ProgramHeaderIterator) !?Elf64_Phdr {810 pub fn next(it: *ProgramHeaderIterator) !?Elf64.Phdr {
811 if (it.index >= it.phnum) return null;811 if (it.index >= it.phnum) return null;
812 defer it.index += 1;812 defer it.index += 1;
813813
814 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);
815 const offset = it.phoff + size * it.index;815 const offset = it.phoff + size * it.index;
816 try it.file_reader.seekTo(offset);816 try it.file_reader.seekTo(offset);
817817
...@@ -828,11 +828,11 @@ pub const ProgramHeaderBufferIterator = struct {...@@ -828,11 +828,11 @@ pub const ProgramHeaderBufferIterator = struct {
828 buf: []const u8,828 buf: []const u8,
829 index: usize = 0,829 index: usize = 0,
830830
831 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64_Phdr {831 pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64.Phdr {
832 if (it.index >= it.phnum) return null;832 if (it.index >= it.phnum) return null;
833 defer it.index += 1;833 defer it.index += 1;
834834
835 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);
836 const offset = @as(usize, @intCast(it.phoff)) + size * it.index;836 const offset = @as(usize, @intCast(it.phoff)) + size * it.index;
837 var reader = Io.Reader.fixed(it.buf[offset..]);837 var reader = Io.Reader.fixed(it.buf[offset..]);
838838
...@@ -840,22 +840,22 @@ pub const ProgramHeaderBufferIterator = struct {...@@ -840,22 +840,22 @@ pub const ProgramHeaderBufferIterator = struct {
840 }840 }
841};841};
842842
843pub 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 {
844 if (is_64) {844 if (is_64) {
845 const phdr = try reader.takeStruct(Elf64_Phdr, endian);845 const phdr = try reader.takeStruct(Elf64.Phdr, endian);
846 return phdr;846 return phdr;
847 }847 }
848848
849 const phdr = try reader.takeStruct(Elf32.Phdr, endian);849 const phdr = try reader.takeStruct(Elf32.Phdr, endian);
850 return .{850 return .{
851 .p_type = @backingInt(phdr.type),851 .type = phdr.type,
852 .p_offset = phdr.offset,852 .offset = phdr.offset,
853 .p_vaddr = phdr.vaddr,853 .vaddr = phdr.vaddr,
854 .p_paddr = phdr.paddr,854 .paddr = phdr.paddr,
855 .p_filesz = phdr.filesz,855 .filesz = phdr.filesz,
856 .p_memsz = phdr.memsz,856 .memsz = phdr.memsz,
857 .p_flags = @backingInt(phdr.flags),857 .flags = phdr.flags,
858 .p_align = phdr.@"align",858 .@"align" = phdr.@"align",
859 };859 };
860}860}
861861
lib/std/zig/system.zig+6-6
...@@ -603,15 +603,15 @@ fn abiAndDynamicLinkerFromFile(...@@ -603,15 +603,15 @@ fn abiAndDynamicLinkerFromFile(
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 @backingInt(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;
...@@ -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 @backingInt(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+101-101
...@@ -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),
...@@ -337,7 +337,7 @@ pub fn createEmpty(...@@ -337,7 +337,7 @@ pub fn createEmpty(
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),
...@@ -345,7 +345,7 @@ pub fn createEmpty(...@@ -345,7 +345,7 @@ pub fn createEmpty(
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);
...@@ -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 != @backingInt(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,8 +1471,8 @@ fn writePhdrTable(self: *Elf) !void {...@@ -1471,8 +1471,8 @@ 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) {
...@@ -1486,19 +1486,19 @@ fn writePhdrTable(self: *Elf) !void {...@@ -1486,19 +1486,19 @@ fn writePhdrTable(self: *Elf) !void {
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);
...@@ -1623,7 +1623,7 @@ pub fn writeElfHeader(self: *Elf) !void {...@@ -1623,7 +1623,7 @@ pub fn writeElfHeader(self: *Elf) !void {
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;
...@@ -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 @backingInt(elf.PT.NULL) => 0,2265 .NULL => 0,
2266 @backingInt(elf.PT.PHDR) => 1,2266 .PHDR => 1,
2267 @backingInt(elf.PT.INTERP) => 2,2267 .INTERP => 2,
2268 @backingInt(elf.PT.LOAD) => 3,2268 .LOAD => 3,
2269 @backingInt(elf.PT.DYNAMIC), @backingInt(elf.PT.TLS) => 4,2269 .DYNAMIC, .TLS => 4,
2270 @backingInt(elf.PT.GNU_EH_FRAME) => 5,2270 .GNU_EH_FRAME => 5,
2271 @backingInt(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);
...@@ -2673,10 +2673,10 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {...@@ -2673,10 +2673,10 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void {
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;
...@@ -2819,12 +2819,12 @@ pub fn allocateAllocSections(self: *Elf) !void {...@@ -2819,12 +2819,12 @@ pub fn allocateAllocSections(self: *Elf) !void {
2819 const first = slice.items(.shdr)[cover.items[0]];2819 const first = slice.items(.shdr)[cover.items[0]];
2820 const phndx = self.getPhdr(.{ .type = @backingInt(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 .type = @fromBackingInt(phdr.p_type),3352 .type = phdr.type,
3353 .flags = @fromBackingInt(phdr.p_flags),3353 .flags = phdr.flags,
3354 .offset = @intCast(phdr.p_offset),3354 .offset = @intCast(phdr.offset),
3355 .vaddr = @intCast(phdr.p_vaddr),3355 .vaddr = @intCast(phdr.vaddr),
3356 .paddr = @intCast(phdr.p_paddr),3356 .paddr = @intCast(phdr.paddr),
3357 .filesz = @intCast(phdr.p_filesz),3357 .filesz = @intCast(phdr.filesz),
3358 .memsz = @intCast(phdr.p_memsz),3358 .memsz = @intCast(phdr.memsz),
3359 .@"align" = @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 @backingInt(elf.PT.LOAD) => "LOAD",3909 .LOAD => "LOAD",
3910 @backingInt(elf.PT.TLS) => "TLS",3910 .TLS => "TLS",
3911 @backingInt(elf.PT.GNU_EH_FRAME) => "GNU_EH_FRAME",3911 .GNU_EH_FRAME => "GNU_EH_FRAME",
3912 @backingInt(elf.PT.GNU_STACK) => "GNU_STACK",3912 .GNU_STACK => "GNU_STACK",
3913 @backingInt(elf.PT.DYNAMIC) => "DYNAMIC",3913 .DYNAMIC => "DYNAMIC",
3914 @backingInt(elf.PT.INTERP) => "INTERP",3914 .INTERP => "INTERP",
3915 @backingInt(elf.PT.NULL) => "NULL",3915 .NULL => "NULL",
3916 @backingInt(elf.PT.PHDR) => "PHDR",3916 .PHDR => "PHDR",
3917 @backingInt(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