| author | |
| committer | |
| log | f8c6193e4c4a2e404def6c641bbd958a0226c486 |
| tree | fa6b099b60ef0836f1cc348dda42471c7a75754c |
| parent | e88566253d65a83e3564f95504fe034b4287c340 |
| parent | 9bc9544bc8df2b0fe5ad82bd5459a9ce7ca55bac |
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 | 435 | |
| 436 | 436 | var program_headers = elf_hdr.iterateProgramHeaders(in); |
| 437 | 437 | while (try program_headers.next()) |phdr| { |
| 438 | if (phdr.p_type == elf.PT_LOAD) { | |
| 438 | if (phdr.type == .LOAD) { | |
| 439 | 439 | const newSegment = try allocator.create(BinaryElfSegment); |
| 440 | 440 | |
| 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; | |
| 445 | 445 | newSegment.binaryOffset = 0; |
| 446 | 446 | newSegment.firstSection = null; |
| 447 | 447 | |
| ... | ... | @@ -495,8 +495,8 @@ const BinaryElfOutput = struct { |
| 495 | 495 | return self; |
| 496 | 496 | } |
| 497 | 497 | |
| 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); | |
| 500 | 500 | } |
| 501 | 501 | |
| 502 | 502 | fn sectionValidForOutput(shdr: anytype) bool { |
lib/std/Build/Step/Compile.zig+1-1| ... | ... | @@ -101,7 +101,7 @@ each_lib_rpath: ?bool = null, |
| 101 | 101 | /// This option overrides the CLI argument passed to `zig build`. |
| 102 | 102 | build_id: ?std.zig.BuildId = null, |
| 103 | 103 | |
| 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 | |
| 105 | 105 | /// file. |
| 106 | 106 | link_eh_frame_hdr: bool = false, |
| 107 | 107 | link_emit_relocs: bool = false, |
lib/std/debug/SelfInfo/Elf.zig+1-1| ... | ... | @@ -518,7 +518,7 @@ const DlIterContext = struct { |
| 518 | 518 | for (info.phdr[0..info.phnum]) |phdr| { |
| 519 | 519 | if (phdr.type != .LOAD) continue; |
| 520 | 520 | try context.si.ranges.append(gpa, .{ |
| 521 | // Overflowing addition handles VSDOs having p_vaddr = 0xffffffffff700000 | |
| 521 | // Overflowing addition handles VSDOs having vaddr = 0xffffffffff700000 | |
| 522 | 522 | .start = info.addr +% phdr.vaddr, |
| 523 | 523 | .len = phdr.memsz, |
| 524 | 524 | .module_index = module_index, |
lib/std/dynamic_library.zig+17-17| ... | ... | @@ -103,7 +103,7 @@ pub fn get_DYNAMIC() ?[*]const elf.Dyn { |
| 103 | 103 | |
| 104 | 104 | pub fn linkmap_iterator() error{InvalidExe}!LinkMap.Iterator { |
| 105 | 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 | 107 | return .{ .current = null }; |
| 108 | 108 | }; |
| 109 | 109 | |
| ... | ... | @@ -261,10 +261,10 @@ pub const ElfDynLib = struct { |
| 261 | 261 | i += 1; |
| 262 | 262 | ph_addr += eh.e_phentsize; |
| 263 | 263 | }) { |
| 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)), | |
| 268 | 268 | else => {}, |
| 269 | 269 | } |
| 270 | 270 | } |
| ... | ... | @@ -292,23 +292,23 @@ pub const ElfDynLib = struct { |
| 292 | 292 | i += 1; |
| 293 | 293 | ph_addr += eh.e_phentsize; |
| 294 | 294 | }) { |
| 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 => { | |
| 298 | 298 | // The VirtAddr may not be page-aligned; in such case there will be |
| 299 | 299 | // 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); | |
| 303 | 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 | 305 | _ = try posix.mmap( |
| 306 | 306 | ptr, |
| 307 | 307 | extended_memsz, |
| 308 | 308 | prot, |
| 309 | 309 | .{ .TYPE = .PRIVATE, .FIXED = true }, |
| 310 | 310 | file.handle, |
| 311 | ph.p_offset - extra_bytes, | |
| 311 | ph.offset - extra_bytes, | |
| 312 | 312 | ); |
| 313 | 313 | }, |
| 314 | 314 | else => {}, |
| ... | ... | @@ -517,11 +517,11 @@ pub const ElfDynLib = struct { |
| 517 | 517 | return null; |
| 518 | 518 | } |
| 519 | 519 | |
| 520 | fn elfToProt(elf_prot: u64) posix.PROT { | |
| 520 | fn elfToProt(elf_prot: elf.PF) posix.PROT { | |
| 521 | 521 | 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, | |
| 525 | 525 | }; |
| 526 | 526 | } |
| 527 | 527 | }; |
lib/std/elf.zig+15-84| ... | ... | @@ -290,47 +290,6 @@ pub const VER_FLG_BASE = 1; |
| 290 | 290 | /// Weak version identifier |
| 291 | 291 | pub const VER_FLG_WEAK = 2; |
| 292 | 292 | |
| 293 | /// Deprecated, use `@intFromEnum(std.elf.PT.NULL)` | |
| 294 | pub const PT_NULL = @backingInt(std.elf.PT.NULL); | |
| 295 | /// Deprecated, use `@intFromEnum(std.elf.PT.LOAD)` | |
| 296 | pub const PT_LOAD = @backingInt(std.elf.PT.LOAD); | |
| 297 | /// Deprecated, use `@intFromEnum(std.elf.PT.DYNAMIC)` | |
| 298 | pub const PT_DYNAMIC = @backingInt(std.elf.PT.DYNAMIC); | |
| 299 | /// Deprecated, use `@intFromEnum(std.elf.PT.INTERP)` | |
| 300 | pub const PT_INTERP = @backingInt(std.elf.PT.INTERP); | |
| 301 | /// Deprecated, use `@intFromEnum(std.elf.PT.NOTE)` | |
| 302 | pub const PT_NOTE = @backingInt(std.elf.PT.NOTE); | |
| 303 | /// Deprecated, use `@intFromEnum(std.elf.PT.SHLIB)` | |
| 304 | pub const PT_SHLIB = @backingInt(std.elf.PT.SHLIB); | |
| 305 | /// Deprecated, use `@intFromEnum(std.elf.PT.PHDR)` | |
| 306 | pub const PT_PHDR = @backingInt(std.elf.PT.PHDR); | |
| 307 | /// Deprecated, use `@intFromEnum(std.elf.PT.TLS)` | |
| 308 | pub const PT_TLS = @backingInt(std.elf.PT.TLS); | |
| 309 | /// Deprecated, use `std.elf.PT.NUM`. | |
| 310 | pub const PT_NUM = PT.NUM; | |
| 311 | /// Deprecated, use `@intFromEnum(std.elf.PT.LOOS)` | |
| 312 | pub const PT_LOOS = @backingInt(std.elf.PT.LOOS); | |
| 313 | /// Deprecated, use `@intFromEnum(std.elf.PT.GNU_EH_FRAME)` | |
| 314 | pub const PT_GNU_EH_FRAME = @backingInt(std.elf.PT.GNU_EH_FRAME); | |
| 315 | /// Deprecated, use `@intFromEnum(std.elf.PT.GNU_STACK)` | |
| 316 | pub const PT_GNU_STACK = @backingInt(std.elf.PT.GNU_STACK); | |
| 317 | /// Deprecated, use `@intFromEnum(std.elf.PT.GNU_RELRO)` | |
| 318 | pub const PT_GNU_RELRO = @backingInt(std.elf.PT.GNU_RELRO); | |
| 319 | /// Deprecated, use `@intFromEnum(std.elf.PT.LOSUNW)` | |
| 320 | pub const PT_LOSUNW = @backingInt(std.elf.PT.LOSUNW); | |
| 321 | /// Deprecated, use `@intFromEnum(std.elf.PT.SUNWBSS)` | |
| 322 | pub const PT_SUNWBSS = @backingInt(std.elf.PT.SUNWBSS); | |
| 323 | /// Deprecated, use `@intFromEnum(std.elf.PT.SUNWSTACK)` | |
| 324 | pub const PT_SUNWSTACK = @backingInt(std.elf.PT.SUNWSTACK); | |
| 325 | /// Deprecated, use `@intFromEnum(std.elf.PT.HISUNW)` | |
| 326 | pub const PT_HISUNW = @backingInt(std.elf.PT.HISUNW); | |
| 327 | /// Deprecated, use `@intFromEnum(std.elf.PT.HIOS)` | |
| 328 | pub const PT_HIOS = @backingInt(std.elf.PT.HIOS); | |
| 329 | /// Deprecated, use `@intFromEnum(std.elf.PT.LOPROC)` | |
| 330 | pub const PT_LOPROC = @backingInt(std.elf.PT.LOPROC); | |
| 331 | /// Deprecated, use `@intFromEnum(std.elf.PT.HIPROC)` | |
| 332 | pub const PT_HIPROC = @backingInt(std.elf.PT.HIPROC); | |
| 333 | ||
| 334 | 293 | pub const PN_XNUM = 0xffff; |
| 335 | 294 | |
| 336 | 295 | /// Deprecated, use `@intFromEnum(std.elf.SHT.NULL)` |
| ... | ... | @@ -848,11 +807,11 @@ pub const ProgramHeaderIterator = struct { |
| 848 | 807 | file_reader: *Io.File.Reader, |
| 849 | 808 | index: usize = 0, |
| 850 | 809 | |
| 851 | pub fn next(it: *ProgramHeaderIterator) !?Elf64_Phdr { | |
| 810 | pub fn next(it: *ProgramHeaderIterator) !?Elf64.Phdr { | |
| 852 | 811 | if (it.index >= it.phnum) return null; |
| 853 | 812 | defer it.index += 1; |
| 854 | 813 | |
| 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 | 815 | const offset = it.phoff + size * it.index; |
| 857 | 816 | try it.file_reader.seekTo(offset); |
| 858 | 817 | |
| ... | ... | @@ -869,11 +828,11 @@ pub const ProgramHeaderBufferIterator = struct { |
| 869 | 828 | buf: []const u8, |
| 870 | 829 | index: usize = 0, |
| 871 | 830 | |
| 872 | pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64_Phdr { | |
| 831 | pub fn next(it: *ProgramHeaderBufferIterator) !?Elf64.Phdr { | |
| 873 | 832 | if (it.index >= it.phnum) return null; |
| 874 | 833 | defer it.index += 1; |
| 875 | 834 | |
| 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 | 836 | const offset = @as(usize, @intCast(it.phoff)) + size * it.index; |
| 878 | 837 | var reader = Io.Reader.fixed(it.buf[offset..]); |
| 879 | 838 | |
| ... | ... | @@ -881,22 +840,22 @@ pub const ProgramHeaderBufferIterator = struct { |
| 881 | 840 | } |
| 882 | 841 | }; |
| 883 | 842 | |
| 884 | pub fn takeProgramHeader(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64_Phdr { | |
| 843 | pub fn takeProgramHeader(reader: *Io.Reader, is_64: bool, endian: Endian) !Elf64.Phdr { | |
| 885 | 844 | if (is_64) { |
| 886 | const phdr = try reader.takeStruct(Elf64_Phdr, endian); | |
| 845 | const phdr = try reader.takeStruct(Elf64.Phdr, endian); | |
| 887 | 846 | return phdr; |
| 888 | 847 | } |
| 889 | 848 | |
| 890 | const phdr = try reader.takeStruct(Elf32_Phdr, endian); | |
| 849 | const phdr = try reader.takeStruct(Elf32.Phdr, endian); | |
| 891 | 850 | 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", | |
| 900 | 859 | }; |
| 901 | 860 | } |
| 902 | 861 | |
| ... | ... | @@ -1276,28 +1235,6 @@ pub const Elf64_Ehdr = extern struct { |
| 1276 | 1235 | e_shnum: Half, |
| 1277 | 1236 | e_shstrndx: Half, |
| 1278 | 1237 | }; |
| 1279 | /// Deprecated, use `std.elf.Elf32.Phdr` | |
| 1280 | pub 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` | |
| 1291 | pub 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 | 1238 | /// Deprecated, use `std.elf.Elf32.Shdr` |
| 1302 | 1239 | pub const Elf32_Shdr = extern struct { |
| 1303 | 1240 | sh_name: Word, |
| ... | ... | @@ -1568,12 +1505,6 @@ pub const Ehdr = switch (@sizeOf(usize)) { |
| 1568 | 1505 | 8 => Elf64_Ehdr, |
| 1569 | 1506 | else => @compileError("expected pointer size of 32 or 64"), |
| 1570 | 1507 | }; |
| 1571 | /// Deprecated, use `std.elf.ElfN.Phdr` | |
| 1572 | pub const Phdr = switch (@sizeOf(usize)) { | |
| 1573 | 4 => Elf32_Phdr, | |
| 1574 | 8 => Elf64_Phdr, | |
| 1575 | else => @compileError("expected pointer size of 32 or 64"), | |
| 1576 | }; | |
| 1577 | 1508 | pub const Dyn = switch (@sizeOf(usize)) { |
| 1578 | 1509 | 4 => Elf32_Dyn, |
| 1579 | 1510 | 8 => Elf64_Dyn, |
lib/std/os/emscripten.zig+1-1| ... | ... | @@ -730,7 +730,7 @@ pub const clock_t = i32; |
| 730 | 730 | pub const dl_phdr_info = extern struct { |
| 731 | 731 | addr: usize, |
| 732 | 732 | name: ?[*:0]const u8, |
| 733 | phdr: [*]std.elf.Phdr, | |
| 733 | phdr: [*]std.elf.ElfN.Phdr, | |
| 734 | 734 | phnum: u16, |
| 735 | 735 | }; |
| 736 | 736 |
lib/std/os/linux/tls.zig+12-12| ... | ... | @@ -22,7 +22,7 @@ const page_size_min = std.heap.page_size_min; |
| 22 | 22 | /// Represents an ELF TLS variant. |
| 23 | 23 | /// |
| 24 | 24 | /// 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 | 27 | /// The location of the DTV does not actually matter. For simplicity, we put it in the TLS area, but |
| 28 | 28 | /// there is no actual ABI requirement that it reside there. |
| ... | ... | @@ -480,17 +480,17 @@ pub fn getThreadPointer() usize { |
| 480 | 480 | }; |
| 481 | 481 | } |
| 482 | 482 | |
| 483 | fn computeAreaDesc(phdrs: []elf.Phdr) void { | |
| 483 | fn computeAreaDesc(phdrs: []elf.ElfN.Phdr) void { | |
| 484 | 484 | @setRuntimeSafety(false); |
| 485 | 485 | @disableInstrumentation(); |
| 486 | 486 | |
| 487 | var tls_phdr: ?*elf.Phdr = null; | |
| 487 | var tls_phdr: ?*elf.ElfN.Phdr = null; | |
| 488 | 488 | var img_base: usize = 0; |
| 489 | 489 | |
| 490 | 490 | 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, | |
| 494 | 494 | else => {}, |
| 495 | 495 | } |
| 496 | 496 | } |
| ... | ... | @@ -500,12 +500,12 @@ fn computeAreaDesc(phdrs: []elf.Phdr) void { |
| 500 | 500 | var block_size: usize = undefined; |
| 501 | 501 | |
| 502 | 502 | if (tls_phdr) |phdr| { |
| 503 | align_factor = phdr.p_align; | |
| 503 | align_factor = phdr.@"align"; | |
| 504 | 504 | |
| 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; | |
| 509 | 509 | } else { |
| 510 | 510 | align_factor = @alignOf(usize); |
| 511 | 511 | |
| ... | ... | @@ -651,7 +651,7 @@ var main_thread_area_buffer: [0x1000]u8 align(page_size_min) = undefined; |
| 651 | 651 | |
| 652 | 652 | /// Computes the layout of the static TLS area, allocates the area, initializes all of its fields, |
| 653 | 653 | /// and assigns the architecture-specific value to the TP register. |
| 654 | pub fn initStatic(phdrs: []elf.Phdr) void { | |
| 654 | pub fn initStatic(phdrs: []elf.ElfN.Phdr) void { | |
| 655 | 655 | @setRuntimeSafety(false); |
| 656 | 656 | @disableInstrumentation(); |
| 657 | 657 |
lib/std/os/linux/vdso.zig+5-5| ... | ... | @@ -19,14 +19,14 @@ pub fn lookup(vername: []const u8, name: []const u8) usize { |
| 19 | 19 | i += 1; |
| 20 | 20 | ph_addr += eh.e_phentsize; |
| 21 | 21 | }) { |
| 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) { | |
| 24 | 24 | // 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 | 26 | // Wrapping operations are used on this line as well as subsequent calculations relative to base |
| 27 | 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, | |
| 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)), | |
| 30 | 30 | else => {}, |
| 31 | 31 | } |
| 32 | 32 | } |
lib/std/pie.zig+3-3| ... | ... | @@ -293,7 +293,7 @@ inline fn getDynamicSymbol() [*]const elf.Dyn { |
| 293 | 293 | }; |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | pub fn relocate(phdrs: []const elf.Phdr) void { | |
| 296 | pub fn relocate(phdrs: []const elf.ElfN.Phdr) void { | |
| 297 | 297 | @setRuntimeSafety(false); |
| 298 | 298 | @disableInstrumentation(); |
| 299 | 299 | |
| ... | ... | @@ -303,8 +303,8 @@ pub fn relocate(phdrs: []const elf.Phdr) void { |
| 303 | 303 | // the theoretical load addresses for the `_DYNAMIC` symbol. |
| 304 | 304 | const base_addr = base: { |
| 305 | 305 | 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; | |
| 308 | 308 | } |
| 309 | 309 | // This is not supposed to happen for well-formed binaries. |
| 310 | 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 | 75 | // Count how many libraries are loaded |
| 76 | 76 | counter.* += @as(usize, 1); |
| 77 | 77 | |
| 78 | // The image should contain at least a PT_LOAD segment | |
| 78 | // The image should contain at least a PT.LOAD segment | |
| 79 | 79 | if (info.phnum < 1) return error.MissingPtLoadSegment; |
| 80 | 80 | |
| 81 | 81 | // 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 | 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 | }; |
| 594 | 594 | |
| 595 | 595 | // 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 | 621 | std.os.linux.tls.initStatic(phdrs); |
| 622 | 622 | } |
| 623 | 623 | |
| 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 | |
| 625 | 625 | // program header. However the kernel does not recognize it; it always gives 8 MiB. |
| 626 | 626 | // Here we look for the stack size in our program headers and use setrlimit |
| 627 | 627 | // to ask for more stack space. |
| ... | ... | @@ -645,19 +645,19 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn { |
| 645 | 645 | std.process.exit(callMainWithArgs(argc, argv, envp)); |
| 646 | 646 | } |
| 647 | 647 | |
| 648 | fn expandStackSize(phdrs: []elf.Phdr) void { | |
| 648 | fn expandStackSize(phdrs: []elf.ElfN.Phdr) void { | |
| 649 | 649 | @disableInstrumentation(); |
| 650 | 650 | 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); | |
| 655 | 655 | |
| 656 | 656 | // Silently fail if we are unable to get limits. |
| 657 | 657 | const limits = std.posix.getrlimit(.STACK) catch break; |
| 658 | 658 | |
| 659 | 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); | |
| 661 | 661 | |
| 662 | 662 | if (wanted_stack_size > limits.cur) { |
| 663 | 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 | 702 | .linux => { |
| 703 | 703 | const at_phdr = std.c.getauxval(elf.AT_PHDR); |
| 704 | 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 | 706 | expandStackSize(phdrs); |
| 707 | 707 | }, |
| 708 | 708 | .windows => { |
lib/std/zig/system.zig+8-8| ... | ... | @@ -597,23 +597,23 @@ fn abiAndDynamicLinkerFromFile( |
| 597 | 597 | .ofmt = query.ofmt orelse Target.ObjectFormat.default(os.tag, cpu.arch), |
| 598 | 598 | .dynamic_linker = query.dynamic_linker orelse .none, |
| 599 | 599 | }; |
| 600 | var rpath_offset: ?u64 = null; // Found inside PT_DYNAMIC | |
| 600 | var rpath_offset: ?u64 = null; // Found inside PT.DYNAMIC | |
| 601 | 601 | const look_for_ld = query.dynamic_linker == null; |
| 602 | 602 | |
| 603 | 603 | var got_dyn_section: bool = false; |
| 604 | 604 | { |
| 605 | 605 | 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 => { | |
| 608 | 608 | got_dyn_section = true; |
| 609 | 609 | |
| 610 | 610 | if (look_for_ld) { |
| 611 | const p_filesz = phdr.p_filesz; | |
| 611 | const p_filesz = phdr.filesz; | |
| 612 | 612 | if (p_filesz > result.dynamic_linker.buffer.len) return error.NameTooLong; |
| 613 | 613 | const filesz: usize = @intCast(p_filesz); |
| 614 | try file_reader.seekTo(phdr.p_offset); | |
| 614 | try file_reader.seekTo(phdr.offset); | |
| 615 | 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 | 617 | const len = filesz - 1; |
| 618 | 618 | // dynamic_linker.max_byte is "max", not "len". |
| 619 | 619 | // We know it will fit in u8 because we check against dynamic_linker.buffer.len above. |
| ... | ... | @@ -631,11 +631,11 @@ fn abiAndDynamicLinkerFromFile( |
| 631 | 631 | } |
| 632 | 632 | }, |
| 633 | 633 | // We only need this for detecting glibc version. |
| 634 | elf.PT_DYNAMIC => { | |
| 634 | .DYNAMIC => { | |
| 635 | 635 | got_dyn_section = true; |
| 636 | 636 | |
| 637 | 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 | 639 | while (try dyn_it.next()) |dyn| { |
| 640 | 640 | if (dyn.d_tag == elf.DT_RUNPATH) { |
| 641 | 641 | rpath_offset = dyn.d_val; |
src/link/Elf.zig+125-125| ... | ... | @@ -127,7 +127,7 @@ const SectionIndexes = struct { |
| 127 | 127 | symtab: ?u32 = null, |
| 128 | 128 | }; |
| 129 | 129 | |
| 130 | const ProgramHeaderList = std.ArrayList(elf.Elf64_Phdr); | |
| 130 | const ProgramHeaderList = std.ArrayList(elf.Elf64.Phdr); | |
| 131 | 131 | |
| 132 | 132 | const OptionalProgramHeaderIndex = enum(u16) { |
| 133 | 133 | none = std.math.maxInt(u16), |
| ... | ... | @@ -159,21 +159,21 @@ const ProgramHeaderIndex = enum(u16) { |
| 159 | 159 | }; |
| 160 | 160 | |
| 161 | 161 | const ProgramHeaderIndexes = struct { |
| 162 | /// PT_PHDR | |
| 162 | /// PT.PHDR | |
| 163 | 163 | table: OptionalProgramHeaderIndex = .none, |
| 164 | /// PT_LOAD for PHDR table | |
| 164 | /// PT.LOAD for PHDR table | |
| 165 | 165 | /// We add this special load segment to ensure the EHDR and PHDR table are always |
| 166 | 166 | /// loaded into memory. |
| 167 | 167 | table_load: OptionalProgramHeaderIndex = .none, |
| 168 | /// PT_INTERP | |
| 168 | /// PT.INTERP | |
| 169 | 169 | interp: OptionalProgramHeaderIndex = .none, |
| 170 | /// PT_DYNAMIC | |
| 170 | /// PT.DYNAMIC | |
| 171 | 171 | dynamic: OptionalProgramHeaderIndex = .none, |
| 172 | /// PT_GNU_EH_FRAME | |
| 172 | /// PT.GNU_EH_FRAME | |
| 173 | 173 | gnu_eh_frame: OptionalProgramHeaderIndex = .none, |
| 174 | /// PT_GNU_STACK | |
| 174 | /// PT.GNU_STACK | |
| 175 | 175 | gnu_stack: OptionalProgramHeaderIndex = .none, |
| 176 | /// PT_TLS | |
| 176 | /// PT.TLS | |
| 177 | 177 | /// TODO I think ELF permits multiple TLS segments but for now, assume one per file. |
| 178 | 178 | tls: OptionalProgramHeaderIndex = .none, |
| 179 | 179 | }; |
| ... | ... | @@ -334,23 +334,23 @@ pub fn createEmpty( |
| 334 | 334 | if (!is_obj_or_ar) { |
| 335 | 335 | try self.dynstrtab.append(gpa, 0); |
| 336 | 336 | |
| 337 | // Initialize PT_PHDR program header | |
| 337 | // Initialize PT.PHDR program header | |
| 338 | 338 | 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), | |
| 341 | 341 | }; |
| 342 | 342 | const ehsize: u64 = switch (self.ptr_width) { |
| 343 | 343 | .p32 => @sizeOf(elf.Elf32_Ehdr), |
| 344 | 344 | .p64 => @sizeOf(elf.Elf64_Ehdr), |
| 345 | 345 | }; |
| 346 | 346 | 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), | |
| 349 | 349 | }; |
| 350 | 350 | const max_nphdrs = comptime getMaxNumberOfPhdrs(); |
| 351 | 351 | const reserved: u64 = mem.alignForward(u64, padToIdeal(max_nphdrs * phsize), self.page_size); |
| 352 | 352 | self.phdr_indexes.table = (try self.addPhdr(.{ |
| 353 | .type = elf.PT_PHDR, | |
| 353 | .type = @backingInt(elf.PT.PHDR), | |
| 354 | 354 | .flags = elf.PF_R, |
| 355 | 355 | .@"align" = p_align, |
| 356 | 356 | .addr = self.image_base + ehsize, |
| ... | ... | @@ -359,7 +359,7 @@ pub fn createEmpty( |
| 359 | 359 | .memsz = reserved, |
| 360 | 360 | })).toOptional(); |
| 361 | 361 | self.phdr_indexes.table_load = (try self.addPhdr(.{ |
| 362 | .type = elf.PT_LOAD, | |
| 362 | .type = @backingInt(elf.PT.LOAD), | |
| 363 | 363 | .flags = elf.PF_R, |
| 364 | 364 | .@"align" = self.page_size, |
| 365 | 365 | .addr = self.image_base, |
| ... | ... | @@ -514,11 +514,11 @@ fn detectAllocCollision(self: *Elf, start: u64, size: u64) !?u64 { |
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | 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; | |
| 520 | 520 | if (start < test_end) { |
| 521 | if (end > phdr.p_offset) return test_end; | |
| 521 | if (end > phdr.offset) return test_end; | |
| 522 | 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 | 538 | if (section.sh_offset < min_pos) min_pos = section.sh_offset; |
| 539 | 539 | } |
| 540 | 540 | 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; | |
| 543 | 543 | } |
| 544 | 544 | return min_pos - start; |
| 545 | 545 | } |
| ... | ... | @@ -1471,34 +1471,34 @@ fn writePhdrTable(self: *Elf) !void { |
| 1471 | 1471 | const phdr_table = &self.phdrs.items[self.phdr_indexes.table.int().?]; |
| 1472 | 1472 | |
| 1473 | 1473 | 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, | |
| 1476 | 1476 | }); |
| 1477 | 1477 | |
| 1478 | 1478 | switch (self.ptr_width) { |
| 1479 | 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 | 1481 | defer gpa.free(buf); |
| 1482 | 1482 | |
| 1483 | 1483 | for (buf, 0..) |*phdr, i| { |
| 1484 | 1484 | phdr.* = phdrTo32(self.phdrs.items[i]); |
| 1485 | 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 | 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 | 1493 | defer gpa.free(buf); |
| 1494 | 1494 | |
| 1495 | 1495 | for (buf, 0..) |*phdr, i| { |
| 1496 | 1496 | phdr.* = self.phdrs.items[i]; |
| 1497 | 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 | 1581 | const entry_sym = obj.entrySymbol(self) orelse break :blk 0; |
| 1582 | 1582 | break :blk @intCast(entry_sym.address(.{}, self)); |
| 1583 | 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 | 1585 | switch (self.ptr_width) { |
| 1586 | 1586 | .p32 => { |
| 1587 | 1587 | mem.writeInt(u32, hdr_buf[index..][0..4], @intCast(e_entry), endian); |
| ... | ... | @@ -1622,8 +1622,8 @@ pub fn writeElfHeader(self: *Elf) !void { |
| 1622 | 1622 | index += 2; |
| 1623 | 1623 | |
| 1624 | 1624 | 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), | |
| 1627 | 1627 | }; |
| 1628 | 1628 | mem.writeInt(u16, hdr_buf[index..][0..2], e_phentsize, endian); |
| 1629 | 1629 | index += 2; |
| ... | ... | @@ -2091,26 +2091,26 @@ fn initSpecialPhdrs(self: *Elf) !void { |
| 2091 | 2091 | |
| 2092 | 2092 | if (self.section_indexes.interp != null and self.phdr_indexes.interp == .none) { |
| 2093 | 2093 | self.phdr_indexes.interp = (try self.addPhdr(.{ |
| 2094 | .type = elf.PT_INTERP, | |
| 2094 | .type = @backingInt(elf.PT.INTERP), | |
| 2095 | 2095 | .flags = elf.PF_R, |
| 2096 | 2096 | .@"align" = 1, |
| 2097 | 2097 | })).toOptional(); |
| 2098 | 2098 | } |
| 2099 | 2099 | if (self.section_indexes.dynamic != null and self.phdr_indexes.dynamic == .none) { |
| 2100 | 2100 | self.phdr_indexes.dynamic = (try self.addPhdr(.{ |
| 2101 | .type = elf.PT_DYNAMIC, | |
| 2101 | .type = @backingInt(elf.PT.DYNAMIC), | |
| 2102 | 2102 | .flags = elf.PF_R | elf.PF_W, |
| 2103 | 2103 | })).toOptional(); |
| 2104 | 2104 | } |
| 2105 | 2105 | if (self.section_indexes.eh_frame_hdr != null and self.phdr_indexes.gnu_eh_frame == .none) { |
| 2106 | 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 | 2108 | .flags = elf.PF_R, |
| 2109 | 2109 | })).toOptional(); |
| 2110 | 2110 | } |
| 2111 | 2111 | if (self.phdr_indexes.gnu_stack == .none) { |
| 2112 | 2112 | self.phdr_indexes.gnu_stack = (try self.addPhdr(.{ |
| 2113 | .type = elf.PT_GNU_STACK, | |
| 2113 | .type = @backingInt(elf.PT.GNU_STACK), | |
| 2114 | 2114 | .flags = elf.PF_W | elf.PF_R, |
| 2115 | 2115 | .memsz = self.base.stack_size, |
| 2116 | 2116 | .@"align" = 1, |
| ... | ... | @@ -2122,7 +2122,7 @@ fn initSpecialPhdrs(self: *Elf) !void { |
| 2122 | 2122 | } else false; |
| 2123 | 2123 | if (has_tls and self.phdr_indexes.tls == .none) { |
| 2124 | 2124 | self.phdr_indexes.tls = (try self.addPhdr(.{ |
| 2125 | .type = elf.PT_TLS, | |
| 2125 | .type = @backingInt(elf.PT.TLS), | |
| 2126 | 2126 | .flags = elf.PF_R, |
| 2127 | 2127 | .@"align" = 1, |
| 2128 | 2128 | })).toOptional(); |
| ... | ... | @@ -2260,15 +2260,15 @@ fn setHashSections(self: *Elf) !void { |
| 2260 | 2260 | } |
| 2261 | 2261 | } |
| 2262 | 2262 | |
| 2263 | fn 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, | |
| 2263 | fn 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, | |
| 2272 | 2272 | else => 7, |
| 2273 | 2273 | }; |
| 2274 | 2274 | } |
| ... | ... | @@ -2282,12 +2282,12 @@ fn sortPhdrs( |
| 2282 | 2282 | const Entry = struct { |
| 2283 | 2283 | phndx: u16, |
| 2284 | 2284 | |
| 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 | 2286 | const lhs_phdr = program_headers[lhs.phndx]; |
| 2287 | 2287 | const rhs_phdr = program_headers[rhs.phndx]; |
| 2288 | 2288 | const lhs_rank = phdrRank(lhs_phdr); |
| 2289 | 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 | 2291 | return lhs_rank < rhs_rank; |
| 2292 | 2292 | } |
| 2293 | 2293 | }; |
| ... | ... | @@ -2299,7 +2299,7 @@ fn sortPhdrs( |
| 2299 | 2299 | } |
| 2300 | 2300 | |
| 2301 | 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); | |
| 2303 | 2303 | |
| 2304 | 2304 | const backlinks = try gpa.alloc(u16, entries.len); |
| 2305 | 2305 | defer gpa.free(backlinks); |
| ... | ... | @@ -2655,8 +2655,8 @@ fn addLoadPhdrs(self: *Elf) error{OutOfMemory}!void { |
| 2655 | 2655 | if (shdr.sh_type == elf.SHT_NULL) continue; |
| 2656 | 2656 | if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue; |
| 2657 | 2657 | 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) }); | |
| 2660 | 2660 | } |
| 2661 | 2661 | } |
| 2662 | 2662 | } |
| ... | ... | @@ -2672,11 +2672,11 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void { |
| 2672 | 2672 | .p64 => @sizeOf(elf.Elf64_Ehdr), |
| 2673 | 2673 | }; |
| 2674 | 2674 | 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), | |
| 2677 | 2677 | }; |
| 2678 | 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); | |
| 2680 | 2680 | |
| 2681 | 2681 | if (needed_size > available_space) { |
| 2682 | 2682 | // In this case, we have two options: |
| ... | ... | @@ -2689,10 +2689,10 @@ fn allocatePhdrTable(self: *Elf) error{OutOfMemory}!void { |
| 2689 | 2689 | err.addNote("required 0x{x}, available 0x{x}", .{ needed_size, available_space }); |
| 2690 | 2690 | } |
| 2691 | 2691 | |
| 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; | |
| 2696 | 2696 | } |
| 2697 | 2697 | |
| 2698 | 2698 | /// Allocates alloc sections and creates load segments for sections |
| ... | ... | @@ -2758,7 +2758,7 @@ pub fn allocateAllocSections(self: *Elf) !void { |
| 2758 | 2758 | // of any section that is contained in a cover and use it to align |
| 2759 | 2759 | // the start address of the segement (and first section). |
| 2760 | 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; | |
| 2762 | 2762 | |
| 2763 | 2763 | for (covers) |cover| { |
| 2764 | 2764 | if (cover.items.len == 0) continue; |
| ... | ... | @@ -2817,14 +2817,14 @@ pub fn allocateAllocSections(self: *Elf) !void { |
| 2817 | 2817 | } |
| 2818 | 2818 | |
| 2819 | 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 | 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 | 2823 | 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; | |
| 2826 | 2826 | var new_offset = try self.findFreeSpace(filesz, @"align"); |
| 2827 | phdr.p_offset = new_offset; | |
| 2827 | phdr.offset = new_offset; | |
| 2828 | 2828 | |
| 2829 | 2829 | log.debug("moving phdr({d}) from 0x{x} to 0x{x}", .{ phndx, old_offset, new_offset }); |
| 2830 | 2830 | |
| ... | ... | @@ -2854,11 +2854,11 @@ pub fn allocateAllocSections(self: *Elf) !void { |
| 2854 | 2854 | } |
| 2855 | 2855 | } |
| 2856 | 2856 | |
| 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"; | |
| 2862 | 2862 | |
| 2863 | 2863 | addr = mem.alignForward(u64, addr, self.page_size); |
| 2864 | 2864 | } |
| ... | ... | @@ -2902,12 +2902,12 @@ fn allocateSpecialPhdrs(self: *Elf) void { |
| 2902 | 2902 | if (pair[0].int()) |index| { |
| 2903 | 2903 | const shdr = slice.items(.shdr)[pair[1].?]; |
| 2904 | 2904 | 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; | |
| 2911 | 2911 | } |
| 2912 | 2912 | } |
| 2913 | 2913 | |
| ... | ... | @@ -2924,25 +2924,25 @@ fn allocateSpecialPhdrs(self: *Elf) void { |
| 2924 | 2924 | shndx += 1; |
| 2925 | 2925 | continue; |
| 2926 | 2926 | } |
| 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; | |
| 2931 | 2931 | shndx += 1; |
| 2932 | phdr.p_align = @max(phdr.p_align, shdr.sh_addralign); | |
| 2932 | phdr.@"align" = @max(phdr.@"align", shdr.sh_addralign); | |
| 2933 | 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; | |
| 2937 | 2937 | |
| 2938 | 2938 | while (shndx < shdrs.len) : (shndx += 1) { |
| 2939 | 2939 | const next = shdrs[shndx]; |
| 2940 | 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 | 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 | 3347 | return @intCast(@divExact(self.getTarget().ptrBitWidth(), 8)); |
| 3348 | 3348 | } |
| 3349 | 3349 | |
| 3350 | fn phdrTo32(phdr: elf.Elf64_Phdr) elf.Elf32_Phdr { | |
| 3350 | fn phdrTo32(phdr: elf.Elf64.Phdr) elf.Elf32.Phdr { | |
| 3351 | 3351 | 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"), | |
| 3360 | 3360 | }; |
| 3361 | 3361 | } |
| 3362 | 3362 | |
| ... | ... | @@ -3397,7 +3397,7 @@ fn getPhdr(self: *Elf, opts: struct { |
| 3397 | 3397 | if (self.phdr_indexes.table_load.int()) |index| { |
| 3398 | 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 | 3401 | return @fromBackingInt(@intCast(phndx)); |
| 3402 | 3402 | } |
| 3403 | 3403 | return .none; |
| ... | ... | @@ -3415,14 +3415,14 @@ fn addPhdr(self: *Elf, opts: struct { |
| 3415 | 3415 | const gpa = self.base.comp.gpa; |
| 3416 | 3416 | const index: ProgramHeaderIndex = @fromBackingInt(@intCast(self.phdrs.items.len)); |
| 3417 | 3417 | 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", | |
| 3426 | 3426 | }); |
| 3427 | 3427 | return index; |
| 3428 | 3428 | } |
| ... | ... | @@ -3673,9 +3673,9 @@ pub fn tpAddress(self: *Elf) i64 { |
| 3673 | 3673 | const index = self.phdr_indexes.tls.int() orelse return 0; |
| 3674 | 3674 | const phdr = self.phdrs.items[index]; |
| 3675 | 3675 | 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, | |
| 3679 | 3679 | else => |arch| std.debug.panic("TODO implement getTpAddress for {s}", .{@tagName(arch)}), |
| 3680 | 3680 | }; |
| 3681 | 3681 | return @intCast(addr); |
| ... | ... | @@ -3684,13 +3684,13 @@ pub fn tpAddress(self: *Elf) i64 { |
| 3684 | 3684 | pub fn dtpAddress(self: *Elf) i64 { |
| 3685 | 3685 | const index = self.phdr_indexes.tls.int() orelse return 0; |
| 3686 | 3686 | const phdr = self.phdrs.items[index]; |
| 3687 | return @intCast(phdr.p_vaddr); | |
| 3687 | return @intCast(phdr.vaddr); | |
| 3688 | 3688 | } |
| 3689 | 3689 | |
| 3690 | 3690 | pub fn tlsAddress(self: *Elf) i64 { |
| 3691 | 3691 | const index = self.phdr_indexes.tls.int() orelse return 0; |
| 3692 | 3692 | const phdr = self.phdrs.items[index]; |
| 3693 | return @intCast(phdr.p_vaddr); | |
| 3693 | return @intCast(phdr.vaddr); | |
| 3694 | 3694 | } |
| 3695 | 3695 | |
| 3696 | 3696 | pub 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 | 3886 | |
| 3887 | 3887 | const FormatPhdr = struct { |
| 3888 | 3888 | elf_file: *Elf, |
| 3889 | phdr: elf.Elf64_Phdr, | |
| 3889 | phdr: elf.Elf64.Phdr, | |
| 3890 | 3890 | }; |
| 3891 | 3891 | |
| 3892 | fn fmtPhdr(self: *Elf, phdr: elf.Elf64_Phdr) std.fmt.Alt(FormatPhdr, formatPhdr) { | |
| 3892 | fn fmtPhdr(self: *Elf, phdr: elf.Elf64.Phdr) std.fmt.Alt(FormatPhdr, formatPhdr) { | |
| 3893 | 3893 | return .{ .data = .{ |
| 3894 | 3894 | .phdr = phdr, |
| 3895 | 3895 | .elf_file = self, |
| ... | ... | @@ -3898,28 +3898,28 @@ fn fmtPhdr(self: *Elf, phdr: elf.Elf64_Phdr) std.fmt.Alt(FormatPhdr, formatPhdr) |
| 3898 | 3898 | |
| 3899 | 3899 | fn formatPhdr(ctx: FormatPhdr, writer: *std.Io.Writer) std.Io.Writer.Error!void { |
| 3900 | 3900 | 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; | |
| 3904 | 3904 | var flags: [3]u8 = @splat('_'); |
| 3905 | 3905 | if (exec) flags[0] = 'X'; |
| 3906 | 3906 | if (write) flags[1] = 'W'; |
| 3907 | 3907 | 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", | |
| 3918 | 3918 | else => "UNKNOWN", |
| 3919 | 3919 | }; |
| 3920 | 3920 | 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, | |
| 3923 | 3923 | }); |
| 3924 | 3924 | } |
| 3925 | 3925 |
src/link/Lld.zig+2-2| ... | ... | @@ -1023,8 +1023,8 @@ fn elfLink(lld: *Lld, arena: Allocator) !void { |
| 1023 | 1023 | } |
| 1024 | 1024 | |
| 1025 | 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. | |
| 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 | |
| 1028 | 1028 | // ld.elf_so fails loading dynamic libraries with "not found" error. |
| 1029 | 1029 | // See https://github.com/ziglang/zig/issues/9109 . |
| 1030 | 1030 | try argv.append("--no-rosegment"); |